-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_z_integration.py
More file actions
154 lines (118 loc) · 4.3 KB
/
test_z_integration.py
File metadata and controls
154 lines (118 loc) · 4.3 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
143
144
145
146
147
148
149
150
151
152
153
154
import pytest
import configstore
DOTENV_CONTENTS = u'''
SECRET_KEY=1234dot
ENVIRONMENT=PRODUCTION
EMAIL_DEBUG=1
PROMO2=off
'''
DOTENV_EMPTY = u'''
'''
def test_envvars_override_dotenv(monkeypatch, tmp_path):
monkeypatch.setenv('ENVIRONMENT', 'STAGING')
path = tmp_path / 'config.env'
path.write_text(DOTENV_CONTENTS)
store = configstore.Store([
configstore.EnvVarBackend(),
configstore.DotenvBackend(str(path)),
])
secret_key = store.get_setting('SECRET_KEY')
environment = store.get_setting('ENVIRONMENT')
assert secret_key == '1234dot'
assert environment == 'STAGING'
def test_dotenv_overrides_envvars(monkeypatch, tmp_path):
monkeypatch.setenv('ENVIRONMENT', 'STAGING')
path = tmp_path / 'config.env'
path.write_text(DOTENV_CONTENTS)
store = configstore.Store([
configstore.DotenvBackend(str(path)),
configstore.EnvVarBackend(),
])
secret_key = store.get_setting('SECRET_KEY')
environment = store.get_setting('ENVIRONMENT')
assert secret_key == '1234dot'
assert environment == 'PRODUCTION'
def test_dotenv_empty_file(monkeypatch, tmp_path):
monkeypatch.setenv('ENVIRONMENT', 'STAGING')
path = tmp_path / 'config.env'
path.write_text(DOTENV_EMPTY)
store = configstore.Store([
configstore.DotenvBackend(str(path)),
configstore.EnvVarBackend(),
])
with pytest.raises(Exception) as excinfo:
store.get_setting('DEBUG')
environment = store.get_setting('ENVIRONMENT')
assert "Couldn't find setting" in str(excinfo.value)
assert environment == 'STAGING'
def test_dict_defaults(monkeypatch):
defaults = {
'DEBUG': False,
'TIMEOUT': 100,
}
monkeypatch.setenv('TIMEOUT', '5')
monkeypatch.setenv('EMAIL', 'smtp://localhost')
store = configstore.Store([
configstore.EnvVarBackend(),
configstore.DictBackend(defaults),
])
debug = store.get_setting('DEBUG')
timeout = int(store.get_setting('TIMEOUT'))
email = store.get_setting('EMAIL')
assert debug is False
assert timeout == 5
assert email == 'smtp://localhost'
def test_interpolation(monkeypatch, tmp_path):
monkeypatch.setenv('ENVIRONMENT', 'STAGING')
monkeypatch.setenv('REDIS_HOST', 'redis')
monkeypatch.setenv('REDIS_PORT', '4012')
path = tmp_path / 'config.env'
path.write_text(DOTENV_CONTENTS)
path.write_text(
'APP_NAME=supertest-${ENVIRONMENT}-1\n'
# make sure more than one variable is possible
'REDIS_URL=https://${REDIS_HOST}:${REDIS_PORT}\n'
)
store = configstore.Store([
configstore.EnvVarBackend(),
configstore.DotenvBackend(str(path)),
])
environment = store.get_setting('ENVIRONMENT')
app_name = store.get_setting('APP_NAME')
session_url = store.get_setting('SESSION_URL', '${REDIS_URL}/2')
assert environment == 'STAGING'
assert app_name == 'supertest-STAGING-1'
assert session_url == 'https://redis:4012/2'
def test_conversion(monkeypatch, tmp_path):
monkeypatch.setenv('DEBUG', 'on')
path = tmp_path / 'config.env'
path.write_text(DOTENV_CONTENTS)
store = configstore.Store([
configstore.DotenvBackend(str(path)),
configstore.EnvVarBackend(),
])
# basic test
email_as_string = store.get_setting('EMAIL_DEBUG')
email_as_bool = store.get_setting('EMAIL_DEBUG', asbool=True)
assert email_as_string == '1'
assert email_as_bool is True
# setting found in store, value true
debug = store.get_setting('DEBUG', asbool=True)
# not found, default is boolean
promo1 = store.get_setting('PROMO1', True, asbool=True)
# found, value false
promo2 = store.get_setting('PROMO2', asbool=True)
# not found, default converted from string
promo3 = store.get_setting('PROMO3', '1', asbool=True)
# not found, default w/ interpolation, false
promo3 = store.get_setting('PROMO3', '${PROMO2}', asbool=True)
# not found, default w/ interpolation, true
extra_debug = store.get_setting('EXTRA_DEBUG', '${DEBUG}', asbool=True)
assert debug is True
assert promo1 is True
assert promo2 is False
assert promo3 is False
assert extra_debug is True
# error in conversion bubbles up
with pytest.raises(ValueError):
store.get_setting('ENVIRONMENT', asbool=True)