summaryrefslogtreecommitdiff
path: root/finance/test_flow.py
blob: db1b6950d3fa9d69b2ec5f835695b473c672cf30 (plain)
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
from datetime import datetime
from decimal import Decimal

from finance.flow import Flow, simulate


def test_flow_integration() -> None:
    fl = Flow(
        amount=Decimal(3000.0),
        since=datetime(2023, 1, 1),
        until=datetime(2026, 1, 5),
    )

    tests = (
        (datetime(2024, 3, 12), datetime(2024, 4, 2), Decimal(3000.0)),
        (datetime(2024, 3, 1), datetime(2024, 3, 15), Decimal(3000.0)),
        (datetime(2024, 2, 25), datetime(2024, 3, 1), Decimal(3000.0)),
        (datetime(2024, 2, 25), datetime(2024, 6, 12), Decimal(12000.0)),
        (datetime(2022, 1, 5), datetime(2022, 9, 14), Decimal(0.0)),
        (datetime(2022, 1, 5), datetime(2023, 1, 1), Decimal(3000.0)),
        (datetime(2022, 7, 4), datetime(2024, 12, 8), Decimal(72000.0)),
        (datetime(2025, 11, 4), datetime(2026, 5, 9), Decimal(6000.0)),
        (datetime(2026, 1, 5), datetime(2027, 1, 1), Decimal(0.0)),
        (datetime(2027, 5, 28), datetime(2028, 7, 7), Decimal(0.0)),
    )

    for test in tests:
        assert fl.integrate(start=test[0], end=test[1]) == test[2]


def test_simulate() -> None:
    flows = (Flow(amount=Decimal(100.0), since=None, until=None),)

    dates, values = simulate(
        start=datetime(2024, 1, 1),
        end=datetime(2024, 4, 1),
        flows=flows,
    )

    assert dates == [
        datetime(2024, 1, 1),
        datetime(2024, 2, 1),
        datetime(2024, 3, 1),
        datetime(2024, 4, 1),
    ]

    assert values == [
        Decimal(100.0),
        Decimal(200.0),
        Decimal(300.0),
        Decimal(400.0),
    ]