async def test_filter_by_aggregation_field_trim(self): await Tournament.create(name=" 1 ") await Tournament.create(name="2 ") tournaments = await Tournament.annotate(trimmed_name=Trim("name")).filter(trimmed_name="1") self.assertEqual(len(tournaments), 1) self.assertSetEqual({(t.name, t.trimmed_name) for t in tournaments}, {(" 1 ", "1")})
async def test_filter_by_aggregation_field_trim(self): await self.model.create(chars=" aaa ") obj = await self.model.annotate(chars_trim=Trim("chars") ).filter(chars_trim="aaa") self.assertEqual(len(obj), 1) self.assertEqual(obj[0].chars_trim, "aaa")
async def test_values_annotations_trim(self): await Tournament.create(name=" x") await Tournament.create(name=" y ") tournaments = await Tournament.annotate(name_trim=Trim("name")).values("name", "name_trim") self.assertListSortEqual( tournaments, [{"name": " x", "name_trim": "x"}, {"name": " y ", "name_trim": "y"}] )
async def test_values_list_annotations_trim(self): await Tournament.create(name=" x") await Tournament.create(name=" y ") tournaments = await Tournament.annotate(name_trim=Trim("name")).values_list( "name", "name_trim" ) self.assertListSortEqual(tournaments, [(" x", "x"), (" y ", "y")])
async def test_select_related_with_annotation(self): tournament = await Tournament.create(name="New Tournament") reporter = await Reporter.create(name="Reporter") event = await Event.create(name="With reporter", tournament=tournament, reporter=reporter) event = (await Event.filter(pk=event.pk).select_related("reporter").annotate( tournament_name=Trim("tournament__name")).first()) self.assertEqual(event.reporter, reporter) self.assertTrue(hasattr(event, "tournament_name")) self.assertEqual(event.tournament_name, tournament.name)
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_concat_functions(self): author = await Author.create(name="Some One") await Book.create(name="Physics Book", author=author, rating=4, subject="physics ") await Book.create(name="Mathematics Book", author=author, rating=3, subject=" mathematics") await Book.create(name="No-subject Book", author=author, rating=3) ret = (await Book.all().annotate(long_info=Max( Concat("name", "(", Coalesce(Trim("subject"), "others"), ")")) ).values("long_info")) self.assertEqual(ret, [{"long_info": "Physics Book(physics)"}])
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"))
async def test_function(self): obj1 = await self.model.create(chars=" aaa ") await self.model.filter(eyedee=obj1.eyedee).update(chars=Trim("chars")) obj2 = await self.model.get(eyedee=obj1.eyedee) self.assertEqual(obj2.chars, "aaa")