|
| 1 | +from flattentool.json_input import JSONParser |
| 2 | + |
| 3 | +def test_xml_empty(): |
| 4 | + parser = JSONParser( |
| 5 | + json_filename='flattentool/tests/fixtures/empty.xml', |
| 6 | + root_list_path='iati-activity', |
| 7 | + schema_parser=None, |
| 8 | + root_id='', |
| 9 | + xml=True, |
| 10 | + id_name='iati-identifier') |
| 11 | + parser.parse() |
| 12 | + assert list(parser.main_sheet) == [] |
| 13 | + assert parser.main_sheet.lines == [] |
| 14 | + assert parser.sub_sheets == {} |
| 15 | + |
| 16 | + |
| 17 | +def test_xml_basic_example(): |
| 18 | + parser = JSONParser( |
| 19 | + json_filename='examples/iati/expected.xml', |
| 20 | + root_list_path='iati-activity', |
| 21 | + schema_parser=None, |
| 22 | + root_id='', |
| 23 | + xml=True, |
| 24 | + id_name='iati-identifier') |
| 25 | + parser.parse() |
| 26 | + assert list(parser.main_sheet) == ['iati-identifier', 'reporting-org/@ref', 'reporting-org/@type', 'reporting-org/narrative', 'participating-org/@ref', 'participating-org/@role', 'activity-status/@code', 'activity-date/@iso-date', 'activity-date/@type', 'title/narrative', 'description/narrative'] |
| 27 | + assert parser.main_sheet.lines == [ |
| 28 | + {'activity-date/@type': '1', 'reporting-org/narrative': 'Organisation name', 'participating-org/@ref': 'AA-AAA-123456789', 'title/narrative': 'A title', 'participating-org/@role': '1', 'reporting-org/@ref': 'AA-AAA-123456789', 'iati-identifier': 'AA-AAA-123456789-ABC123', 'reporting-org/@type': '40', 'description/narrative': 'A description', 'activity-date/@iso-date': '2011-10-01', 'activity-status/@code': '2'}, |
| 29 | + {'activity-date/@type': '2', 'reporting-org/narrative': 'Organisation name', 'participating-org/@ref': 'AA-AAA-123456789', 'title/narrative': 'Another title', 'participating-org/@role': '1', 'reporting-org/@ref': 'AA-AAA-123456789', 'iati-identifier': 'AA-AAA-123456789-ABC124', 'reporting-org/@type': '40', 'description/narrative': 'Another description', 'activity-date/@iso-date': '2016-01-01', 'activity-status/@code': '3'} |
| 30 | + ] |
| 31 | + assert set(parser.sub_sheets.keys()) == set(['transaction', 'recipient-country']) |
| 32 | + assert list(parser.sub_sheets['transaction']) == ['iati-identifier', 'transaction/0/transaction-type/@code', 'transaction/0/transaction-date/@iso-date', 'transaction/0/value/@value-date', 'transaction/0/value'] |
| 33 | + assert parser.sub_sheets['transaction'].lines == [ |
| 34 | + {'transaction/0/value/@value-date': '2012-01-01', 'iati-identifier': 'AA-AAA-123456789-ABC123', 'transaction/0/transaction-date/@iso-date': '2012-01-01', 'transaction/0/value': '10', 'transaction/0/transaction-type/@code': '2'}, |
| 35 | + {'transaction/0/value/@value-date': '2012-03-03', 'iati-identifier': 'AA-AAA-123456789-ABC123', 'transaction/0/transaction-date/@iso-date': '2012-03-03', 'transaction/0/value': '20', 'transaction/0/transaction-type/@code': '3'}, |
| 36 | + {'transaction/0/value/@value-date': '2013-04-04', 'iati-identifier': 'AA-AAA-123456789-ABC124', 'transaction/0/transaction-date/@iso-date': '2013-04-04', 'transaction/0/value': '30', 'transaction/0/transaction-type/@code': '2'}, |
| 37 | + {'transaction/0/value/@value-date': '2013-05-05', 'iati-identifier': 'AA-AAA-123456789-ABC124', 'transaction/0/transaction-date/@iso-date': '2013-05-05', 'transaction/0/value': '40', 'transaction/0/transaction-type/@code': '3'} |
| 38 | + ] |
| 39 | + assert list(parser.sub_sheets['recipient-country']) == ['iati-identifier', 'recipient-country/0/@code', 'recipient-country/0/@percentage'] |
| 40 | + assert parser.sub_sheets['recipient-country'].lines == [ |
| 41 | + {'iati-identifier': 'AA-AAA-123456789-ABC123', 'recipient-country/0/@code': 'AF', 'recipient-country/0/@percentage': '40'}, |
| 42 | + {'iati-identifier': 'AA-AAA-123456789-ABC123', 'recipient-country/0/@code': 'XK', 'recipient-country/0/@percentage': '60'}, |
| 43 | + {'iati-identifier': 'AA-AAA-123456789-ABC124', 'recipient-country/0/@code': 'AG', 'recipient-country/0/@percentage': '30'}, |
| 44 | + {'iati-identifier': 'AA-AAA-123456789-ABC124', 'recipient-country/0/@code': 'XK', 'recipient-country/0/@percentage': '70'} |
| 45 | + ] |
0 commit comments