From 771e06af2aacff5d544c3004fa6548749ed81a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Thu, 21 Sep 2017 14:21:12 -0400 Subject: [PATCH] Added a test for QuerySet.select_related() reverse caching. --- tests/select_related/tests.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/select_related/tests.py b/tests/select_related/tests.py index c19984e267..0af10f207f 100644 --- a/tests/select_related/tests.py +++ b/tests/select_related/tests.py @@ -150,6 +150,15 @@ class SelectRelatedTests(TestCase): self.assertEqual(obj.parent_1, parent_1) self.assertEqual(obj.parent_2, parent_2) + def test_reverse_relation_caching(self): + species = Species.objects.select_related('genus').filter(name='melanogaster').first() + with self.assertNumQueries(0): + self.assertEqual(species.genus.name, 'Drosophila') + # The species_set reverse relation isn't cached. + self.assertEqual(species.genus._state.fields_cache, {}) + with self.assertNumQueries(1): + self.assertEqual(species.genus.species_set.first().name, 'melanogaster') + def test_select_related_after_values(self): """ Running select_related() after calling values() raises a TypeError