83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
from pathlib import Path
|
|
|
|
from cashflow_model import (
|
|
Account,
|
|
Asset,
|
|
FinancialModel,
|
|
ForecastScenario,
|
|
Liability,
|
|
RecurringCashflow,
|
|
Transaction,
|
|
)
|
|
|
|
|
|
class TestAccount:
|
|
def test_create(self):
|
|
a = Account(name="Test", balance=100.0)
|
|
assert a.name == "Test"
|
|
assert a.balance == 100.0
|
|
assert a.currency == "USD"
|
|
|
|
def test_to_dict_roundtrip(self):
|
|
a = Account(name="Test", balance=100.0)
|
|
d = a.to_dict()
|
|
a2 = Account.from_dict(d)
|
|
assert a2.name == a.name
|
|
assert a2.balance == a.balance
|
|
assert a2.currency == a.currency
|
|
|
|
|
|
class TestTransaction:
|
|
def test_create(self):
|
|
t = Transaction(amount=500.0, category="food")
|
|
assert t.amount == 500.0
|
|
|
|
def test_roundtrip(self):
|
|
t = Transaction(amount=-100.0, category="rent", description="test")
|
|
d = t.to_dict()
|
|
t2 = Transaction.from_dict(d)
|
|
assert t2.amount == t.amount
|
|
assert t2.category == t.category
|
|
assert t2.description == t.description
|
|
|
|
|
|
class TestFinancialModel:
|
|
def test_save_load(self, tmp_path: Path):
|
|
model = FinancialModel()
|
|
model.accounts.append(Account(name="Test", balance=100.0))
|
|
model.transactions.append(Transaction(amount=50.0, category="income"))
|
|
|
|
p = tmp_path / "model.json"
|
|
model.save(p)
|
|
assert p.exists()
|
|
|
|
loaded = FinancialModel.load(p)
|
|
assert len(loaded.accounts) == 1
|
|
assert len(loaded.transactions) == 1
|
|
assert loaded.accounts[0].name == "Test"
|
|
|
|
def test_empty_model(self):
|
|
model = FinancialModel()
|
|
d = model.to_dict()
|
|
assert d["accounts"] == []
|
|
assert d["transactions"] == []
|
|
|
|
def test_all_entities_roundtrip(self, tmp_path: Path):
|
|
model = FinancialModel(
|
|
accounts=[Account(name="A"), Account(name="B")],
|
|
transactions=[Transaction(amount=100.0)],
|
|
recurring=[RecurringCashflow(amount=50.0)],
|
|
assets=[Asset(name="Stock", value=1000.0)],
|
|
liabilities=[Liability(name="Loan", balance=500.0, interest=5.0, payment=100.0)],
|
|
scenarios=[ForecastScenario(name="test")],
|
|
)
|
|
p = tmp_path / "full.json"
|
|
model.save(p)
|
|
loaded = FinancialModel.load(p)
|
|
assert len(loaded.accounts) == 2
|
|
assert len(loaded.transactions) == 1
|
|
assert len(loaded.recurring) == 1
|
|
assert len(loaded.assets) == 1
|
|
assert len(loaded.liabilities) == 1
|
|
assert len(loaded.scenarios) == 1
|