MCPcopy Index your code
hub / github.com/tortoise/tortoise-orm / test_group_aggregation

Function test_group_aggregation

tests/test_aggregation.py:132–159  ·  view source on GitHub ↗
(db)

Source from the content-addressed store, hash-verified

130
131@pytest.mark.asyncio
132async def test_group_aggregation(db):
133 author = await Author.create(name="Some One")
134 await Book.create(name="First!", author=author, rating=4)
135 await Book.create(name="Second!", author=author, rating=3)
136 await Book.create(name="Third!", author=author, rating=3)
137
138 authors = await Author.all().annotate(average_rating=Avg("books__rating"))
139 assert authors[0].average_rating == pytest.approx(3.3333333333, rel=1e-5)
140
141 authors = await Author.all().annotate(average_rating=Avg("books__rating")).values()
142 assert authors[0]["average_rating"] == pytest.approx(3.3333333333, rel=1e-5)
143
144 authors = (
145 await Author.all()
146 .annotate(average_rating=Avg("books__rating"))
147 .values("id", "name", "average_rating")
148 )
149 assert authors[0]["average_rating"] == pytest.approx(3.3333333333, rel=1e-5)
150
151 authors = await Author.all().annotate(average_rating=Avg("books__rating")).values_list()
152 assert authors[0][2] == pytest.approx(3.3333333333, rel=1e-5)
153
154 authors = (
155 await Author.all()
156 .annotate(average_rating=Avg("books__rating"))
157 .values_list("id", "name", "average_rating")
158 )
159 assert authors[0][2] == pytest.approx(3.3333333333, rel=1e-5)
160
161
162@pytest.mark.asyncio

Callers

nothing calls this directly

Calls 6

AvgClass · 0.90
createMethod · 0.45
annotateMethod · 0.45
allMethod · 0.45
valuesMethod · 0.45
values_listMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…