-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_state.py
179 lines (138 loc) · 4.44 KB
/
test_state.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import warnings
import pytest
import deal
import deal.introspection
from deal._imports import deactivate
from deal._state import state
from .test_runtime.helpers import run_sync
pytestmark = pytest.mark.filterwarnings('ignore:It is pytest but deal is disabled')
@pytest.fixture
def restore_state():
state.reset()
yield
state.removed = False
state.reset()
deactivate()
def count_contracts(func) -> int:
return len(list(deal.introspection.get_contracts(func)))
def test_contract_state_switch_custom_param(restore_state):
func = deal.pre(lambda x: x > 0)(lambda x: x * 2)
deal.disable()
func(-2)
deal.enable()
with pytest.raises(deal.PreContractError):
func(-2)
def test_contract_state_switch_default_param(restore_state):
func = deal.pre(lambda x: x > 0)(lambda x: x * 2)
deal.disable()
assert func(-2) == -4
deal.enable()
with pytest.raises(deal.PreContractError):
func(-2)
def test_contract_state_switch_default_param_async(restore_state):
@deal.pre(lambda x: x > 0)
async def func(x):
return x * 2
deal.disable()
assert run_sync(func(-2)) == -4
deal.enable()
with pytest.raises(deal.PreContractError):
run_sync(func(-2))
def test_contract_state_switch_default_param_generator(restore_state):
@deal.pre(lambda x: x > 0)
def func(x):
yield x * 2
deal.disable()
assert list(func(-2)) == [-4]
deal.enable()
with pytest.raises(deal.PreContractError):
list(func(-2))
def test_state_disable_permament(restore_state):
@deal.pre(lambda x: x > 0)
@deal.inherit
@deal.pure
def func1(x):
yield x * 2
deal.disable(permament=True)
@deal.pre(lambda x: x > 0)
@deal.inherit
@deal.pure
def func2(x):
yield x * 2
assert count_contracts(func1) == 3
assert count_contracts(func2) == 0
def test_state_disable_permament__cant_disable_twice(restore_state):
deal.disable(permament=True)
with pytest.raises(RuntimeError):
deal.disable(permament=True)
with pytest.raises(RuntimeError):
deal.enable()
with pytest.raises(RuntimeError):
deal.reset()
def test_state_switch_module_load(restore_state):
with pytest.raises(RuntimeError):
deal.module_load()
deal.disable()
deal.activate()
deal.module_load()
def test_state_switch_module_load_debug(restore_state):
with pytest.raises(RuntimeError):
deal.module_load()
deal.disable()
deal.activate()
deal.enable()
def test_state_switch_activate(restore_state):
assert deal.activate()
assert deactivate()
deal.disable()
assert not deal.activate()
@pytest.fixture
def set_env_vars():
old_vars = os.environ.copy()
yield os.environ.update
os.environ.clear()
os.environ.update(old_vars)
@pytest.mark.parametrize('env_vars, expected', [
(dict(), None),
(dict(CI='true'), None),
(dict(GCLOUD_PROJECT='example'), 'It is GCP but deal is enabled'),
(dict(LAMBDA_TASK_ROOT='/home/'), 'It is AWS but deal is enabled'),
])
def test_enable__warnings(restore_state, env_vars, set_env_vars, expected):
os.environ.clear()
set_env_vars(env_vars)
if expected:
with pytest.warns(RuntimeWarning) as warns:
deal.enable()
assert len(warns) == 1
assert str(warns[0].message) == f'{expected}. Is it intentional?'
else:
with warnings.catch_warnings():
warnings.simplefilter('error')
deal.enable()
with warnings.catch_warnings():
warnings.simplefilter('error')
deal.enable(warn=False)
@pytest.mark.parametrize('env_vars, expected', [
(dict(), None),
(dict(GCLOUD_PROJECT='example'), None),
(dict(LAMBDA_TASK_ROOT='/home/'), None),
(dict(CI='true'), 'It is CI but deal is disabled'),
(dict(PYTEST_CURRENT_TEST='test_example'), 'It is pytest but deal is disabled'),
])
def test_disable__warnings(restore_state, env_vars, set_env_vars, expected):
os.environ.clear()
set_env_vars(env_vars)
if expected:
with pytest.warns(RuntimeWarning) as warns:
deal.disable()
assert len(warns) == 1
assert str(warns[0].message) == f'{expected}. Is it intentional?'
else:
with warnings.catch_warnings():
warnings.simplefilter('error')
deal.disable()
with warnings.catch_warnings():
warnings.simplefilter('error')
deal.disable(warn=False)