def test_build_nested_objects_with_polymorphism(self): books = [{ 'title': "Book1", "isbn": "abc-123", 'num_pages': 1, 'rrp': 20.4, 'fiction': True, 'genre': 'sci-fi', 'published': [], 'authors': [], 'publisher': None, '$': 'tests.resources.Book' }, { 'title': "Book2", "isbn": "def-456", 'num_pages': 1, 'rrp': 20.4, 'fiction': True, 'genre': 'sci-fi', 'published': [], 'authors': [], 'publisher': None, '$': 'tests.resources.Book' }] library = {'name': 'John Smith Library', 'books': books} expected = sorted(build_object_graph(books, full_clean=False), key=lambda s: s.title) actual = sorted(build_object_graph(library, resource=Library, full_clean=False).books, key=lambda s: s.title) assert actual == expected
def test_build_object_graph_empty_dict(self): with self.assertRaises(ValidationError) as ctx: build_object_graph({}, Book) self.assertEqual(dict( title=['This field cannot be null.'], num_pages=['This field cannot be null.'], fiction=['This field cannot be null.'], genre=['This field cannot be null.'], published=['This field cannot be null.'], authors=['List cannot contain null entries.'], ), ctx.exception.error_messages)
def test_build_object_graph_empty_dict(self): with self.assertRaises(ValidationError) as ctx: build_object_graph({}, Book) self.assertEqual(dict( title=['This field cannot be null.'], isbn=['This field cannot be null.'], num_pages=['This field cannot be null.'], fiction=['This field cannot be null.'], genre=['This field cannot be null.'], published=['This field cannot be null.'], authors=['List cannot contain null entries.'], ), ctx.exception.error_messages)
def test_build_object_graph_empty_dict(self): with pytest.raises(ValidationError) as ctx: build_object_graph({}, Book) assert dict( title=['This field cannot be null.'], isbn=['This field cannot be null.'], num_pages=['This field cannot be null.'], fiction=['This field cannot be null.'], genre=['This field cannot be null.'], published=['This field cannot be null.'], authors=['List cannot contain null entries.'], ) == ctx.value.error_messages
def test_build_nested_objects(self): subscribers = [ {'name': 'John Smith', 'address': 'Oak Lane 1234'}, {'name': 'Johnny Smith', 'address': 'Oak Lane 1235'}] library = { 'name': 'John Smith Library', 'subscribers': subscribers } expected = sorted(build_object_graph(subscribers, resource=Subscriber, full_clean=False), key=lambda s: s.name) actual = sorted(build_object_graph(library, resource=Library, full_clean=False).subscribers, key=lambda s: s.name) self.assertEqual(actual, expected)
def test_build_nested_objects_with_polymorphism(self): books = [{'title': "Book1", "isbn": "abc-123", 'num_pages': 1, 'rrp': 20.4, 'fiction': True, 'genre': 'sci-fi', 'published': [], 'authors': [], 'publisher': None, '$': 'tests.resources.Book'}, {'title': "Book2", "isbn": "def-456", 'num_pages': 1, 'rrp': 20.4, 'fiction': True, 'genre': 'sci-fi', 'published': [], 'authors': [], 'publisher': None, '$': 'tests.resources.Book'}] library = { 'name': 'John Smith Library', 'books': books } expected = sorted(build_object_graph(books, full_clean=False), key=lambda s: s.title) actual = sorted(build_object_graph(library, resource=Library, full_clean=False).books, key=lambda s: s.title) self.assertEqual(actual, expected)
def test_build_object_graph_from_list(self): books = build_object_graph([dict( title="Book1" ), dict( title="Book2" )], Book, full_clean=False) self.assertEqual([dict( title="Book1", isbn=None, num_pages=None, rrp=20.4, fiction=None, genre=None, published=None, authors=None, publisher=None ), dict( title="Book2", isbn=None, num_pages=None, rrp=20.4, fiction=None, genre=None, published=None, authors=None, publisher=None )], [book.to_dict() for book in books])
def load(fp, resource=None, full_clean=True, default_to_not_supplied=False): """ Load a from a YAML encoded file. If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. I one is not supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree. :param fp: a file pointer to read YAML data from. :param resource: A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied. :param full_clean: Do a full clean of the object as part of the loading process. :param default_to_not_supplied: Used for loading partial resources. Any fields not supplied are replaced with NOT_SUPPLIED. :returns: A resource object or object graph of resources loaded from file. """ # try: return resources.build_object_graph( # The SafeLoader is used here, this is to allow for CSafeLoader to be used. yaml.load(fp, SafeLoader), # nosec - B506:yaml_load resource, full_clean, False, default_to_not_supplied)
def inner(*args, **kwargs): data = func(*args, **kwargs) if codec: opts = codec_opts or {} opts.setdefault('full_clean', full_clean) return codec.loads(data, resource, **opts) else: return build_object_graph(data, resource, full_clean)
def test_build_object_graph_empty_dict_no_clean(self): book = build_object_graph({}, Book, full_clean=False) assert dict(title=None, isbn=None, num_pages=None, rrp=20.4, fiction=None, genre=None, published=None, authors=None, publisher=None) == book.to_dict()
def load(fp, resource=None, encoding='UTF8', full_clean=True): """ Load a from a MessagePack encoded file. See :py:meth:`loads` for more details of the loading operation. :param fp: a file pointer to read MessagePack data from. :param resource: A resource instance or a resource name to use as the base for creating a resource. :param full_clean: Do a full clean of the object as part of the loading process. :returns: A resource object or object graph of resources loaded from file. """ return resources.build_object_graph(msgpack.load(fp, encoding=encoding), resource, full_clean)
def test_build_object_graph_empty_dict_no_clean(self): book = build_object_graph({}, Book, full_clean=False) self.assertEqual(dict( title=None, num_pages=None, rrp=20.4, fiction=None, genre=None, published=None, authors=None, publisher=None ), book.to_dict())
def load(fp, resource=None, encoding='UTF8', full_clean=True, default_to_not_supplied=False): """ Load a from a MessagePack encoded file. See :py:meth:`loads` for more details of the loading operation. :param fp: a file pointer to read MessagePack data from. :param resource: A resource instance or a resource name to use as the base for creating a resource. :param encoding: Encoding to use when loading file :param full_clean: Do a full clean of the object as part of the loading process. :returns: A resource object or object graph of resources loaded from file. """ return resources.build_object_graph(msgpack.load(fp, encoding=encoding), resource, full_clean, default_to_not_supplied)
def loads(s, resource=None, encoding='UTF8', full_clean=True): """ Load from a MessagePack encoded string/bytes. If a ``resource`` value is supplied it is used as the base resource for the supplied MessagePack data. I one is not supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree. :param s: String to load and parse. :param resource: A resource instance or a resource name to use as the base for creating a resource. :param full_clean: Do a full clean of the object as part of the loading process. :returns: A resource object or object graph of resources parsed from supplied string. """ return resources.build_object_graph(msgpack.loads(s, encoding=encoding), resource, full_clean, copy_dict=False)
def loads(s, resource=None, encoding='UTF8', full_clean=True, default_to_not_supplied=False): """ Load from a MessagePack encoded string/bytes. If a ``resource`` value is supplied it is used as the base resource for the supplied MessagePack data. I one is not supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree. :param s: String to load and parse. :param resource: A resource instance or a resource name to use as the base for creating a resource. :param full_clean: Do a full clean of the object as part of the loading process. :param encoding: Encoding to use when loading file :returns: A resource object or object graph of resources parsed from supplied string. """ return resources.build_object_graph(msgpack.loads(s, encoding=encoding), resource, full_clean, False, default_to_not_supplied)
def test_create_resource_from_dict_with_default_to_not_supplied(self): book = build_object_graph({ "title": "Foo", "num_pages": 42 }, Book, full_clean=False, default_to_not_supplied=True) self.assertEqual(dict( title="Foo", isbn=NOT_PROVIDED, num_pages=42, rrp=NOT_PROVIDED, fiction=NOT_PROVIDED, genre=NOT_PROVIDED, published=NOT_PROVIDED, authors=NOT_PROVIDED, publisher=NOT_PROVIDED ), book.to_dict())
def loads(s, resource=None): """ Load from a JSON encoded string. If a ``resource`` value is supplied it is used as the base resource for the supplied JSON. I one is not supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree. :param s: String to load and parse. :param resource: A resource instance or a resource name to use as the base for creating a resource. :returns: A resource object or object graph of resources parsed from supplied string. """ if isinstance(resource, type) and issubclass(resource, resources.Resource): resource_name = resource._meta.resource_name else: resource_name = resource return resources.build_object_graph(json.loads(s), resource_name)
def loads(s, resource=None, full_clean=True): """ Load from a JSON encoded string. If a ``resource`` value is supplied it is used as the base resource for the supplied JSON. I one is not supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree. :param s: String to load and parse. :param resource: A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied. :param full_clean: Do a full clean of the object as part of the loading process. :returns: A resource object or object graph of resources parsed from supplied string. """ try: return resources.build_object_graph(json.loads(s), resource, full_clean, copy_dict=False) except (ValueError, TypeError) as ex: raise CodecDecodeError(str(ex))
def load(fp, resource=None, full_clean=True, default_to_not_supplied=False): """ Load a from a YAML encoded file. If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. I one is not supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree. :param fp: a file pointer to read YAML data from. :param resource: A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied. :param full_clean: Do a full clean of the object as part of the loading process. :param default_to_not_supplied: Used for loading partial resources. Any fields not supplied are replaced with NOT_SUPPLIED. :returns: A resource object or object graph of resources loaded from file. """ # try: return resources.build_object_graph(yaml.load(fp, SafeLoader), resource, full_clean, False, default_to_not_supplied)
def loads(s, resource=None, full_clean=True, default_to_not_supplied=False): """ Load from a JSON encoded string. If a ``resource`` value is supplied it is used as the base resource for the supplied JSON. I one is not supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree. :param s: String to load and parse. :param resource: A resource type, resource name or list of resources and names to use as the base for creating a resource. If a list is supplied the first item will be used if a resource type is not supplied. :param full_clean: Do a full clean of the object as part of the loading process. :param default_to_not_supplied: Used for loading partial resources. Any fields not supplied are replaced with NOT_SUPPLIED. :returns: A resource object or object graph of resources parsed from supplied string. """ try: return resources.build_object_graph(json.loads(s), resource, full_clean, False, default_to_not_supplied) except (ValueError, TypeError) as ex: raise CodecDecodeError(str(ex))