diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
index 84e90ff..be920c6 100644
a
|
b
|
forward, backwards and self references.
|
10 | 10 | |
11 | 11 | import datetime |
12 | 12 | import decimal |
13 | | import unittest |
14 | | from cStringIO import StringIO |
| 13 | try: |
| 14 | from cStringIO import StringIO |
| 15 | except ImportError: |
| 16 | from StringIO import StringIO |
15 | 17 | |
16 | | from django.utils.functional import curry |
17 | | from django.core import serializers |
18 | | from django.db import transaction, DEFAULT_DB_ALIAS |
19 | | from django.core import management |
20 | 18 | from django.conf import settings |
| 19 | from django.core import serializers, management |
| 20 | from django.db import transaction, DEFAULT_DB_ALIAS |
| 21 | from django.test import TestCase |
| 22 | from django.utils.functional import curry |
21 | 23 | |
22 | 24 | from models import * |
23 | 25 | |
… |
… |
def im2m_create(pk, klass, data):
|
59 | 61 | |
60 | 62 | def im_create(pk, klass, data): |
61 | 63 | instance = klass(id=pk) |
62 | | setattr(instance, 'right_id', data['right']) |
63 | | setattr(instance, 'left_id', data['left']) |
| 64 | instance.right_id = data['right'] |
| 65 | instance.left_id = data['left'] |
64 | 66 | if 'extra' in data: |
65 | | setattr(instance, 'extra', data['extra']) |
| 67 | instance.extra = data['extra'] |
66 | 68 | models.Model.save_base(instance, raw=True) |
67 | 69 | return [instance] |
68 | 70 | |
… |
… |
def inherited_create(pk, klass, data):
|
96 | 98 | def data_compare(testcase, pk, klass, data): |
97 | 99 | instance = klass.objects.get(id=pk) |
98 | 100 | testcase.assertEqual(data, instance.data, |
99 | | "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (pk,data, type(data), instance.data, type(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 | ) |
100 | 104 | |
101 | 105 | def generic_compare(testcase, pk, klass, data): |
102 | 106 | instance = klass.objects.get(id=pk) |
… |
… |
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
|
348 | 352 | |
349 | 353 | # Dynamically create serializer tests to ensure that all |
350 | 354 | # registered serializers are automatically tested. |
351 | | class SerializerTests(unittest.TestCase): |
| 355 | class SerializerTests(TestCase): |
352 | 356 | pass |
353 | 357 | |
354 | 358 | def serializerTest(format, self): |
355 | | # Clear the database first |
356 | | management.call_command('flush', verbosity=0, interactive=False) |
357 | 359 | |
358 | 360 | # Create all the objects defined in the test data |
359 | 361 | objects = [] |
360 | 362 | instance_count = {} |
361 | | transaction.enter_transaction_management() |
362 | | try: |
363 | | transaction.managed(True) |
364 | | for (func, pk, klass, datum) in test_data: |
365 | | objects.extend(func[0](pk, klass, datum)) |
366 | | instance_count[klass] = 0 |
367 | | transaction.commit() |
368 | | except: |
369 | | transaction.rollback() |
370 | | transaction.leave_transaction_management() |
371 | | raise |
372 | | transaction.leave_transaction_management() |
| 363 | for (func, pk, klass, datum) in test_data: |
| 364 | objects.extend(func[0](pk, klass, datum)) |
373 | 365 | |
374 | 366 | # Get a count of the number of objects created for each class |
375 | 367 | for klass in instance_count: |
… |
… |
def serializerTest(format, self):
|
381 | 373 | # Serialize the test database |
382 | 374 | serialized_data = serializers.serialize(format, objects, indent=2) |
383 | 375 | |
384 | | # Flush the database and recreate from the serialized data |
385 | | management.call_command('flush', verbosity=0, interactive=False) |
386 | | transaction.enter_transaction_management() |
387 | | try: |
388 | | transaction.managed(True) |
389 | | for obj in serializers.deserialize(format, serialized_data): |
390 | | obj.save() |
391 | | transaction.commit() |
392 | | except: |
393 | | transaction.rollback() |
394 | | transaction.leave_transaction_management() |
395 | | raise |
396 | | transaction.leave_transaction_management() |
| 376 | for obj in serializers.deserialize(format, serialized_data): |
| 377 | obj.save() |
397 | 378 | |
398 | 379 | # Assert that the deserialized data is the same |
399 | 380 | # as the original source |
… |
… |
def serializerTest(format, self):
|
406 | 387 | self.assertEquals(count, klass.objects.count()) |
407 | 388 | |
408 | 389 | def fieldsTest(format, self): |
409 | | # Clear the database first |
410 | | management.call_command('flush', verbosity=0, interactive=False) |
411 | | |
412 | | obj = ComplexModel(field1='first',field2='second',field3='third') |
| 390 | obj = ComplexModel(field1='first', field2='second', field3='third') |
413 | 391 | obj.save_base(raw=True) |
414 | 392 | |
415 | 393 | # Serialize then deserialize the test database |
… |
… |
def fieldsTest(format, self):
|
422 | 400 | self.assertEqual(result.object.field3, 'third') |
423 | 401 | |
424 | 402 | def streamTest(format, self): |
425 | | # Clear the database first |
426 | | management.call_command('flush', verbosity=0, interactive=False) |
427 | | |
428 | 403 | obj = ComplexModel(field1='first',field2='second',field3='third') |
429 | 404 | obj.save_base(raw=True) |
430 | 405 | |
… |
… |
def streamTest(format, self):
|
440 | 415 | stream.close() |
441 | 416 | |
442 | 417 | for format in serializers.get_serializer_formats(): |
443 | | setattr(SerializerTests, 'test_'+format+'_serializer', curry(serializerTest, format)) |
444 | | setattr(SerializerTests, 'test_'+format+'_serializer_fields', curry(fieldsTest, format)) |
| 418 | setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format)) |
| 419 | setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format)) |
445 | 420 | if format != 'python': |
446 | | setattr(SerializerTests, 'test_'+format+'_serializer_stream', curry(streamTest, format)) |
| 421 | setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format)) |