- ExcelRepository: canonical columns with ru/en aliases, date/number normalization (Excel dates, DD.MM.YYYY, '1 234,56', currency symbols, parentheses), account name->UUID resolution, per-row errors with sheet!row, warnings for formulas without cached values, duplicate names and default exchange rates - ExcelTemplate: skeleton generator (number/date formats, dropdowns for currency, frequency en/ru and account, 'Инструкция' sheet); ship data/model_template.xlsx - CLI: cf template-xlsx [--force]; cf import-xlsx --dry-run with rich error table, import writes nothing when the file has errors - RecurringCashflow.account (empty = all accounts) + name/UUID matching in forecast; cf config recurring-add --account, account column in info/recurring-list - Forecast: runs are idempotent, 'Итог: Баланс' sums all accounts of the last month - README: 'Сборка модели в Excel' section - ruff check . clean (9 pre-existing import/line-length findings fixed) - MetaAgent: T9 archived, req-T9 approved, manifest/checkpoints/state synced 118 tests pass
354 lines
12 KiB
Python
354 lines
12 KiB
Python
from typer.testing import CliRunner
|
|
|
|
from infrastructure.cli.app import app
|
|
from infrastructure.cli.paths import MODEL_PATH, TEMPLATE_PATH
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
def _cleanup():
|
|
if MODEL_PATH.exists():
|
|
MODEL_PATH.unlink()
|
|
|
|
|
|
class TestCli:
|
|
def test_init(self):
|
|
_cleanup()
|
|
result = runner.invoke(app, ["init"])
|
|
assert result.exit_code == 0
|
|
assert "Пустая модель" in result.stdout
|
|
|
|
def test_forecast_after_init(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
result = runner.invoke(app, ["forecast", "--months", "3"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_forecast_with_currency(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
result = runner.invoke(app, ["forecast", "--months", "3", "--currency", "USD"])
|
|
assert result.exit_code == 0
|
|
assert "$" in result.stdout
|
|
|
|
def test_unknown_scenario(self):
|
|
result = runner.invoke(app, ["scenario", "unknown"])
|
|
assert result.exit_code != 0
|
|
|
|
def test_help(self):
|
|
result = runner.invoke(app, ["--help"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_info_empty(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
result = runner.invoke(app, ["info"])
|
|
assert result.exit_code == 0
|
|
assert "Базовая валюта" in result.stdout
|
|
|
|
def test_config_base_currency_set(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
result = runner.invoke(app, ["config", "base-currency", "EUR"])
|
|
assert result.exit_code == 0
|
|
assert "EUR" in result.stdout
|
|
|
|
info = runner.invoke(app, ["info"])
|
|
assert "EUR" in info.stdout
|
|
|
|
def test_config_base_currency_show(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
result = runner.invoke(app, ["config", "base-currency"])
|
|
assert result.exit_code == 0
|
|
assert "RUB" in result.stdout
|
|
|
|
def test_config_account_crud(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
|
|
args = ["config", "account-add", "--name", "Тестовый", "--balance", "1000"]
|
|
add = runner.invoke(app, args)
|
|
assert add.exit_code == 0
|
|
assert "Тестовый" in add.stdout
|
|
|
|
lst = runner.invoke(app, ["config", "account-list"])
|
|
assert "Тестовый" in lst.stdout
|
|
|
|
remove = runner.invoke(app, ["config", "account-remove", "Тестовый"])
|
|
assert remove.exit_code == 0
|
|
|
|
lst2 = runner.invoke(app, ["config", "account-list"])
|
|
assert "Тестовый" not in lst2.stdout
|
|
|
|
def test_config_transaction_crud(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
runner.invoke(app, ["config", "account-add", "--name", "Счёт", "--balance", "0"])
|
|
|
|
add = runner.invoke(app, [
|
|
"config", "transaction-add",
|
|
"--account", "Счёт",
|
|
"--amount", "5000",
|
|
"--category", "income",
|
|
"--description", "Зарплата",
|
|
])
|
|
assert add.exit_code == 0
|
|
assert "доход" in add.stdout
|
|
|
|
lst = runner.invoke(app, ["config", "transaction-list"])
|
|
assert "Зарплата" in lst.stdout
|
|
|
|
def test_config_asset_crud(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
|
|
add = runner.invoke(app, [
|
|
"config", "asset-add",
|
|
"--name", "Квартира",
|
|
"--value", "5000000",
|
|
"--growth", "5",
|
|
])
|
|
assert add.exit_code == 0
|
|
assert "Квартира" in add.stdout
|
|
|
|
lst = runner.invoke(app, ["config", "asset-list"])
|
|
assert "Квартира" in lst.stdout
|
|
|
|
def test_config_liability_crud(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
|
|
add = runner.invoke(app, [
|
|
"config", "liability-add",
|
|
"--name", "Кредит",
|
|
"--balance", "100000",
|
|
"--interest", "10",
|
|
"--payment", "5000",
|
|
])
|
|
assert add.exit_code == 0
|
|
assert "Кредит" in add.stdout
|
|
|
|
def test_config_recurring_crud(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
|
|
add = runner.invoke(app, [
|
|
"config", "recurring-add",
|
|
"--amount", "-500",
|
|
"--category", "аренда",
|
|
"--frequency", "monthly",
|
|
])
|
|
assert add.exit_code == 0
|
|
|
|
lst = runner.invoke(app, ["config", "recurring-list"])
|
|
assert "аренда" in lst.stdout
|
|
|
|
def test_config_rate_crud(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
|
|
set_r = runner.invoke(app, ["config", "rate-set", "EUR", "RUB", "90"])
|
|
assert set_r.exit_code == 0
|
|
|
|
lst = runner.invoke(app, ["config", "rate-list"])
|
|
assert "EUR" in lst.stdout
|
|
assert "90" in lst.stdout
|
|
|
|
def test_info_with_data(self):
|
|
_cleanup()
|
|
runner.invoke(app, ["init"])
|
|
acc_args = ["config", "account-add", "--name", "Основной", "--balance", "50000"]
|
|
runner.invoke(app, acc_args)
|
|
asset_args = ["config", "asset-add", "--name", "Акции",
|
|
"--value", "100000", "--growth", "8.0"]
|
|
runner.invoke(app, asset_args)
|
|
|
|
result = runner.invoke(app, ["info"])
|
|
assert "Основной" in result.stdout
|
|
assert "Акции" in result.stdout
|
|
|
|
|
|
# ── Excel: template-xlsx / import-xlsx ─────────────────────────
|
|
|
|
_GOOD_ACCOUNTS = [("Основной", "RUB", 100000)]
|
|
_GOOD_TRANSACTIONS = [
|
|
("2026-11-01", "Основной", "salary", 50000, "Зарплата"),
|
|
("2026-11-05", "Основной", "rent", -35000, "Аренда"),
|
|
]
|
|
|
|
|
|
def _build_workbook(path, *, accounts=None, transactions=None):
|
|
"""Собрать минимальный .xlsx руками: Accounts + Transactions (без id)."""
|
|
from openpyxl import Workbook
|
|
|
|
wb = Workbook()
|
|
wb.remove(wb.active)
|
|
|
|
accounts_ws = wb.create_sheet("Accounts")
|
|
accounts_ws.append(["name", "currency", "balance"])
|
|
for row in _GOOD_ACCOUNTS if accounts is None else accounts:
|
|
accounts_ws.append(list(row))
|
|
|
|
transactions_ws = wb.create_sheet("Transactions")
|
|
transactions_ws.append(["date", "account", "category", "amount", "description"])
|
|
for row in _GOOD_TRANSACTIONS if transactions is None else transactions:
|
|
transactions_ws.append(list(row))
|
|
|
|
wb.save(path)
|
|
return path
|
|
|
|
|
|
class TestTemplateXlsx:
|
|
def test_creates_file_at_given_path(self, tmp_path):
|
|
target = tmp_path / "my_template.xlsx"
|
|
result = runner.invoke(app, ["template-xlsx", str(target)])
|
|
|
|
assert result.exit_code == 0, result.stdout
|
|
assert target.exists()
|
|
assert "cf import-xlsx" in result.stdout
|
|
|
|
def test_default_path_is_data_template(self):
|
|
import inspect
|
|
|
|
from infrastructure.cli.commands import template_xlsx as module
|
|
|
|
default = inspect.signature(module.template_xlsx).parameters["path"].default
|
|
assert default.default == TEMPLATE_PATH
|
|
|
|
def test_default_path_used_when_omitted(self, tmp_path, monkeypatch):
|
|
from pathlib import Path
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
result = runner.invoke(app, ["template-xlsx"])
|
|
|
|
assert result.exit_code == 0, result.stdout
|
|
assert Path(TEMPLATE_PATH).exists()
|
|
|
|
def test_existing_file_without_force(self, tmp_path):
|
|
target = tmp_path / "template.xlsx"
|
|
first = runner.invoke(app, ["template-xlsx", str(target)])
|
|
assert first.exit_code == 0
|
|
before = target.read_bytes()
|
|
|
|
second = runner.invoke(app, ["template-xlsx", str(target)])
|
|
|
|
assert second.exit_code != 0
|
|
assert "--force" in second.stdout
|
|
assert target.exists()
|
|
assert target.read_bytes() == before
|
|
|
|
def test_force_overwrites(self, tmp_path):
|
|
from openpyxl import load_workbook
|
|
|
|
target = tmp_path / "template.xlsx"
|
|
target.write_bytes(b"not a workbook")
|
|
|
|
result = runner.invoke(app, ["template-xlsx", str(target), "--force"])
|
|
|
|
assert result.exit_code == 0, result.stdout
|
|
wb = load_workbook(target)
|
|
try:
|
|
assert "Accounts" in wb.sheetnames
|
|
finally:
|
|
wb.close()
|
|
|
|
|
|
class TestImportXlsx:
|
|
def test_dry_run_good_book_does_not_save(self, tmp_path, monkeypatch):
|
|
target = _build_workbook(tmp_path / "good.xlsx")
|
|
calls = []
|
|
monkeypatch.setattr("infrastructure.cli.commands.import_xlsx.save_model", calls.append)
|
|
|
|
result = runner.invoke(app, ["import-xlsx", str(target), "--dry-run"])
|
|
|
|
assert result.exit_code == 0, result.stdout
|
|
assert "не сохранена" in result.stdout
|
|
assert "счета — 1" in result.stdout
|
|
assert calls == []
|
|
|
|
def test_unknown_account_blocks_save(self, tmp_path, monkeypatch):
|
|
target = _build_workbook(
|
|
tmp_path / "bad.xlsx",
|
|
transactions=[("2026-11-01", "Неизвестный", "rent", -1000, "Аренда")],
|
|
)
|
|
calls = []
|
|
monkeypatch.setattr("infrastructure.cli.commands.import_xlsx.save_model", calls.append)
|
|
|
|
result = runner.invoke(app, ["import-xlsx", str(target)])
|
|
|
|
assert result.exit_code == 1, result.stdout
|
|
assert "Transactions!2" in result.stdout
|
|
assert "Transactions!2 [account]" in result.stdout
|
|
assert "Первая ошибка" in result.stdout
|
|
assert calls == []
|
|
|
|
def test_zero_amount_blocks_save(self, tmp_path, monkeypatch):
|
|
target = _build_workbook(
|
|
tmp_path / "zero.xlsx",
|
|
transactions=[("2026-11-01", "Основной", "rent", 0, "Ноль")],
|
|
)
|
|
calls = []
|
|
monkeypatch.setattr("infrastructure.cli.commands.import_xlsx.save_model", calls.append)
|
|
|
|
result = runner.invoke(app, ["import-xlsx", str(target)])
|
|
|
|
assert result.exit_code == 1, result.stdout
|
|
assert "Transactions!2" in result.stdout
|
|
assert calls == []
|
|
|
|
def test_good_book_saves_once(self, tmp_path, monkeypatch):
|
|
target = _build_workbook(tmp_path / "good.xlsx")
|
|
calls = []
|
|
monkeypatch.setattr("infrastructure.cli.commands.import_xlsx.save_model", calls.append)
|
|
|
|
result = runner.invoke(app, ["import-xlsx", str(target)])
|
|
|
|
assert result.exit_code == 0, result.stdout
|
|
assert len(calls) == 1
|
|
assert "Импортировано" in result.stdout
|
|
assert "Базовая валюта" in result.stdout
|
|
|
|
def test_missing_file_exits_1(self, tmp_path, monkeypatch):
|
|
calls = []
|
|
monkeypatch.setattr("infrastructure.cli.commands.import_xlsx.save_model", calls.append)
|
|
|
|
result = runner.invoke(app, ["import-xlsx", str(tmp_path / "nope.xlsx")])
|
|
|
|
assert result.exit_code == 1
|
|
assert calls == []
|
|
|
|
|
|
class TestRecurringAccountScope:
|
|
def test_recurring_add_binds_account_by_name(self):
|
|
_cleanup()
|
|
try:
|
|
runner.invoke(app, ["init"])
|
|
runner.invoke(app, ["config", "account-add", "--name", "Основной", "--balance", "0"])
|
|
|
|
add = runner.invoke(app, [
|
|
"config", "recurring-add",
|
|
"--amount", "-500",
|
|
"--account", "основной",
|
|
])
|
|
assert add.exit_code == 0, add.stdout
|
|
|
|
lst = runner.invoke(app, ["config", "recurring-list"])
|
|
assert "Основной" in lst.stdout
|
|
|
|
info = runner.invoke(app, ["info"])
|
|
assert "Основной" in info.stdout
|
|
finally:
|
|
_cleanup()
|
|
|
|
def test_recurring_add_unknown_account_fails(self):
|
|
_cleanup()
|
|
try:
|
|
runner.invoke(app, ["init"])
|
|
result = runner.invoke(app, [
|
|
"config", "recurring-add", "--amount", "-500", "--account", "Нет такого",
|
|
])
|
|
assert result.exit_code == 1
|
|
finally:
|
|
_cleanup()
|