def test_field_internal_value(self): location = mommy.make(Location) serializer = LocationSerializer(instance=location) file_field = serializer.get_field('document') storage = file_field.model_field.storage def exists(name): return name == 'bar' def size(*args): return 1 storage.exists = exists storage.size = size with self.assertRaises(ValidationError): file_field.to_internal_value('foo') self.assertEquals(file_field.to_internal_value('bar'), 'bar')
def test_recursive_serializer(self): s = LocationSerializer( request_fields={'cats': { 'parent': { 'parent': True } }}) cats_field = s.get_all_fields()['cats'] l1 = cats_field.serializer.child # .child because list l2 = l1.get_all_fields()['parent'].serializer l3 = l2.get_all_fields()['parent'].serializer l4 = l3.get_all_fields()['parent'].serializer self.assertIsNot(l2, l3) # l3 and l4 should be same cached instance because both have # request_fields=True (l3 by inheritence, l4 by default) self.assertIs(l3, l4)
def test_recursive_serializer(self): s = LocationSerializer( request_fields={ 'cats': { 'parent': { 'parent': True } } } ) cats_field = s.get_all_fields()['cats'] l1 = cats_field.serializer.child # .child because list l2 = l1.get_all_fields()['parent'].serializer l3 = l2.get_all_fields()['parent'].serializer l4 = l3.get_all_fields()['parent'].serializer self.assertIsNot(l2, l3) # l3 and l4 should be same cached instance because both have # request_fields=True (l3 by inheritence, l4 by default) self.assertIs(l3, l4)