Skip to content

Commit 3fb433e

Browse files
authored
Merge pull request #170 from OpenDataServices/cove-838-float-int
[OpenDataServices/cove#838] Convert integer float to int
2 parents f712220 + 8cd2f74 commit 3fb433e

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

flattentool/input.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def convert_type(type_string, value, timezone = pytz.timezone('UTC')):
9494
elif type_string == '':
9595
if type(value) == datetime.datetime:
9696
return timezone.localize(value).isoformat()
97+
if type(value) == float and int(value) == value:
98+
return int(value)
9799
return value if type(value) in [int] else text_type(value)
98100
else:
99101
raise ValueError('Unrecognised type: "{}"'.format(type_string))
3.58 KB
Binary file not shown.

flattentool/tests/test_input_SpreadsheetInput.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,25 @@ def test_xlsx_input_integer(self):
9898

9999
assert list(xlsxinput.get_sheet_lines('main')) == \
100100
[{'colA': 1}]
101+
if sys.version_info[0] == 2:
102+
assert type(list(xlsxinput.get_sheet_lines('main'))[0]['colA']) == long
103+
else:
104+
assert type(list(xlsxinput.get_sheet_lines('main'))[0]['colA']) == int
101105
assert xlsxinput.sub_sheet_names == ['main']
102106

107+
def test_xlsx_input_integer2(self):
108+
xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/integer2.xlsx')
109+
110+
xlsxinput.read_sheets()
111+
112+
assert list(xlsxinput.get_sheet_lines('Sheet1')) == \
113+
[{'activity-status/@code': 2}]
114+
# This is a float, but is converted to an int in the unflatten step, see
115+
# test_input_SpreadsheetInput_unflatten.py
116+
# 'Basic with float'
117+
assert type(list(xlsxinput.get_sheet_lines('Sheet1'))[0]['activity-status/@code']) == float
118+
assert xlsxinput.sub_sheet_names == ['Sheet1']
119+
103120
def test_xlsx_input_formula(self):
104121
""" When a forumla is present, we should use the value, rather than the
105122
formula itself. """

flattentool/tests/test_input_SpreadsheetInput_unflatten.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ def inject_root_id(root_id, d):
6161
[],
6262
True
6363
),
64+
(
65+
'Basic with float',
66+
# 3.0 is converted to 3
67+
# This is needed to handle google docs xlsx properly
68+
# https://github.com/OpenDataServices/cove/issues/838
69+
[{
70+
'ROOT_ID': '1',
71+
'id': 2,
72+
'testA': 3.0
73+
}],
74+
[{
75+
'ROOT_ID': '1',
76+
'id': 2,
77+
'testA': 3
78+
}],
79+
[],
80+
True
81+
),
6482
(
6583
'Basic with zero',
6684
[{

0 commit comments

Comments
 (0)