Skip to content

Commit e0ecec3

Browse files
committed
[#57] Update the tests to work with newest openpyxl
There's a couple of oddities here, but I think it's important to be using this latest version to fix OpenDataServices/cove#176 I've flagged the potential problems as new GitHub issues: * Python 2 ints not roundtripping properly #79 * Sheets with just one cell are not handled properly #78
1 parent 8d8275b commit e0ecec3

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

flattentool/tests/test_init.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from flattentool import decimal_default, unflatten
44
from decimal import Decimal
55
import json
6+
import sys
67

78

89
def test_decimal_default():
@@ -202,4 +203,4 @@ def test_unflatten_xslx_unicode(tmpdir):
202203
output_name=tmpdir.join('release.json').strpath,
203204
main_sheet_name='main')
204205
reloaded_json = json.load(tmpdir.join('release.json'))
205-
assert reloaded_json == {'main': [{'ocid': 1, 'id': 'éαГ😼𝒞人'}]}
206+
assert reloaded_json == {'main': [{'ocid': 1 if sys.version > '3' else '1', 'id': 'éαГ😼𝒞人'}]}

flattentool/tests/test_input.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ def test_csv_no_files(self, tmpdir):
9494

9595
def test_xlsx_no_file(self, tmpdir):
9696
xlsxinput = XLSXInput(input_name=tmpdir.strpath.join('test.xlsx'), main_sheet_name='main')
97-
with pytest.raises(openpyxl.exceptions.InvalidFileException):
98-
xlsxinput.read_sheets()
97+
if sys.version > '3':
98+
with pytest.raises(FileNotFoundError):
99+
xlsxinput.read_sheets()
100+
else:
101+
with pytest.raises(IOError):
102+
xlsxinput.read_sheets()
99103

100104
def test_xlsx_no_main_sheet(self):
101105
xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/basic.xlsx', main_sheet_name='notmain')

flattentool/tests/test_output.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def test_blank_sheets(tmpdir):
3434
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
3535
assert wb.get_sheet_names() == ['release']
3636
assert len(wb['release'].rows) == 1
37-
assert len(wb['release'].rows[0]) == 1
38-
assert wb['release'].rows[0][0].value == None
37+
assert len(wb['release'].rows[0]) == 0
3938

4039
# Check CSV is Empty
4140
assert tmpdir.join('release').listdir() == [ tmpdir.join('release').join('release.csv') ]
@@ -47,7 +46,7 @@ def test_populated_header(tmpdir):
4746
subsheet = Sheet(root_id='ocid')
4847
subsheet.add_field('c')
4948
spreadsheet_output = spreadsheet_output_class(
50-
parser=MockParser(['a'], {'b': subsheet}),
49+
parser=MockParser(['a', 'd'], {'b': subsheet}),
5150
main_sheet_name='release',
5251
output_name=os.path.join(tmpdir.strpath, 'release'+output.FORMATS_SUFFIX[format_name]))
5352
spreadsheet_output.write_sheets()
@@ -56,7 +55,7 @@ def test_populated_header(tmpdir):
5655
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
5756
assert wb.get_sheet_names() == ['release', 'b']
5857
assert len(wb['release'].rows) == 1
59-
assert [ x.value for x in wb['release'].rows[0] ] == [ 'a' ]
58+
assert [ x.value for x in wb['release'].rows[0] ] == [ 'a', 'd' ]
6059
assert len(wb['b'].rows) == 1
6160
assert [ x.value for x in wb['b'].rows[0] ] == [ 'ocid', 'c' ]
6261

@@ -65,14 +64,14 @@ def test_populated_header(tmpdir):
6564
tmpdir.join('release').join('release.csv'),
6665
tmpdir.join('release').join('b.csv')
6766
])
68-
assert tmpdir.join('release', 'release.csv').read().strip('\r\n') == 'a'
67+
assert tmpdir.join('release', 'release.csv').read().strip('\r\n') == 'a,d'
6968
assert tmpdir.join('release', 'b.csv').read().strip('\r\n') == 'ocid,c'
7069

7170

7271
def test_empty_lines(tmpdir):
7372
subsheet = Sheet(root_id='ocid')
7473
subsheet.add_field('c')
75-
parser = MockParser(['a'], {'b': subsheet})
74+
parser = MockParser(['a', 'd'], {'b': subsheet})
7675
parser.main_sheet.lines = []
7776
for format_name, spreadsheet_output_class in output.FORMATS.items():
7877
spreadsheet_output = spreadsheet_output_class(
@@ -85,7 +84,7 @@ def test_empty_lines(tmpdir):
8584
wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
8685
assert wb.get_sheet_names() == ['release', 'b']
8786
assert len(wb['release'].rows) == 1
88-
assert [ x.value for x in wb['release'].rows[0] ] == [ 'a' ]
87+
assert [ x.value for x in wb['release'].rows[0] ] == [ 'a', 'd' ]
8988
assert len(wb['b'].rows) == 1
9089
assert [ x.value for x in wb['b'].rows[0] ] == [ 'ocid', 'c' ]
9190

@@ -94,7 +93,7 @@ def test_empty_lines(tmpdir):
9493
tmpdir.join('release').join('release.csv'),
9594
tmpdir.join('release').join('b.csv')
9695
])
97-
assert tmpdir.join('release', 'release.csv').read().strip('\r\n') == 'a'
96+
assert tmpdir.join('release', 'release.csv').read().strip('\r\n') == 'a,d'
9897
assert tmpdir.join('release', 'b.csv').read().strip('\r\n') == 'ocid,c'
9998

10099

flattentool/tests/test_roundtrip.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from flattentool import unflatten, flatten
22
import json
33
import pytest
4+
import sys
45

56

67
@pytest.mark.parametrize('output_format', ['xlsx', 'csv'])
@@ -9,12 +10,12 @@ def test_roundtrip(tmpdir, output_format):
910
base_name = 'flattentool/tests/fixtures/tenders_releases_base.json'
1011
flatten(
1112
input_name=input_name,
12-
output_name=tmpdir.join('flattened').strpath,
13+
output_name=tmpdir.join('flattened').strpath+'.'+output_format,
1314
output_format=output_format,
1415
schema='flattentool/tests/fixtures/release-schema.json',
1516
main_sheet_name='releases')
1617
unflatten(
17-
input_name=tmpdir.join('flattened').strpath,
18+
input_name=tmpdir.join('flattened').strpath+'.'+output_format,
1819
output_name=tmpdir.join('roundtrip.json').strpath,
1920
input_format=output_format,
2021
base_json=base_name,
@@ -37,15 +38,15 @@ def test_roundtrip_360(tmpdir, output_format, use_titles):
3738
input_name = 'flattentool/tests/fixtures/WellcomeTrust-grants_fixed_2_grants.json'
3839
flatten(
3940
input_name=input_name,
40-
output_name=tmpdir.join('flattened').strpath,
41+
output_name=tmpdir.join('flattened').strpath+'.'+output_format,
4142
output_format=output_format,
4243
schema='flattentool/tests/fixtures/360-giving-schema.json',
4344
main_sheet_name='grants',
4445
root_list_path='grants',
4546
root_id='',
4647
use_titles=use_titles)
4748
unflatten(
48-
input_name=tmpdir.join('flattened').strpath,
49+
input_name=tmpdir.join('flattened').strpath+'.'+output_format,
4950
output_name=tmpdir.join('roundtrip.json').strpath,
5051
input_format=output_format,
5152
schema='flattentool/tests/fixtures/360-giving-schema.json',
@@ -58,7 +59,8 @@ def test_roundtrip_360(tmpdir, output_format, use_titles):
5859

5960
# Currently not enough information to successfully roundtrip that values
6061
# are numbers, when this is not required by the schema
61-
if output_format == 'csv':
62+
# for CSV, and for openpyxl under Python 2
63+
if output_format == 'csv' or sys.version_info < (3, 0):
6264
for grant in original_json['grants']:
6365
grant['plannedDates'][0]['duration'] = str(grant['plannedDates'][0]['duration'])
6466

0 commit comments

Comments
 (0)