Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a way to get env vars from Settings class #60

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ docker/lint:
$(DOCKER_COMPOSE) run ${APP_NAME} poetry run ruff check .

docker/lint/fix:
$(DOCKER_COMPOSE) run ${APP_NAME} poetry run ruff . --fix
$(DOCKER_COMPOSE) run ${APP_NAME} poetry run ruff check . --fix

docker/run:
$(DOCKER_COMPOSE) run ${APP_NAME} poetry run python ${MAIN_ENTRYPOINT}
Expand Down
3 changes: 3 additions & 0 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def __init__(self, file: str = 'settings.conf'):
def get(self, name: str, default_value: Any = None) -> Any:
return self._get_from_section(self.env, name) or self._get_from_section('default', name) or default_value

def get_from_env(self, name: str, default_value: Any = None) -> Any:
return getenv(name, default_value)

def _get_from_section(self, section: str, var: str) -> Any:
if section in self.config_parser and var in self.config_parser[section]:
return self.config_parser[section][var]
Expand Down
8 changes: 8 additions & 0 deletions tests/config/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ def test_get_setting_value_from_test_env_with_success(self):
def test_get_setting_value_from_dev_env_with_success(self):
dev_settings = Settings(file='./tests/config/settings_to_test.conf')
self.assertEqual(dev_settings.get('app_var'), 'dev-app-var')

@mock.patch.dict(os.environ, {'name': 'value'}, clear=True)
def test_get_value_from_env_var_with_success(self):
self.assertEqual(self.settings.get_from_env('name'), 'value')

@mock.patch.dict(os.environ, {'name1': 'value'}, clear=True)
def test_get_default_value_from_env_var_with_success(self):
self.assertEqual(self.settings.get_from_env('name', 'default'), 'default')
Loading