示例#1
0
    async def test_filter_by_aggregation_field_upper(self):
        await self.model.create(chars="aAa")
        obj = await self.model.annotate(chars_upper=Upper("chars")
                                        ).filter(chars_upper="AAA")

        self.assertEqual(len(obj), 1)
        self.assertEqual(obj[0].chars_upper, "AAA")
示例#2
0
 async def test_filter_by_annotation_upper(self):
     await Tournament.create(name="ToUrnAmEnT")
     await Tournament.create(name="new TOURnament")
     tournaments = await Tournament.annotate(name_upper=Upper("name"))
     self.assertEqual(len(tournaments), 2)
     self.assertSetEqual({t.name_upper
                          for t in tournaments},
                         {"TOURNAMENT", "NEW TOURNAMENT"})
示例#3
0
 async def test_group_by_annotate_result(self):
     ret = (
         await Book.annotate(upper_name=Upper("author__name"), count=Count("id"))
         .group_by("upper_name")
         .values("upper_name", "count")
     )
     self.assertEqual(
         ret, [{"upper_name": "AUTHOR1", "count": 10}, {"upper_name": "AUTHOR2", "count": 5}]
     )
示例#4
0
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()
示例#5
0
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"))