Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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.º 11
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.º 12
0
 def test_serialize_file_field(self):
     FileData().save()
     self.assertEquals(
         FixtureSerializer().serialize('json', FileData.objects.all()),
         serializers.serialize('json', FileData.objects.all()))
Exemplo n.º 13
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.º 14
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))