Skip to content

Commit

Permalink
Warn on missing input columns
Browse files Browse the repository at this point in the history
  • Loading branch information
sbscully committed Aug 23, 2024
1 parent 8047401 commit dc76ee4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
20 changes: 15 additions & 5 deletions opencage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,21 @@ async def read_input(self, input, queue):
break

async def read_one_line(self, row, row_id):
if self.options.input_columns:
# input_columns option uses 1-based indexing
address = [row[i-1] for i in self.options.input_columns]
elif self.options.command == 'reverse':
address = [row[0], row[1]]
if self.options.command == 'reverse':
input_columns = [1, 2]
elif self.options.input_columns:
input_columns = self.options.input_columns
else:
input_columns = None

if input_columns:
address = []
try:
for column in input_columns:
# input_columns option uses 1-based indexing
address.append(row[column - 1])
except IndexError:
self.log(f"Missing input column {column} in {row}")
else:
address = row

Expand Down
15 changes: 14 additions & 1 deletion test/cli/test_cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@ def test_headers():
'address,lat,lng,_type,country,county,city,postcode,road,house_number,confidence,formatted',
'"Rathausmarkt 1,Hamburg,20095,Germany",51.9526622,7.6324709,social_facility,Germany,,Münster,48153,Friedrich-Ebert-Straße,7,9,"Chance e.V., Friedrich-Ebert-Straße 7, 48153 Münster, Germany"'
]
)
)

def test_input_errors(capfd):
main([
"reverse",
"--api-key", "6d0e711d72d74daeb2b0bfd2a5cdfdba",
"--input", "test/fixtures/reverse_with_errors.csv",
"--output", "test/fixtures/output.csv",
"--no-progress"
])

_, err = capfd.readouterr()
assert 'Missing input column 2 in' in err
assert 'Expected two comma-separated values' in err
1 change: 1 addition & 0 deletions test/fixtures/reverse_with_errors.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
50.101010

0 comments on commit dc76ee4

Please sign in to comment.