Skip to content

Commit a2e8cfa

Browse files
authored
Merge pull request #208 from OpenDataServices/cove-1030-xml-comma-array
Don't split text value of xml array
2 parents 8d3d32e + 59f371b commit a2e8cfa

File tree

7 files changed

+84
-6
lines changed

7 files changed

+84
-6
lines changed

examples/iati_multilang/cmd.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$ flatten-tool unflatten --xml --id-name iati-identifier --root-list-path iati-activity -f csv examples/iati_multilang
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<iati-activities>
2+
<!--XML generated by flatten-tool-->
3+
<iati-activity>
4+
<iati-identifier>AA-AAA-123456789-ABC123</iati-identifier>
5+
<reporting-org ref="AA-AAA-123456789" type="40">
6+
<narrative>Organisation name</narrative>
7+
</reporting-org>
8+
<participating-org ref="AA-AAA-123456789" role="1"/>
9+
<activity-status code="2"/>
10+
<activity-date iso-date="2011-10-01" type="1"/>
11+
<recipient-country code="AF" percentage="40"/>
12+
<recipient-country code="XK" percentage="60"/>
13+
<title>
14+
<narrative xml:lang="en">A title, with comma</narrative>
15+
<narrative xml:lang="fr">Un titre</narrative>
16+
</title>
17+
<description>
18+
<narrative>A description</narrative>
19+
</description>
20+
<transaction>
21+
<transaction-type code="2"/>
22+
<transaction-date iso-date="2012-01-01"/>
23+
<value value-date="2012-01-01">10</value>
24+
</transaction>
25+
<transaction>
26+
<transaction-type code="3"/>
27+
<transaction-date iso-date="2012-03-03"/>
28+
<value value-date="2012-03-03">20</value>
29+
</transaction>
30+
</iati-activity>
31+
<iati-activity>
32+
<iati-identifier>AA-AAA-123456789-ABC124</iati-identifier>
33+
<reporting-org ref="AA-AAA-123456789" type="40">
34+
<narrative>Organisation name</narrative>
35+
</reporting-org>
36+
<participating-org ref="AA-AAA-123456789" role="1"/>
37+
<activity-status code="3"/>
38+
<activity-date iso-date="2016-01-01" type="2"/>
39+
<recipient-country code="AG" percentage="30"/>
40+
<recipient-country code="XK" percentage="70"/>
41+
<title>
42+
<narrative xml:lang="en">Another title; with semicolon</narrative>
43+
<narrative xml:lang="fr">Un autre titre</narrative>
44+
</title>
45+
<description>
46+
<narrative>Another description</narrative>
47+
</description>
48+
<transaction>
49+
<transaction-type code="2"/>
50+
<transaction-date iso-date="2013-04-04"/>
51+
<value value-date="2013-04-04">30</value>
52+
</transaction>
53+
<transaction>
54+
<transaction-type code="3"/>
55+
<transaction-date iso-date="2013-05-05"/>
56+
<value value-date="2013-05-05">40</value>
57+
</transaction>
58+
</iati-activity>
59+
</iati-activities>

examples/iati_multilang/main.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
iati-identifier,reporting-org/@ref,reporting-org/@type,reporting-org/narrative,participating-org/@role,participating-org/@ref,activity-status/@code,activity-date/@type,activity-date/@iso-date,recipient-country/0/@code,recipient-country/0/@percentage,recipient-country/1/@code,recipient-country/1/@percentage,title/narrative/0/@xml:lang,title/narrative/0,title/narrative/1/@xml:lang,title/narrative/1,description/narrative
2+
AA-AAA-123456789-ABC123,AA-AAA-123456789,40,Organisation name,1,AA-AAA-123456789,2,1,2011-10-01,AF,40,XK,60,en,"A title, with comma",fr,Un titre,A description
3+
AA-AAA-123456789-ABC124,AA-AAA-123456789,40,Organisation name,1,AA-AAA-123456789,3,2,2016-01-01,AG,30,XK,70,en,Another title; with semicolon,fr,Un autre titre,Another description
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
iati-identifier,transaction/0/transaction-type/@code,transaction/0/transaction-date/@iso-date,transaction/0/value/@value-date,transaction/0/value
2+
AA-AAA-123456789-ABC123,2,2012-01-01,2012-01-01,10
3+
AA-AAA-123456789-ABC123,3,2012-03-03,2012-03-03,20
4+
AA-AAA-123456789-ABC124,2,2013-04-04,2013-04-04,30
5+
AA-AAA-123456789-ABC124,3,2013-05-05,2013-05-05,40

flattentool/input.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,14 @@ def unflatten_main_with_parser(parser, line, timezone, xml, id_name):
740740
continue
741741

742742
value = cell.cell_value
743-
converted_value = convert_type(current_type or '', value, timezone)
743+
if xml and current_type == 'array':
744+
# In xml "arrays" can have text values, if they're the final element
745+
# However the type of the text value itself should not be "array",
746+
# as that would split the text on commas, which we don't want.
747+
# https://github.com/OpenDataServices/cove/issues/1030
748+
converted_value = convert_type('', value, timezone)
749+
else:
750+
converted_value = convert_type(current_type or '', value, timezone)
744751
cell.cell_value = converted_value
745752
if converted_value is not None and converted_value != '':
746753
if xml:

flattentool/tests/test_docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def test_examples_in_docs():
9090
tests_passed += 1
9191
# Check that the number of tests were run that we expected
9292
if sys.version_info[:2] < (3,4):
93-
assert tests_passed == 35
94-
else:
9593
assert tests_passed == 36
94+
else:
95+
assert tests_passed == 37
9696

9797
def _simplify_warnings(lines):
9898
return '\n'.join([_simplify_line(line) for line in lines.split('\n')])

flattentool/tests/test_unflatten.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import os
12
import json
3+
import pytest
24

35
from flattentool import unflatten
46

@@ -57,15 +59,16 @@ def test_360_fields_case_insensitive(tmpdir):
5759
assert output_json_grants == output_json_space_case
5860

5961

60-
def test_unflatten_xml(tmpdir):
62+
@pytest.mark.parametrize('dirname', ['examples/iati', 'examples/iati_multilang'])
63+
def test_unflatten_xml(tmpdir, dirname):
6164
unflatten(
62-
input_name='examples/iati',
65+
input_name=dirname,
6366
output_name=tmpdir.join('output.xml').strpath,
6467
input_format='csv',
6568
root_list_path='iati-activity',
6669
id_name='iati-identifier',
6770
xml=True)
68-
assert open('examples/iati/expected.xml').read() == tmpdir.join('output.xml').read()
71+
assert open(os.path.join(dirname, 'expected.xml')).read() == tmpdir.join('output.xml').read()
6972

7073

7174
def test_unflatten_org_xml(tmpdir):

0 commit comments

Comments
 (0)