-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using configparser instead of dynaconf to manage the settings
- Loading branch information
1 parent
7b94289
commit d4b9254
Showing
8 changed files
with
94 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[default] | ||
app_name=python-boilerplate-project | ||
|
||
[development] | ||
|
||
|
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,8 +1,24 @@ | ||
from dynaconf import Dynaconf | ||
|
||
settings = Dynaconf( | ||
settings_files=["./settings.toml"], | ||
environments=True, | ||
load_dotenv=True, | ||
env_switcher="ENV", | ||
) | ||
from configparser import ConfigParser | ||
from os import getenv | ||
from typing import Any | ||
|
||
|
||
class Settings: | ||
__slots__ = ['config_parser', 'env'] | ||
config_parser: ConfigParser | ||
env: str | ||
|
||
def __init__(self, file: str = 'settings.conf'): | ||
self.config_parser = ConfigParser() | ||
self.config_parser.read(file) | ||
self.env = getenv('ENV', 'development') | ||
|
||
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_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] | ||
return None | ||
|
||
settings = Settings() |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[default] | ||
app_name=app-name | ||
app_var=default-app-var | ||
sample_of_int_var=10 | ||
sample_of_float_var=10.10 | ||
|
||
[development] | ||
app_var=development-app-var | ||
|
||
[test] | ||
app_var=test-app-var | ||
|
||
[qa] | ||
app_var=qa-app-var | ||
|
||
[prod] | ||
app_var=prod-app-var |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
from unittest import TestCase, mock | ||
|
||
from src.config.settings import Settings | ||
|
||
|
||
class SettingsTest(TestCase): | ||
def setUp(self) -> None: | ||
self.settings = Settings(file='./tests/config/settings_to_test.conf') | ||
|
||
def test_get_setting_value_with_success(self): | ||
self.assertEqual(self.settings.get('app_name'), 'app-name') | ||
|
||
def test_get_default_value_when_setting_not_found_with_success(self): | ||
self.assertEqual(self.settings.get('not_found_var', 'default_value'), 'default_value') | ||
|
||
def test_get_setting_int_value_with_success(self): | ||
self.assertEqual(int(self.settings.get('sample_of_int_var')), 10) | ||
|
||
def test_get_setting_float_value_with_success(self): | ||
self.assertEqual(float(self.settings.get('sample_of_float_var')), 10.10) | ||
|
||
@mock.patch.dict(os.environ, {'ENV': 'prod'}, clear=True) | ||
def test_get_setting_value_from_production_env_with_success(self): | ||
prod_settings = Settings(file='./tests/config/settings_to_test.conf') | ||
self.assertEqual(prod_settings.get('app_var'), 'prod-app-var') | ||
|
||
@mock.patch.dict(os.environ, {'ENV': 'qa'}, clear=True) | ||
def test_get_setting_value_from_qa_env_with_success(self): | ||
qa_settings = Settings(file='./tests/config/settings_to_test.conf') | ||
self.assertEqual(qa_settings.get('app_var'), 'qa-app-var') | ||
|
||
@mock.patch.dict(os.environ, {'ENV': 'test'}, clear=True) | ||
def test_get_setting_value_from_test_env_with_success(self): | ||
test_settings = Settings(file='./tests/config/settings_to_test.conf') | ||
self.assertEqual(test_settings.get('app_var'), 'test-app-var') | ||
|
||
@mock.patch.dict(os.environ, {'ENV': 'development'}, clear=True) | ||
def test_get_setting_value_from_development_env_with_success(self): | ||
dev_settings = Settings(file='./tests/config/settings_to_test.conf') | ||
self.assertEqual(dev_settings.get('app_var'), 'development-app-var') |