def test_list_operations(): herd = Herd(animals=[Bird('A', 'f'), Worm('X'), Bird('B', 'm'), Worm('Y')]) assert isinstance(herd.animals, ObjectList) assert len(herd.animals) == 4 assert herd.animals[0] == Bird('A', 'f') assert herd.animals[-1] == Worm('Y') assert list(herd.animals.instances_of(Worm)) == [Worm('X'), Worm('Y')]
def test_pickled_with_django_serializer(): """ Test serializing Django object with pickled field to a stream. Note: When serializing objects with Django serialization interface (django.core.serializers), the dump method of the serializer_class is not called, but serialize method is. This means that data is serialized in the dictionary format, but it is not pickled. """ # Create object leader = Bird('Boss', 'f') animals = ObjectList([Worm('Joe'), Bird('Andy', 'm')]) obj = PickledHerd(leader=leader, animals=animals) obj.save() # Serialize objects = PickledHerd.objects.filter(pk=obj.pk) json_string = serializers.serialize('json', objects) # Check serialized data serialized_data = json.loads(json_string) assert serialized_data == [{ 'model': 'testapp_polyfield_basic.pickledherd', 'pk': obj.pk, 'fields': { 'animals': [{ 'D': { 'name': 'Joe', 'age': 0 }, 'T': 'testapp_polyfield_basic.animals.Worm' }, { 'D': { 'name': 'Andy', 'sex': 'm' }, 'T': 'testapp_polyfield_basic.animals.Bird' }], 'leader': { 'D': { 'name': 'Boss', 'sex': 'f' }, 'T': 'testapp_polyfield_basic.animals.Bird' }, }, }]
def test_polyfields_with_django_serializer(): # Create object owner = NonAnimal('Owner') contents = [NonAnimal('B'), Bird('Donald', 'm'), NonAnimal('A')] obj = FunnyContainer(owner=owner, contents=ObjectList(contents)) obj.save() # Serialize objects = FunnyContainer.objects.filter(pk=obj.pk) json_string = serializers.serialize('json', objects) # Check serialized data serialized_data = json.loads(json_string) nonanimal_type = __name__ + '.NonAnimal' bird_type = 'testapp_polyfield_basic.animals.Bird' assert serialized_data == [{ 'model': 'testapp_polyfield_basic.funnycontainer', 'pk': obj.pk, 'fields': { 'contents': [ { 'type': nonanimal_type, 'data': { 'key': 'B' } }, { 'type': bird_type, 'data': { 'name': 'Donald', 'sex': 'm' } }, { 'type': nonanimal_type, 'data': { 'key': 'A' } }, ], 'owner': { 'type': nonanimal_type, 'data': { 'key': 'Owner' } }, 'parent': None, 'bird': { 'type': bird_type, 'data': { 'name': 'Daisy', 'sex': 'f' } }, }, }]
def get_objs(): check_postgres_support() from testapp_polyfield_pg.models import PostgresJsonObject # Create the objects for i in range(0, 10): bird = Bird('Bird ' + chr(ord('A') + i), ['f', 'm'][i % 2]) PostgresJsonObject.objects.create(obj=bird, bare_json=bird.to_dict()) for i in range(0, 20): worm = Worm('Worm ' + chr(ord('A') + i)) worm.set_age(i + 1) PostgresJsonObject.objects.create(obj=worm, bare_json=worm.to_dict()) # Test instances bird_b = Bird('Bird B', 'm') worm_b = Worm.from_dict({'name': 'Worm B', 'age': 2}) return (PostgresJsonObject.objects, bird_b, worm_b)
def test_polylistfield_invalid_container(): herd = Herd() herd.animals = Bird('Bob', 'm') # TODO: Make this raise with pytest.raises(TypeError): herd.save()
def test_polylistfield_invalid_subclass(): herd = Herd() herd.animals.extend([Bird('Bob', 'm'), NonAnimal()]) # TODO: Raise here with pytest.raises(TypeError): herd.save()
def test_polyfield_roundtrip(): herd = Herd() herd.leader = Bird('Eagle', 'f') herd.save() assert Herd.objects.get(pk=herd.pk).leader == herd.leader
def test_polylistfield_roundtrip(): herd = Herd() herd.animals.extend([Bird('Bob', 'm'), Worm('Wally')]) herd.save() assert Herd.objects.get(pk=herd.pk).animals == herd.animals
def test_pickled_polylistfield_roundtrip(): herd = PickledHerd() herd.animals = ObjectList([Bird('Bob', 'm'), Worm('Wally')]) herd.save() assert PickledHerd.objects.get(pk=herd.pk).animals == herd.animals