Skip to content

Commit

Permalink
Correctness fix of previous hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Mar 24, 2020
1 parent 6ad40e0 commit 3452958
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
6 changes: 3 additions & 3 deletions tests/fields/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ async def test_create(self):
obj = await testmodels.DatetimeFields.get(id=obj0.id)
self.assertEqual(obj.datetime, now)
self.assertEqual(obj.datetime_null, None)
self.assertLess(obj.datetime_auto - now, timedelta(microseconds=10000))
self.assertLess(obj.datetime_add - now, timedelta(microseconds=10000))
self.assertLess(obj.datetime_auto - now, timedelta(microseconds=20000))
self.assertLess(obj.datetime_add - now, timedelta(microseconds=20000))
datetime_auto = obj.datetime_auto
sleep(0.011)
sleep(0.012)
await obj.save()
obj2 = await testmodels.DatetimeFields.get(id=obj.id)
self.assertEqual(obj2.datetime, now)
Expand Down
20 changes: 1 addition & 19 deletions tortoise/backends/asyncpg/executor.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import uuid
from typing import Any, List, Optional
from typing import List, Optional

import asyncpg
from pypika import Parameter
from pypika.terms import Term, ValueWrapper

from tortoise import Model
from tortoise.backends.base.executor import BaseExecutor
from tortoise.filters import is_in, not_in


def postgres_is_in(field: Term, value: Any) -> Term:
if value:
return field.isin(value)
return ValueWrapper(False)


def post_gres_not_in(field: Term, value: Any) -> Term:
if value:
return field.notin(value) | field.isnull()
return ValueWrapper(True)


class AsyncpgExecutor(BaseExecutor):
FILTER_FUNC_OVERRIDE = {
is_in: postgres_is_in,
not_in: post_gres_not_in,
}
EXPLAIN_PREFIX = "EXPLAIN (FORMAT JSON, VERBOSE)"
DB_NATIVE = BaseExecutor.DB_NATIVE | {bool, uuid.UUID}

Expand Down
10 changes: 5 additions & 5 deletions tortoise/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pypika import Table
from pypika.enums import SqlTypes
from pypika.functions import Cast, Upper
from pypika.terms import Criterion, Term, ValueWrapper
from pypika.terms import BasicCriterion, Criterion, Equality, Term, ValueWrapper

from tortoise.fields import Field
from tortoise.fields.relational import BackwardFKRelation, ManyToManyFieldInstance
Expand Down Expand Up @@ -45,16 +45,16 @@ def string_encoder(value: Any, instance: "Model", field: Field) -> str:
##############################################################################


def is_in(field: Term, value: Any) -> Term:
def is_in(field: Term, value: Any) -> Criterion:
if value:
return field.isin(value)
return ValueWrapper(0)
return BasicCriterion(Equality.eq, ValueWrapper(1), ValueWrapper(0))


def not_in(field: Term, value: Any) -> Term:
def not_in(field: Term, value: Any) -> Criterion:
if value:
return field.notin(value) | field.isnull()
return ValueWrapper(1)
return BasicCriterion(Equality.eq, ValueWrapper(1), ValueWrapper(1))


def between_and(field: Term, value: Tuple[Any, Any]) -> Criterion:
Expand Down

0 comments on commit 3452958

Please sign in to comment.