Esempio n. 1
0
def serializerTest(self, format):

    # Create all the objects defined in the test data
    objects = []
    instance_count = {}
    for (func, pk, klass, datum) in 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()

    # Add the generic tagged objects to the object list
    objects.extend(Tag.objects.all())

    # Serialize the test database
    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())
Esempio n. 2
0
def natural_key_serializer_test(self, format):
    # Create all the objects defined in the test data
    with connection.constraint_checks_disabled():
        objects = [
            NaturalKeyAnchor.objects.create(id=1100,
                                            data="Natural Key Anghor"),
            FKDataNaturalKey.objects.create(id=1101, data_id=1100),
            FKDataNaturalKey.objects.create(id=1102, data_id=None),
        ]
    # Serialize the test database
    serialized_data = serializers.serialize(format,
                                            objects,
                                            indent=2,
                                            use_natural_foreign_keys=True)

    for obj in serializers.deserialize(format, serialized_data):
        obj.save()

    # Assert that the deserialized data is the same
    # as the original source
    for obj in objects:
        instance = obj.__class__.objects.get(id=obj.pk)
        self.assertEqual(
            obj.data, instance.data,
            "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" %
            (
                obj.pk,
                obj.data,
                type(obj.data),
                instance,
                type(instance.data),
            ))
Esempio n. 3
0
    def test_forward_refs(self):
        """
        Objects ids can be referenced before they are
        defined in the serialization data.
        """
        # The deserialization process needs to run in a transaction in order
        # to test forward reference handling.
        with transaction.atomic():
            objs = serializers.deserialize(self.serializer_name, self.fwd_ref_str)
            with connection.constraint_checks_disabled():
                for obj in objs:
                    obj.save()

        for model_cls in (Category, Author, Article):
            self.assertEqual(model_cls.objects.all().count(), 1)
        art_obj = Article.objects.all()[0]
        self.assertEqual(art_obj.categories.all().count(), 1)
        self.assertEqual(art_obj.author.name, "Agnes")
Esempio n. 4
0
 def test_check_constraints(self):
     """
     Constraint checks should raise an IntegrityError when bad data is in the DB.
     """
     with transaction.atomic():
         # Create an Article.
         Article.objects.create(
             headline="Test article",
             pub_date=datetime.datetime(2010, 9, 4),
             reporter=self.r,
         )
         # Retrieve it from the DB
         a = Article.objects.get(headline="Test article")
         a.reporter_id = 30
         with connection.constraint_checks_disabled():
             a.save()
             with self.assertRaises(IntegrityError):
                 connection.check_constraints()
         transaction.set_rollback(True)
Esempio n. 5
0
 def test_disable_constraint_checks_context_manager(self):
     """
     When constraint checks are disabled (using context manager), should be
     able to write bad data without IntegrityErrors.
     """
     with transaction.atomic():
         # Create an Article.
         Article.objects.create(
             headline="Test article",
             pub_date=datetime.datetime(2010, 9, 4),
             reporter=self.r,
         )
         # Retrieve it from the DB
         a = Article.objects.get(headline="Test article")
         a.reporter_id = 30
         try:
             with connection.constraint_checks_disabled():
                 a.save()
         except IntegrityError:
             self.fail("IntegrityError should not have occurred.")
         transaction.set_rollback(True)