def _assert_invalid(processor, value, xml_string): """Assert the processor rejects the XML and value as invalid.""" with pytest.raises(_ValidationError): xml.parse_from_string(processor, xml_string) with pytest.raises(_ValidationError): xml.serialize_to_string(processor, value)
def test_parse_dictionary_missing(): """Parse a missing dictionary""" xml_string = """ <person> <name>John Doe</name> <demographics> <age>25</age> <gender>male</gender> </demographics> </person> """ demographics = xml.dictionary('demographics', [ xml.integer('age'), xml.string('gender'), ]) address = xml.dictionary('address', [ xml.string('street'), xml.integer('zip'), xml.string('state'), ]) person = xml.dictionary('person', [ xml.string('name'), demographics, address, ]) with pytest.raises(xml.MissingValue): xml.parse_from_string(person, xml_string)
def test_processor_locations_parsing(): """Get processor location in hooks callback.""" expected_locations = [ xml.ProcessorLocation(element_path='data', array_index=None), xml.ProcessorLocation(element_path='value', array_index=None) ] def trace(state, _): assert isinstance(state, xml.ProcessorStateView) assert expected_locations == list(state.locations) hooks = xml.Hooks( after_parse=trace, before_serialize=trace, ) processor = xml.dictionary('data', [ xml.integer('value', hooks=hooks), ]) xml_string = strip_xml(""" <data> <value>1</value> </data> """) value = {'value': 1} xml.parse_from_string(processor, xml_string) xml.serialize_to_string(processor, value)
def test_parse_primitive_root_parser(): """Parse with a primitive-valued root element""" xml_string = """ <root>15</root> """ processor = xml.integer('root') with pytest.raises(xml.InvalidRootProcessor): xml.parse_from_string(processor, xml_string)
def test_parse_primitive_missing(): """Parses a missing primitive value""" xml_string = """ <root> <wrong-value>15</wrong-value> </root> """ processor = xml.dictionary('root', [xml.integer('value')]) with pytest.raises(xml.MissingValue): xml.parse_from_string(processor, xml_string)
def test_parse_array_root_non_nested(): """Parse a non-nested array as the root processor""" xml_string = """ <root> <value>1</value> <value>2</value> </root> """ processor = xml.array(xml.integer('value')) with pytest.raises(xml.InvalidRootProcessor): xml.parse_from_string(processor, xml_string)
def test_parse_array_root_missing(): """Parse an array as the root processor""" xml_string = """ <wrong-array> <value>1</value> <value>2</value> </wrong-array> """ processor = xml.array(xml.integer('value'), nested='array') with pytest.raises(xml.MissingValue): xml.parse_from_string(processor, xml_string)
def _assert_error_message(processor, value, xml_string, expected_location): with pytest.raises(_ValidationError) as parse_exception: xml.parse_from_string(processor, xml_string) actual_parse_message = str(parse_exception.value) print(actual_parse_message) assert actual_parse_message.endswith(expected_location) with pytest.raises(_ValidationError) as serialize_exception: xml.serialize_to_string(processor, value) actual_serialize_message = str(serialize_exception.value) assert actual_serialize_message.endswith(expected_location)
def test_parse_attribute_missing_element(): """Parses an attribute value with a missing element""" xml_string = """ <root> <wrong-element value="27">Hello</wrong-element> </root> """ processor = xml.dictionary('root', [xml.integer('element', attribute='value')]) with pytest.raises(xml.MissingValue): xml.parse_from_string(processor, xml_string)
def test_parse_int_invalid(): """Parse an invalid int value""" xml_string = """ <root> <value>hello</value> </root> """ processor = xml.dictionary('root', [ xml.integer('value'), ]) with pytest.raises(xml.InvalidPrimitiveValue): xml.parse_from_string(processor, xml_string)
def test_parse_dictionary_root_missing(): """Parse a dictionary as root""" xml_string = """ <wrong-root> <value>hello</value> </wrong-root> """ processor = xml.dictionary('root', [ xml.string('value'), ]) with pytest.raises(xml.MissingValue): xml.parse_from_string(processor, xml_string)
def _collection_to_games(self, data): def after_status_hook(_, status): return [tag for tag, value in status.items() if value == "1"] game_in_collection_processor = xml.dictionary( "items", [ xml.array( xml.dictionary('item', [ xml.integer(".", attribute="objectid", alias="id"), xml.string("name"), xml.string("thumbnail", required=False, alias="image"), xml.string("version/item/thumbnail", required=False, alias="image_version"), xml.dictionary( "status", [ xml.string(".", attribute="fortrade"), xml.string(".", attribute="own"), xml.string(".", attribute="preordered"), xml.string(".", attribute="prevowned"), xml.string(".", attribute="want"), xml.string(".", attribute="wanttobuy"), xml.string(".", attribute="wanttoplay"), xml.string(".", attribute="wishlist"), ], alias='tags', hooks=xml.Hooks(after_parse=after_status_hook)), xml.integer("numplays"), ], required=False, alias="items"), ) ]) collection = xml.parse_from_string(game_in_collection_processor, data) collection = collection["items"] return collection
def _plays_to_games(self, data): def after_players_hook(_, status): return status["name"] if "name" in status else "Unknown" plays_processor = xml.dictionary("plays", [ xml.array( xml.dictionary('play', [ xml.integer(".", attribute="id", alias="playid"), xml.dictionary('item', [ xml.string(".", attribute="name", alias="gamename"), xml.integer(".", attribute="objectid", alias="gameid") ], alias='game'), xml.array( xml.dictionary( 'players/player', [ xml.string(".", attribute="name", required=False, default="Unknown") ], required=False, alias='players', hooks=xml.Hooks(after_parse=after_players_hook))) ], required=False, alias="plays")) ]) plays = xml.parse_from_string(plays_processor, data) plays = plays["plays"] return plays
def test_named_tuple_parse(): """Parse a namedtuple value""" xml_string = """ <author> <name>Robert A. Heinlein</name> <book> <title>Starship Troopers</title> <year-published>1959</year-published> </book> <book> <title>Stranger in a Strange Land</title> <year-published>1961</year-published> </book> </author> """ processor = xml.named_tuple('author', Author, [ xml.string('name'), xml.array(xml.named_tuple('book', Book, [ xml.string('title'), xml.integer('year-published', alias='year_published') ]), alias='books') ]) expected = Author(name='Robert A. Heinlein', books=[ Book(title='Starship Troopers', year_published=1959), Book(title='Stranger in a Strange Land', year_published=1961) ]) actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_user_object_parse_nested(): """Parse a user object as a nested element in the document""" xml_string = """ <root> <position>quarterback</position> <person> <name>Bob</name> <pet>Fluffy</pet> <pet>Spots</pet> </person> </root> """ processor = xml.dictionary('root', [ xml.string('position'), xml.user_object('person', Person, [ xml.string('name'), xml.array(xml.string('pet'), alias='pets'), ]) ]) expected = { 'position': 'quarterback', 'person': Person().set_values(name='Bob', pets=['Fluffy', 'Spots']) } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_user_object_parse_aliased(): """Parse an aliased user object""" xml_string = """ <root> <book>Harry Potter</book> <person> <name>Ron</name> <age>18</age> </person> </root> """ processor = xml.dictionary('root', [ xml.string('book'), xml.user_object('person', Person, [xml.string('name'), xml.integer('age')], alias='character') ]) expected = { 'book': 'Harry Potter', 'character': Person().set_values(name='Ron', age=18) } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def parse(self): result = xml.parse_from_string(self.data_processor, self.file_contents) for item in result['corpus'][0]['paraphrases']: item = item['values'] paraphraseObj = Paraphrase(item[0], item[1], item[2], item[3], item[4], item[6]) yield paraphraseObj
def test_parse_array_embedded_aliased(): """Parse array embedded within its parent element""" xml_string = """ <root> <message>Goodbye, World!</message> <value>765</value> <value>3456</value> </root> """ values_array = xml.array(xml.integer('value'), alias='numbers') processor = xml.dictionary('root', [ xml.string('message'), values_array, ]) expected = { 'message': 'Goodbye, World!', 'numbers': [765, 3456], } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_array_nested_empty_optional(): """Parse nested empty array""" xml_string = """ <root> <message>Hello, World!</message> <numbers /> </root> """ numbers_array = xml.array(xml.integer('number', required=False), nested='numbers') processor = xml.dictionary('root', [ xml.string('message'), numbers_array, ]) expected = { 'message': 'Hello, World!', 'numbers': [], } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_array_of_arrays(): """Parse array of arrays""" xml_string = """ <root-array> <values> <value>1</value> <value>17</value> <value>33</value> </values> <values> <value>99</value> </values> </root-array> """ values_array = xml.array(xml.integer('value'), nested='values') root_processor = xml.array(values_array, nested='root-array') expected = [ [1, 17, 33], [99], ] actual = xml.parse_from_string(root_processor, xml_string) assert expected == actual
def test_primitive_default_present(): """Parses primitive values with defaults specified""" xml_string = """ <root> <boolean>false</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', required=False, default=True), xml.floating_point('float', required=False, default=0.0), xml.integer('int', required=False, default=0), xml.string('string', required=False, default=''), ]) expected = { 'boolean': False, 'float': 3.14, 'int': 1, 'string': 'Hello, World', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def get(self): med_processor = xml.dictionary('response', [ xml.dictionary('header', [ xml.string('resultCode'), xml.string('resultMsg') ]), xml.dictionary('body', [ xml.integer('numOfRows'), xml.integer('pageNo'), xml.integer('totalCount'), xml.dictionary('items', [ xml.array(xml.dictionary('item', [ xml.string('INGR_ENG_NAME'), xml.string('INGR_KOR_NAME'), xml.string('ITEM_NAME_ENG'), xml.string('ITEM_NAME_KOR'), xml.string('SELLING_CORP'), xml.string('DOSAGE_FORM'), xml.string('STRENGTH'), xml.string('GROUPING_NO'), xml.string('PMS_EXP_DATE'), xml.string('KOR_SUIT_YN') ])) ]) ]) ]) med_api = requests.get('http://apis.data.go.kr/1470000/' 'MdcinPatentInfoService/' 'getMdcinPatentInfoList?' 'serviceKey=j1p%2FEIuaPbMKsRuWzOMygNZKwyo2LYZzAWWBwZwxFLc%2BTzuRHN8ROyeJYje%2FPEvs7Hsp6OCVK1fQFt5UcaTocA%3D%3D&' 'pageNo=1&startPage=1&numOfRows=100&pageSize=100') med_xml = med_api.text med_dict = xml.parse_from_string(med_processor, med_xml) items = med_dict['body']['items']['item'] return render_template(self.template, items=items)
def test_parse_array_optional_present(): """Parse optional array that is present""" xml_string = """ <root> <message>Hello, World!</message> <value>45</value> <value>908</value> </root> """ values_array = xml.array(xml.integer('value', required=False), alias='numbers') processor = xml.dictionary('root', [ xml.string('message'), values_array, ]) expected = { 'message': 'Hello, World!', 'numbers': [45, 908], } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_array_embedded(): """Parse array embedded within its parent element""" xml_string = """ <root> <message>Hello, World!</message> <value>21</value> <value>17</value> <value>90</value> <value>6</value> </root> """ values_array = xml.array(xml.integer('value')) processor = xml.dictionary('root', [ xml.string('message'), values_array, ]) expected = { 'message': 'Hello, World!', 'value': [21, 17, 90, 6], } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_array_nested(): """Parse nested array""" xml_string = """ <root> <message>Hello, World!</message> <numbers> <number>1</number> <number>2</number> </numbers> </root> """ numbers_array = xml.array(xml.integer('number'), nested='numbers') processor = xml.dictionary('root', [ xml.string('message'), numbers_array, ]) expected = { 'message': 'Hello, World!', 'numbers': [1, 2], } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def _transform_test_case_run(processor, value, xml_string): """Runs the given value transform test case""" actual_value = xml.parse_from_string(processor, xml_string) assert value == actual_value actual_xml_string = xml.serialize_to_string(processor, value) assert xml_string == actual_xml_string
def test_parse_primitive_aliased(): """Parses primitive values with aliases""" xml_string = """ <root> <boolean>true</boolean> <float>3.14</float> <int>1</int> <string>Hello, World</string> </root> """ processor = xml.dictionary('root', [ xml.boolean('boolean', alias='b'), xml.floating_point('float', alias='f'), xml.integer('int', alias='i'), xml.string('string', alias='s'), ]) expected = { 'b': True, 'f': 3.14, 'i': 1, 's': 'Hello, World', } actual = xml.parse_from_string(processor, xml_string) assert expected == actual
def test_parse_dictionary_aliased(): """Parses a dictionary value that is aliased""" xml_string = """ <person> <name>John Doe</name> <demographics> <age>25</age> <gender>male</gender> </demographics> </person> """ stats = xml.dictionary('demographics', [ xml.integer('age'), xml.string('gender'), ], alias='stats') person = xml.dictionary('person', [ xml.string('name'), stats, ]) expected = { 'name': 'John Doe', 'stats': { 'age': 25, 'gender': 'male', }, } actual = xml.parse_from_string(person, xml_string) assert expected == actual
def _assert_valid(processor, value, xml_string): """Assert the processor accepts the XML and value as valid.""" actual_value = xml.parse_from_string(processor, xml_string) assert value == actual_value actual_xml_string = xml.serialize_to_string(processor, value) assert xml_string == actual_xml_string
def test_parse_missing_non_required_namedtuple_issue_24(): """Parse XML with a non-required namedtuple value.""" Author = namedtuple('Author', [ 'name', 'genre', ]) Genre = namedtuple('Genre', [ 'name', ]) processor = xml.named_tuple('author', Author, [ xml.string('name'), xml.named_tuple('genre', Genre, [ xml.string('name') ], required=False) ]) author_xml = """ <author> <name>Robert A. Heinlein</name> </author> """ expected_value = Author(name='Robert A. Heinlein', genre=None) actual_value = xml.parse_from_string(processor, author_xml) assert expected_value == actual_value