Files
oqyude 6470978693 D29: объект путей AppPaths с базой данных из CF_DATA_DIR
infrastructure/cli/paths.py: AppPaths(data_dir, exports_dir) с model_path;
from_env() читает CF_DATA_DIR (дефолт ./data — поведение не меняется) на
каждую команду. CF_DATA_DIR переопределяет только данные модели: экспорты
остаются в ./exports относительно CWD. load_model/save_model, cf init и
cf import-xlsx идут через объект; дефолт cf export-xlsx прежний.
Тесты: дефолтные пути, переопределение, init/config/info/import/export.
2026-10-25 16:49:33 +03:00

90 lines
3.5 KiB
Python

"""D29: объект путей и переопределение базы данных через CF_DATA_DIR."""
from pathlib import Path
from typer.testing import CliRunner
from domain import FinancialModel
from infrastructure.cli.app import app
from infrastructure.cli.paths import AppPaths
from infrastructure.repositories import ExcelRepository
runner = CliRunner()
class TestAppPaths:
def test_defaults_are_unchanged(self, monkeypatch):
monkeypatch.delenv("CF_DATA_DIR", raising=False)
paths = AppPaths.from_env()
assert paths.data_dir == Path("data")
assert paths.model_path == Path("data/model.json")
assert paths.exports_dir == Path("exports")
def test_data_dir_env_overrides_only_data(self, monkeypatch, tmp_path: Path):
custom = tmp_path / "custom-data"
monkeypatch.setenv("CF_DATA_DIR", str(custom))
paths = AppPaths.from_env()
assert paths.data_dir == custom
assert paths.model_path == custom / "model.json"
# Экспорты остаются относительно CWD и от CF_DATA_DIR не зависят.
assert paths.exports_dir == Path("exports")
def test_env_is_read_per_call_not_at_import(self, monkeypatch, tmp_path: Path):
custom = tmp_path / "late-env"
monkeypatch.setenv("CF_DATA_DIR", str(custom))
# Модуль уже импортирован выше — значение всё равно подхватилось.
assert AppPaths.from_env().model_path == custom / "model.json"
class TestDataDirEnvInCli:
def test_init_writes_model_into_cf_data_dir(self, isolated_cwd: Path, monkeypatch, tmp_path: Path):
custom = tmp_path / "cf-data"
monkeypatch.setenv("CF_DATA_DIR", str(custom))
result = runner.invoke(app, ["init"])
assert result.exit_code == 0
assert (custom / "model.json").exists()
assert not (isolated_cwd / "data" / "model.json").exists()
assert "Пустая модель создана" in result.stdout
def test_config_and_info_read_and_write_cf_data_dir(self, isolated_cwd: Path, monkeypatch, tmp_path: Path):
custom = tmp_path / "cf-data"
monkeypatch.setenv("CF_DATA_DIR", str(custom))
assert runner.invoke(app, ["init"]).exit_code == 0
add = runner.invoke(app, ["config", "account-add", "--name", "Из CF_DATA_DIR", "--balance", "1000"])
assert add.exit_code == 0
info = runner.invoke(app, ["info"])
assert info.exit_code == 0
assert "Из CF_DATA_DIR" in info.stdout
assert (custom / "model.json").exists()
def test_export_xlsx_default_path_ignores_cf_data_dir(self, isolated_cwd: Path, monkeypatch, tmp_path: Path):
monkeypatch.setenv("CF_DATA_DIR", str(tmp_path / "cf-data"))
runner.invoke(app, ["init"])
result = runner.invoke(app, ["export-xlsx"])
assert result.exit_code == 0
assert (isolated_cwd / "exports" / "forecast.xlsx").exists()
def test_import_xlsx_saves_model_into_cf_data_dir(self, isolated_cwd: Path, monkeypatch, tmp_path: Path):
source = tmp_path / "source.xlsx"
ExcelRepository().save(FinancialModel(base_currency="EUR"), source)
custom = tmp_path / "cf-data"
monkeypatch.setenv("CF_DATA_DIR", str(custom))
result = runner.invoke(app, ["import-xlsx", str(source)])
assert result.exit_code == 0
assert (custom / "model.json").exists()
assert not (isolated_cwd / "data" / "model.json").exists()