Skip to content

Commit

Permalink
ourresearch/unsub-private#61 - use formula check in _convert_blind me…
Browse files Browse the repository at this point in the history
…thod for file upload
  • Loading branch information
sckott committed May 2, 2022
1 parent 486b2f2 commit c5170f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def _convert_blind(workbook):
with open(csv_file_name, 'w', encoding='utf-8') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for row in sheet.iter_rows(min_row=1):
if 'f' in [w.data_type for w in row]:
raise RuntimeError('Uploaded files can not contain formulas')
writer.writerow([cell.value for cell in row])

csv_file_names.append(csv_file_name)
Expand Down
2 changes: 1 addition & 1 deletion package_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def load(self, package_id, file_name, commit=False):
self._copy_raw_to_s3(file_name, package_id, num_rows=None, error="error_reading_file")
return {"success": False, "message": "error_reading_file", "warnings": []}
except RuntimeError as e:
print("Runtime Error processing file {}".format(str(e)))
print("Runtime Error processing file: {}".format(str(e)))
self._copy_raw_to_s3(file_name, package_id, num_rows=None, error="parsing_error", error_details=str(e))
return {"success": False, "message": str(e), "warnings": []}

Expand Down
9 changes: 7 additions & 2 deletions tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ def test_convert_xls_to_xlsx():
assert '.xlsx' in x
assert os.path.isfile(x)

def test_convert_spreadsheet_to_csv():
# FIXME: parsed=True is never used in the codebase, so just testing False for now
def test_convert_spreadsheet_to_csv_parsed_good_file():
x = convert_spreadsheet_to_csv('tests/test_files/counter/counter4_jr1_2018_01.xlsx', parsed=False)
assert isinstance(x, list)
assert isinstance(x[0], str)
assert os.path.isfile(x[0])
# FIXME: The below should be True, but dates are messed up in the output csv
# assert filecmp.cmp(x[0], 'tests/test_files/counter/counter4_jr1_2018_01.csv')


def test_convert_spreadsheet_to_csv_parsed_with_formulas():
# not using parsed=True, but does check for formulas
with pytest.raises(RuntimeError) as err:
convert_spreadsheet_to_csv('tests/test_files/journal_price/with-formulas.xlsx', parsed=False)
assert "Uploaded files can not contain formulas" in str(err.value)

0 comments on commit c5170f4

Please sign in to comment.