def test_mixed(self, django_assert_num_queries): for i in range(10): if i % 2 == 0: parent = None else: parent = factories.CatFactory( archetype=factories.ArchetypeFactory(num_toes=3)) factories.CatFactory( parent=parent, archetype=factories.ArchetypeFactory(num_toes=5)) with django_assert_num_queries(1): for i, cat in enumerate( models.Cat.objects.include('parent').filter( archetype__num_toes=5)): if i % 2 == 0: assert cat.parent is None else: assert cat.parent is not None with django_assert_num_queries(1): for i, cat in enumerate( models.Cat.objects.filter( archetype__num_toes=5).include('parent')): if i % 2 == 0: assert cat.parent is None else: assert cat.parent is not None
def test_nested(self, django_assert_num_queries): for _ in range(10): parent = factories.CatFactory() for _ in range(10): child = factories.CatFactory(parent=parent) with django_assert_num_queries(1): for cat in models.Cat.objects.include( 'children__archetype').filter(parent__isnull=True): assert len(cat.children.all()) == 10 for child in cat.children.all(): assert child.archetype is not None assert models.Cat.objects.include('children__archetype').filter( parent__isnull=True).count() == 10
def test_many_to_one(self, django_assert_num_queries): for _ in range(10): parent = factories.CatFactory() for _ in range(2): factories.CatFactory(parent=parent) with django_assert_num_queries(1): for cat in models.Cat.objects.include('children').filter( parent__isnull=True): assert len(cat.children.all()) == 2 for child in cat.children.all(): assert child.parent == cat assert len(models.Cat.objects.filter(parent__isnull=True)) == 10
def test_multiple_nested(self, django_assert_num_queries): for i in range(10): factories.CatFactory( archetype=factories.ArchetypeFactory(num_toes=5), parent=factories.CatFactory( archetype=factories.ArchetypeFactory(num_toes=3))) with django_assert_num_queries(1): for i, cat in enumerate( models.Cat.objects.include( 'archetype', 'parent__archetype').filter(archetype__num_toes=5)): assert cat.archetype.num_toes == 5 assert cat.parent.archetype.num_toes == 3 assert cat.parent_id == cat.parent.id assert cat.archetype_id == cat.archetype.id assert cat.parent.archetype_id == cat.parent.archetype.id
def test_nullable(self, django_assert_num_queries): for _ in range(10): factories.CatFactory() assert len(models.Cat.objects.include('parent').all()) == 10 with django_assert_num_queries(1): for cat in models.Cat.objects.include('parent'): assert cat.parent is None
def test_reverse_gfk(self, django_assert_num_queries): cat = factories.CatFactory() for _ in range(7): factories.AliasFactory(describes=cat) with django_assert_num_queries(1): ket = models.Cat.objects.include('aliases').get(pk=cat.id) assert len(ket.aliases.all()) == 7
def test_include(self, django_assert_num_queries): cat1 = factories.CatFactory() siblings = factories.CatFactory.create_batch(5) cat1.siblings.add(*siblings) with django_assert_num_queries(1): cat = models.Cat.objects.filter( id=cat1.id).include('siblings').first() for sib in cat.siblings.all(): assert sib in siblings assert len(cat.siblings.all()) == 5
def test_related_filter_requeries(self, django_assert_num_queries): a1, a2 = factories.ArchetypeFactory(), factories.ArchetypeFactory() for _ in range(10): parent = factories.CatFactory() for i in range(10): factories.CatFactory(archetype=a1 if i % 2 == 0 else a2, parent=parent) for parent in models.Cat.objects.include('children').filter( parent__isnull=True): with django_assert_num_queries(0): assert len(parent.children.all()) == 10 with django_assert_num_queries(1): assert len(parent.children.filter(archetype=a1)) == 5 with django_assert_num_queries(1): assert len(parent.children.all().filter(archetype=a2)) == 5
def test_nested(self, django_assert_num_queries): cat1 = factories.CatFactory() cat1.siblings.add(*factories.CatFactory.create_batch(3)) for sib in cat1.siblings.all(): sib.siblings.add(*factories.CatFactory.create_batch(2)) with django_assert_num_queries(1): cat1_i = models.Cat.objects.include('siblings__siblings').filter( id=cat1.id).first() assert len(cat1_i.siblings.all()) == 3 for sib in cat1_i.siblings.all(): assert len(sib.siblings.all()) == 2
def test_nested_same(self, django_assert_num_queries): for _ in range(10): parent = factories.CatFactory() for _ in range(10): child = factories.CatFactory(parent=parent) for _ in range(10): factories.CatFactory(parent=child) with django_assert_num_queries(1): for cat in models.Cat.objects.include('children__children').filter( parent__isnull=True): assert len(cat.children.all()) == 10 for child in cat.children.all(): assert child.parent == cat assert len(child.children.all()) == 10 for grandchild in child.children.all(): assert grandchild.parent == child assert models.Cat.objects.include('children__children').filter( parent__isnull=True).count() == 10
def test_one_query(self, django_assert_num_queries): archetype = factories.ArchetypeFactory( color='Someone is looking for you', num_toes=6) for _ in range(10): factories.CatFactory(archetype=archetype) with django_assert_num_queries(1): for cat in models.Cat.objects.include('archetype'): assert cat.archetype is not None assert cat.archetype is not archetype assert cat.archetype.pk == archetype.pk assert cat.archetype.num_toes == 6 assert cat.archetype.color == 'Someone is looking for you'
def test_include(self, django_assert_num_queries): cats = factories.CatFactory.create_batch(5) for cat in cats: factories.CatFactory(emergency_contact=cat) with django_assert_num_queries(1): for cat in models.Cat.objects.filter( emergency_contact__isnull=False).include( 'emergency_contact__emergency_contact_for'): assert cat.emergency_contact is not None assert cat.emergency_contact.emergency_contact_for.id == cat.id assert isinstance(cat.emergency_contact, models.Cat) assert cat.emergency_contact.id != cat.id with django_assert_num_queries(1): for cat in models.Cat.objects.filter( emergency_contact__isnull=True).include( 'emergency_contact'): assert cat.emergency_contact is None
def test_filter(self, django_assert_num_queries): cat1 = factories.CatFactory() siblings = factories.CatFactory.create_batch(3, name='Henry') siblings += factories.CatFactory.create_batch(2, name='George') cat1.siblings.add(*siblings) # If .filter is called, our cache is invalid and must be ignored with django_assert_num_queries(4): cat = models.Cat.objects.filter( id=cat1.id).include('siblings').first() for sib in cat.siblings.filter(): assert sib in siblings for sib in cat.siblings.filter(name='Henry'): assert sib.name == 'Henry' assert sib in siblings[:3] for sib in cat.siblings.filter(name='George'): assert sib.name == 'George' assert sib in siblings[3:] assert len(cat.siblings.all()) == 5
def test_empty(self): for _ in range(10): factories.CatFactory() assert len(models.Cat.objects.include().all()) == 10 assert models.Cat.objects.include().all().count() == 10
def test_many_to_one(self): for _ in range(10): factories.CatFactory() assert len(models.Cat.objects.include('archetype').all()) == 10