Skip to content

Commit 463858f

Browse files
authored
Merge pull request #140 from markferry/fix-83-postfinance-date-format
fix #83: postfinance dateutil.parser
2 parents c860627 + 4627e24 commit 463858f

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/tariochbctools/importers/postfinance/importer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import csv
22
import logging
33
import re
4-
from datetime import datetime, timedelta
4+
from datetime import timedelta
55
from decimal import Decimal
66

77
import beangulp
8+
import dateutil.parser
89
from beancount.core import data
910

1011

@@ -31,7 +32,7 @@ def extract(self, filepath: str, existing: data.Entries) -> data.Entries:
3132
for row in reader:
3233
try:
3334
book_date_str, text, credit, debit, val_date, balance_str = tuple(row)
34-
book_date = datetime.strptime(book_date_str, "%Y-%m-%d").date()
35+
book_date = dateutil.parser.parse(book_date_str).date()
3536
if credit:
3637
amount = data.Amount(Decimal(credit), self.currency)
3738
elif debit:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
from beancount.core.amount import Amount
3+
from beancount.core.number import D
4+
5+
from tariochbctools.importers.postfinance import importer as pfimp
6+
7+
TEST_CSV = """
8+
Booking date;Notification text;Credit in CHF;Debit in CHF;Value;Balance in CHF
9+
31.12.2022;"Preis für Bankpaket Smart";;-5;31.12.2022;7881.98
10+
"""
11+
12+
TEST_CSV_2021 = """
13+
Booking date;Notification text;Credit in CHF;Debit in CHF;Value;Balance in CHF
14+
2022-12-31;"Preis für Bankpaket Smart";;-5;2022-12-31;7881.98
15+
"""
16+
17+
18+
@pytest.fixture(name="importer")
19+
def importer_fixture():
20+
importer = pfimp.Importer(".*.csv", "Assets:PostFinance:CHF")
21+
yield importer
22+
23+
24+
@pytest.fixture
25+
def tmp_csv(tmp_path, request):
26+
csv = tmp_path / "test.csv"
27+
csv.write_text(request.param)
28+
yield csv
29+
30+
31+
@pytest.mark.parametrize("tmp_csv", [TEST_CSV], indirect=True)
32+
def test_identify(importer, tmp_csv):
33+
assert importer.identify(str(tmp_csv))
34+
35+
36+
@pytest.mark.parametrize(
37+
"tmp_csv",
38+
(TEST_CSV, TEST_CSV_2021),
39+
ids=("dd.mm.yyyy", "yyyy-mm-dd"),
40+
indirect=True,
41+
)
42+
def test_extract(importer, tmp_csv):
43+
entries = importer.extract(str(tmp_csv), [])
44+
assert entries
45+
assert entries[0].postings[-1].units == Amount(D(-5), "CHF")
46+
assert entries[0].date.year == 2022

0 commit comments

Comments
 (0)