Skip to content

Commit

Permalink
Remove conversion of default value, update tests accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbedrich committed Oct 9, 2018
1 parent 814c4a5 commit 44dd0e6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 54 deletions.
4 changes: 2 additions & 2 deletions llconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ 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.')

_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:
Expand Down
9 changes: 2 additions & 7 deletions llconfig/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
16 changes: 16 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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
61 changes: 34 additions & 27 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 ...
20 changes: 2 additions & 18 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 44dd0e6

Please sign in to comment.