T9: Excel as the model authoring surface
- 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
This commit is contained in:
1 parent
b90ca4c282
commit
cdda703b37
39 files changed
+2181
-198
No files matched your search
+185
-1
@@ -1,7 +1,7 @@
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from infrastructure.cli.app import app
|
||||
from infrastructure.cli.paths import MODEL_PATH
|
||||
from infrastructure.cli.paths import MODEL_PATH, TEMPLATE_PATH
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
@@ -167,3 +167,187 @@ class TestCli:
|
||||
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()
|
||||
Reference in new issue
Block a user