summaryrefslogtreecommitdiff
path: root/tests/contenttypes_tests
diff options
context:
space:
mode:
authorGiannis Terzopoulos <terzo.giannis@gmail.com>2024-02-09 23:47:06 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2024-03-08 11:09:54 +0100
commit73df8b54a2fab53bec4c7573cda5ad8c869c2fd8 (patch)
tree3bd289c85fb2d99a3f534322ef38f94ca4e33bac /tests/contenttypes_tests
parent74f7fe3f3dd9725e92aa079c5f54b65d66db28de (diff)
Fixed #35044 -- Avoided clearing reverse relations and private fields when accessing deferred fields.
Regression in a7b5ad8b19a08d7d57302ece74f6e26d2887fd9f for reverse relations and possibly in 123b1d3fcf79f091573c40be6da7113a6ef35b62 for private fields.
Diffstat (limited to 'tests/contenttypes_tests')
-rw-r--r--tests/contenttypes_tests/test_fields.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/contenttypes_tests/test_fields.py b/tests/contenttypes_tests/test_fields.py
index 5510f34cd0..15f1dafd63 100644
--- a/tests/contenttypes_tests/test_fields.py
+++ b/tests/contenttypes_tests/test_fields.py
@@ -45,6 +45,18 @@ class GenericForeignKeyTests(TestCase):
new_entity = answer.question
self.assertIsNot(old_entity, new_entity)
+ def test_clear_cached_generic_relation_explicit_fields(self):
+ question = Question.objects.create(text="question")
+ answer = Answer.objects.create(text="answer", question=question)
+ old_question_obj = answer.question
+ # The reverse relation is not refreshed if not passed explicitly in
+ # `fields`.
+ answer.refresh_from_db(fields=["text"])
+ self.assertIs(answer.question, old_question_obj)
+ answer.refresh_from_db(fields=["question"])
+ self.assertIsNot(answer.question, old_question_obj)
+ self.assertEqual(answer.question, old_question_obj)
+
class GenericRelationTests(TestCase):
def test_value_to_string(self):
@@ -55,6 +67,29 @@ class GenericRelationTests(TestCase):
self.assertCountEqual(result, [answer1.pk, answer2.pk])
+class DeferredGenericRelationTests(TestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.question = Question.objects.create(text="question")
+ cls.answer = Answer.objects.create(text="answer", question=cls.question)
+
+ def test_defer_not_clear_cached_private_relations(self):
+ obj = Answer.objects.defer("text").get(pk=self.answer.pk)
+ with self.assertNumQueries(1):
+ obj.question
+ obj.text # Accessing a deferred field.
+ with self.assertNumQueries(0):
+ obj.question
+
+ def test_only_not_clear_cached_private_relations(self):
+ obj = Answer.objects.only("content_type", "object_id").get(pk=self.answer.pk)
+ with self.assertNumQueries(1):
+ obj.question
+ obj.text # Accessing a deferred field.
+ with self.assertNumQueries(0):
+ obj.question
+
+
class GetPrefetchQuerySetDeprecation(TestCase):
def test_generic_relation_warning(self):
Question.objects.create(text="test")