-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EnvironmentConfiguration is now a domain class that exposes a single get(key) method. It will parse environment variables in the form LONGITUDE__PARENT_OBJECT__CHILD_OBJECT__VALUE=42 as {'parent_object': {'child_object': {'value': 42 } } It also allows to recover the values using nested keys ('.' is the joiner): Config.get('parent_object.child_object.value') returns 42 (as integer). Also, if a value can be parsed as integer, it will be parsed.
- Loading branch information
Dani Ramirez
committed
Feb 5, 2019
1 parent
11029de
commit ad0feb7
Showing
4 changed files
with
100 additions
and
70 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
File renamed without changes.
52 changes: 29 additions & 23 deletions
52
longitude/core/tests/test_environment_configuration_dictionary.py
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,32 +1,38 @@ | ||
from unittest import TestCase, mock | ||
from longitude.core.common.config import EnvironmentConfiguration | ||
from longitude.core.common.config import EnvironmentConfiguration as Config | ||
|
||
fake_environment = { | ||
'PATATUELA_RULES': 'my_root_value' | ||
'LONGITUDE__PARENT__CHILD__VALUE_A': '42', | ||
'LONGITUDE__PARENT__CHILD__VALUE_B': 'wut', | ||
'LONGITUDE__VALUE_A': '8008' | ||
} | ||
|
||
|
||
@mock.patch.dict('longitude.core.common.config.os.environ', fake_environment) | ||
class TestConfigurationDictionary(TestCase): | ||
|
||
@mock.patch.dict('longitude.core.common.config.os.environ', fake_environment) | ||
def test_base(self): | ||
d = EnvironmentConfiguration({ | ||
'root_patatuela': '=PATATUELA_RULES', | ||
'patata': 'patata value', | ||
'potato': 'potato value', | ||
'potatoes': [ | ||
'potato A', 'poteito B' | ||
], | ||
'potato_sack': { | ||
'colour': 'meh', | ||
'taste': 'buah', | ||
'texture': { | ||
'external': 'oh no', | ||
'internal': 'omg', | ||
'bumpiness': '=SOME_VALUE_FOR_BUMPINESS' | ||
} | ||
} | ||
}) | ||
def test_existing_values_return_strings_or_integers(self): | ||
self.assertEqual(42, Config.get('parent.child.value_a')) | ||
self.assertEqual('wut', Config.get('parent.child.value_b')) | ||
self.assertEqual(8008, Config.get('value_a')) | ||
|
||
self.assertEqual('my_root_value', d['root_patatuela']) | ||
self.assertEqual('=SOME_VALUE_FOR_BUMPINESS [NOT FOUND]', d['potato_sack']['texture']['bumpiness']) | ||
def test_non_existing_values_return_none(self): | ||
self.assertEqual(None, Config.get('wrong_value')) | ||
self.assertEqual(None, Config.get('wrong_parent.child.value')) | ||
self.assertEqual(None, Config.get('parent.wrong_child.value')) | ||
self.assertEqual(None, Config.get('parent.child.wrong_value')) | ||
self.assertEqual(None, Config.get('parent.wrong_child')) | ||
|
||
def test_existing_nested_values_return_dictionaries(self): | ||
fake_config = { | ||
'parent': | ||
{'child': | ||
{ | ||
'value_a': 42, | ||
'value_b': 'wut' | ||
} | ||
}, | ||
'value_a': 8008 | ||
} | ||
self.assertEqual(fake_config, Config.get()) | ||
self.assertEqual(fake_config['parent']['child'], Config.get('parent.child')) |
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