async def test_filter_by_aggregation_field_length(self): await self.model.create(chars="aaa") await self.model.create(chars="bbbbb") obj = await self.model.annotate(chars_length=Length("chars")).filter(chars_length=3) self.assertEqual(len(obj), 1) self.assertEqual(obj[0].chars_length, 3)
async def test_values_list_annotations_length(self): await Tournament.create(name="Championship") await Tournament.create(name="Super Bowl") tournaments = await Tournament.annotate(name_length=Length("name")).values_list( "name", "name_length" ) self.assertListSortEqual(tournaments, [("Championship", 12), ("Super Bowl", 10)])
async def test_filter_by_aggregation_field_length(self): await Tournament.create(name="12345") await Tournament.create(name="123") await Tournament.create(name="1234") tournaments = await Tournament.annotate(name_len=Length("name")).filter(name_len__gte=4) self.assertEqual(len(tournaments), 2) self.assertSetEqual({t.name for t in tournaments}, {"1234", "12345"})
async def run(): await Tortoise.init(db_url="mysql://*****:*****@localhost:55555/test_demo", modules={"models": ["__main__"]}) await Tortoise.generate_schemas() tournament = await Tournament.create(name="New Tournament", desc="great") await tournament.save() await Tournament.create(name="Second tournament") await Tournament.create(name=" final tournament ") await Event(name="Without participants", tournament_id=tournament.id).save() event = Event(name="Test", tournament_id=tournament.id) await event.save() participants = [] for i in range(10): team = Team(name=f"Team {(i + 1)}") await team.save() participants.append(team) await event.participants.add(*participants) # await event.participants.add(participants[0], participants[1]) print(await Tournament.all().annotate(events_count=Count("events")).filter(events_count__lte=3).values()) print( await Tournament .annotate(events_count_with_filter=Count("events", _filter=Q(name="New Tournament"))) .filter(events_count_with_filter__gte=0).values() ) print(await Event.annotate(lowest_team_id=Count("participants__id")).values()) print(await Tournament.annotate(events_count=Count("events")).order_by("events_count")) print(await Event.annotate(tournament_test_id=Sum("tournament__id")).first()) print( await Tournament.annotate(clean_desciption=Coalesce("desc", "hehe")).values() ) print( await Tournament.annotate(trimmed_name=Trim("name")).values() ) print( await Tournament.annotate(name_len=Length("name")).filter( name_len__gt=len("New Tournament") ) ) print(await Tournament.annotate(name_lo=Lower("name")).filter(name_lo="new tournament").values()) print(await Tournament.annotate(name_lo=Upper("name")).filter(name_lo="NEW TOURNAMENT").values()) print()
async def test_values_annotations_length(self): await Tournament.create(name="Championship") await Tournament.create(name="Super Bowl") tournaments = await Tournament.annotate(name_slength=Length("name")).values( "name", "name_slength" ) self.assertListSortEqual( tournaments, [ {"name": "Championship", "name_slength": 12}, {"name": "Super Bowl", "name_slength": 10}, ], )
async def test_filter_by_aggregation_field_comparison_length(self): t1 = await Tournament.create(name="Tournament") await Event.create(name="event1", tournament=t1) await Event.create(name="event2", tournament=t1) t2 = await Tournament.create(name="contest") await Event.create(name="event3", tournament=t2) await Tournament.create(name="Championship") t4 = await Tournament.create(name="local") await Event.create(name="event4", tournament=t4) await Event.create(name="event5", tournament=t4) tournaments = await Tournament.annotate( name_len=Length("name"), event_count=Count("events")).filter(name_len__gt=5, event_count=2) self.assertEqual(len(tournaments), 1) self.assertSetEqual({t.name for t in tournaments}, {"Tournament"})
async def run(): await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]}) await Tortoise.generate_schemas() tournament = await Tournament.create(name="New Tournament", desc="great") await tournament.save() await Tournament.create(name="Second tournament") await Tournament.create(name=" final tournament ") await Event(name="Without participants", tournament_id=tournament.id).save() event = Event(name="Test", tournament_id=tournament.id) await event.save() participants = [] for i in range(2): team = Team(name=f"Team {(i + 1)}") await team.save() participants.append(team) await event.participants.add(participants[0], participants[1]) await event.participants.add(participants[0], participants[1]) print(await Tournament.all().annotate(events_count=Count("events") ).filter(events_count__gte=1)) print(await Event.filter(id=event.id).first().annotate( lowest_team_id=Min("participants__id"))) print(await Tournament.all().annotate(events_count=Count("events") ).order_by("events_count")) print(await Event.all().annotate(tournament_test_id=Sum("tournament__id") ).first()) print(await Tournament.annotate(clean_desciption=Coalesce("desc") ).filter(clean_desciption="")) print(await Tournament.annotate(trimmed_name=Trim("name") ).filter(trimmed_name="final tournament")) print(await Tournament.annotate(name_len=Length("name") ).filter(name_len__gt=len("New Tournament"))) print(await Tournament.annotate(name_lo=Lower("name") ).filter(name_lo="new tournament")) print(await Tournament.annotate(name_lo=Upper("name") ).filter(name_lo="NEW TOURNAMENT"))