Skip to content

Commit

Permalink
Unify field names in queryset classes (#1751)
Browse files Browse the repository at this point in the history
* Unify field naming in AwaitableQuery subclasses

* Make fields of querysets private
  • Loading branch information
henadzit authored Nov 1, 2024
1 parent 5e3c6f8 commit 9e84736
Show file tree
Hide file tree
Showing 2 changed files with 299 additions and 273 deletions.
81 changes: 81 additions & 0 deletions tests/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ParamsError,
)
from tortoise.expressions import F, RawSQL, Subquery
from tortoise.functions import Length

# TODO: Test the many exceptions in QuerySet
# TODO: .filter(intnum_null=None) does not work as expected
Expand Down Expand Up @@ -689,3 +690,83 @@ def test_does_not_exist(self):

class TestMultiple(TestNotExist):
exp_cls = MultipleObjectsReturned


class TestQueryReuse(test.TestCase):
async def test_annotations(self):
a = await Tournament.create(name="A")

base_query = Tournament.annotate(id_plus_one=F("id") + 1)
query1 = base_query.annotate(id_plus_two=F("id") + 2)
query2 = base_query.annotate(id_plus_three=F("id") + 3)
res = await query1.first()
self.assertEqual(res.id_plus_one, a.id + 1)
self.assertEqual(res.id_plus_two, a.id + 2)
with self.assertRaises(AttributeError):
getattr(res, "id_plus_three")

res = await query2.first()
self.assertEqual(res.id_plus_one, a.id + 1)
self.assertEqual(res.id_plus_three, a.id + 3)
with self.assertRaises(AttributeError):
getattr(res, "id_plus_two")

res = await query1.first()
with self.assertRaises(AttributeError):
getattr(res, "id_plus_three")

async def test_filters(self):
a = await Tournament.create(name="A")
b = await Tournament.create(name="B")
await Tournament.create(name="C")

base_query = Tournament.exclude(name="C")
tournaments = await base_query
self.assertSetEqual(set(tournaments), {a, b})

tournaments = await base_query.exclude(name="A")
self.assertSetEqual(set(tournaments), {b})

tournaments = await base_query.exclude(name="B")
self.assertSetEqual(set(tournaments), {a})

async def test_joins(self):
tournament_a = await Tournament.create(name="A")
tournament_b = await Tournament.create(name="B")
tournament_c = await Tournament.create(name="C")
event_a = await Event.create(name="A", tournament=tournament_a)
event_b = await Event.create(name="B", tournament=tournament_b)
await Event.create(name="C", tournament=tournament_c)

base_query = Event.exclude(tournament__name="C")
events = await base_query
self.assertSetEqual(set(events), {event_a, event_b})

events = await base_query.exclude(name="A")
self.assertSetEqual(set(events), {event_b})

events = await base_query.exclude(name="B")
self.assertSetEqual(set(events), {event_a})

async def test_order_by(self):
a = await Tournament.create(name="A")
b = await Tournament.create(name="B")

base_query = Tournament.all().order_by("name")
tournaments = await base_query
self.assertEqual(tournaments, [a, b])

tournaments = await base_query.order_by("-name")
self.assertEqual(tournaments, [b, a])

@test.requireCapability(dialect=NotEQ("mssql"))
async def test_values_with_annotations(self):
await Tournament.create(name="Championship")
await Tournament.create(name="Super Bowl")

base_query = Tournament.annotate(name_length=Length("name"))
tournaments = await base_query.values_list("name")
self.assertListSortEqual(tournaments, [("Championship",), ("Super Bowl",)])

tournaments = await base_query.values_list("name_length")
self.assertListSortEqual(tournaments, [(10,), (12,)])
Loading

0 comments on commit 9e84736

Please sign in to comment.