serialized_data = serializers.serialize(format, objects, indent=2) for obj in serializers.deserialize(format, serialized_data): obj.save() # Assert that the deserialized data is the same # as the original source for (func, pk, klass, datum) in test_data: func[1](self, pk, klass, datum) # Assert that the number of objects deserialized is the # same as the number that was serialized. for klass, count in instance_count.items(): self.assertEqual(count, klass.objects.count()) serializerTest = skipUnlessDBFeature('supports_binary_field')(serializerTest) def naturalKeySerializerTest(format, self): # Create all the objects defined in the test data objects = [] instance_count = {} for (func, pk, klass, datum) in natural_key_test_data: with connection.constraint_checks_disabled(): objects.extend(func[0](pk, klass, datum)) # Get a count of the number of objects created for each class for klass in instance_count: instance_count[klass] = klass.objects.count() # use_natural_keys is deprecated and to be removed in Django 1.9
transaction.enter_transaction_management() self.assertEqual(connection.isolation_level, self._read_committed) transaction.leave_transaction_management() self.assertEqual(connection.isolation_level, self._read_committed) transaction.leave_transaction_management() self.assertEqual(connection.isolation_level, self._autocommit) def tearDown(self): connections[DEFAULT_DB_ALIAS] = self._old_backend TestPostgresAutocommit = skipUnless(connection.vendor == 'postgresql', "This test only valid for PostgreSQL")(TestPostgresAutocommit) TestPostgresAutoCommit = skipUnlessDBFeature('supports_transactions')( TestPostgresAutocommit) class TestManyToManyAddTransaction(TransactionTestCase): def test_manyrelated_add_commit(self): "Test for https://code.djangoproject.com/ticket/16818" a = M2mA.objects.create() b = M2mB.objects.create(fld=10) a.others.add(b) # We're in a TransactionTestCase and have not changed transaction # behavior from default of "autocommit", so this rollback should not # actually do anything. If it does in fact undo our add, that's a bug # that the bulk insert was not auto-committed. transaction.rollback() self.assertEqual(a.others.count(), 1)