Skip to content

Commit

Permalink
join utils in decouple
Browse files Browse the repository at this point in the history
  • Loading branch information
ZandorSabino committed Dec 12, 2021
1 parent 90b0bb8 commit 7e90cd3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
14 changes: 12 additions & 2 deletions decouple.py
Expand Up @@ -5,7 +5,6 @@
from shlex import shlex
from io import open
from collections import OrderedDict
from util import strtobool

# Useful for very coarse version differentiation.
PYVERSION = sys.version_info
Expand All @@ -26,6 +25,18 @@

DEFAULT_ENCODING = 'UTF-8'


def strtobool(value):
_value = value.lower()
if _value in {"y", "yes", "t", "true", "on", "1"}:
result = True
elif _value in {"n", "no", "f", "false", "off", "0"}:
result = False
else:
raise ValueError(" ".join(("invalid truth value", value)))
return result


class UndefinedValueError(Exception):
pass

Expand Down Expand Up @@ -261,7 +272,6 @@ def __init__(self, flat=None, cast=text_type, choices=None):
self._valid_values.extend(self.flat)
self._valid_values.extend([value for value, _ in self.choices])


def __call__(self, value):
transform = self.cast(value)
if transform not in self._valid_values:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_strtobool.py
@@ -0,0 +1,28 @@
import pytest
from decouple import strtobool


def test_true_values():
true_list = ["y", "yes", "t", "true", "on", "1"]
for item in true_list:
assert strtobool(item) == 1


def test_false_values():
false_list = ["n", "no", "f", "false", "off", "0"]
for item in false_list:
assert strtobool(item) == 0


@pytest.mark.parametrize(
"test_input,expected",
[
("Invalid_Value_1", "invalid truth value Invalid_Value_1"),
("1nv4l1d_V4lu3_2", "invalid truth value 1nv4l1d_V4lu3_2"),
("invalid_value_3", "invalid truth value invalid_value_3"),
],
)
def test_eval(test_input, expected):
with pytest.raises(ValueError) as execinfo:
strtobool(test_input)
assert str(execinfo.value) == expected
21 changes: 0 additions & 21 deletions tests/test_util.py

This file was deleted.

9 changes: 0 additions & 9 deletions util.py

This file was deleted.

0 comments on commit 7e90cd3

Please sign in to comment.