PageRenderTime 162ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/serializers_regress/tests.py

https://code.google.com/p/mango-py/
Python | 418 lines | 369 code | 24 blank | 25 comment | 10 complexity | d952e4100a9aa31ba2b0315f5ebf8b47 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. (generic_obj, 200, GenericData, ['Generic Object 1', 'tag1', 'tag2']),
  201. (generic_obj, 201, GenericData, ['Generic Object 2', 'tag2', 'tag3']),
  202. (data_obj, 300, Anchor, "Anchor 1"),
  203. (data_obj, 301, Anchor, "Anchor 2"),
  204. (data_obj, 302, UniqueAnchor, "UAnchor 1"),
  205. (fk_obj, 400, FKData, 300), # Post reference
  206. (fk_obj, 401, FKData, 500), # Pre reference
  207. (fk_obj, 402, FKData, None), # Empty reference
  208. (m2m_obj, 410, M2MData, []), # Empty set
  209. (m2m_obj, 411, M2MData, [300,301]), # Post reference
  210. (m2m_obj, 412, M2MData, [500,501]), # Pre reference
  211. (m2m_obj, 413, M2MData, [300,301,500,501]), # Pre and Post reference
  212. (o2o_obj, None, O2OData, 300), # Post reference
  213. (o2o_obj, None, O2OData, 500), # Pre reference
  214. (fk_obj, 430, FKSelfData, 431), # Pre reference
  215. (fk_obj, 431, FKSelfData, 430), # Post reference
  216. (fk_obj, 432, FKSelfData, None), # Empty reference
  217. (m2m_obj, 440, M2MSelfData, []),
  218. (m2m_obj, 441, M2MSelfData, []),
  219. (m2m_obj, 442, M2MSelfData, [440, 441]),
  220. (m2m_obj, 443, M2MSelfData, [445, 446]),
  221. (m2m_obj, 444, M2MSelfData, [440, 441, 445, 446]),
  222. (m2m_obj, 445, M2MSelfData, []),
  223. (m2m_obj, 446, M2MSelfData, []),
  224. (fk_obj, 450, FKDataToField, "UAnchor 1"),
  225. (fk_obj, 451, FKDataToField, "UAnchor 2"),
  226. (fk_obj, 452, FKDataToField, None),
  227. (fk_obj, 460, FKDataToO2O, 300),
  228. (im2m_obj, 470, M2MIntermediateData, None),
  229. #testing post- and prereferences and extra fields
  230. (im_obj, 480, Intermediate, {'right': 300, 'left': 470}),
  231. (im_obj, 481, Intermediate, {'right': 300, 'left': 490}),
  232. (im_obj, 482, Intermediate, {'right': 500, 'left': 470}),
  233. (im_obj, 483, Intermediate, {'right': 500, 'left': 490}),
  234. (im_obj, 484, Intermediate, {'right': 300, 'left': 470, 'extra': "extra"}),
  235. (im_obj, 485, Intermediate, {'right': 300, 'left': 490, 'extra': "extra"}),
  236. (im_obj, 486, Intermediate, {'right': 500, 'left': 470, 'extra': "extra"}),
  237. (im_obj, 487, Intermediate, {'right': 500, 'left': 490, 'extra': "extra"}),
  238. (im2m_obj, 490, M2MIntermediateData, []),
  239. (data_obj, 500, Anchor, "Anchor 3"),
  240. (data_obj, 501, Anchor, "Anchor 4"),
  241. (data_obj, 502, UniqueAnchor, "UAnchor 2"),
  242. (pk_obj, 601, BooleanPKData, True),
  243. (pk_obj, 602, BooleanPKData, False),
  244. (pk_obj, 610, CharPKData, "Test Char PKData"),
  245. # (pk_obj, 620, DatePKData, datetime.date(2006,6,16)),
  246. # (pk_obj, 630, DateTimePKData, datetime.datetime(2006,6,16,10,42,37)),
  247. (pk_obj, 640, EmailPKData, "hovercraft@example.com"),
  248. # (pk_obj, 650, FilePKData, 'file:///foo/bar/whiz.txt'),
  249. (pk_obj, 660, FilePathPKData, "/foo/bar/whiz.txt"),
  250. (pk_obj, 670, DecimalPKData, decimal.Decimal('12.345')),
  251. (pk_obj, 671, DecimalPKData, decimal.Decimal('-12.345')),
  252. (pk_obj, 672, DecimalPKData, decimal.Decimal('0.0')),
  253. (pk_obj, 673, FloatPKData, 12.345),
  254. (pk_obj, 674, FloatPKData, -12.345),
  255. (pk_obj, 675, FloatPKData, 0.0),
  256. (pk_obj, 680, IntegerPKData, 123456789),
  257. (pk_obj, 681, IntegerPKData, -123456789),
  258. (pk_obj, 682, IntegerPKData, 0),
  259. # (XX, ImagePKData
  260. (pk_obj, 690, IPAddressPKData, "127.0.0.1"),
  261. # (pk_obj, 700, NullBooleanPKData, True),
  262. # (pk_obj, 701, NullBooleanPKData, False),
  263. (pk_obj, 710, PhonePKData, "212-634-5789"),
  264. (pk_obj, 720, PositiveIntegerPKData, 123456789),
  265. (pk_obj, 730, PositiveSmallIntegerPKData, 12),
  266. (pk_obj, 740, SlugPKData, "this-is-a-slug"),
  267. (pk_obj, 750, SmallPKData, 12),
  268. (pk_obj, 751, SmallPKData, -12),
  269. (pk_obj, 752, SmallPKData, 0),
  270. # (pk_obj, 760, TextPKData, """This is a long piece of text.
  271. # It contains line breaks.
  272. # Several of them.
  273. # The end."""),
  274. # (pk_obj, 770, TimePKData, datetime.time(10,42,37)),
  275. (pk_obj, 780, USStatePKData, "MA"),
  276. # (pk_obj, 790, XMLPKData, "<foo></foo>"),
  277. (data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006,6,16,10,42,37)),
  278. (data_obj, 810, ModifyingSaveData, 42),
  279. (inherited_obj, 900, InheritAbstractModel, {'child_data':37,'parent_data':42}),
  280. (inherited_obj, 910, ExplicitInheritBaseModel, {'child_data':37,'parent_data':42}),
  281. (inherited_obj, 920, InheritBaseModel, {'child_data':37,'parent_data':42}),
  282. (data_obj, 1000, BigIntegerData, 9223372036854775807),
  283. (data_obj, 1001, BigIntegerData, -9223372036854775808),
  284. (data_obj, 1002, BigIntegerData, 0),
  285. (data_obj, 1003, BigIntegerData, None),
  286. (data_obj, 1004, LengthModel, 0),
  287. (data_obj, 1005, LengthModel, 1),
  288. ]
  289. # Because Oracle treats the empty string as NULL, Oracle is expected to fail
  290. # when field.empty_strings_allowed is True and the value is None; skip these
  291. # tests.
  292. if connection.features.interprets_empty_strings_as_nulls:
  293. test_data = [data for data in test_data
  294. if not (data[0] == data_obj and
  295. data[2]._meta.get_field('data').empty_strings_allowed and
  296. data[3] is None)]
  297. # Regression test for #8651 -- a FK to an object iwth PK of 0
  298. # This won't work on MySQL since it won't let you create an object
  299. # with a primary key of 0,
  300. if connection.features.allows_primary_key_0:
  301. test_data.extend([
  302. (data_obj, 0, Anchor, "Anchor 0"),
  303. (fk_obj, 465, FKData, 0),
  304. ])
  305. # Dynamically create serializer tests to ensure that all
  306. # registered serializers are automatically tested.
  307. class SerializerTests(TestCase):
  308. pass
  309. def serializerTest(format, self):
  310. # Create all the objects defined in the test data
  311. objects = []
  312. instance_count = {}
  313. for (func, pk, klass, datum) in test_data:
  314. objects.extend(func[0](pk, klass, datum))
  315. # Get a count of the number of objects created for each class
  316. for klass in instance_count:
  317. instance_count[klass] = klass.objects.count()
  318. # Add the generic tagged objects to the object list
  319. objects.extend(Tag.objects.all())
  320. # Serialize the test database
  321. serialized_data = serializers.serialize(format, objects, indent=2)
  322. for obj in serializers.deserialize(format, serialized_data):
  323. obj.save()
  324. # Assert that the deserialized data is the same
  325. # as the original source
  326. for (func, pk, klass, datum) in test_data:
  327. func[1](self, pk, klass, datum)
  328. # Assert that the number of objects deserialized is the
  329. # same as the number that was serialized.
  330. for klass, count in instance_count.items():
  331. self.assertEqual(count, klass.objects.count())
  332. def fieldsTest(format, self):
  333. obj = ComplexModel(field1='first', field2='second', field3='third')
  334. obj.save_base(raw=True)
  335. # Serialize then deserialize the test database
  336. serialized_data = serializers.serialize(format, [obj], indent=2, fields=('field1','field3'))
  337. result = serializers.deserialize(format, serialized_data).next()
  338. # Check that the deserialized object contains data in only the serialized fields.
  339. self.assertEqual(result.object.field1, 'first')
  340. self.assertEqual(result.object.field2, '')
  341. self.assertEqual(result.object.field3, 'third')
  342. def streamTest(format, self):
  343. obj = ComplexModel(field1='first',field2='second',field3='third')
  344. obj.save_base(raw=True)
  345. # Serialize the test database to a stream
  346. stream = StringIO()
  347. serializers.serialize(format, [obj], indent=2, stream=stream)
  348. # Serialize normally for a comparison
  349. string_data = serializers.serialize(format, [obj], indent=2)
  350. # Check that the two are the same
  351. self.assertEqual(string_data, stream.getvalue())
  352. stream.close()
  353. for format in serializers.get_serializer_formats():
  354. setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))
  355. setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format))
  356. if format != 'python':
  357. setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format))