File size: 3,103 Bytes
f91bd05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pytest
from csv_import import parse_bank_csv


def test_german_bank_csv_with_decimal_comma():
    """Test parsing a German bank CSV with semicolon delimiter and decimal comma values."""
    csv_content = b"buchungstag;verwendungszweck;betrag\n2023-01-15;Lohn;1500,00\n2023-01-20;Miete;-800,00\n2023-01-25;Essen;-150,00"

    result = parse_bank_csv(csv_content)

    assert len(result["transactions"]) == 3
    assert result["total_income"] == 1500.00
    assert result["total_expenses"] == 950.00
    assert result["net"] == 550.00
    assert result["count"] == 3
    assert result["monthly_estimate"] == 950.00  # All transactions in same month


def test_description_and_date_extraction():
    """Test that description and date fields are properly extracted."""
    csv_content = b"datum;beschreibung;betrag\n2023-02-01;Supermarkt Einkauf;-50,00\n2023-02-05;Gehalt;2000,00\n2023-02-10;Restaurant;-75,00"

    result = parse_bank_csv(csv_content)

    assert len(result["transactions"]) == 3
    assert result["transactions"][0]["date"] == "2023-02-01"
    assert result["transactions"][0]["description"] == "Supermarkt Einkauf"
    assert result["transactions"][0]["amount"] == -50.00

    assert result["transactions"][1]["date"] == "2023-02-05"
    assert result["transactions"][1]["description"] == "Gehalt"
    assert result["transactions"][1]["amount"] == 2000.00


def test_missing_optional_fields():
    """Test that missing optional description/date fields are handled gracefully."""
    csv_content = b"betrag;umsatz\n100,00;100,00\n-50,00;-50,00\n"

    result = parse_bank_csv(csv_content)

    assert len(result["transactions"]) == 2
    assert result["transactions"][0]["date"] == ""
    assert result["transactions"][0]["description"] == ""
    assert result["transactions"][0]["amount"] == 100.00

    assert result["transactions"][1]["date"] == ""
    assert result["transactions"][1]["description"] == ""
    assert result["transactions"][1]["amount"] == -50.00


def test_monthly_estimate_over_multiple_months():
    """Test monthly estimate calculation over multiple months."""
    csv_content = b"buchungstag;verwendungszweck;betrag\n2023-01-15;Lohn;1500,00\n2023-01-20;Miete;-800,00\n2023-02-10;Essen;-150,00\n2023-02-20;Gehalt;1600,00\n2023-03-05;Miete;-850,00"

    result = parse_bank_csv(csv_content)

    assert result["total_expenses"] == 1800.00
    assert result["count"] == 5
    assert result["monthly_estimate"] == 600.00  # 1800 / 3 months


def test_invalid_amount_rows_ignored():
    """Test that invalid or non-numeric amount rows are ignored."""
    csv_content = b"buchungstag;verwendungszweck;betrag\n2023-01-15;Lohn;1500,00\n2023-01-20;Miete;-800,00\n2023-01-25;Essen;N/A\n2023-01-30;Geschenk;abc"

    result = parse_bank_csv(csv_content)

    assert len(result["transactions"]) == 2  # Only 2 valid transactions
    assert result["total_income"] == 1500.00
    assert result["total_expenses"] == 800.00
    assert result["net"] == 700.00
    assert result["count"] == 2
    assert result["monthly_estimate"] == 800.00  # All transactions in same month