def test_students_cached(self, db): """students property is cached to avoid redundant queries.""" e = factories.RelationshipFactory.create().elder with utils.assert_num_queries(1): list(e.students) list(e.students)
def test_get_user_select_related(db): """get_user method returns user with profile pre-selected.""" profile = factories.ProfileFactory.create() backend = auth_backend.EmailBackend() user = backend.get_user(profile.user.id) with utils.assert_num_queries(0): user.profile
def test_authenticate_select_related(db): """authenticate method returns user with profile pre-selected.""" factories.ProfileFactory.create(user__email='*****@*****.**', user__password='******') backend = auth_backend.EmailBackend() user = backend.authenticate(username='******', password='******') with utils.assert_num_queries(0): user.profile
def test_authenticate_select_related(db): """authenticate method returns user with profile pre-selected.""" factories.ProfileFactory.create( user__email='*****@*****.**', user__password='******') backend = auth_backend.EmailBackend() user = backend.authenticate(username='******', password='******') with utils.assert_num_queries(0): user.profile
def test_prefetch_elders(self, db): """Can prefetch elders to avoid N queries.""" # A and B demonstrate that relationship lists are ordered by name r1b = factories.RelationshipFactory.create(from_profile__name='B') r1a = factories.RelationshipFactory.create(to_profile=r1b.student, from_profile__name='A') r2 = factories.RelationshipFactory.create() # one query for profiles and one for elder relationships with utils.assert_num_queries(2): qs = model.Profile.objects.prefetch_elders() assert set(tuple(p.elders) for p in qs) == {(r1a.elder, r1b.elder), (r2.elder, ), ()}
def test_prefetch_relationships(self, db): """Can prefetch students and elders in a single query.""" r1 = factories.RelationshipFactory.create() r2 = factories.RelationshipFactory.create() # one query for profiles and one for relationships with utils.assert_num_queries(2): # order_by call demonstrates persistence after a clone qs = model.Profile.objects.prefetch_relationships().order_by( 'name') assert set(tuple(p.students) for p in qs) == {(r1.student, ), (r2.student, ), ()} assert set(tuple(p.elders) for p in qs) == {(r1.elder, ), (r2.elder, ), ()}
def test_prefetch_students(self, db): """Can prefetch students to avoid N queries.""" # A and B demonstrate that relationship lists are ordered by name r1b = factories.RelationshipFactory.create(to_profile__name='B') r1a = factories.RelationshipFactory.create( from_profile=r1b.elder, to_profile__name='A') r2 = factories.RelationshipFactory.create() # one query for profiles and one for student relationships with utils.assert_num_queries(2): qs = model.Profile.objects.prefetch_students() assert set( tuple(p.students) for p in qs ) == {(r1a.student, r1b.student), (r2.student,), ()}
def test_prefetch_with_already_populated_result_cache(self, db): """Can prefetch on an already-populated queryset.""" r1 = factories.RelationshipFactory.create() r2 = factories.RelationshipFactory.create() qs = model.Profile.objects.all() # populate its result cache len(qs) # set it to prefetch without triggering a clone qs._prefetch_elders = True # just one query for elder relationships with utils.assert_num_queries(1): assert set(tuple(p.elders) for p in qs) == {(r1.elder, ), (r2.elder, ), ()}
def test_prefetch_relationships(self, db): """Can prefetch students and elders in a single query.""" r1 = factories.RelationshipFactory.create() r2 = factories.RelationshipFactory.create() # one query for profiles and one for relationships with utils.assert_num_queries(2): # order_by call demonstrates persistence after a clone qs = model.Profile.objects.prefetch_relationships().order_by('name') assert set( tuple(p.students) for p in qs ) == {(r1.student,), (r2.student,), ()} assert set( tuple(p.elders) for p in qs ) == {(r1.elder,), (r2.elder,), ()}
def test_prefetch_with_already_populated_result_cache(self, db): """Can prefetch on an already-populated queryset.""" r1 = factories.RelationshipFactory.create() r2 = factories.RelationshipFactory.create() qs = model.Profile.objects.all() # populate its result cache len(qs) # set it to prefetch without triggering a clone qs._prefetch_elders = True # just one query for elder relationships with utils.assert_num_queries(1): assert set( tuple(p.elders) for p in qs ) == {(r1.elder,), (r2.elder,), ()}