-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate configuration functions in a class
Why these changes are being introduced: * Streamline logging configuration and environment variable management How this addresses that need: * Create Config class * Clearly identify required vs. optional env vars * Update dependent modules to use Config * Replace os.getenv() calls with Config.<ENV-VAR> * Update unit tests in test_config Side effects of this change: * None
- Loading branch information
1 parent
6cca678
commit 050aa01
Showing
5 changed files
with
125 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,70 @@ | ||
import logging | ||
|
||
from harvester.config import configure_logger, configure_sentry | ||
import pytest | ||
|
||
from harvester.config import Config | ||
|
||
def test_configure_logger_not_verbose(): | ||
logger = logging.getLogger(__name__) | ||
result = configure_logger(logger, verbose=False) | ||
assert logger.getEffectiveLevel() == int(logging.INFO) | ||
assert result == "Logger 'tests.test_config' configured with level=INFO" | ||
|
||
def test_configure_logger_defaults_to_root_logger(): | ||
config = Config(logger=None) | ||
assert config.logger.name == "root" | ||
|
||
|
||
def test_configure_logger_verbose(caplog): | ||
def test_configure_logger_accepts_specific_logger(): | ||
logger = logging.getLogger(__name__) | ||
result = configure_logger(logger, verbose=True) | ||
assert logger.getEffectiveLevel() == int(logging.DEBUG) | ||
assert result == "Logger 'tests.test_config' configured with level=DEBUG" | ||
config = Config(logger=logger) | ||
assert config.logger.name == "tests.test_config" | ||
|
||
|
||
def test_configure_logger_not_verbose(config): | ||
result = config.configure_logger(verbose=False) | ||
assert config.logger.getEffectiveLevel() == int(logging.INFO) | ||
assert result == "Logger 'root' configured with level=INFO" | ||
|
||
|
||
def test_configure_sentry_no_env_variable(monkeypatch): | ||
def test_configure_logger_verbose(config): | ||
result = config.configure_logger(verbose=True) | ||
assert config.logger.getEffectiveLevel() == int(logging.DEBUG) | ||
assert result == "Logger 'root' configured with level=DEBUG" | ||
|
||
|
||
def test_configure_sentry_no_env_variable(config, monkeypatch): | ||
config.configure_logger(verbose=False) | ||
monkeypatch.delenv("SENTRY_DSN", raising=False) | ||
result = configure_sentry() | ||
result = config.configure_sentry() | ||
assert result == "No Sentry DSN found, exceptions will not be sent to Sentry" | ||
|
||
|
||
def test_configure_sentry_env_variable_is_none(monkeypatch): | ||
def test_configure_sentry_env_variable_is_none(config, monkeypatch): | ||
monkeypatch.setenv("SENTRY_DSN", "None") | ||
result = configure_sentry() | ||
result = config.configure_sentry() | ||
assert result == "No Sentry DSN found, exceptions will not be sent to Sentry" | ||
|
||
|
||
def test_configure_sentry_env_variable_is_dsn(monkeypatch): | ||
def test_configure_sentry_env_variable_is_dsn(config, monkeypatch): | ||
monkeypatch.setenv("SENTRY_DSN", "https://[email protected]/123456") | ||
result = configure_sentry() | ||
result = config.configure_sentry() | ||
assert result == "Sentry DSN found, exceptions will be sent to Sentry with env=test" | ||
|
||
|
||
def test_config_check_required_env_vars_success(config): | ||
config.check_required_env_vars() | ||
|
||
|
||
def test_config_check_required_env_vars_error(config, monkeypatch): | ||
monkeypatch.delenv("SENTRY_DSN") | ||
with pytest.raises( | ||
OSError, match="Missing required environment variables: SENTRY_DSN" | ||
): | ||
config.check_required_env_vars() | ||
|
||
|
||
def test_config_env_var_access_success(config): | ||
assert config.STATUS_UPDATE_INTERVAL == "1000" | ||
|
||
|
||
def test_config_env_var_access_error(config): | ||
with pytest.raises( | ||
AttributeError, match="'DOES_NOT_EXIST' not a valid configuration variable" | ||
): | ||
_ = config.DOES_NOT_EXIST |