Debian Codegraph @

tests/queries/test_bulk_update.py

raw
1 import datetime
2
3 from django.core.exceptions import FieldDoesNotExist
4 from django.db.models import F, IntegerField, Value
5 from django.db.models.functions import Coalesce, Lower
6 from django.db.utils import IntegrityError
7 from django.test import TestCase, override_settings, skipUnlessDBFeature
8
9 from .models import (
10 Article,
11 CustomDbColumn,
12 CustomPk,
13 Detail,
14 Food,
15 Individual,
16 JSONFieldNullable,
17 Member,
18 Note,
19 Number,
20 Order,
21 Paragraph,
22 RelatedObject,
23 SingleObject,
24 SpecialCategory,
25 Tag,
26 Valid,
27 )
28
29
30 class WriteToOtherRouter:
31 def db_for_write(self, model, **hints):
32 return "other"
33
34
35 class BulkUpdateNoteTests(TestCase):
36 @classmethod
37 def setUpTestData(cls):
38 cls.notes = [Note.objects.create(note=str(i), misc=str(i)) for i in range(10)]
39
40 def create_tags(self):
41 self.tags = [Tag.objects.create(name=str(i)) for i in range(10)]
42
43 def test_simple(self):
44 for note in self.notes:
45 note.note = "test-%s" % note.id
46 with self.assertNumQueries(1):
47 Note.objects.bulk_update(self.notes, ["note"])
48 self.assertCountEqual(
49 Note.objects.values_list("note", flat=True),
50 [cat.note for cat in self.notes],
51 )
52
53 def test_multiple_fields(self):
54 for note in self.notes:
55 note.note = "test-%s" % note.id
56 note.misc = "misc-%s" % note.id
57 with self.assertNumQueries(1):
58 Note.objects.bulk_update(self.notes, ["note", "misc"])
59 self.assertCountEqual(
60 Note.objects.values_list("note", flat=True),
61 [cat.note for cat in self.notes],
62 )
63 self.assertCountEqual(
64 Note.objects.values_list("misc", flat=True),
65 [cat.misc for cat in self.notes],
66 )
67
68 def test_batch_size(self):
69 with self.assertNumQueries(len(self.notes)):
70 Note.objects.bulk_update(self.notes, fields=["note"], batch_size=1)
71
72 def test_unsaved_models(self):
73 objs = self.notes + [Note(note="test", misc="test")]
74 msg = "All bulk_update() objects must have a primary key set."
75 with self.assertRaisesMessage(ValueError, msg):
76 Note.objects.bulk_update(objs, fields=["note"])
77
78 def test_foreign_keys_do_not_lookup(self):
79 self.create_tags()
80 for note, tag in zip(self.notes, self.tags):
81 note.tag = tag
82 with self.assertNumQueries(1):
83 Note.objects.bulk_update(self.notes, ["tag"])
84 self.assertSequenceEqual(Note.objects.filter(tag__isnull=False), self.notes)
85
86 def test_set_field_to_null(self):
87 self.create_tags()
88 Note.objects.update(tag=self.tags[0])
89 for note in self.notes:
90 note.tag = None
91 Note.objects.bulk_update(self.notes, ["tag"])
92 self.assertCountEqual(Note.objects.filter(tag__isnull=True), self.notes)
93
94 def test_set_mixed_fields_to_null(self):
95 self.create_tags()
96 midpoint = len(self.notes) // 2
97 top, bottom = self.notes[:midpoint], self.notes[midpoint:]
98 for note in top:
99 note.tag = None
100 for note in bottom:
101 note.tag = self.tags[0]
102 Note.objects.bulk_update(self.notes, ["tag"])
103 self.assertCountEqual(Note.objects.filter(tag__isnull=True), top)
104 self.assertCountEqual(Note.objects.filter(tag__isnull=False), bottom)
105
106 def test_functions(self):
107 Note.objects.update(note="TEST")
108 for note in self.notes:
109 note.note = Lower("note")
110 Note.objects.bulk_update(self.notes, ["note"])
111 self.assertEqual(set(Note.objects.values_list("note", flat=True)), {"test"})
112
113 # Tests that use self.notes go here, otherwise put them in another class.
114
115
116 class BulkUpdateTests(TestCase):
117 databases = {"default", "other"}
118
119 def test_no_fields(self):
120 msg = "Field names must be given to bulk_update()."
121 with self.assertRaisesMessage(ValueError, msg):
122 Note.objects.bulk_update([], fields=[])
123
124 def test_invalid_batch_size(self):
125 msg = "Batch size must be a positive integer."
126 with self.assertRaisesMessage(ValueError, msg):
127 Note.objects.bulk_update([], fields=["note"], batch_size=-1)
128 with self.assertRaisesMessage(ValueError, msg):
129 Note.objects.bulk_update([], fields=["note"], batch_size=0)
130
131 def test_nonexistent_field(self):
132 with self.assertRaisesMessage(
133 FieldDoesNotExist, "Note has no field named 'nonexistent'"
134 ):
135 Note.objects.bulk_update([], ["nonexistent"])
136
137 pk_fields_error = "bulk_update() cannot be used with primary key fields."
138
139 def test_update_primary_key(self):
140 with self.assertRaisesMessage(ValueError, self.pk_fields_error):
141 Note.objects.bulk_update([], ["id"])
142
143 def test_update_custom_primary_key(self):
144 with self.assertRaisesMessage(ValueError, self.pk_fields_error):
145 CustomPk.objects.bulk_update([], ["name"])
146
147 def test_update_inherited_primary_key(self):
148 with self.assertRaisesMessage(ValueError, self.pk_fields_error):
149 SpecialCategory.objects.bulk_update([], ["id"])
150
151 def test_empty_objects(self):
152 with self.assertNumQueries(0):
153 rows_updated = Note.objects.bulk_update([], ["note"])
154 self.assertEqual(rows_updated, 0)
155
156 def test_large_batch(self):
157 Note.objects.bulk_create(
158 [Note(note=str(i), misc=str(i)) for i in range(0, 2000)]
159 )
160 notes = list(Note.objects.all())
161 rows_updated = Note.objects.bulk_update(notes, ["note"])
162 self.assertEqual(rows_updated, 2000)
163
164 def test_updated_rows_when_passing_duplicates(self):
165 note = Note.objects.create(note="test-note", misc="test")
166 rows_updated = Note.objects.bulk_update([note, note], ["note"])
167 self.assertEqual(rows_updated, 1)
168 # Duplicates in different batches.
169 rows_updated = Note.objects.bulk_update([note, note], ["note"], batch_size=1)
170 self.assertEqual(rows_updated, 2)
171
172 def test_only_concrete_fields_allowed(self):
173 obj = Valid.objects.create(valid="test")
174 detail = Detail.objects.create(data="test")
175 paragraph = Paragraph.objects.create(text="test")
176 Member.objects.create(name="test", details=detail)
177 msg = "bulk_update() can only be used with concrete fields."
178 with self.assertRaisesMessage(ValueError, msg):
179 Detail.objects.bulk_update([detail], fields=["member"])
180 with self.assertRaisesMessage(ValueError, msg):
181 Paragraph.objects.bulk_update([paragraph], fields=["page"])
182 with self.assertRaisesMessage(ValueError, msg):
183 Valid.objects.bulk_update([obj], fields=["parent"])
184
185 def test_custom_db_columns(self):
186 model = CustomDbColumn.objects.create(custom_column=1)
187 model.custom_column = 2
188 CustomDbColumn.objects.bulk_update([model], fields=["custom_column"])
189 model.refresh_from_db()
190 self.assertEqual(model.custom_column, 2)
191
192 def test_custom_pk(self):
193 custom_pks = [
194 CustomPk.objects.create(name="pk-%s" % i, extra="") for i in range(10)
195 ]
196 for model in custom_pks:
197 model.extra = "extra-%s" % model.pk
198 CustomPk.objects.bulk_update(custom_pks, ["extra"])
199 self.assertCountEqual(
200 CustomPk.objects.values_list("extra", flat=True),
201 [cat.extra for cat in custom_pks],
202 )
203
204 def test_falsey_pk_value(self):
205 order = Order.objects.create(pk=0, name="test")
206 order.name = "updated"
207 Order.objects.bulk_update([order], ["name"])
208 order.refresh_from_db()
209 self.assertEqual(order.name, "updated")
210
211 def test_inherited_fields(self):
212 special_categories = [
213 SpecialCategory.objects.create(name=str(i), special_name=str(i))
214 for i in range(10)
215 ]
216 for category in special_categories:
217 category.name = "test-%s" % category.id
218 category.special_name = "special-test-%s" % category.special_name
219 SpecialCategory.objects.bulk_update(
220 special_categories, ["name", "special_name"]
221 )
222 self.assertCountEqual(
223 SpecialCategory.objects.values_list("name", flat=True),
224 [cat.name for cat in special_categories],
225 )
226 self.assertCountEqual(
227 SpecialCategory.objects.values_list("special_name", flat=True),
228 [cat.special_name for cat in special_categories],
229 )
230
231 def test_field_references(self):
232 numbers = [Number.objects.create(num=0) for _ in range(10)]
233 for number in numbers:
234 number.num = F("num") + 1
235 Number.objects.bulk_update(numbers, ["num"])
236 self.assertCountEqual(Number.objects.filter(num=1), numbers)
237
238 def test_f_expression(self):
239 notes = [
240 Note.objects.create(note="test_note", misc="test_misc") for _ in range(10)
241 ]
242 for note in notes:
243 note.misc = F("note")
244 Note.objects.bulk_update(notes, ["misc"])
245 self.assertCountEqual(Note.objects.filter(misc="test_note"), notes)
246
247 def test_booleanfield(self):
248 individuals = [Individual.objects.create(alive=False) for _ in range(10)]
249 for individual in individuals:
250 individual.alive = True
251 Individual.objects.bulk_update(individuals, ["alive"])
252 self.assertCountEqual(Individual.objects.filter(alive=True), individuals)
253
254 def test_ipaddressfield(self):
255 for ip in ("2001::1", "1.2.3.4"):
256 with self.subTest(ip=ip):
257 models = [
258 CustomDbColumn.objects.create(ip_address="0.0.0.0")
259 for _ in range(10)
260 ]
261 for model in models:
262 model.ip_address = ip
263 CustomDbColumn.objects.bulk_update(models, ["ip_address"])
264 self.assertCountEqual(
265 CustomDbColumn.objects.filter(ip_address=ip), models
266 )
267
268 def test_datetime_field(self):
269 articles = [
270 Article.objects.create(name=str(i), created=datetime.datetime.today())
271 for i in range(10)
272 ]
273 point_in_time = datetime.datetime(1991, 10, 31)
274 for article in articles:
275 article.created = point_in_time
276 Article.objects.bulk_update(articles, ["created"])
277 self.assertCountEqual(Article.objects.filter(created=point_in_time), articles)
278
279 @skipUnlessDBFeature("supports_json_field")
280 def test_json_field(self):
281 JSONFieldNullable.objects.bulk_create(
282 [JSONFieldNullable(json_field={"a": i}) for i in range(10)]
283 )
284 objs = JSONFieldNullable.objects.all()
285 for obj in objs:
286 obj.json_field = {"c": obj.json_field["a"] + 1}
287 JSONFieldNullable.objects.bulk_update(objs, ["json_field"])
288 self.assertCountEqual(
289 JSONFieldNullable.objects.filter(json_field__has_key="c"), objs
290 )
291
292 @skipUnlessDBFeature("supports_json_field")
293 def test_json_field_sql_null(self):
294 obj = JSONFieldNullable.objects.create(json_field={})
295 test_cases = [
296 ("direct_none_assignment", None),
297 ("value_none_assignment", Value(None)),
298 (
299 "expression_none_assignment",
300 Coalesce(None, None, output_field=IntegerField()),
301 ),
302 ]
303 for label, value in test_cases:
304 with self.subTest(case=label):
305 obj.json_field = value
306 JSONFieldNullable.objects.bulk_update([obj], fields=["json_field"])
307 obj.refresh_from_db()
308 sql_null_qs = JSONFieldNullable.objects.filter(json_field__isnull=True)
309 self.assertSequenceEqual(sql_null_qs, [obj])
310
311 def test_nullable_fk_after_related_save(self):
312 parent = RelatedObject.objects.create()
313 child = SingleObject()
314 parent.single = child
315 parent.single.save()
316 RelatedObject.objects.bulk_update([parent], fields=["single"])
317 self.assertEqual(parent.single_id, parent.single.pk)
318 parent.refresh_from_db()
319 self.assertEqual(parent.single, child)
320
321 def test_unsaved_parent(self):
322 parent = RelatedObject.objects.create()
323 parent.single = SingleObject()
324 msg = (
325 "bulk_update() prohibited to prevent data loss due to unsaved "
326 "related object 'single'."
327 )
328 with self.assertRaisesMessage(ValueError, msg):
329 RelatedObject.objects.bulk_update([parent], fields=["single"])
330
331 def test_unspecified_unsaved_parent(self):
332 parent = RelatedObject.objects.create()
333 parent.single = SingleObject()
334 parent.f = 42
335 RelatedObject.objects.bulk_update([parent], fields=["f"])
336 parent.refresh_from_db()
337 self.assertEqual(parent.f, 42)
338 self.assertIsNone(parent.single)
339
340 @override_settings(DATABASE_ROUTERS=[WriteToOtherRouter()])
341 def test_database_routing(self):
342 note = Note.objects.create(note="create")
343 note.note = "bulk_update"
344 with self.assertNumQueries(1, using="other"):
345 Note.objects.bulk_update([note], fields=["note"])
346
347 @override_settings(DATABASE_ROUTERS=[WriteToOtherRouter()])
348 def test_database_routing_batch_atomicity(self):
349 f1 = Food.objects.create(name="Banana")
350 f2 = Food.objects.create(name="Apple")
351 f1.name = "Kiwi"
352 f2.name = "Kiwi"
353 with self.assertRaises(IntegrityError):
354 Food.objects.bulk_update([f1, f2], fields=["name"], batch_size=1)
355 self.assertIs(Food.objects.filter(name="Kiwi").exists(), False)