class TestOneToOneModel(TestCase): """ Test one-to-one field relationship on a model. """ def setUp(self): self.dumpdata = DumpDataSerializer() self.nested_model = ModelSerializer() self.flat_model = ModelSerializer(depth=0) 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.encode(Profile.objects.all(), 'json'), serializers.serialize('json', Profile.objects.all()) ) def test_onetoone_dumpdata_yaml(self): self.assertEquals( self.dumpdata.encode(Profile.objects.all(), 'yaml'), serializers.serialize('yaml', Profile.objects.all()) ) def test_onetoone_dumpdata_xml(self): self.assertEquals( self.dumpdata.encode(Profile.objects.all(), 'xml'), 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.nested_model.serialize(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.flat_model.serialize(Profile.objects.get(id=1)), expected )
def setUp(self): self.nested_model = ModelSerializer(include=('profile',)) self.flat_model = ModelSerializer(depth=0, include=('profile',)) 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_reverse_fk_flat(self): expected = { 'id': 1, 'email': u'*****@*****.**', 'vehicles': [1, 2] } serializer = ModelSerializer(include=('vehicles',), depth=0) self.assertEquals( serializer.serialize(Owner.objects.get(id=1)), expected )
class TestModelInheritance(TestCase): def setUp(self): self.dumpdata = DumpDataSerializer() self.serializer = ModelSerializer() 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.encode(PremiumAccount.objects.all(), 'json'), 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(PremiumAccount.objects.all()), expected )
class TestSimpleModel(TestCase): def setUp(self): self.dumpdata = DumpDataSerializer() self.serializer = ModelSerializer(depth=0) 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_model(self): self.assertEquals( self.dumpdata.encode(RaceEntry.objects.all(), 'json'), serializers.serialize('json', 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.encode(RaceEntry.objects.all(), 'csv'), expected )
def setUp(self): self.dumpdata = DumpDataSerializer() self.serializer = ModelSerializer() PremiumAccount.objects.create( points=42, company='Foozle Inc.', date_upgraded=datetime.datetime(year=2012, month=4, day=30, hour=9) )
def setUp(self): self.dumpdata = DumpDataSerializer() self.nested_model = ModelSerializer() self.flat_model = ModelSerializer(depth=0) 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) )
class TestReverseOneToOneModel(TestCase): """ Test reverse relationship of one-to-one fields. Note the Django's dumpdata serializer doesn't support reverse relations, which wouldn't make sense in that context, so we don't include them in the tests. """ def setUp(self): self.nested_model = ModelSerializer(include=('profile',)) self.flat_model = ModelSerializer(depth=0, include=('profile',)) 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_reverse_onetoone_nested(self): expected = { 'id': 1, 'email': u'*****@*****.**', 'profile': { 'id': 1, 'country_of_birth': u'UK', 'date_of_birth': datetime.datetime(day=5, month=4, year=1979), 'user': 1 }, } self.assertEquals( self.nested_model.serialize(User.objects.get(id=1)), expected ) def test_reverse_onetoone_flat(self): expected = { 'id': 1, 'email': '*****@*****.**', 'profile': 1, } self.assertEquals( self.flat_model.serialize(User.objects.get(id=1)), expected )
def setUp(self): self.dumpdata = DumpDataSerializer() self.serializer = ModelSerializer(depth=0) 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_naturalkey_reverse_relation(self): """ Ensure that we can use NaturalKeyRelatedField to represent reverse foreign key relationships. """ serializer = ModelSerializer( include=('pets',), related_field=NaturalKeyRelatedField, depth=0 ) 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( serializer.serialize(PetOwner.objects.all()), expected )
def test_naturalkey(self): """ Ensure that we can use NaturalKeyRelatedField to represent foreign key relationships. """ serializer = ModelSerializer( related_field=NaturalKeyRelatedField, depth=0 ) 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(Pet.objects.all()), expected )
def setUp(self): self.dumpdata = DumpDataSerializer() self.nested_model = ModelSerializer() self.flat_model = ModelSerializer(depth=0) 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_reverse_fk_nested(self): 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) } ] } serializer = ModelSerializer(include=('vehicles',)) self.assertEquals( serializer.serialize(Owner.objects.get(id=1)), expected )
class OwnerSerializer(ModelSerializer): vehicles = ModelSerializer()
class TestManyToManyModel(TestCase): """ Test one-to-one field relationship on a model. """ def setUp(self): self.dumpdata = DumpDataSerializer() self.nested_model = ModelSerializer() self.flat_model = ModelSerializer(depth=0) 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.encode(Book.objects.all(), 'json'), serializers.serialize('json', Book.objects.all()) ) self.assertEquals( self.dumpdata.encode(Author.objects.all(), 'json'), serializers.serialize('json', Author.objects.all()) ) def test_m2m_dumpdata_yaml(self): self.assertEquals( self.dumpdata.encode(Book.objects.all(), 'yaml'), serializers.serialize('yaml', Book.objects.all()) ) self.assertEquals( self.dumpdata.encode(Author.objects.all(), 'yaml'), serializers.serialize('yaml', Author.objects.all()) ) # TODO # def test_m2m_dumpdata_xml(self): # # # Hack to ensure field ordering is correct for xml # # dumpdata = DumpDataSerializer() # # dumpdata.fields['fields'].opts.preserve_field_order = True # self.assertEquals( # self.dumpdata.encode(Book.objects.all(), 'xml'), # serializers.serialize('xml', Book.objects.all()) # ) # self.assertEquals( # self.dumpdata.encode(Author.objects.all(), 'xml'), # 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(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(Book.objects.get(id=1)), expected )
class NestedUserSerializer(ModelSerializer): profile = ModelSerializer()
class TestFKModel(TestCase): """ Test one-to-one field relationship on a model. """ def setUp(self): self.dumpdata = DumpDataSerializer() self.nested_model = ModelSerializer() self.flat_model = ModelSerializer(depth=0) 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.encode(Vehicle.objects.all(), 'json'), serializers.serialize('json', Vehicle.objects.all()) ) def test_fk_dumpdata_yaml(self): self.assertEquals( self.dumpdata.encode(Vehicle.objects.all(), 'yaml'), serializers.serialize('yaml', Vehicle.objects.all()) ) def test_fk_dumpdata_xml(self): self.assertEquals( self.dumpdata.encode(Vehicle.objects.all(), 'xml'), 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(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(Vehicle.objects.get(id=1)), expected ) def test_reverse_fk_flat(self): expected = { 'id': 1, 'email': u'*****@*****.**', 'vehicles': [1, 2] } serializer = ModelSerializer(include=('vehicles',), depth=0) self.assertEquals( serializer.serialize(Owner.objects.get(id=1)), expected ) def test_reverse_fk_nested(self): 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) } ] } serializer = ModelSerializer(include=('vehicles',)) self.assertEquals( serializer.serialize(Owner.objects.get(id=1)), expected )