Debian Codegraph @

tests/contenttypes_tests/test_fields.py

raw
1 import json
2
3 from django.contrib.contenttypes.fields import GenericForeignKey
4 from django.contrib.contenttypes.prefetch import GenericPrefetch
5 from django.db import models
6 from django.test import TestCase
7 from django.test.utils import isolate_apps
8 from django.utils.deprecation import RemovedInDjango60Warning
9
10 from .models import Answer, Post, Question
11
12
13 @isolate_apps("contenttypes_tests")
14 class GenericForeignKeyTests(TestCase):
15 def test_str(self):
16 class Model(models.Model):
17 field = GenericForeignKey()
18
19 self.assertEqual(str(Model.field), "contenttypes_tests.Model.field")
20
21 def test_get_content_type_no_arguments(self):
22 with self.assertRaisesMessage(
23 Exception, "Impossible arguments to GFK.get_content_type!"
24 ):
25 Answer.question.get_content_type()
26
27 def test_get_object_cache_respects_deleted_objects(self):
28 question = Question.objects.create(text="Who?")
29 post = Post.objects.create(title="Answer", parent=question)
30
31 question_pk = question.pk
32 Question.objects.all().delete()
33
34 post = Post.objects.get(pk=post.pk)
35 with self.assertNumQueries(1):
36 self.assertEqual(post.object_id, question_pk)
37 self.assertIsNone(post.parent)
38 self.assertIsNone(post.parent)
39
40 def test_clear_cached_generic_relation(self):
41 question = Question.objects.create(text="What is your name?")
42 answer = Answer.objects.create(text="Answer", question=question)
43 old_entity = answer.question
44 answer.refresh_from_db()
45 new_entity = answer.question
46 self.assertIsNot(old_entity, new_entity)
47
48 def test_clear_cached_generic_relation_explicit_fields(self):
49 question = Question.objects.create(text="question")
50 answer = Answer.objects.create(text="answer", question=question)
51 old_question_obj = answer.question
52 # The reverse relation is not refreshed if not passed explicitly in
53 # `fields`.
54 answer.refresh_from_db(fields=["text"])
55 self.assertIs(answer.question, old_question_obj)
56 answer.refresh_from_db(fields=["question"])
57 self.assertIsNot(answer.question, old_question_obj)
58 self.assertEqual(answer.question, old_question_obj)
59
60 def test_clear_cached_generic_relation_when_deferred(self):
61 question = Question.objects.create(text="question")
62 Answer.objects.create(text="answer", question=question)
63 answer = Answer.objects.defer("text").get()
64 old_question_obj = answer.question
65 # The reverse relation is refreshed even when the text field is deferred.
66 answer.refresh_from_db()
67 self.assertIsNot(answer.question, old_question_obj)
68
69
70 class GenericRelationTests(TestCase):
71 def test_value_to_string(self):
72 question = Question.objects.create(text="test")
73 answer1 = Answer.objects.create(question=question)
74 answer2 = Answer.objects.create(question=question)
75 result = json.loads(Question.answer_set.field.value_to_string(question))
76 self.assertCountEqual(result, [answer1.pk, answer2.pk])
77
78
79 class DeferredGenericRelationTests(TestCase):
80 @classmethod
81 def setUpTestData(cls):
82 cls.question = Question.objects.create(text="question")
83 cls.answer = Answer.objects.create(text="answer", question=cls.question)
84
85 def test_defer_not_clear_cached_private_relations(self):
86 obj = Answer.objects.defer("text").get(pk=self.answer.pk)
87 with self.assertNumQueries(1):
88 obj.question
89 obj.text # Accessing a deferred field.
90 with self.assertNumQueries(0):
91 obj.question
92
93 def test_only_not_clear_cached_private_relations(self):
94 obj = Answer.objects.only("content_type", "object_id").get(pk=self.answer.pk)
95 with self.assertNumQueries(1):
96 obj.question
97 obj.text # Accessing a deferred field.
98 with self.assertNumQueries(0):
99 obj.question
100
101
102 class GetPrefetchQuerySetDeprecation(TestCase):
103 def test_generic_relation_warning(self):
104 Question.objects.create(text="test")
105 questions = Question.objects.all()
106 msg = (
107 "get_prefetch_queryset() is deprecated. Use get_prefetch_querysets() "
108 "instead."
109 )
110 with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
111 questions[0].answer_set.get_prefetch_queryset(questions)
112 self.assertEqual(ctx.filename, __file__)
113
114 def test_generic_foreign_key_warning(self):
115 answers = Answer.objects.all()
116 msg = (
117 "get_prefetch_queryset() is deprecated. Use get_prefetch_querysets() "
118 "instead."
119 )
120 with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
121 Answer.question.get_prefetch_queryset(answers)
122 self.assertEqual(ctx.filename, __file__)
123
124
125 class GetPrefetchQuerySetsTests(TestCase):
126 def test_duplicate_querysets(self):
127 question = Question.objects.create(text="What is your name?")
128 answer = Answer.objects.create(text="Joe", question=question)
129 answer = Answer.objects.get(pk=answer.pk)
130 msg = "Only one queryset is allowed for each content type."
131 with self.assertRaisesMessage(ValueError, msg):
132 models.prefetch_related_objects(
133 [answer],
134 GenericPrefetch(
135 "question",
136 [
137 Question.objects.all(),
138 Question.objects.filter(text__startswith="test"),
139 ],
140 ),
141 )
142
143 def test_generic_relation_invalid_length(self):
144 Question.objects.create(text="test")
145 questions = Question.objects.all()
146 msg = (
147 "querysets argument of get_prefetch_querysets() should have a length of 1."
148 )
149 with self.assertRaisesMessage(ValueError, msg):
150 questions[0].answer_set.get_prefetch_querysets(
151 instances=questions,
152 querysets=[Answer.objects.all(), Question.objects.all()],
153 )