Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
henadzit committed Oct 24, 2024
1 parent bfa700e commit c0c16b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
20 changes: 10 additions & 10 deletions tests/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ async def test_count_after_aggregate(self):
.count()
)

assert ret == 2
self.assertEqual(ret, 2)

async def test_exist_after_aggregate(self):
author = await Author.create(name="1")
Expand All @@ -200,15 +200,15 @@ async def test_exist_after_aggregate(self):
.exists()
)

assert ret is True
self.assertTrue(ret)

ret = (
await Author.all()
.annotate(average_rating=Avg("books__rating"))
.filter(average_rating__gte=4)
.exists()
)
assert ret is False
self.assertFalse(ret)

async def test_count_after_aggregate_m2m(self):
tournament = await Tournament.create(name="1")
Expand All @@ -233,40 +233,40 @@ async def test_count_after_aggregate_m2m(self):
.prefetch_related("participants")
)
result = await query
assert len(result) == 2
self.assertEqual(len(result), 2)

res = await query.count()
assert res == 2
self.assertEqual(res, 2)

async def test_count_without_matching(self) -> None:
await Tournament.create(name="Test")

query = Tournament.annotate(events_count=Count("events")).filter(events_count__gt=0).count()
result = await query
assert result == 0
self.assertEqual(result, 0)

async def test_int_sum_on_models_with_validators(self) -> None:
await ValidatorModel.create(max_value=2)
await ValidatorModel.create(max_value=2)

query = ValidatorModel.annotate(sum=Sum("max_value")).values("sum")
result = await query
assert result == [{"sum": 4}]
self.assertEqual(result, [{"sum": 4}])

async def test_int_sum_math_on_models_with_validators(self) -> None:
await ValidatorModel.create(max_value=4)
await ValidatorModel.create(max_value=4)

query = ValidatorModel.annotate(sum=Sum(F("max_value") * F("max_value"))).values("sum")
result = await query
assert result == [{"sum": 32}]
self.assertEqual(result, [{"sum": 32}])

async def test_decimal_sum_on_models_with_validators(self) -> None:
await ValidatorModel.create(min_value_decimal=2.0)

query = ValidatorModel.annotate(sum=Sum("min_value_decimal")).values("sum")
result = await query
assert result == [{"sum": Decimal("2.0")}]
self.assertEqual(result, [{"sum": Decimal("2.0")}])

async def test_decimal_sum_with_math_on_models_with_validators(self) -> None:
await ValidatorModel.create(min_value_decimal=2.0)
Expand All @@ -275,4 +275,4 @@ async def test_decimal_sum_with_math_on_models_with_validators(self) -> None:
sum=Sum(F("min_value_decimal") - F("min_value_decimal") * F("min_value_decimal"))
).values("sum")
result = await query
assert result == [{"sum": Decimal("-2.0")}]
self.assertEqual(result, [{"sum": Decimal("-2.0")}])
1 change: 1 addition & 0 deletions tortoise/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def to_python_value(self, value: Any, validate=True) -> Any:
Converts from the DB type to the Python type.
:param value: Value from DB
:param validate: Should validation be performed
"""
if value is not None and not isinstance(value, self.field_type):
value = self.field_type(value) # pylint: disable=E1102
Expand Down

0 comments on commit c0c16b0

Please sign in to comment.