async def test_aggregation(self): tournament = Tournament(name="New Tournament") await tournament.save() await Tournament.create(name="Second 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="Team {}".format(i + 1)) await team.save() participants.append(team) await event.participants.add(participants[0], participants[1]) await event.participants.add(participants[0], participants[1]) tournaments_with_count = (await Tournament.all().annotate( events_count=Count("events")).filter(events_count__gte=1)) self.assertEqual(len(tournaments_with_count), 1) self.assertEqual(tournaments_with_count[0].events_count, 2) event_with_lowest_team_id = (await Event.filter( id=event.id ).first().annotate(lowest_team_id=Min("participants__id"))) self.assertEqual(event_with_lowest_team_id.lowest_team_id, participants[0].id) ordered_tournaments = (await Tournament.all().annotate( events_count=Count("events")).order_by("events_count")) self.assertEqual(len(ordered_tournaments), 2) self.assertEqual(ordered_tournaments[1].id, tournament.id) event_with_annotation = (await Event.all().annotate( tournament_test_id=Sum("tournament__id")).first()) self.assertEqual(event_with_annotation.tournament_test_id, event_with_annotation.tournament_id)
async def test_prefetch_unknown_field(self): with self.assertRaises(OperationalError): tournament = await Tournament.create(name='tournament') await Event.create(name='First', tournament=tournament) await Event.create(name='Second', tournament=tournament) await Tournament.all().prefetch_related( Prefetch('events1', queryset=Event.filter(name='First'))).first()
async def test_prefetch_unknown_field(self): with self.assertRaises(OperationalError): tournament = await Tournament.create(name="tournament") await Event.create(name="First", tournament=tournament) await Event.create(name="Second", tournament=tournament) await Tournament.all().prefetch_related( Prefetch("events1", queryset=Event.filter(name="First"))).first()
async def test_prefetching(self): tournament = await Tournament.create(name='tournament') await Event.create(name='First', tournament=tournament) await Event.create(name='Second', tournament=tournament) tournament_with_filtered = await Tournament.all().prefetch_related( Prefetch('events', queryset=Event.filter(name='First'))).first() tournament = await Tournament.first().prefetch_related('events') self.assertEqual(len(tournament_with_filtered.events), 1) self.assertEqual(len(tournament.events), 2)
async def test_prefetch_object(self): tournament = await Tournament.create(name="tournament") await Event.create(name="First", tournament=tournament) await Event.create(name="Second", tournament=tournament) tournament_with_filtered = (await Tournament.all().prefetch_related( Prefetch("events", queryset=Event.filter(name="First"))).first()) tournament = await Tournament.first().prefetch_related("events") self.assertEqual(len(tournament_with_filtered.events), 1) self.assertEqual(len(tournament.events), 2)
async def test_filtering(self): tournament = Tournament(name='Tournament') await tournament.save() second_tournament = Tournament(name='Tournament 2') await second_tournament.save() event_first = Event(name='1', tournament=tournament) await event_first.save() event_second = Event(name='2', tournament=second_tournament) await event_second.save() event_third = Event(name='3', tournament=tournament) await event_third.save() event_forth = Event(name='4', tournament=second_tournament) await event_forth.save() team_first = Team(name='First') await team_first.save() team_second = Team(name='Second') await team_second.save() await team_first.events.add(event_first) await event_second.participants.add(team_second) found_events = await Event.filter( Q(id__in=[event_first.id, event_second.id]) | Q(name='3')).filter(participants__not=team_second.id ).order_by('name', 'tournament_id').distinct() self.assertEqual(len(found_events), 2) self.assertEqual(found_events[0].id, event_first.id) self.assertEqual(found_events[1].id, event_third.id) await Team.filter(events__tournament_id=tournament.id ).order_by('-events__name') await Tournament.filter(events__name__in=['1', '3'], ).distinct() teams = await Team.filter(name__icontains='CON') self.assertEqual(len(teams), 1) self.assertEqual(teams[0].name, 'Second') tournaments = await Tournament.filter( events__participants__name__startswith='Fir') self.assertEqual(len(tournaments), 1) self.assertEqual(tournaments[0], tournament)
async def test_filtering(self): tournament = Tournament(name="Tournament") await tournament.save() second_tournament = Tournament(name="Tournament 2") await second_tournament.save() event_first = Event(name="1", tournament=tournament) await event_first.save() event_second = Event(name="2", tournament=second_tournament) await event_second.save() event_third = Event(name="3", tournament=tournament) await event_third.save() event_forth = Event(name="4", tournament=second_tournament) await event_forth.save() team_first = Team(name="First") await team_first.save() team_second = Team(name="Second") await team_second.save() await team_first.events.add(event_first) await event_second.participants.add(team_second) found_events = (await Event.filter( Q(id__in=[event_first.id, event_second.id]) | Q(name="3")).filter(participants__not=team_second.id ).order_by("name", "tournament_id").distinct()) self.assertEqual(len(found_events), 2) self.assertEqual(found_events[0].id, event_first.id) self.assertEqual(found_events[1].id, event_third.id) await Team.filter(events__tournament_id=tournament.id ).order_by("-events__name") await Tournament.filter(events__name__in=["1", "3"]).distinct() teams = await Team.filter(name__icontains="CON") self.assertEqual(len(teams), 1) self.assertEqual(teams[0].name, "Second") tournaments = await Tournament.filter( events__participants__name__startswith="Fir") self.assertEqual(len(tournaments), 1) self.assertEqual(tournaments[0], tournament)
async def test_reset_queryset_on_query(self): tournament = await Tournament.create(name="New Tournament") event = await Event.create(name="Test", tournament_id=tournament.id) participants = [] for i in range(2): team = await Team.create(name="Team {}".format(i + 1)) participants.append(team) await event.participants.add(*participants) queryset = Event.all().annotate(count=Count("participants")) await queryset.first() await queryset.filter(name="Test").first()
async def test_relations(self): tournament = Tournament(name="New Tournament") await tournament.save() 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="Team {}".format(i + 1)) await team.save() participants.append(team) await event.participants.add(participants[0], participants[1]) await event.participants.add(participants[0], participants[1]) with self.assertRaises(NoValuesFetched): [team.id for team in event.participants] # pylint: disable=W0104 teamids = [] async for team in event.participants: teamids.append(team.id) self.assertEqual(teamids, [participants[0].id, participants[1].id]) self.assertEqual([team.id for team in event.participants], [participants[0].id, participants[1].id]) self.assertEqual(event.participants[0].id, participants[0].id) selected_events = await Event.filter(participants=participants[0].id ).prefetch_related( "participants", "tournament") self.assertEqual(len(selected_events), 1) self.assertEqual(selected_events[0].tournament.id, tournament.id) self.assertEqual(len(selected_events[0].participants), 2) await participants[0].fetch_related("events") self.assertEqual(participants[0].events[0], event) await Team.fetch_for_list(participants, "events") await Team.filter(events__tournament__id=tournament.id) await Event.filter(tournament=tournament) await Tournament.filter(events__name__in=["Test", "Prod"]).distinct() result = await Event.filter(id=event.id ).values("id", "name", tournament="tournament__name") self.assertEqual(result[0]["tournament"], tournament.name) result = await Event.filter(id=event.id ).values_list("id", "participants__name") self.assertEqual(len(result), 2)
async def test_relations(self): tournament = Tournament(name='New Tournament') await tournament.save() 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='Team {}'.format(i + 1)) await team.save() participants.append(team) await event.participants.add(participants[0], participants[1]) await event.participants.add(participants[0], participants[1]) with self.assertRaises(NoValuesFetched): [team.id for team in event.participants] # pylint: disable=W0104 teamids = [] async for team in event.participants: teamids.append(team.id) self.assertEqual(teamids, [participants[0].id, participants[1].id]) self.assertEqual([team.id for team in event.participants], [participants[0].id, participants[1].id]) self.assertEqual(event.participants[0].id, participants[0].id) selected_events = await Event.filter(participants=participants[0].id ).prefetch_related( 'participants', 'tournament') self.assertEqual(len(selected_events), 1) self.assertEqual(selected_events[0].tournament.id, tournament.id) self.assertEqual(len(selected_events[0].participants), 2) await participants[0].fetch_related('events') self.assertEqual(participants[0].events[0], event) await Team.fetch_for_list(participants, 'events') await Team.filter(events__tournament__id=tournament.id) await Event.filter(tournament=tournament) await Tournament.filter(events__name__in=['Test', 'Prod']).distinct() result = await Event.filter(id=event.id ).values('id', 'name', tournament='tournament__name') self.assertEqual(result[0]['tournament'], tournament.name) result = await Event.filter(id=event.id ).values_list('id', 'participants__name') self.assertEqual(len(result), 2)
async def test_prefetch_nested(self): tournament = await Tournament.create(name='tournament') event = await Event.create(name='First', tournament=tournament) await Event.create(name='Second', tournament=tournament) team = await Team.create(name='1') team_second = await Team.create(name='2') await event.participants.add(team, team_second) fetched_tournaments = await Tournament.all().prefetch_related( Prefetch('events', queryset=Event.filter(name='First')), Prefetch('events__participants', queryset=Team.filter(name='1'))).first() self.assertEqual(len(fetched_tournaments.events[0].participants), 1)
async def test_prefetch_nested(self): tournament = await Tournament.create(name="tournament") event = await Event.create(name="First", tournament=tournament) await Event.create(name="Second", tournament=tournament) team = await Team.create(name="1") team_second = await Team.create(name="2") await event.participants.add(team, team_second) fetched_tournaments = (await Tournament.all().prefetch_related( Prefetch("events", queryset=Event.filter(name="First")), Prefetch("events__participants", queryset=Team.filter(name="1")), ).first()) self.assertEqual(len(fetched_tournaments.events[0].participants), 1)
async def test_prefetch_nested_with_aggregation(self): tournament = await Tournament.create(name='tournament') event = await Event.create(name='First', tournament=tournament) await Event.create(name='Second', tournament=tournament) team = await Team.create(name='1') team_second = await Team.create(name='2') await event.participants.add(team, team_second) fetched_tournaments = await Tournament.all().prefetch_related( Prefetch('events', queryset=Event.annotate( teams=Count('participants')).filter(teams=2)) ).first() self.assertEqual(len(fetched_tournaments.events), 1) self.assertEqual(fetched_tournaments.events[0].id, event.id)
async def test_prefetch_nested_with_aggregation(self): tournament = await Tournament.create(name="tournament") event = await Event.create(name="First", tournament=tournament) await Event.create(name="Second", tournament=tournament) team = await Team.create(name="1") team_second = await Team.create(name="2") await event.participants.add(team, team_second) fetched_tournaments = (await Tournament.all().prefetch_related( Prefetch("events", queryset=Event.annotate( teams=Count("participants")).filter(teams=2)) ).first()) self.assertEqual(len(fetched_tournaments.events), 1) self.assertEqual(fetched_tournaments.events[0].id, event.id)