Skip to content

Commit

Permalink
parsers: Trading212: Minor refactor
Browse files Browse the repository at this point in the history
Refactor Trading212 parser and tests to make them more similar to the
ones for Freetrade.
  • Loading branch information
tacgomes committed Nov 21, 2024
1 parent 4256a76 commit 72d6629
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/investir/parser/trading212.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,24 @@ def parse(self) -> ParsingResult:
"Deposit": self._parse_transfer,
"Withdrawal": self._parse_transfer,
"Interest on cash": self._parse_interest,
"Currency conversion": lambda _: None,
"Currency conversion": None,
}

with self._csv_file.open(encoding="utf-8") as file:
for row in csv.DictReader(file):
tr_type = row["Action"]

if tr_type not in parse_fn:
raise_or_warn(TransactionTypeError(self._csv_file, row, tr_type))
continue

if fn := parse_fn.get(tr_type):
currency_total = row["Currency (Total)"]
if currency_total != "GBP":
raise_or_warn(
CurrencyError(self._csv_file, row, currency_total)
)
fn(row)
else:
raise_or_warn(TransactionTypeError(self._csv_file, row, tr_type))

return ParsingResult(
self._orders, self._dividends, self._transfers, self._interest
Expand Down
4 changes: 4 additions & 0 deletions tests/parsers/test_trading212.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

@pytest.fixture(name="create_parser")
def fixture_create_parser(tmp_path) -> Callable:
config.reset()
config.include_fx_fees = True

def _create_parser(rows: Sequence[Mapping[str, str]]) -> Trading212Parser:
Expand Down Expand Up @@ -301,6 +302,9 @@ def test_parser_invalid_transaction_type(create_parser):
with pytest.raises(TransactionTypeError):
parser.parse()

config.strict = False
parser.parse()


def test_parser_invalid_account_currency(create_parser):
order = dict(ACQUISITION)
Expand Down

0 comments on commit 72d6629

Please sign in to comment.