PageRenderTime 116ms CodeModel.GetById 27ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/regressiontests/serializers_regress/tests.py

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