Skip to content

Commit

Permalink
added environ tests that loads types. Closes #387
Browse files Browse the repository at this point in the history
  • Loading branch information
liiight committed Nov 13, 2020
1 parent d6733b9 commit df2a7c0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 4 additions & 2 deletions notifiers/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}


Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
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
from notifiers.utils.helpers import text_to_bool
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",
Expand Down Expand Up @@ -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"
)

0 comments on commit df2a7c0

Please sign in to comment.