diff --git a/notifiers/utils/helpers.py b/notifiers/utils/helpers.py index 88d56c30..00983e19 100644 --- a/notifiers/utils/helpers.py +++ b/notifiers/utils/helpers.py @@ -44,10 +44,12 @@ def dict_from_environs(prefix: str, name: str, args: list) -> dict: """ # todo consider changing via the environs lib log.debug("starting to collect environs using prefix: '%s'", prefix) + prefix = f'{prefix.rstrip("_")}_'.upper() + name = f'{name.rstrip("_")}_'.upper() return { - arg: os.environ.get(f"{prefix}{name}_{arg}".upper()) + arg: os.environ.get(f"{prefix}{name}{arg}".upper()) for arg in args - if os.environ.get(f"{prefix}{name}_{arg}".upper()) + if os.environ.get(f"{prefix}{name}{arg}".upper()) } diff --git a/tests/conftest.py b/tests/conftest.py index 83e7c2d4..74d71278 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,7 +20,7 @@ from notifiers.providers import _all_providers from notifiers.utils.helpers import text_to_bool -log = logging.getLogger(__name__) +log = logging.getLogger("notifiers") class MockProxy: diff --git a/tests/test_utils.py b/tests/test_utils.py index 6b317231..a4b4ee77 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,6 @@ import pytest +from notifiers.models.schema import ResourceSchema from notifiers.utils.helpers import dict_from_environs from notifiers.utils.helpers import merge_dicts from notifiers.utils.helpers import snake_to_camel_case @@ -7,6 +8,14 @@ from notifiers.utils.requests import file_list_for_request +class TypeTest(ResourceSchema): + string = "" + integer = 0 + float_ = 0.1 + bool_ = True + bytes = b"" + + class TestHelpers: @pytest.mark.parametrize( "text, result", @@ -71,3 +80,21 @@ def test_file_list_for_request(self, tmp_path): file_list_2 = file_list_for_request([file_1, file_2], "foo", "foo_mimetype") assert len(file_list_2) == 2 assert all(len(member[1]) == 3 for member in file_list_2) + + def test_schema_from_environs(self, monkeypatch): + prefix = "NOTIFIERS" + name = "ENV_TEST" + env_data = { + "string": "foo", + "integer": "8", + "float_": "1.1", + "bool_": "true", + "bytes": "baz", + } + for key, value in env_data.items(): + monkeypatch.setenv(f"{prefix}_{name}_{key}".upper(), value) + + data = dict_from_environs(prefix, name, list(env_data)) + assert TypeTest.parse_obj(data) == TypeTest( + string="foo", integer=8, float_=1.1, bool_=True, bytes=b"baz" + )