-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_getters.py
123 lines (89 loc) · 4.86 KB
/
test_getters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
from unittest import mock
import pytest
from enva.getters import get_env_bool, get_env_float, get_env_int, get_env_with_fallback
class TestGetEnvWithFallback:
@mock.patch.dict(os.environ, {"TEST_ENV1": "value1", "TEST_ENV2": "value2"})
def test_retrieves_primary_value_if_set(self):
assert get_env_with_fallback("TEST_ENV1", "TEST_ENV2") == "value1"
@mock.patch.dict(os.environ, {"TEST_ENV2": "value2"})
def test_retrieves_secondary_value_if_primary_not_set(self):
assert get_env_with_fallback("TEST_ENV1", "TEST_ENV2") == "value2"
def test_returns_None_if_no_variables_Set(self):
assert get_env_with_fallback("TEST_ENV1", "TEST_ENV2") is None
class TestGetEnvInt:
@mock.patch.dict(os.environ, {"SOME_VAR": "10"})
def test_get_env_int_returns_value_as_int_if_valid(self):
assert get_env_int("SOME_VAR") == 10
def test_get_env_int_returns_None_if_var_non_existent(self):
assert get_env_int("SOME_VAR") is None
@mock.patch.dict(os.environ, {"SOME_VAR": "10"})
def test_get_env_int_returns_env_var_value_over_default(self):
assert get_env_int("SOME_VAR", 12) == 10
def test_get_env_int_returns_default_if_var_non_existent_and_default_provided(self):
assert get_env_int("SOME_VAR", 12) == 12
@mock.patch.dict(os.environ, {"SOME_VAR": "ten"})
def test_get_env_int_raises_if_conversion_error_and_raise_flag_provided(self):
with pytest.raises(TypeError):
get_env_int("SOME_VAR", raise_typeerror=True)
def test_get_env_int_raises_if_env_var_non_existent_and_raise_flag_provided(self):
with pytest.raises(TypeError):
get_env_int("SOME_VAR", raise_typeerror=True)
def test_get_env_int_returns_default_even_if_raise_flag_provided(self):
assert get_env_int("SOME_VAR", 15, raise_typeerror=True) == 15
class TestGetEnvFloat:
@mock.patch.dict(os.environ, {"SOME_VAR": "10.8"})
def test_get_env_float_returns_value_as_float_if_valid(self):
assert get_env_float("SOME_VAR") == 10.8
def test_get_env_float_returns_None_if_var_non_existent(self):
assert get_env_float("SOME_VAR") is None
@mock.patch.dict(os.environ, {"SOME_VAR": "10"})
def test_get_env_float_returns_env_var_value_over_default(self):
assert get_env_float("SOME_VAR", 12.2) == 10.0
def test_get_env_float_returns_default_if_var_non_existent_and_default_provided(
self,
):
assert get_env_float("SOME_VAR", 12.5) == 12.5
@mock.patch.dict(os.environ, {"SOME_VAR": "ten"})
def test_get_env_float_raises_if_conversion_error_and_raise_flag_provided(self):
with pytest.raises(TypeError):
get_env_float("SOME_VAR", raise_typeerror=True)
def test_get_env_float_raises_if_env_var_non_existent_and_raise_flag_provided(self):
with pytest.raises(TypeError):
get_env_float("SOME_VAR", raise_typeerror=True)
def test_get_env_float_returns_default_even_if_raise_flag_provided(self):
assert get_env_float("SOME_VAR", 15.5, raise_typeerror=True) == 15.5
class TestGetEnvBool:
@mock.patch.dict(os.environ, {"SOME_VAR": "True"})
def test_get_env_bool_returns_value_as_bool_if_valid(self):
assert get_env_bool("SOME_VAR") is True
@mock.patch.dict(os.environ, {"SOME_VAR": "1"})
def test_get_env_bool_returns_1_as_True(self):
assert get_env_bool("SOME_VAR") is True
@mock.patch.dict(os.environ, {"SOME_VAR": "False"})
def test_get_env_bool_returns_False_string_as_False(self):
assert get_env_bool("SOME_VAR") is False
@mock.patch.dict(os.environ, {"SOME_VAR": "0"})
def test_get_env_bool_returns_0_as_False(self):
assert get_env_bool("SOME_VAR") is False
@mock.patch.dict(os.environ, {"SOME_VAR": "10"})
def test_get_env_bool_returns_string_as_None(self):
assert get_env_bool("SOME_VAR") is None
def test_get_env_bool_returns_None_if_var_non_existent(self):
assert get_env_bool("SOME_VAR") is None
@mock.patch.dict(os.environ, {"SOME_VAR": "True"})
def test_get_env_bool_returns_env_var_value_over_default(self):
assert get_env_bool("SOME_VAR", False) is True
def test_get_env_bool_returns_default_if_var_non_existent_and_default_provided(
self,
):
assert get_env_bool("SOME_VAR", False) == False
@mock.patch.dict(os.environ, {"SOME_VAR": "ten"})
def test_get_env_bool_raises_if_conversion_error_and_raise_flag_provided(self):
with pytest.raises(TypeError):
get_env_bool("SOME_VAR", raise_typeerror=True)
def test_get_env_bool_raises_if_env_var_non_existent_and_raise_flag_provided(self):
with pytest.raises(TypeError):
get_env_bool("SOME_VAR", raise_typeerror=True)
def test_get_env_bool_returns_default_even_if_raise_flag_provided(self):
assert get_env_bool("SOME_VAR", False, raise_typeerror=True) == False