Skip to content

Commit

Permalink
Tolerate broken CSV files (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
oittaa authored Nov 19, 2021
1 parent 4954bb0 commit 1ec2e27
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 3 additions & 4 deletions ibkr_report/exchangerates.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ def add_to_exchange_rates(self, rates_file: Iterable[bytes]) -> None:
if currencies and re.match(r"^\d\d\d\d-\d\d-\d\d$", items[0]):
# And the following rows like "2015-01-20,1.1579,137.37,..."
date_rates = {}
for key, val in enumerate(items):
if key == 0 or not currencies[key] or not is_number(val):
continue
date_rates[currencies[key]] = val
for cur, val in zip(currencies, items):
if is_number(val):
date_rates[cur] = val
if date_rates:
rates[items[0]] = date_rates
log.debug("Adding currency data from %d rows.", len(rates))
Expand Down
3 changes: 3 additions & 0 deletions test-data/eurofxref-broken.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Date,USD
2021-10-27
2021-10-26,1.1618,132.47
8 changes: 8 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ def test_far_in_the_future(self):
with self.assertRaises(ValueError):
self.rates.get_rate("USD", "CAD", "2500-01-01")

def test_broken_input(self):
with open("test-data/eurofxref-broken.csv", "rb") as file:
self.rates.add_to_exchange_rates(file)
eur_usd = self.rates.get_rate("EUR", "USD", "2021-10-26")
self.assertEqual(eur_usd, Decimal("1.1618"))
eur_usd = self.rates.get_rate("EUR", "USD", "2021-10-25")
self.assertEqual(eur_usd, Decimal("1.1603"))


if __name__ == "__main__":
unittest.main()

0 comments on commit 1ec2e27

Please sign in to comment.