def test_unionclass_object_type(): @A.union(style=A.union.Style.flat) class Person(abc.ABC): @abc.abstractmethod def say_hello(self) -> str: ... @A.union.subtype(Person) @dataclasses.dataclass class Student(Person): def say_hello(self) -> str: return 'Hi I study' @A.union.subtype(Person) @dataclasses.dataclass @A.typeinfo(name='teacher') class Teacher(Person): def say_hello(self) -> str: return 'Hi I teach' assert mapper().deserialize({ 'type': 'Student' }, Person).say_hello() == 'Hi I study' assert mapper().deserialize({ 'type': 'teacher' }, Person).say_hello() == 'Hi I teach'
def test_union_keyed(): @A.union(style=A.union.Style.keyed) class Person(abc.ABC): @abc.abstractmethod def say_hello(self) -> str: ... @A.union.subtype(Person) @dataclasses.dataclass class Student(Person): name: str def say_hello(self) -> str: return f'Hi I study and my name is {self.name}' @A.union.subtype(Person) @dataclasses.dataclass @A.typeinfo(name='teacher') class Teacher(Person): def say_hello(self) -> str: return 'Hi I teach' assert mapper().deserialize({ 'Student': { 'name': 'John' } }, Person).say_hello() == 'Hi I study and my name is John' assert mapper().deserialize({ 'teacher': {} }, Person).say_hello() == 'Hi I teach'
def test_file_content_api(): @dataclasses.dataclass class FileContentMarkdown: markdown: str @dataclasses.dataclass class FileContentMedia: media: bytes FileContent = te.Annotated[ t.Union[FileContentMarkdown, FileContentMedia, ], A.union({ 'markdown': FileContentMarkdown, 'media': FileContentMedia, }, style=A.union.Style.flat)] assert mapper().serialize(FileContentMarkdown('Hello'), FileContent) == { 'type': 'markdown', 'markdown': 'Hello' } assert mapper().deserialize({ 'type': 'markdown', 'markdown': 'Hello' }, FileContent) == FileContentMarkdown('Hello') assert mapper().serialize(FileContentMedia(b'Hello'), FileContent) == { 'type': 'media', 'media': 'SGVsbG8=' } assert mapper().deserialize({ 'type': 'media', 'media': 'SGVsbG8=' }, FileContent) == FileContentMedia(b'Hello')
def test_pathlib_converter(): u = uuid.uuid4() assert mapper().serialize(u, uuid.UUID) == str(u) assert mapper().deserialize(str(u), uuid.UUID) == u with pytest.raises(ConversionError) as excinfo: mapper().deserialize('foobar!', uuid.UUID) assert str(excinfo.value) == """badly formed hexadecimal UUID string
def test_dynamic_subtypes(): assert mapper().deserialize({'other': { 'type': 'b', 'b': {} }}, Acls) == Acls(Bcls()) assert mapper().deserialize({'other': { 'type': 'c', 'c': {} }}, Acls) == Acls(Ccls())
def get_license_metadata(license_name: str) -> DejaCodeLicense: """ Retrives the HTML metadata page for the specified license from the DejaCode website and extracts information such as the name, category, license type, standard notice and license text. """ url = BASE_URL.format(license_name.replace(' ', '-').lower()) response = requests.get(url) response.raise_for_status() html = response.text soup = bs4.BeautifulSoup(html, 'html.parser') # Get the keys that need to be extracted from the dataclass field aliases. from databind.core.types.schema import dataclass_to_schema from databind.json import mapper, load schema = dataclass_to_schema(DejaCodeLicense, mapper()) extract_keys = [ field.aliases[0] for field in schema.fields.values() if field.aliases ] data = {} for key in extract_keys: data[key] = _get_table_value_by_key(soup, key) data['Publication year'] = int(data['Publication year']) if data['Standard notice']: data['Standard notice'] = textwrap.dedent(data['Standard notice']) data['license_text'] = _get_license_text(soup) return load(data, DejaCodeLicense)
def test_unionclass_from_annotated(): MyUnion = te.Annotated[t.Union[int, str], A.union({ 'int': int, 'str': str }, name='MyUnion')] assert mapper().deserialize({'type': 'int', 'int': 42}, MyUnion) == 42 assert mapper().deserialize({ 'type': 'str', 'str': 'foobar' }, MyUnion) == 'foobar' assert mapper().serialize(42, MyUnion) == {'type': 'int', 'int': 42} assert mapper().serialize('foobar', MyUnion) == { 'type': 'str', 'str': 'foobar' }
def test_enum_converter(): assert mapper().serialize(Color.RED, Color) == 'RED' assert mapper().deserialize('RED', Color) == Color.RED assert mapper().serialize(Color.BLUE, Color) == 'ImBlue' assert mapper().deserialize('ImBlue', Color) == Color.BLUE assert mapper().deserialize('BLUE', Color) == Color.BLUE m = mapper() m.add_field_annotation(Color, 'GREEN', A.alias('greeeeen')) assert m.serialize(Color.RED, Color) == 'RED' assert m.serialize(Color.GREEN, Color) == 'greeeeen' assert m.deserialize('GREEN', Color) == Color.GREEN assert m.deserialize('greeeeen', Color) == Color.GREEN
def test_pathlib_converter(): assert mapper().serialize(pathlib.PosixPath('/bin/bash'), pathlib.Path) == '/bin/bash' assert mapper().deserialize( '/bin/bash', pathlib.PosixPath) == pathlib.PosixPath('/bin/bash')