Beispiel #1
0
    def test_cleanup(self):
        """Test that the serialization cleans up after itself."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithBasicField

        patched = ('__reduce__', '__setstate__', '__getstate__')
        expected = {}
        actual = {}
        for patch in patched:
            try:
                expected[patch] = getattr(models.Model, patch)
            except:
                expected[patch] = None

        m = ModelWithBasicField.objects.create(name='foo')
        model_decode(model_encode(m))

        for patch in patched:
            try:
                actual[patch] = getattr(models.Model, patch)
            except:
                actual[patch] = None

        for key, value in expected.items():
            self.assertIn(key, actual, 'Expected key %s to exist in actual' % key)
            self.assertEqual(actual[key], value, "Expected %s in actual to be equal to %s not %s." % (key, value, actual[key]))
Beispiel #2
0
    def test_cleanup(self):
        """Test that the serialization cleans up after itself."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithBasicField

        patched = ('__reduce__', '__setstate__', '__getstate__')
        expected = {}
        actual = {}
        for patch in patched:
            try:
                expected[patch] = getattr(models.Model, patch)
            except:
                expected[patch] = None

        m = ModelWithBasicField.objects.create(name='foo')
        model_decode(model_encode(m))

        for patch in patched:
            try:
                actual[patch] = getattr(models.Model, patch)
            except:
                actual[patch] = None

        for key, value in expected.items():
            self.assertIn(key, actual,
                          'Expected key %s to exist in actual' % key)
            self.assertEqual(
                actual[key], value,
                "Expected %s in actual to be equal to %s not %s." %
                (key, value, actual[key]))
Beispiel #3
0
    def test_basic_patch(self):
        """Test that patching works as expected."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithBasicField

        expected = ModelWithBasicField.objects.create(name='foo')
        actual = model_decode(model_encode(expected))

        self.assertGreater(expected.pk, 0)
        self.assertEqual(actual.pk, expected.pk)
        self.assertEqual(actual.name, 'foo')
Beispiel #4
0
    def test_inherited_model(self):
        """Test that patching an inherited models works as expected."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithParentModel

        expected = ModelWithParentModel.objects.create(name='foo')
        actual = model_decode(model_encode(expected))

        self.assertGreater(expected.pk, 0)
        self.assertEqual(actual.pk, expected.pk)
        self.assertEqual(actual.name, 'foo')
Beispiel #5
0
    def test_inherited_model(self):
        """Test that patching an inherited models works as expected."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithParentModel

        expected = ModelWithParentModel.objects.create(name='foo')
        actual = model_decode(model_encode(expected))

        self.assertGreater(expected.pk, 0)
        self.assertEqual(actual.pk, expected.pk)
        self.assertEqual(actual.name, 'foo')
Beispiel #6
0
    def test_basic_patch(self):
        """Test that patching works as expected."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithBasicField

        expected = ModelWithBasicField.objects.create(name='foo')
        actual = model_decode(model_encode(expected))

        self.assertGreater(expected.pk, 0)
        self.assertEqual(actual.pk, expected.pk)
        self.assertEqual(actual.name, 'foo')
Beispiel #7
0
    def test_deep_patch(self):
        """Test that models contained deep inside a dict are serialized correctly."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithBasicField

        bar = ModelWithBasicField.objects.create(name='bar')
        eggs = ModelWithBasicField.objects.create(name='sausage')
        expected = {'foo': {'bar': bar, 'eggs': eggs}}
        actual = model_decode(model_encode(expected))

        self.assertEqual(actual, expected)
Beispiel #8
0
    def test_deep_patch(self):
        """Test that models contained deep inside a dict are serialized correctly."""
        from django_cereal.pickle import model_encode, model_decode
        from django_cereal.tests.testapp.models import ModelWithBasicField
        
        bar = ModelWithBasicField.objects.create(name='bar')
        eggs = ModelWithBasicField.objects.create(name='sausage')
        expected = {'foo': {'bar': bar,
                            'eggs': eggs}}
        actual = model_decode(model_encode(expected))

        self.assertEqual(actual, expected)