Debian Codegraph @

tests/serializers/test_data.py

raw
1 """
2 A test spanning all the capabilities of all the serializers.
3
4 This class defines sample data and a dynamically generated
5 test case that is capable of testing the capabilities of
6 the serializers. This includes all valid data values, plus
7 forward, backwards and self references.
8 """
9
10 import datetime
11 import decimal
12 import uuid
13 from collections import namedtuple
14
15 from django.core import serializers
16 from django.db import connection, models
17 from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
18
19 from .models import (
20 Anchor,
21 AutoNowDateTimeData,
22 BigIntegerData,
23 BinaryData,
24 BooleanData,
25 BooleanPKData,
26 CharData,
27 CharPKData,
28 DateData,
29 DatePKData,
30 DateTimeData,
31 DateTimePKData,
32 DecimalData,
33 DecimalPKData,
34 EmailData,
35 EmailPKData,
36 ExplicitInheritBaseModel,
37 FileData,
38 FilePathData,
39 FilePathPKData,
40 FKData,
41 FKDataToField,
42 FKDataToO2O,
43 FKSelfData,
44 FKToUUID,
45 FloatData,
46 FloatPKData,
47 GenericData,
48 GenericIPAddressData,
49 GenericIPAddressPKData,
50 ImageData,
51 InheritAbstractModel,
52 InheritBaseModel,
53 IntegerData,
54 IntegerPKData,
55 Intermediate,
56 LengthModel,
57 M2MData,
58 M2MIntermediateData,
59 M2MSelfData,
60 ModifyingSaveData,
61 O2OData,
62 PositiveBigIntegerData,
63 PositiveIntegerData,
64 PositiveIntegerPKData,
65 PositiveSmallIntegerData,
66 PositiveSmallIntegerPKData,
67 SlugData,
68 SlugPKData,
69 SmallData,
70 SmallPKData,
71 Tag,
72 TextData,
73 TextPKData,
74 TimeData,
75 TimePKData,
76 UniqueAnchor,
77 UUIDData,
78 UUIDDefaultData,
79 )
80 from .tests import register_tests
81
82 # A set of functions that can be used to recreate
83 # test data objects of various kinds.
84 # The save method is a raw base model save, to make
85 # sure that the data in the database matches the
86 # exact test case.
87
88
89 def data_create(pk, klass, data):
90 instance = klass(id=pk)
91 instance.data = data
92 models.Model.save_base(instance, raw=True)
93 return [instance]
94
95
96 def generic_create(pk, klass, data):
97 instance = klass(id=pk)
98 instance.data = data[0]
99 models.Model.save_base(instance, raw=True)
100 for tag in data[1:]:
101 instance.tags.create(data=tag)
102 return [instance]
103
104
105 def fk_create(pk, klass, data):
106 instance = klass(id=pk)
107 setattr(instance, "data_id", data)
108 models.Model.save_base(instance, raw=True)
109 return [instance]
110
111
112 def m2m_create(pk, klass, data):
113 instance = klass(id=pk)
114 models.Model.save_base(instance, raw=True)
115 instance.data.set(data)
116 return [instance]
117
118
119 def im2m_create(pk, klass, data):
120 instance = klass(id=pk)
121 models.Model.save_base(instance, raw=True)
122 return [instance]
123
124
125 def im_create(pk, klass, data):
126 instance = klass(id=pk)
127 instance.right_id = data["right"]
128 instance.left_id = data["left"]
129 if "extra" in data:
130 instance.extra = data["extra"]
131 models.Model.save_base(instance, raw=True)
132 return [instance]
133
134
135 def o2o_create(pk, klass, data):
136 instance = klass()
137 instance.data_id = data
138 models.Model.save_base(instance, raw=True)
139 return [instance]
140
141
142 def pk_create(pk, klass, data):
143 instance = klass()
144 instance.data = data
145 models.Model.save_base(instance, raw=True)
146 return [instance]
147
148
149 def inherited_create(pk, klass, data):
150 instance = klass(id=pk, **data)
151 # This isn't a raw save because:
152 # 1) we're testing inheritance, not field behavior, so none
153 # of the field values need to be protected.
154 # 2) saving the child class and having the parent created
155 # automatically is easier than manually creating both.
156 models.Model.save(instance)
157 created = [instance]
158 for klass in instance._meta.parents:
159 created.append(klass.objects.get(id=pk))
160 return created
161
162
163 # A set of functions that can be used to compare
164 # test data objects of various kinds
165
166
167 def data_compare(testcase, pk, klass, data):
168 instance = klass.objects.get(id=pk)
169 if klass == BinaryData and data is not None:
170 testcase.assertEqual(
171 bytes(data),
172 bytes(instance.data),
173 "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)"
174 % (
175 pk,
176 repr(bytes(data)),
177 type(data),
178 repr(bytes(instance.data)),
179 type(instance.data),
180 ),
181 )
182 else:
183 testcase.assertEqual(
184 data,
185 instance.data,
186 "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)"
187 % (
188 pk,
189 data,
190 type(data),
191 instance,
192 type(instance.data),
193 ),
194 )
195
196
197 def generic_compare(testcase, pk, klass, data):
198 instance = klass.objects.get(id=pk)
199 testcase.assertEqual(data[0], instance.data)
200 testcase.assertEqual(data[1:], [t.data for t in instance.tags.order_by("id")])
201
202
203 def fk_compare(testcase, pk, klass, data):
204 instance = klass.objects.get(id=pk)
205 testcase.assertEqual(data, instance.data_id)
206
207
208 def m2m_compare(testcase, pk, klass, data):
209 instance = klass.objects.get(id=pk)
210 testcase.assertEqual(data, [obj.id for obj in instance.data.order_by("id")])
211
212
213 def im2m_compare(testcase, pk, klass, data):
214 klass.objects.get(id=pk)
215 # actually nothing else to check, the instance just should exist
216
217
218 def im_compare(testcase, pk, klass, data):
219 instance = klass.objects.get(id=pk)
220 testcase.assertEqual(data["left"], instance.left_id)
221 testcase.assertEqual(data["right"], instance.right_id)
222 if "extra" in data:
223 testcase.assertEqual(data["extra"], instance.extra)
224 else:
225 testcase.assertEqual("doesn't matter", instance.extra)
226
227
228 def o2o_compare(testcase, pk, klass, data):
229 instance = klass.objects.get(data=data)
230 testcase.assertEqual(data, instance.data_id)
231
232
233 def pk_compare(testcase, pk, klass, data):
234 instance = klass.objects.get(data=data)
235 testcase.assertEqual(data, instance.data)
236
237
238 def inherited_compare(testcase, pk, klass, data):
239 instance = klass.objects.get(id=pk)
240 for key, value in data.items():
241 testcase.assertEqual(value, getattr(instance, key))
242
243
244 # Define some test helpers. Each has a pair of functions: one to create objects and one
245 # to make assertions against objects of a particular type.
246 TestHelper = namedtuple("TestHelper", ["create_object", "compare_object"])
247 data_obj = TestHelper(data_create, data_compare)
248 generic_obj = TestHelper(generic_create, generic_compare)
249 fk_obj = TestHelper(fk_create, fk_compare)
250 m2m_obj = TestHelper(m2m_create, m2m_compare)
251 im2m_obj = TestHelper(im2m_create, im2m_compare)
252 im_obj = TestHelper(im_create, im_compare)
253 o2o_obj = TestHelper(o2o_create, o2o_compare)
254 pk_obj = TestHelper(pk_create, pk_compare)
255 inherited_obj = TestHelper(inherited_create, inherited_compare)
256 uuid_obj = uuid.uuid4()
257
258 test_data = [
259 # Format: (test helper, PK value, Model Class, data)
260 (data_obj, 1, BinaryData, memoryview(b"\x05\xfd\x00")),
261 (data_obj, 5, BooleanData, True),
262 (data_obj, 6, BooleanData, False),
263 (data_obj, 7, BooleanData, None),
264 (data_obj, 10, CharData, "Test Char Data"),
265 (data_obj, 11, CharData, ""),
266 (data_obj, 12, CharData, "None"),
267 (data_obj, 13, CharData, "null"),
268 (data_obj, 14, CharData, "NULL"),
269 # (We use something that will fit into a latin1 database encoding here,
270 # because that is still the default used on many system setups.)
271 (data_obj, 16, CharData, "\xa5"),
272 (data_obj, 20, DateData, datetime.date(2006, 6, 16)),
273 (data_obj, 21, DateData, None),
274 (data_obj, 30, DateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
275 (data_obj, 31, DateTimeData, None),
276 (data_obj, 40, EmailData, "[email protected]"),
277 (data_obj, 42, EmailData, ""),
278 (data_obj, 50, FileData, "file:///foo/bar/whiz.txt"),
279 # (data_obj, 51, FileData, None),
280 (data_obj, 52, FileData, ""),
281 (data_obj, 60, FilePathData, "/foo/bar/whiz.txt"),
282 (data_obj, 62, FilePathData, ""),
283 (data_obj, 70, DecimalData, decimal.Decimal("12.345")),
284 (data_obj, 71, DecimalData, decimal.Decimal("-12.345")),
285 (data_obj, 72, DecimalData, decimal.Decimal("0.0")),
286 (data_obj, 73, DecimalData, None),
287 (data_obj, 74, FloatData, 12.345),
288 (data_obj, 75, FloatData, -12.345),
289 (data_obj, 76, FloatData, 0.0),
290 (data_obj, 77, FloatData, None),
291 (data_obj, 80, IntegerData, 123456789),
292 (data_obj, 81, IntegerData, -123456789),
293 (data_obj, 82, IntegerData, 0),
294 (data_obj, 83, IntegerData, None),
295 (data_obj, 95, GenericIPAddressData, "fe80:1424:2223:6cff:fe8a:2e8a:2151:abcd"),
296 (data_obj, 96, GenericIPAddressData, None),
297 (data_obj, 110, PositiveBigIntegerData, 9223372036854775807),
298 (data_obj, 111, PositiveBigIntegerData, None),
299 (data_obj, 120, PositiveIntegerData, 123456789),
300 (data_obj, 121, PositiveIntegerData, None),
301 (data_obj, 130, PositiveSmallIntegerData, 12),
302 (data_obj, 131, PositiveSmallIntegerData, None),
303 (data_obj, 140, SlugData, "this-is-a-slug"),
304 (data_obj, 142, SlugData, ""),
305 (data_obj, 150, SmallData, 12),
306 (data_obj, 151, SmallData, -12),
307 (data_obj, 152, SmallData, 0),
308 (data_obj, 153, SmallData, None),
309 (
310 data_obj,
311 160,
312 TextData,
313 """This is a long piece of text.
314 It contains line breaks.
315 Several of them.
316 The end.""",
317 ),
318 (data_obj, 161, TextData, ""),
319 (data_obj, 170, TimeData, datetime.time(10, 42, 37)),
320 (data_obj, 171, TimeData, None),
321 (generic_obj, 200, GenericData, ["Generic Object 1", "tag1", "tag2"]),
322 (generic_obj, 201, GenericData, ["Generic Object 2", "tag2", "tag3"]),
323 (data_obj, 300, Anchor, "Anchor 1"),
324 (data_obj, 301, Anchor, "Anchor 2"),
325 (data_obj, 302, UniqueAnchor, "UAnchor 1"),
326 (fk_obj, 400, FKData, 300), # Post reference
327 (fk_obj, 401, FKData, 500), # Pre reference
328 (fk_obj, 402, FKData, None), # Empty reference
329 (m2m_obj, 410, M2MData, []), # Empty set
330 (m2m_obj, 411, M2MData, [300, 301]), # Post reference
331 (m2m_obj, 412, M2MData, [500, 501]), # Pre reference
332 (m2m_obj, 413, M2MData, [300, 301, 500, 501]), # Pre and Post reference
333 (o2o_obj, None, O2OData, 300), # Post reference
334 (o2o_obj, None, O2OData, 500), # Pre reference
335 (fk_obj, 430, FKSelfData, 431), # Pre reference
336 (fk_obj, 431, FKSelfData, 430), # Post reference
337 (fk_obj, 432, FKSelfData, None), # Empty reference
338 (m2m_obj, 440, M2MSelfData, []),
339 (m2m_obj, 441, M2MSelfData, []),
340 (m2m_obj, 442, M2MSelfData, [440, 441]),
341 (m2m_obj, 443, M2MSelfData, [445, 446]),
342 (m2m_obj, 444, M2MSelfData, [440, 441, 445, 446]),
343 (m2m_obj, 445, M2MSelfData, []),
344 (m2m_obj, 446, M2MSelfData, []),
345 (fk_obj, 450, FKDataToField, "UAnchor 1"),
346 (fk_obj, 451, FKDataToField, "UAnchor 2"),
347 (fk_obj, 452, FKDataToField, None),
348 (fk_obj, 460, FKDataToO2O, 300),
349 (im2m_obj, 470, M2MIntermediateData, None),
350 # testing post- and pre-references and extra fields
351 (im_obj, 480, Intermediate, {"right": 300, "left": 470}),
352 (im_obj, 481, Intermediate, {"right": 300, "left": 490}),
353 (im_obj, 482, Intermediate, {"right": 500, "left": 470}),
354 (im_obj, 483, Intermediate, {"right": 500, "left": 490}),
355 (im_obj, 484, Intermediate, {"right": 300, "left": 470, "extra": "extra"}),
356 (im_obj, 485, Intermediate, {"right": 300, "left": 490, "extra": "extra"}),
357 (im_obj, 486, Intermediate, {"right": 500, "left": 470, "extra": "extra"}),
358 (im_obj, 487, Intermediate, {"right": 500, "left": 490, "extra": "extra"}),
359 (im2m_obj, 490, M2MIntermediateData, []),
360 (data_obj, 500, Anchor, "Anchor 3"),
361 (data_obj, 501, Anchor, "Anchor 4"),
362 (data_obj, 502, UniqueAnchor, "UAnchor 2"),
363 (pk_obj, 601, BooleanPKData, True),
364 (pk_obj, 602, BooleanPKData, False),
365 (pk_obj, 610, CharPKData, "Test Char PKData"),
366 (pk_obj, 620, DatePKData, datetime.date(2006, 6, 16)),
367 (pk_obj, 630, DateTimePKData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
368 (pk_obj, 640, EmailPKData, "[email protected]"),
369 (pk_obj, 660, FilePathPKData, "/foo/bar/whiz.txt"),
370 (pk_obj, 670, DecimalPKData, decimal.Decimal("12.345")),
371 (pk_obj, 671, DecimalPKData, decimal.Decimal("-12.345")),
372 (pk_obj, 672, DecimalPKData, decimal.Decimal("0.0")),
373 (pk_obj, 673, FloatPKData, 12.345),
374 (pk_obj, 674, FloatPKData, -12.345),
375 (pk_obj, 675, FloatPKData, 0.0),
376 (pk_obj, 680, IntegerPKData, 123456789),
377 (pk_obj, 681, IntegerPKData, -123456789),
378 (pk_obj, 682, IntegerPKData, 0),
379 (pk_obj, 695, GenericIPAddressPKData, "fe80:1424:2223:6cff:fe8a:2e8a:2151:abcd"),
380 (pk_obj, 720, PositiveIntegerPKData, 123456789),
381 (pk_obj, 730, PositiveSmallIntegerPKData, 12),
382 (pk_obj, 740, SlugPKData, "this-is-a-slug"),
383 (pk_obj, 750, SmallPKData, 12),
384 (pk_obj, 751, SmallPKData, -12),
385 (pk_obj, 752, SmallPKData, 0),
386 (pk_obj, 770, TimePKData, datetime.time(10, 42, 37)),
387 (pk_obj, 791, UUIDData, uuid_obj),
388 (fk_obj, 792, FKToUUID, uuid_obj),
389 (pk_obj, 793, UUIDDefaultData, uuid_obj),
390 (data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
391 (data_obj, 810, ModifyingSaveData, 42),
392 (inherited_obj, 900, InheritAbstractModel, {"child_data": 37, "parent_data": 42}),
393 (
394 inherited_obj,
395 910,
396 ExplicitInheritBaseModel,
397 {"child_data": 37, "parent_data": 42},
398 ),
399 (inherited_obj, 920, InheritBaseModel, {"child_data": 37, "parent_data": 42}),
400 (data_obj, 1000, BigIntegerData, 9223372036854775807),
401 (data_obj, 1001, BigIntegerData, -9223372036854775808),
402 (data_obj, 1002, BigIntegerData, 0),
403 (data_obj, 1003, BigIntegerData, None),
404 (data_obj, 1004, LengthModel, 0),
405 (data_obj, 1005, LengthModel, 1),
406 ]
407
408 if ImageData is not None:
409 test_data.extend(
410 [
411 (data_obj, 86, ImageData, "file:///foo/bar/whiz.png"),
412 # (data_obj, 87, ImageData, None),
413 (data_obj, 88, ImageData, ""),
414 ]
415 )
416
417
418 class SerializerDataTests(TestCase):
419 pass
420
421
422 def assert_serializer(self, format, data):
423 # Create all the objects defined in the test data.
424 objects = []
425 for test_helper, pk, model, data_value in data:
426 with connection.constraint_checks_disabled():
427 objects.extend(test_helper.create_object(pk, model, data_value))
428
429 # Get a count of the number of objects created for each model class.
430 instance_counts = {}
431 for _, _, model, _ in data:
432 if model not in instance_counts:
433 instance_counts[model] = model.objects.count()
434
435 # Add the generic tagged objects to the object list.
436 objects.extend(Tag.objects.all())
437
438 # Serialize the test database.
439 serialized_data = serializers.serialize(format, objects, indent=2)
440
441 for obj in serializers.deserialize(format, serialized_data):
442 obj.save()
443
444 # Assert that the deserialized data is the same as the original source.
445 for test_helper, pk, model, data_value in data:
446 with self.subTest(model=model, data_value=data_value):
447 test_helper.compare_object(self, pk, model, data_value)
448
449 # Assert no new objects were created.
450 for model, count in instance_counts.items():
451 with self.subTest(model=model, count=count):
452 self.assertEqual(count, model.objects.count())
453
454
455 def serializerTest(self, format):
456 assert_serializer(self, format, test_data)
457
458
459 @skipUnlessDBFeature("allows_auto_pk_0")
460 def serializerTestPK0(self, format):
461 # FK to an object with PK of 0. This won't work on MySQL without the
462 # NO_AUTO_VALUE_ON_ZERO SQL mode since it won't let you create an object
463 # with an autoincrement primary key of 0.
464 data = [
465 (data_obj, 0, Anchor, "Anchor 0"),
466 (fk_obj, 1, FKData, 0),
467 ]
468 assert_serializer(self, format, data)
469
470
471 @skipIfDBFeature("interprets_empty_strings_as_nulls")
472 def serializerTestNullValueStingField(self, format):
473 data = [
474 (data_obj, 1, BinaryData, None),
475 (data_obj, 2, CharData, None),
476 (data_obj, 3, EmailData, None),
477 (data_obj, 4, FilePathData, None),
478 (data_obj, 5, SlugData, None),
479 (data_obj, 6, TextData, None),
480 ]
481 assert_serializer(self, format, data)
482
483
484 @skipUnlessDBFeature("supports_index_on_text_field")
485 def serializerTestTextFieldPK(self, format):
486 data = [
487 (
488 pk_obj,
489 1,
490 TextPKData,
491 """This is a long piece of text.
492 It contains line breaks.
493 Several of them.
494 The end.""",
495 ),
496 ]
497 assert_serializer(self, format, data)
498
499
500 register_tests(SerializerDataTests, "test_%s_serializer", serializerTest)
501 register_tests(SerializerDataTests, "test_%s_serializer_pk_0", serializerTestPK0)
502 register_tests(
503 SerializerDataTests,
504 "test_%s_serializer_null_value_string_field",
505 serializerTestNullValueStingField,
506 )
507 register_tests(
508 SerializerDataTests,
509 "test_%s_serializer_text_field_pk",
510 serializerTestTextFieldPK,
511 )