Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /elisiariocouto_leggen /tests /unit /test_config.py
| """Tests for configuration management.""" | |
| from unittest.mock import patch | |
| import pytest | |
| from leggen.utils.config import Config | |
| from tests.conftest import reset_config_singleton | |
| _AUTH_CONFIG = { | |
| "username": "testuser", | |
| "password_hash": "$2b$12$VNxOF9xF676elZu5M9pW7eYZ.p.qVJsh/n6jIsHE3iNnHWBtUxr/y", | |
| "api_key": "lgn_test-api-key-for-testing", | |
| "jwt_secret": "test-jwt-secret-for-testing-only", | |
| "jwt_expiry_minutes": 60, | |
| } | |
| class TestConfig: | |
| """Test configuration management.""" | |
| def _restore_config_singleton(self): | |
| """`Config()` is a process-global singleton, so every test below that | |
| pokes at `config._config*` leaks into the rest of the session. Clear it | |
| afterwards so the next access reloads the canonical test config.""" | |
| try: | |
| yield | |
| finally: | |
| reset_config_singleton() | |
| def test_singleton_behavior(self): | |
| """Test that Config is a singleton.""" | |
| config1 = Config() | |
| config2 = Config() | |
| assert config1 is config2 | |
| def test_load_config_success(self, temp_config_dir, test_key_path): | |
| """Test successful configuration loading.""" | |
| config_data = { | |
| "auth": _AUTH_CONFIG, | |
| "enablebanking": { | |
| "application_id": "test-app-id", | |
| "key_path": str(test_key_path), | |
| "url": "https://api.enablebanking.com", | |
| }, | |
| # Legacy no-op section — must be tolerated and dropped, not rejected | |
| "database": {"sqlite": True}, | |
| } | |
| config_file = temp_config_dir / "config.toml" | |
| with open(config_file, "wb") as f: | |
| import tomli_w | |
| tomli_w.dump(config_data, f) | |
| config = Config() | |
| # Reset singleton state for testing | |
| config._config = None | |
| config._config_path = None | |
| config._config_model = None | |
| result = config.load_config(str(config_file)) | |
| # Result should contain validated config data | |
| assert result["enablebanking"]["application_id"] == "test-app-id" | |
| assert config.enablebanking_config["application_id"] == "test-app-id" | |
| assert "database" not in result | |
| def test_load_config_file_not_found(self): | |
| """Test handling of missing configuration file.""" | |
| config = Config() | |
| config._config = None # Reset for test | |
| with pytest.raises(FileNotFoundError): | |
| config.load_config("/nonexistent/config.toml") | |
| def test_save_config_success(self, temp_config_dir, test_key_path): | |
| """Test successful configuration saving.""" | |
| config_data = { | |
| "auth": _AUTH_CONFIG, | |
| "enablebanking": { | |
| "application_id": "new-app-id", | |
| "key_path": str(test_key_path), | |
| "url": "https://api.enablebanking.com", | |
| }, | |
| } | |
| config_file = temp_config_dir / "new_config.toml" | |
| config = Config() | |
| config._config = None | |
| config._config_model = None | |
| config.save_config(config_data, str(config_file)) | |
| # Verify file was created and contains correct data | |
| assert config_file.exists() | |
| import tomllib | |
| with open(config_file, "rb") as f: | |
| saved_data = tomllib.load(f) | |
| assert saved_data["enablebanking"]["application_id"] == "new-app-id" | |
| def test_update_section_success(self, temp_config_dir, test_key_path): | |
| """Test updating entire configuration section.""" | |
| initial_config = { | |
| "auth": _AUTH_CONFIG, | |
| "enablebanking": { | |
| "application_id": "test-app-id", | |
| "key_path": str(test_key_path), | |
| "url": "https://api.enablebanking.com", | |
| }, | |
| } | |
| config_file = temp_config_dir / "config.toml" | |
| with open(config_file, "wb") as f: | |
| import tomli_w | |
| tomli_w.dump(initial_config, f) | |
| config = Config() | |
| config._config = None | |
| config._config_model = None | |
| config.load_config(str(config_file)) | |
| new_filters_config = {"case_insensitive": ["salary"]} | |
| config.update_section("filters", new_filters_config) | |
| assert config.filters_config["case_insensitive"] == ["salary"] | |
| def test_scheduler_config_defaults(self): | |
| """Test scheduler configuration with defaults.""" | |
| config = Config() | |
| config._config = {} # Empty config | |
| scheduler_config = config.scheduler_config | |
| assert scheduler_config["sync"]["enabled"] is True | |
| assert scheduler_config["sync"]["hour"] == 3 | |
| assert scheduler_config["sync"]["minute"] == 0 | |
| assert scheduler_config["sync"]["cron"] is None | |
| def test_scheduler_config_custom(self): | |
| """Test scheduler configuration with custom values.""" | |
| custom_config = { | |
| "scheduler": { | |
| "sync": { | |
| "enabled": False, | |
| "hour": 6, | |
| "minute": 30, | |
| "cron": "0 6 * * 1-5", | |
| } | |
| } | |
| } | |
| config = Config() | |
| config._config = custom_config | |
| scheduler_config = config.scheduler_config | |
| assert scheduler_config["sync"]["enabled"] is False | |
| assert scheduler_config["sync"]["hour"] == 6 | |
| assert scheduler_config["sync"]["minute"] == 30 | |
| assert scheduler_config["sync"]["cron"] == "0 6 * * 1-5" | |
| def test_set_config_path_wins_over_environment( | |
| self, temp_config_dir, test_key_path | |
| ): | |
| """An explicit path (e.g. the --config flag) beats LEGGEN_CONFIG_FILE.""" | |
| config_data = { | |
| "auth": _AUTH_CONFIG, | |
| "enablebanking": { | |
| "application_id": "from-flag", | |
| "key_path": str(test_key_path), | |
| "url": "https://api.enablebanking.com", | |
| }, | |
| } | |
| flag_config = temp_config_dir / "flag.toml" | |
| with open(flag_config, "wb") as f: | |
| import tomli_w | |
| tomli_w.dump(config_data, f) | |
| config = Config() | |
| config._config = None | |
| config._config_path = None | |
| config._config_model = None | |
| with patch.dict("os.environ", {"LEGGEN_CONFIG_FILE": "/nonexistent/env.toml"}): | |
| config.set_config_path(str(flag_config)) | |
| result = config.load_config() | |
| assert result["enablebanking"]["application_id"] == "from-flag" | |
| assert config._config_path == str(flag_config) | |
| def test_environment_variable_config_path(self): | |
| """Test using environment variable for config path.""" | |
| with patch.dict( | |
| "os.environ", {"LEGGEN_CONFIG_FILE": "/custom/path/config.toml"} | |
| ): | |
| config = Config() | |
| config._config = None | |
| with ( | |
| patch("builtins.open", side_effect=FileNotFoundError), | |
| pytest.raises(FileNotFoundError), | |
| ): | |
| config.load_config() | |
| def test_notifications_config(self): | |
| """Test notifications configuration access.""" | |
| custom_config = { | |
| "notifications": { | |
| "discord": {"webhook": "https://discord.webhook", "enabled": True}, | |
| "telegram": {"token": "bot-token", "chat_id": 123}, | |
| } | |
| } | |
| config = Config() | |
| config._config = custom_config | |
| notifications = config.notifications_config | |
| assert notifications["discord"]["webhook"] == "https://discord.webhook" | |
| assert notifications["telegram"]["token"] == "bot-token" | |
| def test_filters_config(self): | |
| """Test filters configuration access.""" | |
| custom_config = { | |
| "filters": { | |
| "case_insensitive": ["salary", "utility"], | |
| "case_sensitive": ["SpecificStore"], | |
| } | |
| } | |
| config = Config() | |
| config._config = custom_config | |
| filters = config.filters_config | |
| assert "salary" in filters["case_insensitive"] | |
| assert "utility" in filters["case_insensitive"] | |
| assert "SpecificStore" in filters["case_sensitive"] | |
Xet Storage Details
- Size:
- 8.2 kB
- Xet hash:
- 1fa428c22cc81036b2e7c1de3af0bb3a6e816fe916a19b7f688837cd80a8f543
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.