test_data.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. """
  2. A test spanning all the capabilities of all the serializers.
  3. This class defines sample data and a dynamically generated
  4. test case that is capable of testing the capabilities of
  5. the serializers. This includes all valid data values, plus
  6. forward, backwards and self references.
  7. """
  8. from __future__ import unicode_literals
  9. import datetime
  10. import decimal
  11. import uuid
  12. from django.core import serializers
  13. from django.db import connection, models
  14. from django.test import TestCase
  15. from django.utils import six
  16. from .models import (
  17. Anchor, AutoNowDateTimeData, BigIntegerData, BinaryData, BooleanData,
  18. BooleanPKData, CharData, CharPKData, DateData, DateTimeData, DecimalData,
  19. DecimalPKData, EmailData, EmailPKData, ExplicitInheritBaseModel, FileData,
  20. FilePathData, FilePathPKData, FKData, FKDataToField, FKDataToO2O,
  21. FKSelfData, FKToUUID, FloatData, FloatPKData, GenericData,
  22. GenericIPAddressData, GenericIPAddressPKData, InheritAbstractModel,
  23. InheritBaseModel, IntegerData, IntegerPKData, Intermediate, LengthModel,
  24. M2MData, M2MIntermediateData, M2MSelfData, ModifyingSaveData,
  25. NullBooleanData, O2OData, PositiveIntegerData, PositiveIntegerPKData,
  26. PositiveSmallIntegerData, PositiveSmallIntegerPKData, SlugData, SlugPKData,
  27. SmallData, SmallPKData, Tag, TextData, TimeData, UniqueAnchor, UUIDData,
  28. )
  29. from .tests import register_tests
  30. # A set of functions that can be used to recreate
  31. # test data objects of various kinds.
  32. # The save method is a raw base model save, to make
  33. # sure that the data in the database matches the
  34. # exact test case.
  35. def data_create(pk, klass, data):
  36. instance = klass(id=pk)
  37. instance.data = data
  38. models.Model.save_base(instance, raw=True)
  39. return [instance]
  40. def generic_create(pk, klass, data):
  41. instance = klass(id=pk)
  42. instance.data = data[0]
  43. models.Model.save_base(instance, raw=True)
  44. for tag in data[1:]:
  45. instance.tags.create(data=tag)
  46. return [instance]
  47. def fk_create(pk, klass, data):
  48. instance = klass(id=pk)
  49. setattr(instance, 'data_id', data)
  50. models.Model.save_base(instance, raw=True)
  51. return [instance]
  52. def m2m_create(pk, klass, data):
  53. instance = klass(id=pk)
  54. models.Model.save_base(instance, raw=True)
  55. instance.data.set(data)
  56. return [instance]
  57. def im2m_create(pk, klass, data):
  58. instance = klass(id=pk)
  59. models.Model.save_base(instance, raw=True)
  60. return [instance]
  61. def im_create(pk, klass, data):
  62. instance = klass(id=pk)
  63. instance.right_id = data['right']
  64. instance.left_id = data['left']
  65. if 'extra' in data:
  66. instance.extra = data['extra']
  67. models.Model.save_base(instance, raw=True)
  68. return [instance]
  69. def o2o_create(pk, klass, data):
  70. instance = klass()
  71. instance.data_id = data
  72. models.Model.save_base(instance, raw=True)
  73. return [instance]
  74. def pk_create(pk, klass, data):
  75. instance = klass()
  76. instance.data = data
  77. models.Model.save_base(instance, raw=True)
  78. return [instance]
  79. def inherited_create(pk, klass, data):
  80. instance = klass(id=pk, **data)
  81. # This isn't a raw save because:
  82. # 1) we're testing inheritance, not field behavior, so none
  83. # of the field values need to be protected.
  84. # 2) saving the child class and having the parent created
  85. # automatically is easier than manually creating both.
  86. models.Model.save(instance)
  87. created = [instance]
  88. for klass, field in instance._meta.parents.items():
  89. created.append(klass.objects.get(id=pk))
  90. return created
  91. # A set of functions that can be used to compare
  92. # test data objects of various kinds
  93. def data_compare(testcase, pk, klass, data):
  94. instance = klass.objects.get(id=pk)
  95. if klass == BinaryData and data is not None:
  96. testcase.assertEqual(bytes(data), bytes(instance.data),
  97. "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (
  98. pk, repr(bytes(data)), type(data), repr(bytes(instance.data)),
  99. type(instance.data))
  100. )
  101. else:
  102. testcase.assertEqual(data, instance.data,
  103. "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (
  104. pk, data, type(data), instance, type(instance.data))
  105. )
  106. def generic_compare(testcase, pk, klass, data):
  107. instance = klass.objects.get(id=pk)
  108. testcase.assertEqual(data[0], instance.data)
  109. testcase.assertEqual(data[1:], [t.data for t in instance.tags.order_by('id')])
  110. def fk_compare(testcase, pk, klass, data):
  111. instance = klass.objects.get(id=pk)
  112. testcase.assertEqual(data, instance.data_id)
  113. def m2m_compare(testcase, pk, klass, data):
  114. instance = klass.objects.get(id=pk)
  115. testcase.assertEqual(data, [obj.id for obj in instance.data.order_by('id')])
  116. def im2m_compare(testcase, pk, klass, data):
  117. klass.objects.get(id=pk)
  118. # actually nothing else to check, the instance just should exist
  119. def im_compare(testcase, pk, klass, data):
  120. instance = klass.objects.get(id=pk)
  121. testcase.assertEqual(data['left'], instance.left_id)
  122. testcase.assertEqual(data['right'], instance.right_id)
  123. if 'extra' in data:
  124. testcase.assertEqual(data['extra'], instance.extra)
  125. else:
  126. testcase.assertEqual("doesn't matter", instance.extra)
  127. def o2o_compare(testcase, pk, klass, data):
  128. instance = klass.objects.get(data=data)
  129. testcase.assertEqual(data, instance.data_id)
  130. def pk_compare(testcase, pk, klass, data):
  131. instance = klass.objects.get(data=data)
  132. testcase.assertEqual(data, instance.data)
  133. def inherited_compare(testcase, pk, klass, data):
  134. instance = klass.objects.get(id=pk)
  135. for key, value in data.items():
  136. testcase.assertEqual(value, getattr(instance, key))
  137. # Define some data types. Each data type is
  138. # actually a pair of functions; one to create
  139. # and one to compare objects of that type
  140. data_obj = (data_create, data_compare)
  141. generic_obj = (generic_create, generic_compare)
  142. fk_obj = (fk_create, fk_compare)
  143. m2m_obj = (m2m_create, m2m_compare)
  144. im2m_obj = (im2m_create, im2m_compare)
  145. im_obj = (im_create, im_compare)
  146. o2o_obj = (o2o_create, o2o_compare)
  147. pk_obj = (pk_create, pk_compare)
  148. inherited_obj = (inherited_create, inherited_compare)
  149. uuid_obj = uuid.uuid4()
  150. test_data = [
  151. # Format: (data type, PK value, Model Class, data)
  152. (data_obj, 1, BinaryData, six.memoryview(b"\x05\xFD\x00")),
  153. (data_obj, 2, BinaryData, None),
  154. (data_obj, 5, BooleanData, True),
  155. (data_obj, 6, BooleanData, False),
  156. (data_obj, 10, CharData, "Test Char Data"),
  157. (data_obj, 11, CharData, ""),
  158. (data_obj, 12, CharData, "None"),
  159. (data_obj, 13, CharData, "null"),
  160. (data_obj, 14, CharData, "NULL"),
  161. (data_obj, 15, CharData, None),
  162. # (We use something that will fit into a latin1 database encoding here,
  163. # because that is still the default used on many system setups.)
  164. (data_obj, 16, CharData, '\xa5'),
  165. (data_obj, 20, DateData, datetime.date(2006, 6, 16)),
  166. (data_obj, 21, DateData, None),
  167. (data_obj, 30, DateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
  168. (data_obj, 31, DateTimeData, None),
  169. (data_obj, 40, EmailData, "hovercraft@example.com"),
  170. (data_obj, 41, EmailData, None),
  171. (data_obj, 42, EmailData, ""),
  172. (data_obj, 50, FileData, 'file:///foo/bar/whiz.txt'),
  173. # (data_obj, 51, FileData, None),
  174. (data_obj, 52, FileData, ""),
  175. (data_obj, 60, FilePathData, "/foo/bar/whiz.txt"),
  176. (data_obj, 61, FilePathData, None),
  177. (data_obj, 62, FilePathData, ""),
  178. (data_obj, 70, DecimalData, decimal.Decimal('12.345')),
  179. (data_obj, 71, DecimalData, decimal.Decimal('-12.345')),
  180. (data_obj, 72, DecimalData, decimal.Decimal('0.0')),
  181. (data_obj, 73, DecimalData, None),
  182. (data_obj, 74, FloatData, 12.345),
  183. (data_obj, 75, FloatData, -12.345),
  184. (data_obj, 76, FloatData, 0.0),
  185. (data_obj, 77, FloatData, None),
  186. (data_obj, 80, IntegerData, 123456789),
  187. (data_obj, 81, IntegerData, -123456789),
  188. (data_obj, 82, IntegerData, 0),
  189. (data_obj, 83, IntegerData, None),
  190. # (XX, ImageData
  191. (data_obj, 95, GenericIPAddressData, "fe80:1424:2223:6cff:fe8a:2e8a:2151:abcd"),
  192. (data_obj, 96, GenericIPAddressData, None),
  193. (data_obj, 100, NullBooleanData, True),
  194. (data_obj, 101, NullBooleanData, False),
  195. (data_obj, 102, NullBooleanData, None),
  196. (data_obj, 120, PositiveIntegerData, 123456789),
  197. (data_obj, 121, PositiveIntegerData, None),
  198. (data_obj, 130, PositiveSmallIntegerData, 12),
  199. (data_obj, 131, PositiveSmallIntegerData, None),
  200. (data_obj, 140, SlugData, "this-is-a-slug"),
  201. (data_obj, 141, SlugData, None),
  202. (data_obj, 142, SlugData, ""),
  203. (data_obj, 150, SmallData, 12),
  204. (data_obj, 151, SmallData, -12),
  205. (data_obj, 152, SmallData, 0),
  206. (data_obj, 153, SmallData, None),
  207. (data_obj, 160, TextData, """This is a long piece of text.
  208. It contains line breaks.
  209. Several of them.
  210. The end."""),
  211. (data_obj, 161, TextData, ""),
  212. (data_obj, 162, TextData, None),
  213. (data_obj, 170, TimeData, datetime.time(10, 42, 37)),
  214. (data_obj, 171, TimeData, None),
  215. (generic_obj, 200, GenericData, ['Generic Object 1', 'tag1', 'tag2']),
  216. (generic_obj, 201, GenericData, ['Generic Object 2', 'tag2', 'tag3']),
  217. (data_obj, 300, Anchor, "Anchor 1"),
  218. (data_obj, 301, Anchor, "Anchor 2"),
  219. (data_obj, 302, UniqueAnchor, "UAnchor 1"),
  220. (fk_obj, 400, FKData, 300), # Post reference
  221. (fk_obj, 401, FKData, 500), # Pre reference
  222. (fk_obj, 402, FKData, None), # Empty reference
  223. (m2m_obj, 410, M2MData, []), # Empty set
  224. (m2m_obj, 411, M2MData, [300, 301]), # Post reference
  225. (m2m_obj, 412, M2MData, [500, 501]), # Pre reference
  226. (m2m_obj, 413, M2MData, [300, 301, 500, 501]), # Pre and Post reference
  227. (o2o_obj, None, O2OData, 300), # Post reference
  228. (o2o_obj, None, O2OData, 500), # Pre reference
  229. (fk_obj, 430, FKSelfData, 431), # Pre reference
  230. (fk_obj, 431, FKSelfData, 430), # Post reference
  231. (fk_obj, 432, FKSelfData, None), # Empty reference
  232. (m2m_obj, 440, M2MSelfData, []),
  233. (m2m_obj, 441, M2MSelfData, []),
  234. (m2m_obj, 442, M2MSelfData, [440, 441]),
  235. (m2m_obj, 443, M2MSelfData, [445, 446]),
  236. (m2m_obj, 444, M2MSelfData, [440, 441, 445, 446]),
  237. (m2m_obj, 445, M2MSelfData, []),
  238. (m2m_obj, 446, M2MSelfData, []),
  239. (fk_obj, 450, FKDataToField, "UAnchor 1"),
  240. (fk_obj, 451, FKDataToField, "UAnchor 2"),
  241. (fk_obj, 452, FKDataToField, None),
  242. (fk_obj, 460, FKDataToO2O, 300),
  243. (im2m_obj, 470, M2MIntermediateData, None),
  244. # testing post- and prereferences and extra fields
  245. (im_obj, 480, Intermediate, {'right': 300, 'left': 470}),
  246. (im_obj, 481, Intermediate, {'right': 300, 'left': 490}),
  247. (im_obj, 482, Intermediate, {'right': 500, 'left': 470}),
  248. (im_obj, 483, Intermediate, {'right': 500, 'left': 490}),
  249. (im_obj, 484, Intermediate, {'right': 300, 'left': 470, 'extra': "extra"}),
  250. (im_obj, 485, Intermediate, {'right': 300, 'left': 490, 'extra': "extra"}),
  251. (im_obj, 486, Intermediate, {'right': 500, 'left': 470, 'extra': "extra"}),
  252. (im_obj, 487, Intermediate, {'right': 500, 'left': 490, 'extra': "extra"}),
  253. (im2m_obj, 490, M2MIntermediateData, []),
  254. (data_obj, 500, Anchor, "Anchor 3"),
  255. (data_obj, 501, Anchor, "Anchor 4"),
  256. (data_obj, 502, UniqueAnchor, "UAnchor 2"),
  257. (pk_obj, 601, BooleanPKData, True),
  258. (pk_obj, 602, BooleanPKData, False),
  259. (pk_obj, 610, CharPKData, "Test Char PKData"),
  260. # (pk_obj, 620, DatePKData, datetime.date(2006, 6, 16)),
  261. # (pk_obj, 630, DateTimePKData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
  262. (pk_obj, 640, EmailPKData, "hovercraft@example.com"),
  263. # (pk_obj, 650, FilePKData, 'file:///foo/bar/whiz.txt'),
  264. (pk_obj, 660, FilePathPKData, "/foo/bar/whiz.txt"),
  265. (pk_obj, 670, DecimalPKData, decimal.Decimal('12.345')),
  266. (pk_obj, 671, DecimalPKData, decimal.Decimal('-12.345')),
  267. (pk_obj, 672, DecimalPKData, decimal.Decimal('0.0')),
  268. (pk_obj, 673, FloatPKData, 12.345),
  269. (pk_obj, 674, FloatPKData, -12.345),
  270. (pk_obj, 675, FloatPKData, 0.0),
  271. (pk_obj, 680, IntegerPKData, 123456789),
  272. (pk_obj, 681, IntegerPKData, -123456789),
  273. (pk_obj, 682, IntegerPKData, 0),
  274. # (XX, ImagePKData
  275. (pk_obj, 695, GenericIPAddressPKData, "fe80:1424:2223:6cff:fe8a:2e8a:2151:abcd"),
  276. # (pk_obj, 700, NullBooleanPKData, True),
  277. # (pk_obj, 701, NullBooleanPKData, False),
  278. (pk_obj, 720, PositiveIntegerPKData, 123456789),
  279. (pk_obj, 730, PositiveSmallIntegerPKData, 12),
  280. (pk_obj, 740, SlugPKData, "this-is-a-slug"),
  281. (pk_obj, 750, SmallPKData, 12),
  282. (pk_obj, 751, SmallPKData, -12),
  283. (pk_obj, 752, SmallPKData, 0),
  284. # (pk_obj, 760, TextPKData, """This is a long piece of text.
  285. # It contains line breaks.
  286. # Several of them.
  287. # The end."""),
  288. # (pk_obj, 770, TimePKData, datetime.time(10, 42, 37)),
  289. # (pk_obj, 790, XMLPKData, "<foo></foo>"),
  290. (pk_obj, 791, UUIDData, uuid_obj),
  291. (fk_obj, 792, FKToUUID, uuid_obj),
  292. (data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
  293. (data_obj, 810, ModifyingSaveData, 42),
  294. (inherited_obj, 900, InheritAbstractModel, {'child_data': 37, 'parent_data': 42}),
  295. (inherited_obj, 910, ExplicitInheritBaseModel, {'child_data': 37, 'parent_data': 42}),
  296. (inherited_obj, 920, InheritBaseModel, {'child_data': 37, 'parent_data': 42}),
  297. (data_obj, 1000, BigIntegerData, 9223372036854775807),
  298. (data_obj, 1001, BigIntegerData, -9223372036854775808),
  299. (data_obj, 1002, BigIntegerData, 0),
  300. (data_obj, 1003, BigIntegerData, None),
  301. (data_obj, 1004, LengthModel, 0),
  302. (data_obj, 1005, LengthModel, 1),
  303. ]
  304. # Because Oracle treats the empty string as NULL, Oracle is expected to fail
  305. # when field.empty_strings_allowed is True and the value is None; skip these
  306. # tests.
  307. if connection.features.interprets_empty_strings_as_nulls:
  308. test_data = [data for data in test_data
  309. if not (data[0] == data_obj and
  310. data[2]._meta.get_field('data').empty_strings_allowed and
  311. data[3] is None)]
  312. # Regression test for #8651 -- a FK to an object with PK of 0
  313. # This won't work on MySQL since it won't let you create an object
  314. # with an autoincrement primary key of 0,
  315. if connection.features.allows_auto_pk_0:
  316. test_data.extend([
  317. (data_obj, 0, Anchor, "Anchor 0"),
  318. (fk_obj, 465, FKData, 0),
  319. ])
  320. class SerializerDataTests(TestCase):
  321. pass
  322. def serializerTest(format, self):
  323. # Create all the objects defined in the test data
  324. objects = []
  325. instance_count = {}
  326. for (func, pk, klass, datum) in test_data:
  327. with connection.constraint_checks_disabled():
  328. objects.extend(func[0](pk, klass, datum))
  329. # Get a count of the number of objects created for each class
  330. for klass in instance_count:
  331. instance_count[klass] = klass.objects.count()
  332. # Add the generic tagged objects to the object list
  333. objects.extend(Tag.objects.all())
  334. # Serialize the test database
  335. serialized_data = serializers.serialize(format, objects, indent=2)
  336. for obj in serializers.deserialize(format, serialized_data):
  337. obj.save()
  338. # Assert that the deserialized data is the same
  339. # as the original source
  340. for (func, pk, klass, datum) in test_data:
  341. func[1](self, pk, klass, datum)
  342. # Assert that the number of objects deserialized is the
  343. # same as the number that was serialized.
  344. for klass, count in instance_count.items():
  345. self.assertEqual(count, klass.objects.count())
  346. register_tests(SerializerDataTests, 'test_%s_serializer', serializerTest)