Exemplo n.º 1
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.serializer = PremiumAccountSerializer()
     PremiumAccount.objects.create(points=42,
                                   company='Foozle Inc.',
                                   date_upgraded=datetime.datetime(
                                       year=2012, month=4, day=30, hour=9))
Exemplo n.º 2
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.profile_serializer = ProfileSerializer()
     user = User.objects.create(email='*****@*****.**')
     Profile.objects.create(user=user,
                            country_of_birth='UK',
                            date_of_birth=datetime.datetime(day=5,
                                                            month=4,
                                                            year=1979))
Exemplo n.º 3
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     joe = PetOwner.objects.create(first_name='joe',
                                   last_name='adams',
                                   birthdate=datetime.date(year=1965,
                                                           month=8,
                                                           day=27))
     Pet.objects.create(owner=joe, name='splash gordon')
     Pet.objects.create(owner=joe, name='frogger')
Exemplo n.º 4
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.nested_model = NestedVehicleSerializer()
     self.flat_model = VehicleSerializer()
     self.owner = Owner.objects.create(email='*****@*****.**')
     self.car = Vehicle.objects.create(owner=self.owner,
                                       licence='DJANGO42',
                                       date_of_manufacture=datetime.date(
                                           day=6, month=6, year=2005))
     self.bike = Vehicle.objects.create(owner=self.owner,
                                        licence='',
                                        date_of_manufacture=datetime.date(
                                            day=8, month=8, year=1990))
Exemplo n.º 5
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.serializer = RaceEntrySerializer()
     RaceEntry.objects.create(name='John doe',
                              runner_number=6014,
                              start_time=datetime.datetime(year=2012,
                                                           month=4,
                                                           day=30,
                                                           hour=9),
                              finish_time=datetime.datetime(year=2012,
                                                            month=4,
                                                            day=30,
                                                            hour=12,
                                                            minute=25))
Exemplo n.º 6
0
    def test_unicode_pk(self):
        actor_name = u"Za\u017c\u00f3\u0142\u0107"
        movie_title = u'G\u0119\u015bl\u0105 ja\u017a\u0144'
        ac = Actor(name=actor_name)
        mv = Movie(title=movie_title, actor=ac)
        ac.save()
        mv.save()

        serial_str = FixtureSerializer().serialize('json', [ac])
        self.assertEqual(serializers.serialize('json', [ac]), serial_str)

        obj_list = list(FixtureSerializer().deserialize('json', serial_str))
        ac_obj = obj_list[0].object
        self.assertEqual(ac_obj.name, actor_name)
Exemplo n.º 7
0
    def test_fields(self):
        obj = ComplexModel(field1='first', field2='second', field3='third')

        # Serialize then deserialize the test database
        serialized_data = FixtureSerializer().serialize('json', [obj],
                                                        indent=2,
                                                        fields=('field1',
                                                                'field3'))
        result = next(FixtureSerializer().deserialize('json', serialized_data))

        # Check that the deserialized object contains data in only the serialized fields.
        self.assertEqual(result.object.field1, 'first')
        self.assertEqual(result.object.field2, '')
        self.assertEqual(result.object.field3, 'third')
Exemplo n.º 8
0
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.nested_model = NestedBookSerializer()
        self.flat_model = BookSerializer()
        self.lucy = Author.objects.create(name='Lucy Black')
        self.mark = Author.objects.create(name='Mark Green')
        self.cookbook = Book.objects.create(title='Cooking with gas',
                                            in_stock=True)
        self.cookbook.authors = [self.lucy, self.mark]
        self.cookbook.save()

        self.otherbook = Book.objects.create(title='Chimera obscura',
                                             in_stock=False)
        self.otherbook.authors = [self.mark]
        self.otherbook.save()
Exemplo n.º 9
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.serializer = PremiumAccountSerializer()
     PremiumAccount.objects.create(
         points=42,
         company='Foozle Inc.',
         date_upgraded=datetime.datetime(year=2012, month=4, day=30, hour=9)
     )
Exemplo n.º 10
0
class TestNullPKModel(SerializationTestCase):
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.serializer = RaceEntrySerializer()
        self.objs = [
            RaceEntry(name='John doe',
                      runner_number=6014,
                      start_time=datetime.datetime(year=2012,
                                                   month=4,
                                                   day=30,
                                                   hour=9),
                      finish_time=datetime.datetime(year=2012,
                                                    month=4,
                                                    day=30,
                                                    hour=12,
                                                    minute=25))
        ]

    def test_null_pk_dumpdata_json(self):
        self.assertEquals(self.dumpdata.serialize('json', self.objs),
                          serializers.serialize('json', self.objs))

    def test_null_pk_dumpdata_yaml(self):
        self.assertEquals(self.dumpdata.serialize('yaml', self.objs),
                          serializers.serialize('yaml', self.objs))

    def test_null_pk_dumpdata_xml(self):
        self.assertEquals(self.dumpdata.serialize('xml', self.objs),
                          serializers.serialize('xml', self.objs))

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(self.objs, serializer=self.serializer)
        rhs = get_deserialized(self.objs)
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(self.objs, serializer=self.dumpdata)
        rhs = get_deserialized(self.objs)
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize_xml(self):
        lhs = get_deserialized(self.objs,
                               format='xml',
                               serializer=self.dumpdata)
        rhs = get_deserialized(self.objs, format='xml')
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 11
0
class TestManyToManyThroughModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model with a 'through' relationship.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        right = Anchor.objects.create(data='foobar')
        left = M2MIntermediateData.objects.create()
        Intermediate.objects.create(extra='wibble', left=left, right=right)
        self.obj = left

    def test_m2m_through_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', M2MIntermediateData.objects.all()),
            serializers.serialize('json', M2MIntermediateData.objects.all()))
        self.assertEquals(
            self.dumpdata.serialize('json', Anchor.objects.all()),
            serializers.serialize('json', Anchor.objects.all()))
Exemplo n.º 12
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.serializer = RaceEntrySerializer()
     self.objs = [RaceEntry(
         name='John doe',
         runner_number=6014,
         start_time=datetime.datetime(year=2012, month=4, day=30, hour=9),
         finish_time=datetime.datetime(year=2012, month=4, day=30, hour=12, minute=25)
     )]
Exemplo n.º 13
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.profile_serializer = ProfileSerializer()
     user = User.objects.create(email='*****@*****.**')
     Profile.objects.create(
         user=user,
         country_of_birth='UK',
         date_of_birth=datetime.datetime(day=5, month=4, year=1979)
     )
Exemplo n.º 14
0
class TestNullPKModel(SerializationTestCase):
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.serializer = RaceEntrySerializer()
        self.objs = [RaceEntry(
            name='John doe',
            runner_number=6014,
            start_time=datetime.datetime(year=2012, month=4, day=30, hour=9),
            finish_time=datetime.datetime(year=2012, month=4, day=30, hour=12, minute=25)
        )]

    def test_null_pk_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', self.objs),
            serializers.serialize('json', self.objs)
        )

    def test_null_pk_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', self.objs),
            serializers.serialize('yaml', self.objs)
        )

    def test_null_pk_dumpdata_xml(self):
        self.assertEquals(
            self.dumpdata.serialize('xml', self.objs),
            serializers.serialize('xml', self.objs)
        )

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(self.objs, serializer=self.serializer)
        rhs = get_deserialized(self.objs)
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(self.objs, serializer=self.dumpdata)
        rhs = get_deserialized(self.objs)
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize_xml(self):
        lhs = get_deserialized(self.objs, format='xml', serializer=self.dumpdata)
        rhs = get_deserialized(self.objs, format='xml')
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 15
0
class TestManyToManyThroughModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model with a 'through' relationship.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        right = Anchor.objects.create(data='foobar')
        left = M2MIntermediateData.objects.create()
        Intermediate.objects.create(extra='wibble', left=left, right=right)
        self.obj = left

    def test_m2m_through_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', M2MIntermediateData.objects.all()),
            serializers.serialize('json', M2MIntermediateData.objects.all())
        )
        self.assertEquals(
            self.dumpdata.serialize('json', Anchor.objects.all()),
            serializers.serialize('json', Anchor.objects.all())
        )
Exemplo n.º 16
0
 def test_naturalkey_dumpdata_xml(self):
     """
     Ensure that we can replicate the existing dumpdata
     'use_natural_keys' behaviour.
     """
     self.assertEquals(
         FixtureSerializer().serialize('xml',
                                       Pet.objects.all(),
                                       use_natural_keys=True),
         serializers.serialize('xml',
                               Pet.objects.all(),
                               use_natural_keys=True))
Exemplo n.º 17
0
    def test_altering_serialized_output(self):
        """
        Tests the ability to create new objects by
        modifying serialized content.
        """
        old_headline = "Poker has no place on ESPN"
        new_headline = "Poker has no place on television"
        serial_str = FixtureSerializer().serialize('xml', Article.objects.all())

        serial_str = serial_str.replace(old_headline, new_headline)
        models = list(FixtureSerializer().deserialize('xml', serial_str))

        # Prior to saving, old headline is in place
        self.assertTrue(Article.objects.filter(headline=old_headline))
        self.assertFalse(Article.objects.filter(headline=new_headline))

        for model in models:
            model.save()

        # After saving, new headline is in place
        self.assertTrue(Article.objects.filter(headline=new_headline))
        self.assertFalse(Article.objects.filter(headline=old_headline))
Exemplo n.º 18
0
    def test_altering_serialized_output(self):
        """
        Tests the ability to create new objects by
        modifying serialized content.
        """
        old_headline = "Poker has no place on ESPN"
        new_headline = "Poker has no place on television"
        serial_str = FixtureSerializer().serialize('xml',
                                                   Article.objects.all())

        serial_str = serial_str.replace(old_headline, new_headline)
        models = list(FixtureSerializer().deserialize('xml', serial_str))

        # Prior to saving, old headline is in place
        self.assertTrue(Article.objects.filter(headline=old_headline))
        self.assertFalse(Article.objects.filter(headline=new_headline))

        for model in models:
            model.save()

        # After saving, new headline is in place
        self.assertTrue(Article.objects.filter(headline=new_headline))
        self.assertFalse(Article.objects.filter(headline=old_headline))
Exemplo n.º 19
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     self.nested_model = NestedVehicleSerializer()
     self.flat_model = VehicleSerializer()
     self.owner = Owner.objects.create(
         email='*****@*****.**'
     )
     self.car = Vehicle.objects.create(
         owner=self.owner,
         licence='DJANGO42',
         date_of_manufacture=datetime.date(day=6, month=6, year=2005)
     )
     self.bike = Vehicle.objects.create(
         owner=self.owner,
         licence='',
         date_of_manufacture=datetime.date(day=8, month=8, year=1990)
     )
Exemplo n.º 20
0
class TestModelInheritance(SerializationTestCase):
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.serializer = PremiumAccountSerializer()
        PremiumAccount.objects.create(
            points=42,
            company='Foozle Inc.',
            date_upgraded=datetime.datetime(year=2012, month=4, day=30, hour=9)
        )

    def test_dumpdata_child_model(self):
        self.assertEquals(
            self.dumpdata.serialize('json', PremiumAccount.objects.all()),
            serializers.serialize('json', PremiumAccount.objects.all())
        )

    def test_serialize_child_model(self):
        expected = [{
            'id': 1,
            'points': 42,
            'company': 'Foozle Inc.',
            'date_upgraded': datetime.datetime(2012, 4, 30, 9, 0)
        }]
        self.assertEquals(
            self.serializer.serialize('python', PremiumAccount.objects.all()),
            expected
        )

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(PremiumAccount.objects.all(), serializer=self.serializer)
        rhs = get_deserialized(PremiumAccount.objects.all())
        self.assertFalse(deserialized_eq(lhs, rhs))
        # We expect these *not* to match - the dumpdata implementation only
        # includes the base fields.

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(PremiumAccount.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(PremiumAccount.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 21
0
class TestModelInheritance(SerializationTestCase):
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.serializer = PremiumAccountSerializer()
        PremiumAccount.objects.create(points=42,
                                      company='Foozle Inc.',
                                      date_upgraded=datetime.datetime(
                                          year=2012, month=4, day=30, hour=9))

    def test_dumpdata_child_model(self):
        self.assertEquals(
            self.dumpdata.serialize('json', PremiumAccount.objects.all()),
            serializers.serialize('json', PremiumAccount.objects.all()))

    def test_serialize_child_model(self):
        expected = [{
            'id': 1,
            'points': 42,
            'company': 'Foozle Inc.',
            'date_upgraded': datetime.datetime(2012, 4, 30, 9, 0)
        }]
        self.assertEquals(
            self.serializer.serialize('python', PremiumAccount.objects.all()),
            expected)

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(PremiumAccount.objects.all(),
                               serializer=self.serializer)
        rhs = get_deserialized(PremiumAccount.objects.all())
        self.assertFalse(deserialized_eq(lhs, rhs))
        # We expect these *not* to match - the dumpdata implementation only
        # includes the base fields.

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(PremiumAccount.objects.all(),
                               serializer=self.dumpdata)
        rhs = get_deserialized(PremiumAccount.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 22
0
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.nested_model = NestedBookSerializer()
        self.flat_model = BookSerializer()
        self.lucy = Author.objects.create(
            name='Lucy Black'
        )
        self.mark = Author.objects.create(
            name='Mark Green'
        )
        self.cookbook = Book.objects.create(
            title='Cooking with gas',
            in_stock=True
        )
        self.cookbook.authors = [self.lucy, self.mark]
        self.cookbook.save()

        self.otherbook = Book.objects.create(
            title='Chimera obscura',
            in_stock=False
        )
        self.otherbook.authors = [self.mark]
        self.otherbook.save()
Exemplo n.º 23
0
class TestFKModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.nested_model = NestedVehicleSerializer()
        self.flat_model = VehicleSerializer()
        self.owner = Owner.objects.create(email='*****@*****.**')
        self.car = Vehicle.objects.create(owner=self.owner,
                                          licence='DJANGO42',
                                          date_of_manufacture=datetime.date(
                                              day=6, month=6, year=2005))
        self.bike = Vehicle.objects.create(owner=self.owner,
                                           licence='',
                                           date_of_manufacture=datetime.date(
                                               day=8, month=8, year=1990))

    def test_fk_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', Vehicle.objects.all()),
            serializers.serialize('json', Vehicle.objects.all()))

    def test_fk_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', Vehicle.objects.all()),
            serializers.serialize('yaml', Vehicle.objects.all()))

    def test_fk_dumpdata_xml(self):
        self.assertEquals(
            self.dumpdata.serialize('xml', Vehicle.objects.all()),
            serializers.serialize('xml', Vehicle.objects.all()))

    def test_fk_nested(self):
        expected = {
            'id': 1,
            'owner': {
                'id': 1,
                'email': u'*****@*****.**'
            },
            'licence': u'DJANGO42',
            'date_of_manufacture': datetime.date(day=6, month=6, year=2005)
        }
        self.assertEquals(
            self.nested_model.serialize('python', Vehicle.objects.get(id=1)),
            expected)

    def test_fk_flat(self):
        expected = {
            'id': 1,
            'owner': 1,
            'licence': u'DJANGO42',
            'date_of_manufacture': datetime.date(day=6, month=6, year=2005)
        }
        self.assertEquals(
            self.flat_model.serialize('python', Vehicle.objects.get(id=1)),
            expected)

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(Vehicle.objects.all(),
                               serializer=self.flat_model)
        rhs = get_deserialized(Vehicle.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(Vehicle.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(Vehicle.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_reverse_fk_flat(self):
        class OwnerSerializer(ModelSerializer):
            vehicles = PrimaryKeyRelatedField()

        expected = {'id': 1, 'email': u'*****@*****.**', 'vehicles': [1, 2]}

        self.assertEquals(
            OwnerSerializer().serialize('python', Owner.objects.get(id=1)),
            expected)

    def test_reverse_fk_nested(self):
        class OwnerSerializer(ModelSerializer):
            vehicles = ModelSerializer()

        expected = {
            'id':
            1,
            'email':
            u'*****@*****.**',
            'vehicles': [{
                'id':
                1,
                'licence':
                u'DJANGO42',
                'owner':
                1,
                'date_of_manufacture':
                datetime.date(day=6, month=6, year=2005)
            }, {
                'id':
                2,
                'licence':
                u'',
                'owner':
                1,
                'date_of_manufacture':
                datetime.date(day=8, month=8, year=1990)
            }]
        }
        self.assertEquals(
            OwnerSerializer().serialize('python', Owner.objects.get(id=1)),
            expected)
Exemplo n.º 24
0
class TestManyToManyModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.nested_model = NestedBookSerializer()
        self.flat_model = BookSerializer()
        self.lucy = Author.objects.create(name='Lucy Black')
        self.mark = Author.objects.create(name='Mark Green')
        self.cookbook = Book.objects.create(title='Cooking with gas',
                                            in_stock=True)
        self.cookbook.authors = [self.lucy, self.mark]
        self.cookbook.save()

        self.otherbook = Book.objects.create(title='Chimera obscura',
                                             in_stock=False)
        self.otherbook.authors = [self.mark]
        self.otherbook.save()

    def test_m2m_dumpdata_json(self):
        self.assertEquals(self.dumpdata.serialize('json', Book.objects.all()),
                          serializers.serialize('json', Book.objects.all()))
        self.assertEquals(
            self.dumpdata.serialize('json', Author.objects.all()),
            serializers.serialize('json', Author.objects.all()))

    def test_m2m_dumpdata_yaml(self):
        self.assertEquals(self.dumpdata.serialize('yaml', Book.objects.all()),
                          serializers.serialize('yaml', Book.objects.all()))
        self.assertEquals(
            self.dumpdata.serialize('yaml', Author.objects.all()),
            serializers.serialize('yaml', Author.objects.all()))

    def test_m2m_dumpdata_xml(self):
        # # Hack to ensure field ordering is correct for xml
        # dumpdata = FixtureSerializer()
        # dumpdata.fields['fields'].opts.preserve_field_order = True
        self.assertEquals(self.dumpdata.serialize('xml', Book.objects.all()),
                          serializers.serialize('xml', Book.objects.all()))
        self.assertEquals(self.dumpdata.serialize('xml', Author.objects.all()),
                          serializers.serialize('xml', Author.objects.all()))

    def test_m2m_nested(self):
        expected = {
            'id':
            1,
            'title':
            u'Cooking with gas',
            'in_stock':
            True,
            'authors': [{
                'id': 1,
                'name': 'Lucy Black'
            }, {
                'id': 2,
                'name': 'Mark Green'
            }]
        }
        self.assertEquals(
            self.nested_model.serialize('python', Book.objects.get(id=1)),
            expected)

    def test_m2m_flat(self):
        expected = {
            'id': 1,
            'title': u'Cooking with gas',
            'in_stock': True,
            'authors': [1, 2]
        }
        self.assertEquals(
            self.flat_model.serialize('python', Book.objects.get(id=1)),
            expected)

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(Book.objects.all(), serializer=self.flat_model)
        rhs = get_deserialized(Book.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(Book.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(Book.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 25
0
 def test_dumpdata_deserialize(self):
     lhs = get_deserialized(Pet.objects.all(),
                            serializer=FixtureSerializer(),
                            use_natural_keys=True)
     rhs = get_deserialized(Pet.objects.all(), use_natural_keys=True)
     self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 26
0
class TestSimpleModel(SerializationTestCase):
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.serializer = RaceEntrySerializer()
        RaceEntry.objects.create(
            name='John doe',
            runner_number=6014,
            start_time=datetime.datetime(year=2012, month=4, day=30, hour=9),
            finish_time=datetime.datetime(year=2012, month=4, day=30, hour=12, minute=25)
        )

    def test_simple_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', RaceEntry.objects.all()),
            serializers.serialize('json', RaceEntry.objects.all())
        )

    def test_simple_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', RaceEntry.objects.all()),
            serializers.serialize('yaml', RaceEntry.objects.all())
        )

    def test_simple_dumpdata_xml(self):
        self.assertEquals(
            self.dumpdata.serialize('xml', RaceEntry.objects.all()),
            serializers.serialize('xml', RaceEntry.objects.all())
        )

    def test_csv(self):
        expected = (
            "id,name,runner_number,start_time,finish_time\r\n"
            "1,John doe,6014,2012-04-30 09:00:00,2012-04-30 12:25:00\r\n"
        )
        self.assertEquals(
            self.serializer.serialize('csv', RaceEntry.objects.all()),
            expected
        )

    def test_simple_dumpdata_fields(self):
        self.assertEquals(
            self.dumpdata.serialize('json', RaceEntry.objects.all(), fields=('name', 'runner_number')),
            serializers.serialize('json', RaceEntry.objects.all(), fields=('name', 'runner_number'))
        )

    def test_deserialize_fields(self):
        lhs = get_deserialized(RaceEntry.objects.all(), serializer=self.dumpdata, fields=('runner_number',))
        rhs = get_deserialized(RaceEntry.objects.all(), fields=('runner_number',))
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(RaceEntry.objects.all(), serializer=self.serializer)
        rhs = get_deserialized(RaceEntry.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(RaceEntry.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(RaceEntry.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize_xml(self):
        lhs = get_deserialized(RaceEntry.objects.all(), format='xml', serializer=self.dumpdata)
        rhs = get_deserialized(RaceEntry.objects.all(), format='xml')
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 27
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     right = Anchor.objects.create(data='foobar')
     left = M2MIntermediateData.objects.create()
     Intermediate.objects.create(extra='wibble', left=left, right=right)
     self.obj = left
Exemplo n.º 28
0
 def setUp(self):
     self.dumpdata = FixtureSerializer()
     right = Anchor.objects.create(data='foobar')
     left = M2MIntermediateData.objects.create()
     Intermediate.objects.create(extra='wibble', left=left, right=right)
     self.obj = left
Exemplo n.º 29
0
class TestOneToOneModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.profile_serializer = ProfileSerializer()
        user = User.objects.create(email='*****@*****.**')
        Profile.objects.create(user=user,
                               country_of_birth='UK',
                               date_of_birth=datetime.datetime(day=5,
                                                               month=4,
                                                               year=1979))

    def test_onetoone_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', Profile.objects.all()),
            serializers.serialize('json', Profile.objects.all()))

    def test_onetoone_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', Profile.objects.all()),
            serializers.serialize('yaml', Profile.objects.all()))

    def test_onetoone_dumpdata_xml(self):
        self.assertEquals(
            self.dumpdata.serialize('xml', Profile.objects.all()),
            serializers.serialize('xml', Profile.objects.all()))

    def test_onetoone_nested(self):
        expected = {
            'id': 1,
            'user': {
                'id': 1,
                'email': '*****@*****.**'
            },
            'country_of_birth': 'UK',
            'date_of_birth': datetime.datetime(day=5, month=4, year=1979)
        }
        self.assertEquals(
            self.profile_serializer.serialize('python',
                                              Profile.objects.get(id=1),
                                              nested=True), expected)

    def test_onetoone_flat(self):
        expected = {
            'id': 1,
            'user': 1,
            'country_of_birth': 'UK',
            'date_of_birth': datetime.datetime(day=5, month=4, year=1979)
        }
        self.assertEquals(
            self.profile_serializer.serialize('python',
                                              Profile.objects.get(id=1)),
            expected)

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(Profile.objects.all(),
                               serializer=self.profile_serializer)
        rhs = get_deserialized(Profile.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(Profile.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(Profile.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 30
0
class TestSimpleModel(SerializationTestCase):
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.serializer = RaceEntrySerializer()
        RaceEntry.objects.create(name='John doe',
                                 runner_number=6014,
                                 start_time=datetime.datetime(year=2012,
                                                              month=4,
                                                              day=30,
                                                              hour=9),
                                 finish_time=datetime.datetime(year=2012,
                                                               month=4,
                                                               day=30,
                                                               hour=12,
                                                               minute=25))

    def test_simple_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', RaceEntry.objects.all()),
            serializers.serialize('json', RaceEntry.objects.all()))

    def test_simple_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', RaceEntry.objects.all()),
            serializers.serialize('yaml', RaceEntry.objects.all()))

    def test_simple_dumpdata_xml(self):
        self.assertEquals(
            self.dumpdata.serialize('xml', RaceEntry.objects.all()),
            serializers.serialize('xml', RaceEntry.objects.all()))

    def test_csv(self):
        expected = (
            "id,name,runner_number,start_time,finish_time\r\n"
            "1,John doe,6014,2012-04-30 09:00:00,2012-04-30 12:25:00\r\n")
        self.assertEquals(
            self.serializer.serialize('csv', RaceEntry.objects.all()),
            expected)

    def test_simple_dumpdata_fields(self):
        self.assertEquals(
            self.dumpdata.serialize('json',
                                    RaceEntry.objects.all(),
                                    fields=('name', 'runner_number')),
            serializers.serialize('json',
                                  RaceEntry.objects.all(),
                                  fields=('name', 'runner_number')))

    def test_deserialize_fields(self):
        lhs = get_deserialized(RaceEntry.objects.all(),
                               serializer=self.dumpdata,
                               fields=('runner_number', ))
        rhs = get_deserialized(RaceEntry.objects.all(),
                               fields=('runner_number', ))
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(RaceEntry.objects.all(),
                               serializer=self.serializer)
        rhs = get_deserialized(RaceEntry.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(RaceEntry.objects.all(),
                               serializer=self.dumpdata)
        rhs = get_deserialized(RaceEntry.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize_xml(self):
        lhs = get_deserialized(RaceEntry.objects.all(),
                               format='xml',
                               serializer=self.dumpdata)
        rhs = get_deserialized(RaceEntry.objects.all(), format='xml')
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 31
0
class TestOneToOneModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.profile_serializer = ProfileSerializer()
        user = User.objects.create(email='*****@*****.**')
        Profile.objects.create(
            user=user,
            country_of_birth='UK',
            date_of_birth=datetime.datetime(day=5, month=4, year=1979)
        )

    def test_onetoone_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', Profile.objects.all()),
            serializers.serialize('json', Profile.objects.all())
        )

    def test_onetoone_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', Profile.objects.all()),
            serializers.serialize('yaml', Profile.objects.all())
        )

    def test_onetoone_dumpdata_xml(self):
        self.assertEquals(
            self.dumpdata.serialize('xml', Profile.objects.all()),
            serializers.serialize('xml', Profile.objects.all())
        )

    def test_onetoone_nested(self):
        expected = {
            'id': 1,
            'user': {
                'id': 1,
                'email': '*****@*****.**'
            },
            'country_of_birth': 'UK',
            'date_of_birth': datetime.datetime(day=5, month=4, year=1979)
        }
        self.assertEquals(
            NestedProfileSerializer().serialize('python', Profile.objects.get(id=1)),
            expected
        )

    def test_onetoone_flat(self):
        expected = {
            'id': 1,
            'user': 1,
            'country_of_birth': 'UK',
            'date_of_birth': datetime.datetime(day=5, month=4, year=1979)
        }
        self.assertEquals(
            self.profile_serializer.serialize('python', Profile.objects.get(id=1)),
            expected
        )

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(Profile.objects.all(), serializer=self.profile_serializer)
        rhs = get_deserialized(Profile.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(Profile.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(Profile.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 32
0
class TestManyToManyModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.nested_model = NestedBookSerializer()
        self.flat_model = BookSerializer()
        self.lucy = Author.objects.create(
            name='Lucy Black'
        )
        self.mark = Author.objects.create(
            name='Mark Green'
        )
        self.cookbook = Book.objects.create(
            title='Cooking with gas',
            in_stock=True
        )
        self.cookbook.authors = [self.lucy, self.mark]
        self.cookbook.save()

        self.otherbook = Book.objects.create(
            title='Chimera obscura',
            in_stock=False
        )
        self.otherbook.authors = [self.mark]
        self.otherbook.save()

    def test_m2m_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', Book.objects.all()),
            serializers.serialize('json', Book.objects.all())
        )
        self.assertEquals(
            self.dumpdata.serialize('json', Author.objects.all()),
            serializers.serialize('json', Author.objects.all())
        )

    def test_m2m_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', Book.objects.all()),
            serializers.serialize('yaml', Book.objects.all())
        )
        self.assertEquals(
            self.dumpdata.serialize('yaml', Author.objects.all()),
            serializers.serialize('yaml', Author.objects.all())
        )

    def test_m2m_dumpdata_xml(self):
        # # Hack to ensure field ordering is correct for xml
        # dumpdata = FixtureSerializer()
        # dumpdata.fields['fields'].opts.preserve_field_order = True
        self.assertEquals(
            self.dumpdata.serialize('xml', Book.objects.all()),
            serializers.serialize('xml', Book.objects.all())
        )
        self.assertEquals(
            self.dumpdata.serialize('xml', Author.objects.all()),
            serializers.serialize('xml', Author.objects.all())
        )

    def test_m2m_nested(self):
        expected = {
            'id': 1,
            'title': u'Cooking with gas',
            'in_stock': True,
            'authors': [
                {'id': 1, 'name': 'Lucy Black'},
                {'id': 2, 'name': 'Mark Green'}
            ]
        }
        self.assertEquals(
            self.nested_model.serialize('python', Book.objects.get(id=1)),
            expected
        )

    def test_m2m_flat(self):
        expected = {
            'id': 1,
            'title': u'Cooking with gas',
            'in_stock': True,
            'authors': [1, 2]
        }
        self.assertEquals(
            self.flat_model.serialize('python', Book.objects.get(id=1)),
            expected
        )

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(Book.objects.all(), serializer=self.flat_model)
        rhs = get_deserialized(Book.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(Book.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(Book.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 33
0
 def test_serializer_roundtrip(self):
     """Tests that serialized content can be deserialized."""
     serial_str = FixtureSerializer().serialize('xml',
                                                Article.objects.all())
     models = list(FixtureSerializer().deserialize('xml', serial_str))
     self.assertEqual(len(models), 2)
Exemplo n.º 34
0
class TestFKModel(SerializationTestCase):
    """
    Test one-to-one field relationship on a model.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        self.nested_model = NestedVehicleSerializer()
        self.flat_model = VehicleSerializer()
        self.owner = Owner.objects.create(
            email='*****@*****.**'
        )
        self.car = Vehicle.objects.create(
            owner=self.owner,
            licence='DJANGO42',
            date_of_manufacture=datetime.date(day=6, month=6, year=2005)
        )
        self.bike = Vehicle.objects.create(
            owner=self.owner,
            licence='',
            date_of_manufacture=datetime.date(day=8, month=8, year=1990)
        )

    def test_fk_dumpdata_json(self):
        self.assertEquals(
            self.dumpdata.serialize('json', Vehicle.objects.all()),
            serializers.serialize('json', Vehicle.objects.all())
        )

    def test_fk_dumpdata_yaml(self):
        self.assertEquals(
            self.dumpdata.serialize('yaml', Vehicle.objects.all()),
            serializers.serialize('yaml', Vehicle.objects.all())
        )

    def test_fk_dumpdata_xml(self):
        self.assertEquals(
            self.dumpdata.serialize('xml', Vehicle.objects.all()),
            serializers.serialize('xml', Vehicle.objects.all())
        )

    def test_fk_nested(self):
        expected = {
            'id': 1,
            'owner': {
                'id': 1,
                'email': u'*****@*****.**'
            },
            'licence': u'DJANGO42',
            'date_of_manufacture': datetime.date(day=6, month=6, year=2005)
        }
        self.assertEquals(
            self.nested_model.serialize('python', Vehicle.objects.get(id=1)),
            expected
        )

    def test_fk_flat(self):
        expected = {
            'id': 1,
            'owner':  1,
            'licence': u'DJANGO42',
            'date_of_manufacture': datetime.date(day=6, month=6, year=2005)
        }
        self.assertEquals(
            self.flat_model.serialize('python', Vehicle.objects.get(id=1)),
            expected
        )

    def test_modelserializer_deserialize(self):
        lhs = get_deserialized(Vehicle.objects.all(), serializer=self.flat_model)
        rhs = get_deserialized(Vehicle.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(Vehicle.objects.all(), serializer=self.dumpdata)
        rhs = get_deserialized(Vehicle.objects.all())
        self.assertTrue(deserialized_eq(lhs, rhs))

    def test_reverse_fk_flat(self):
        class OwnerSerializer(ModelSerializer):
            vehicles = PrimaryKeyRelatedField()

        expected = {
            'id': 1,
            'email': u'*****@*****.**',
            'vehicles':  [1, 2]
        }

        self.assertEquals(
            OwnerSerializer().serialize('python', Owner.objects.get(id=1)),
            expected
        )

    def test_reverse_fk_nested(self):
        class OwnerSerializer(ModelSerializer):
            vehicles = ModelSerializer()

        expected = {
            'id': 1,
            'email': u'*****@*****.**',
            'vehicles': [
                {
                    'id': 1,
                    'licence': u'DJANGO42',
                    'owner': 1,
                    'date_of_manufacture': datetime.date(day=6, month=6, year=2005)
                }, {
                    'id': 2,
                    'licence': u'',
                    'owner': 1,
                    'date_of_manufacture': datetime.date(day=8, month=8, year=1990)
                }
            ]
        }
        self.assertEquals(
            OwnerSerializer().serialize('python', Owner.objects.get(id=1)),
            expected
        )
Exemplo n.º 35
0
class TestNaturalKey(SerializationTestCase):
    """
    Test one-to-one field relationship on a model.
    """
    def setUp(self):
        self.dumpdata = FixtureSerializer()
        joe = PetOwner.objects.create(first_name='joe',
                                      last_name='adams',
                                      birthdate=datetime.date(year=1965,
                                                              month=8,
                                                              day=27))
        Pet.objects.create(owner=joe, name='splash gordon')
        Pet.objects.create(owner=joe, name='frogger')

    def test_naturalkey_dumpdata_json(self):
        """
        Ensure that we can replicate the existing dumpdata
        'use_natural_keys' behaviour.
        """
        self.assertEquals(
            self.dumpdata.serialize('json',
                                    Pet.objects.all(),
                                    use_natural_keys=True),
            serializers.serialize('json',
                                  Pet.objects.all(),
                                  use_natural_keys=True))

    def test_naturalkey_dumpdata_yaml(self):
        """
        Ensure that we can replicate the existing dumpdata
        'use_natural_keys' behaviour.
        """
        self.assertEquals(
            self.dumpdata.serialize('yaml',
                                    Pet.objects.all(),
                                    use_natural_keys=True),
            serializers.serialize('yaml',
                                  Pet.objects.all(),
                                  use_natural_keys=True))

    def test_naturalkey_dumpdata_xml(self):
        """
        Ensure that we can replicate the existing dumpdata
        'use_natural_keys' behaviour.
        """
        self.assertEquals(
            self.dumpdata.serialize('xml',
                                    Pet.objects.all(),
                                    use_natural_keys=True),
            serializers.serialize('xml',
                                  Pet.objects.all(),
                                  use_natural_keys=True))

    def test_naturalkey(self):
        """
        Ensure that we can use NaturalKeyRelatedField to represent foreign
        key relationships.
        """
        class NaturalKeyModelSerializer(ModelSerializer):
            def get_related_field(self, model_field):
                return NaturalKeyRelatedField()

        serializer = NaturalKeyModelSerializer()

        expected = [
            {
                "owner": (u"joe", u"adams"),  # NK, not PK
                "id": 1,
                "name": u"splash gordon"
            },
            {
                "owner": (u"joe", u"adams"),  # NK, not PK
                "id": 2,
                "name": u"frogger"
            }
        ]
        self.assertEquals(serializer.serialize('python', Pet.objects.all()),
                          expected)

    def test_naturalkey_reverse_relation(self):
        """
        Ensure that we can use NaturalKeyRelatedField to represent
        reverse foreign key relationships.
        """
        class PetOwnerSerializer(ModelSerializer):
            pets = NaturalKeyRelatedField()

        expected = [{
            "first_name": u"joe",
            "last_name": u"adams",
            "id": 1,
            "birthdate": datetime.date(1965, 8, 27),
            "pets": [u"splash gordon", u"frogger"]  # NK, not PK
        }]
        self.assertEquals(
            PetOwnerSerializer().serialize('python', PetOwner.objects.all()),
            expected)

    # def test_modelserializer_deserialize(self):
    #     lhs = get_deserialized(PetOwner.objects.all(), serializer=self.serializer)
    #     rhs = get_deserialized(PetOwner.objects.all())
    #     self.assertTrue(deserialized_eq(lhs, rhs))

    def test_dumpdata_deserialize(self):
        lhs = get_deserialized(Pet.objects.all(),
                               serializer=self.dumpdata,
                               use_natural_keys=True)
        rhs = get_deserialized(Pet.objects.all(), use_natural_keys=True)
        self.assertTrue(deserialized_eq(lhs, rhs))
Exemplo n.º 36
0
 def test_serialize_file_field(self):
     FileData().save()
     self.assertEquals(
         FixtureSerializer().serialize('json', FileData.objects.all()),
         serializers.serialize('json', FileData.objects.all()))