diff options
| author | Tom Forbes <tom@tomforb.es> | 2018-08-07 23:08:23 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-08-17 14:58:45 -0400 |
| commit | c02d473781dc2e8699db8edd37cc77f7d43993fc (patch) | |
| tree | a3343a1ff735eb182abb12ae6aded04e24186b8a /tests/generic_relations/tests.py | |
| parent | bf17f5e88466e3f571065345f5b2fea0d8af89fe (diff) | |
Fixed #29612 -- Added GenericRelation prefetch_related() cache invalidation.
Diffstat (limited to 'tests/generic_relations/tests.py')
| -rw-r--r-- | tests/generic_relations/tests.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/generic_relations/tests.py b/tests/generic_relations/tests.py index 4c27b6f3d5..2e99c5b5cf 100644 --- a/tests/generic_relations/tests.py +++ b/tests/generic_relations/tests.py @@ -499,6 +499,53 @@ class GenericRelationsTests(TestCase): tag = TaggedItem(content_object=spinach) self.assertEqual(tag.content_object, spinach) + def test_create_after_prefetch(self): + platypus = Animal.objects.prefetch_related('tags').get(pk=self.platypus.pk) + self.assertSequenceEqual(platypus.tags.all(), []) + weird_tag = platypus.tags.create(tag='weird') + self.assertSequenceEqual(platypus.tags.all(), [weird_tag]) + + def test_add_after_prefetch(self): + platypus = Animal.objects.prefetch_related('tags').get(pk=self.platypus.pk) + self.assertSequenceEqual(platypus.tags.all(), []) + weird_tag = TaggedItem.objects.create(tag='weird', content_object=platypus) + platypus.tags.add(weird_tag) + self.assertSequenceEqual(platypus.tags.all(), [weird_tag]) + + def test_remove_after_prefetch(self): + weird_tag = self.platypus.tags.create(tag='weird') + platypus = Animal.objects.prefetch_related('tags').get(pk=self.platypus.pk) + self.assertSequenceEqual(platypus.tags.all(), [weird_tag]) + platypus.tags.remove(weird_tag) + self.assertSequenceEqual(platypus.tags.all(), []) + + def test_clear_after_prefetch(self): + weird_tag = self.platypus.tags.create(tag='weird') + platypus = Animal.objects.prefetch_related('tags').get(pk=self.platypus.pk) + self.assertSequenceEqual(platypus.tags.all(), [weird_tag]) + platypus.tags.clear() + self.assertSequenceEqual(platypus.tags.all(), []) + + def test_set_after_prefetch(self): + platypus = Animal.objects.prefetch_related('tags').get(pk=self.platypus.pk) + self.assertSequenceEqual(platypus.tags.all(), []) + furry_tag = TaggedItem.objects.create(tag='furry', content_object=platypus) + platypus.tags.set([furry_tag]) + self.assertSequenceEqual(platypus.tags.all(), [furry_tag]) + weird_tag = TaggedItem.objects.create(tag='weird', content_object=platypus) + platypus.tags.set([weird_tag]) + self.assertSequenceEqual(platypus.tags.all(), [weird_tag]) + + def test_add_then_remove_after_prefetch(self): + furry_tag = self.platypus.tags.create(tag='furry') + platypus = Animal.objects.prefetch_related('tags').get(pk=self.platypus.pk) + self.assertSequenceEqual(platypus.tags.all(), [furry_tag]) + weird_tag = self.platypus.tags.create(tag='weird') + platypus.tags.add(weird_tag) + self.assertSequenceEqual(platypus.tags.all(), [furry_tag, weird_tag]) + platypus.tags.remove(weird_tag) + self.assertSequenceEqual(platypus.tags.all(), [furry_tag]) + class ProxyRelatedModelTest(TestCase): def test_default_behavior(self): |
