Skip to content

Commit

Permalink
Cast BooleanField values correctly on SQLite & MySQL (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigi committed Mar 22, 2020
1 parent cd82404 commit 5444452
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changelog
* Annotations over joins now work correctly with ``values()`` & ``values_list()``
* Ensure ``GROUP BY`` precedes ``HAVING`` to ensure that filtering by aggregates work correctly.
* Fix bug with join query with aggregation
* Cast ``BooleanField`` values correctly on SQLite & MySQL

0.16.1
------
Expand Down
12 changes: 6 additions & 6 deletions tests/fields/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ async def test_empty(self):
async def test_create(self):
obj0 = await testmodels.BooleanFields.create(boolean=True)
obj = await testmodels.BooleanFields.get(id=obj0.id)
self.assertEqual(obj.boolean, True)
self.assertEqual(obj.boolean_null, None)
self.assertIs(obj.boolean, True)
self.assertIs(obj.boolean_null, None)
await obj.save()
obj2 = await testmodels.BooleanFields.get(id=obj.id)
self.assertEqual(obj, obj2)
Expand All @@ -21,15 +21,15 @@ async def test_update(self):
obj0 = await testmodels.BooleanFields.create(boolean=False)
await testmodels.BooleanFields.filter(id=obj0.id).update(boolean=False)
obj = await testmodels.BooleanFields.get(id=obj0.id)
self.assertEqual(obj.boolean, False)
self.assertEqual(obj.boolean_null, None)
self.assertIs(obj.boolean, False)
self.assertIs(obj.boolean_null, None)

async def test_values(self):
obj0 = await testmodels.BooleanFields.create(boolean=True)
values = await testmodels.BooleanFields.get(id=obj0.id).values("boolean")
self.assertEqual(values[0]["boolean"], True)
self.assertIs(values[0]["boolean"], True)

async def test_values_list(self):
obj0 = await testmodels.BooleanFields.create(boolean=True)
values = await testmodels.BooleanFields.get(id=obj0.id).values_list("boolean", flat=True)
self.assertEqual(values[0], True)
self.assertIs(values[0], True)
2 changes: 1 addition & 1 deletion tortoise/backends/asyncpg/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

def Parameter(self, pos: int) -> Parameter:
return Parameter("$%d" % (pos + 1,))
Expand Down
2 changes: 1 addition & 1 deletion tortoise/backends/base/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BaseExecutor:
TO_DB_OVERRIDE: Dict[Type[Field], Callable] = {}
FILTER_FUNC_OVERRIDE: Dict[Callable, Callable] = {}
EXPLAIN_PREFIX: str = "EXPLAIN"
DB_NATIVE = {bytes, str, int, bool, float, decimal.Decimal, datetime.datetime, datetime.date}
DB_NATIVE = {bytes, str, int, float, decimal.Decimal, datetime.datetime, datetime.date}

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion tortoise/backends/sqlite/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SqliteExecutor(BaseExecutor):
fields.DatetimeField: to_db_datetime,
}
EXPLAIN_PREFIX = "EXPLAIN QUERY PLAN"
DB_NATIVE = {bytes, str, int, bool, float}
DB_NATIVE = {bytes, str, int, float}

def Parameter(self, pos: int) -> Parameter:
return Parameter("?")
Expand Down

0 comments on commit 5444452

Please sign in to comment.