"""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()