test_config.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Config tests
  5. """
  6. import os
  7. from data_modules import config as config_module
  8. from data_modules.config import DataModulesConfig, get_config, set_project_root
  9. def test_config_paths_and_defaults(tmp_path):
  10. cfg = DataModulesConfig.from_project_root(tmp_path)
  11. assert cfg.project_root == tmp_path
  12. assert cfg.webnovel_dir.name == ".webnovel"
  13. assert cfg.state_file.name == "state.json"
  14. assert cfg.index_db.name == "index.db"
  15. assert cfg.rag_db.name == "rag.db"
  16. assert cfg.vector_db.name == "vectors.db"
  17. cfg.ensure_dirs()
  18. assert cfg.webnovel_dir.exists()
  19. def test_get_config_and_set_project_root(tmp_path):
  20. set_project_root(tmp_path)
  21. cfg = get_config()
  22. assert cfg.project_root == tmp_path
  23. def test_load_dotenv(monkeypatch, tmp_path):
  24. # prepare .env
  25. env_path = tmp_path / ".env"
  26. env_path.write_text("EMBED_BASE_URL=https://example.com\n", encoding="utf-8")
  27. monkeypatch.chdir(tmp_path)
  28. monkeypatch.delenv("EMBED_BASE_URL", raising=False)
  29. # call loader explicitly
  30. config_module._load_dotenv()
  31. assert os.environ.get("EMBED_BASE_URL") == "https://example.com"
  32. def test_config_default_context_template_weights_dynamic_is_available(tmp_path):
  33. cfg = DataModulesConfig.from_project_root(tmp_path)
  34. dynamic = cfg.context_template_weights_dynamic
  35. assert isinstance(dynamic, dict)
  36. assert "early" in dynamic
  37. assert "mid" in dynamic
  38. assert "late" in dynamic
  39. assert "plot" in dynamic["early"]
  40. def test_config_dynamic_template_weights_are_independent_instances(tmp_path):
  41. cfg1 = DataModulesConfig.from_project_root(tmp_path)
  42. cfg2 = DataModulesConfig.from_project_root(tmp_path)
  43. cfg1.context_template_weights_dynamic["early"]["plot"]["core"] = 0.77
  44. assert cfg2.context_template_weights_dynamic["early"]["plot"]["core"] != 0.77