From 44dd0e679c3db4722ced451d4716f80fe8a36e1f Mon Sep 17 00:00:00 2001 From: Tomas Bedrich Date: Tue, 9 Oct 2018 14:46:53 +0200 Subject: [PATCH] Remove conversion of default value, update tests accordingly --- llconfig/__init__.py | 4 +-- llconfig/converters.py | 9 ++---- tests/__init__.py | 16 ++++++++++ tests/test_converters.py | 61 ++++++++++++++++++++++----------------- tests/test_environment.py | 20 ++----------- 5 files changed, 56 insertions(+), 54 deletions(-) create mode 100644 tests/__init__.py diff --git a/llconfig/__init__.py b/llconfig/__init__.py index 2f2c65f..d0659d9 100644 --- a/llconfig/__init__.py +++ b/llconfig/__init__.py @@ -71,7 +71,7 @@ def init(self, key: str, converter: Callable[[str], Any], default=None): Args: key: Case-sensitive directive name which is used everywhere (in env vars, in config files, in defaults). converter: Function, which is called when converting env variable value to Python. - default: Directive default value. Converter is applied on it. + default: Directive default value. """ if key == self.config_files_env_var: raise KeyError('Conflict between directive name and `config_files_env_var` name.') @@ -79,7 +79,7 @@ def init(self, key: str, converter: Callable[[str], Any], default=None): _check_safe_env_name(key) self._loaded = False - self._default_layer[key] = converter(default) + self._default_layer[key] = default self._converters[key] = converter if converter == bool: diff --git a/llconfig/converters.py b/llconfig/converters.py index 3b2f1ed..2f09408 100644 --- a/llconfig/converters.py +++ b/llconfig/converters.py @@ -7,11 +7,6 @@ def bool_like(val): def json(val): """ - A convenient converter that JSON-deserializes strings or checks if Python - object is JSON-serializable. + A convenient converter that JSON-deserializes strings. """ - if isinstance(val, str): - return _json.loads(val, encoding='utf-8') - else: - _json.dumps(val) - return val + return _json.loads(val, encoding='utf-8') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..c4f887c --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,16 @@ +import contextlib +import os + + +@contextlib.contextmanager +def env(**kwargs): + original = {key: os.getenv(key) for key in kwargs} + os.environ.update({key: str(value) for key, value in kwargs.items()}) + try: + yield + finally: + for key, value in original.items(): + if value is None: + del os.environ[key] + else: + os.environ[key] = value diff --git a/tests/test_converters.py b/tests/test_converters.py index 12cdffd..58a0107 100644 --- a/tests/test_converters.py +++ b/tests/test_converters.py @@ -1,15 +1,18 @@ -import pytest from decimal import Decimal +import pytest + from llconfig import Config from llconfig.converters import bool_like, json +from tests import env @pytest.mark.parametrize('_type', [int, float, complex, Decimal]) def test_python_numeric_types_converters(_type): - c = Config() - c.init('VALUE', _type, '1') - c.load() + c = Config(env_prefix='') + c.init('VALUE', _type) + with env(VALUE='1'): + c.load() assert c['VALUE'] == _type('1') assert c['VALUE'] == 1 @@ -18,39 +21,41 @@ def test_python_numeric_types_converters(_type): @pytest.mark.parametrize('value', ['0', 'no', 'disabled', 'False', 'enabled']) def test_python_bool_type(value): - c = Config() + c = Config(env_prefix='') with pytest.warns(UserWarning): - c.init('VALUE', bool, value) - c.load() + c.init('VALUE', bool) + with env(VALUE=value): + c.load() assert c['VALUE'] is True @pytest.mark.parametrize('value', ['0', 'NO', 'disabled', 'Off', 'false']) def test_bool_like_false(value): - c = Config() - c.init('VALUE', bool_like, value) - c.load() + c = Config(env_prefix='') + c.init('VALUE', bool_like) + with env(VALUE=value): + c.load() assert c['VALUE'] is False @pytest.mark.parametrize('value', ['1', 'YES', 'enabled', 'True', 'on']) def test_bool_like_true(value): - c = Config() - c.init('VALUE', bool_like, value) - c.load() + c = Config(env_prefix='') + c.init('VALUE', bool_like) + with env(VALUE=value): + c.load() assert c['VALUE'] is True def test_json(): - c = Config() - c.init('VALUE1', json, '{"aha": [1, 1.2, false]}') - c.init('VALUE2', json, 1) - with pytest.raises(TypeError): - c.init('VALUE3', json, set()) - c.load() + c = Config(env_prefix='') + c.init('VALUE1', json) + c.init('VALUE2', json) + with env(VALUE1='{"aha": [1, 1.2, false]}', VALUE2='1'): + c.load() assert c['VALUE1'] == {'aha': [1, 1.2, False]} assert c['VALUE2'] == 1 @@ -60,14 +65,16 @@ def test_custom_converter(): def custom(value): return ... - c = Config() - c.init('VALUE', custom, 42) - c.load() - assert c['VALUE'] == ... + c = Config(env_prefix='') + c.init('VALUE', custom) + with env(VALUE='42'): + c.load() + assert c['VALUE'] is ... def test_custom_anonymous_converter(): - c = Config() - c.init('VALUE', lambda x: ..., 42) - c.load() - assert c['VALUE'] == ... + c = Config(env_prefix='') + c.init('VALUE', lambda x: ...) + with env(VALUE='42'): + c.load() + assert c['VALUE'] is ... diff --git a/tests/test_environment.py b/tests/test_environment.py index 353ffee..ef634b8 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -1,21 +1,5 @@ -import contextlib -import os - from llconfig import Config - - -@contextlib.contextmanager -def env(**kwargs): - original = {key: os.getenv(key) for key in kwargs} - os.environ.update({key: str(value) for key, value in kwargs.items()}) - try: - yield - finally: - for key, value in original.items(): - if value is None: - del os.environ[key] - else: - os.environ[key] = value +from tests import env def test_prefixed_variables_override(): @@ -47,7 +31,7 @@ def test_nonprefixed_variables_dont_override_nonempty_prefix(): def test_int_from_environ(): c = Config(env_prefix='PREFIX_') - c.init('NUM', int, 42) + c.init('NUM', int, 666) with env(PREFIX_NUM='42'): c.load()