Files
nifodea/tests/conftest.py
T
oqyude bf764723e4 D10: изолировать ФС в тестах — tmp_path + monkeypatch.chdir
- tests/conftest.py: автофикстура `isolated_cwd` переводит CWD во временный
  каталог на время каждого теста; добавлена session-фикстура `repo_root`
- tests/test_cli.py: удалён `_cleanup()` (он удалял отслеживаемый git файл
  data/model.json) и импорт MODEL_PATH — 13 вызовов
- +2 регрессионных теста: CLI пишет модель в tmp, а отслеживаемый
  data/model.json в дереве репозитория остаётся байт-в-байт прежним

Проверено: `python -m pytest -q` больше не модифицирует data/model.json.
2026-10-25 05:17:32 +03:00

97 lines
3.0 KiB
Python

from pathlib import Path
from uuid import uuid4
import pytest
from domain import (
Account,
Asset,
CurrencyConverter,
ExchangeRate,
FinancialModel,
Liability,
RecurringCashflow,
Transaction,
)
@pytest.fixture(scope="session")
def repo_root() -> Path:
"""Корень репозитория (для проверок, что тесты не пишут в дерево)."""
return Path(__file__).resolve().parent.parent
@pytest.fixture(autouse=True)
def isolated_cwd(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Изоляция ФС: тесты не пишут в рабочее дерево репозитория.
CLI работает с относительными путями (`data/model.json`, `exports/...`),
поэтому достаточно перевести CWD во временный каталог — любой файл,
созданный командой, окажется в tmp_path, а не в дереве репозитория.
До D10 тесты писали в `./data/model.json` и `_cleanup()` удалял
отслеживаемый git файл.
"""
monkeypatch.chdir(tmp_path)
return tmp_path
@pytest.fixture
def sample_model() -> FinancialModel:
acc_id = uuid4()
return FinancialModel(
base_currency="RUB",
accounts=[
Account(id=acc_id, name="Основной счёт", currency="RUB", balance=5000.0),
Account(name="Сбережения", currency="RUB", balance=10000.0),
],
transactions=[
Transaction(
date="2026-01-01",
account=str(acc_id),
category="income",
amount=3000.0,
description="Зарплата",
),
Transaction(
date="2026-01-05",
account=str(acc_id),
category="rent",
amount=-1200.0,
description="Аренда",
),
],
recurring=[
RecurringCashflow(
start_date="2026-01-01",
frequency="monthly",
amount=500.0,
category="income",
),
RecurringCashflow(
start_date="2026-01-01",
frequency="monthly",
amount=-200.0,
category="subscription",
),
],
assets=[
Asset(name="Акции", value=50000.0, growth_rate=8.0),
],
liabilities=[
Liability(name="Кредит", balance=20000.0, interest=5.0, payment=500.0),
],
exchange_rates=[
ExchangeRate(from_currency="USD", to_currency="RUB", rate=80.0),
],
)
@pytest.fixture
def empty_model() -> FinancialModel:
return FinancialModel()
@pytest.fixture
def sample_converter() -> CurrencyConverter:
return CurrencyConverter([ExchangeRate(from_currency="USD", to_currency="RUB", rate=80.0)])