Skip to content

Commit d785d9d

Browse files
committed
Add tests for RepositoryString
1 parent a1e856c commit d785d9d

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

tests/test_string.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# coding: utf-8
2+
import os
3+
import pytest
4+
from decouple import Config, RepositoryString, UndefinedValueError
5+
6+
ENVSTRING = '''
7+
KeyTrue=True\nKeyOne=1\nKeyYes=yes
8+
KeyY=y
9+
KeyOn=on
10+
11+
KeyFalse=False
12+
KeyZero=0
13+
KeyNo=no
14+
KeyN=n
15+
KeyOff=off
16+
KeyEmpty=
17+
18+
# CommentedKey=None
19+
KeyWithSpaces = Some Value With Spaces
20+
KeyWithQuotes="Quoted Value"
21+
'''
22+
23+
24+
@pytest.fixture(scope='module')
25+
def config():
26+
return Config(RepositoryString(ENVSTRING))
27+
28+
29+
def test_string_comment(config):
30+
with pytest.raises(UndefinedValueError):
31+
config('CommentedKey')
32+
33+
34+
def test_string_bool_true(config):
35+
assert config('KeyTrue', cast=bool)
36+
assert config('KeyOne', cast=bool)
37+
assert config('KeyYes', cast=bool)
38+
assert config('KeyY', cast=bool)
39+
assert config('KeyOn', cast=bool)
40+
41+
42+
def test_string_bool_false(config):
43+
assert not config('KeyFalse', cast=bool)
44+
assert not config('KeyZero', cast=bool)
45+
assert not config('KeyNo', cast=bool)
46+
assert not config('KeyOff', cast=bool)
47+
assert not config('KeyN', cast=bool)
48+
assert not config('KeyEmpty', cast=bool)
49+
50+
51+
def test_string_undefined(config):
52+
with pytest.raises(UndefinedValueError):
53+
config('UndefinedKey')
54+
55+
56+
def test_string_default_none(config):
57+
assert config('UndefinedKey', default=None) is None
58+
59+
60+
def test_string_default_bool(config):
61+
assert not config('UndefinedKey', default=False, cast=bool)
62+
assert config('UndefinedKey', default=True, cast=bool)
63+
64+
65+
def test_string_default(config):
66+
assert not config('UndefinedKey', default=False)
67+
assert config('UndefinedKey', default=True)
68+
69+
70+
def test_string_default_invalid_bool(config):
71+
with pytest.raises(ValueError):
72+
config('UndefinedKey', default='NotBool', cast=bool)
73+
74+
75+
def test_string_empty(config):
76+
assert config('KeyEmpty', default=None) == ''
77+
78+
79+
def test_string_support_space(config):
80+
assert config('KeyWithSpaces') == 'Some Value With Spaces'
81+
82+
83+
def test_string_os_environ(config):
84+
os.environ['KeyOverrideByEnv'] = 'This'
85+
assert config('KeyOverrideByEnv') == 'This'
86+
del os.environ['KeyOverrideByEnv']
87+
88+
89+
def test_string_undefined_but_present_in_os_environ(config):
90+
os.environ['KeyOnlyEnviron'] = ''
91+
assert config('KeyOnlyEnviron') == ''
92+
del os.environ['KeyOnlyEnviron']
93+
94+
95+
def test_string_empty_string_means_false(config):
96+
assert not config('KeyEmpty', cast=bool)
97+
98+
99+
def test_string_repo_keyerror(config):
100+
with pytest.raises(KeyError):
101+
config.repository['UndefinedKey']
102+
103+
104+
def test_string_quoted_value(config):
105+
assert config('KeyWithQuotes') == 'Quoted Value'

0 commit comments

Comments
 (0)