-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathtest_env.py
More file actions
142 lines (108 loc) · 3.76 KB
/
Copy pathtest_env.py
File metadata and controls
142 lines (108 loc) · 3.76 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# coding: utf-8
import os
import sys
from mock import patch
import pytest
from decouple import Config, RepositoryEnv, UndefinedValueError
# Useful for very coarse version differentiation.
PY3 = sys.version_info[0] == 3
if PY3:
from io import StringIO
else:
from io import BytesIO as StringIO
ENVFILE = '''
KeyTrue=True
KeyOne=1
KeyYes=yes
KeyOn=on
KeyY=y
KeyFalse=False
KeyZero=0
KeyNo=no
KeyN=n
KeyOff=off
KeyEmpty=
#CommentedKey=None
PercentNotEscaped=%%
NoInterpolation=%(KeyOff)s
IgnoreSpace = text
RespectSingleQuoteSpace = ' text'
RespectDoubleQuoteSpace = " text"
KeyOverrideByEnv=NotThis
KeyWithSingleQuoteEnd=text'
KeyWithSingleQuoteMid=te'xt
KeyWithSingleQuoteBegin='text
KeyWithDoubleQuoteEnd=text"
KeyWithDoubleQuoteMid=te"xt
KeyWithDoubleQuoteBegin="text
KeyIsSingleQuote='
KeyIsDoubleQuote="
KeyHasTwoSingleQuote="'Y'"
KeyHasTwoDoubleQuote='"Y"'
KeyHasMixedQuotesAsData1="Y'
KeyHasMixedQuotesAsData2='Y"
'''
@pytest.fixture(scope='module')
def config():
with patch('decouple.open', return_value=StringIO(ENVFILE), create=True):
return Config(RepositoryEnv('.env'))
def test_env_comment(config):
with pytest.raises(UndefinedValueError):
config('CommentedKey')
def test_env_percent_not_escaped(config):
assert '%%' == config('PercentNotEscaped')
def test_env_no_interpolation(config):
assert '%(KeyOff)s' == config('NoInterpolation')
def test_env_bool_true(config):
assert True is config('KeyTrue', cast=bool)
assert True is config('KeyOne', cast=bool)
assert True is config('KeyYes', cast=bool)
assert True is config('KeyOn', cast=bool)
assert True is config('KeyY', cast=bool)
assert True is config('Key1int', default=1, cast=bool)
def test_env_bool_false(config):
assert False is config('KeyFalse', cast=bool)
assert False is config('KeyZero', cast=bool)
assert False is config('KeyNo', cast=bool)
assert False is config('KeyOff', cast=bool)
assert False is config('KeyN', cast=bool)
assert False is config('KeyEmpty', cast=bool)
assert False is config('Key0int', default=0, cast=bool)
def test_env_os_environ(config):
os.environ['KeyOverrideByEnv'] = 'This'
assert 'This' == config('KeyOverrideByEnv')
del os.environ['KeyOverrideByEnv']
def test_env_undefined_but_present_in_os_environ(config):
os.environ['KeyOnlyEnviron'] = ''
assert '' == config('KeyOnlyEnviron')
del os.environ['KeyOnlyEnviron']
def test_env_undefined(config):
with pytest.raises(UndefinedValueError):
config('UndefinedKey')
def test_env_default_none(config):
assert None is config('UndefinedKey', default=None)
def test_env_empty(config):
assert None is config('KeyEmpty', default=None)
assert '' == config('KeyEmpty')
def test_env_support_space(config):
assert 'text' == config('IgnoreSpace')
assert ' text' == config('RespectSingleQuoteSpace')
assert ' text' == config('RespectDoubleQuoteSpace')
def test_env_empty_string_means_false(config):
assert False is config('KeyEmpty', cast=bool)
def test_env_with_quote(config):
assert "text'" == config('KeyWithSingleQuoteEnd')
assert 'text"' == config('KeyWithDoubleQuoteEnd')
assert "te'xt" == config('KeyWithSingleQuoteMid')
assert "'text" == config('KeyWithSingleQuoteBegin')
assert 'te"xt' == config('KeyWithDoubleQuoteMid')
assert '"text' == config('KeyWithDoubleQuoteBegin')
assert '"' == config('KeyIsDoubleQuote')
assert "'" == config('KeyIsSingleQuote')
assert "'Y'" == config('KeyHasTwoSingleQuote')
assert '"Y"' == config('KeyHasTwoDoubleQuote')
assert '''"Y\'''' == config('KeyHasMixedQuotesAsData1')
assert '''\'Y"''' == config('KeyHasMixedQuotesAsData2')
def test_env_repo_keyerror(config):
with pytest.raises(KeyError):
config.repository['UndefinedKey']