tests.py 16 KB

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