Skip to content

Commit

Permalink
Black fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xfenix committed Oct 18, 2021
1 parent 14f2249 commit 276fe75
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions envcast/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""All tests gathers here."""
from __future__ import annotations

import decimal
import pathlib
import random
Expand Down Expand Up @@ -39,7 +40,9 @@ def generic_assert(tested_value, original_value, key_exists, desired_type):
"""Generic assert fn."""
if key_exists:
if desired_type == bool:
assert tested_value == bool(original_value.lower().strip() in envcast.env.BOOLEAN_VALUES)
assert tested_value == bool(
original_value.lower().strip() in envcast.env.BOOLEAN_VALUES
)
else:
assert tested_value == original_value
else:
Expand Down Expand Up @@ -67,7 +70,9 @@ def test_parse_osgetenv_good_and_bad(monkeypatch, desired_type, key_exists) -> N
@pytest.mark.parametrize("desired_type", FAKE_TYPES_MAP.keys())
@pytest.mark.parametrize("key_exists", (True, False))
@pytest.mark.parametrize("broken_equal_sign", (True, False))
def test_parse_dotenv_good_and_bad(monkeypatch, desired_type, key_exists, broken_equal_sign) -> None:
def test_parse_dotenv_good_and_bad(
monkeypatch, desired_type, key_exists, broken_equal_sign
) -> None:
"""Test for .env provider."""
dotenv_fn: envcast.base.DotEnvProcessor = envcast.base.DotEnvProcessor()
env_key: str = f"TEST_KEY_{FAKE_GEN.pystr()}"
Expand All @@ -85,7 +90,9 @@ def test_parse_dotenv_good_and_bad(monkeypatch, desired_type, key_exists, broken
monkeypatch.setattr("pathlib.Path.is_file", lambda x: True)
dotenv_fn.set_dotenv_path(".")
try:
generic_assert(dotenv_fn(env_key, type_cast=desired_type), original_value, key_exists, desired_type)
generic_assert(
dotenv_fn(env_key, type_cast=desired_type), original_value, key_exists, desired_type
)
except envcast.exceptions.BrokenDotenvStructure:
assert broken_equal_sign

Expand All @@ -95,15 +102,23 @@ def test_parse_dotenv_good_and_bad(monkeypatch, desired_type, key_exists, broken
@pytest.mark.parametrize("separator", sorted(tuple(envcast.env.SEPARATORS_FOR_LIST_TYPE)))
def test_list_types(monkeypatch, desired_type, desired_sub_type, separator) -> None:
"""Test with list based variables."""
generated_values: list = desired_type([FAKE_TYPES_MAP[desired_sub_type]() for _ in range(FAKE_GEN.pyint(10, 50))])
generated_values: list = desired_type(
[FAKE_TYPES_MAP[desired_sub_type]() for _ in range(FAKE_GEN.pyint(10, 50))]
)
env_key: str = f"HEYPRIVET_KAKDELA_A_{FAKE_GEN.pystr()}"
monkeypatch.setenv(
env_key, separator.join([cast_to_str(one_item, desired_sub_type) for one_item in generated_values])
env_key,
separator.join([cast_to_str(one_item, desired_sub_type) for one_item in generated_values]),
)
tested_value: typing.Any = envcast.env(
env_key, type_cast=desired_type, list_type_cast=desired_sub_type
)
tested_value: typing.Any = envcast.env(env_key, type_cast=desired_type, list_type_cast=desired_sub_type)
if desired_sub_type == bool:
generated_values = desired_type(
[bool(one_item.lower().strip() in envcast.env.BOOLEAN_VALUES) for one_item in generated_values]
[
bool(one_item.lower().strip() in envcast.env.BOOLEAN_VALUES)
for one_item in generated_values
]
)
assert tested_value == generated_values

Expand All @@ -118,27 +133,31 @@ def test_list_types_with_empty_list(monkeypatch) -> None:
"""Test with list based variables."""
desired_type: type = list
desired_sub_type: type = int
generated_values: list = desired_type([FAKE_TYPES_MAP[desired_sub_type]() for _ in range(FAKE_GEN.pyint(10, 50))])
generated_values: list = desired_type(
[FAKE_TYPES_MAP[desired_sub_type]() for _ in range(FAKE_GEN.pyint(10, 50))]
)
env_key: str = f"HEYPRIVET_KAKDELA_A_{FAKE_GEN.pystr()}"
monkeypatch.setenv(env_key, "@".join([str(one_item) for one_item in generated_values]))
tested_value: typing.Any = envcast.env(env_key, type_cast=desired_type, list_type_cast=desired_sub_type)
tested_value: typing.Any = envcast.env(
env_key, type_cast=desired_type, list_type_cast=desired_sub_type
)
assert tested_value == []


@pytest.mark.parametrize("test_dotenv_path", (".", "/", "nonexistent"))
@pytest.mark.parametrize("fail_method", ("pathlib.Path.exists", "pathlib.Path.is_file"))
def test_parse_dotenv_setdotenv_path_method(monkeypatch, test_dotenv_path, fail_method) -> None:
"""Test exception with bad test env path."""
monkeypatch.setattr(fail_method, lambda x: False)
monkeypatch.setattr(fail_method, lambda _: False)
with pytest.raises(envcast.exceptions.IncorrectDotenvPath):
envcast.base.DotEnvProcessor().set_dotenv_path(test_dotenv_path)


def test_parse_dotenv_with_is_a_directory_error(monkeypatch) -> None:
"""Test exception with is a directory fail."""
dotenv_fn: envcast.base.DotEnvProcessor = envcast.base.DotEnvProcessor()
monkeypatch.setattr("pathlib.Path.exists", lambda x: True)
monkeypatch.setattr("pathlib.Path.is_file", lambda x: True)
monkeypatch.setattr("pathlib.Path.exists", lambda _: True)
monkeypatch.setattr("pathlib.Path.is_file", lambda _: True)
monkeypatch.setattr("pathlib.Path.read_text", mock.Mock(side_effect=IsADirectoryError))
dotenv_fn.set_dotenv_path(".")
with pytest.raises(envcast.exceptions.IncorrectDotenvPath):
Expand Down

0 comments on commit 276fe75

Please sign in to comment.