-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
test_testutils.py
219 lines (144 loc) · 6.98 KB
/
test_testutils.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from __future__ import unicode_literals
from decimal import Decimal
from django.contrib.auth.models import AnonymousUser
from django.test import TransactionTestCase, RequestFactory
import waffle
from waffle.models import Switch, Flag, Sample
from waffle.testutils import override_switch, override_flag, override_sample
class OverrideSwitchTests(TransactionTestCase):
def test_switch_existed_and_was_active(self):
Switch.objects.create(name='foo', active=True)
with override_switch('foo', active=True):
assert waffle.switch_is_active('foo')
with override_switch('foo', active=False):
assert not waffle.switch_is_active('foo')
# make sure it didn't change 'active' value
assert Switch.objects.get(name='foo').active
def test_switch_existed_and_was_NOT_active(self):
Switch.objects.create(name='foo', active=False)
with override_switch('foo', active=True):
assert waffle.switch_is_active('foo')
with override_switch('foo', active=False):
assert not waffle.switch_is_active('foo')
# make sure it didn't change 'active' value
assert not Switch.objects.get(name='foo').active
def test_new_switch(self):
assert not Switch.objects.filter(name='foo').exists()
with override_switch('foo', active=True):
assert waffle.switch_is_active('foo')
with override_switch('foo', active=False):
assert not waffle.switch_is_active('foo')
assert not Switch.objects.filter(name='foo').exists()
def test_as_decorator(self):
assert not Switch.objects.filter(name='foo').exists()
@override_switch('foo', active=True)
def test_enabled():
assert waffle.switch_is_active('foo')
test_enabled()
@override_switch('foo', active=False)
def test_disabled():
assert not waffle.switch_is_active('foo')
test_disabled()
assert not Switch.objects.filter(name='foo').exists()
def test_restores_after_exception(self):
Switch.objects.create(name='foo', active=True)
def inner():
with override_switch('foo', active=False):
raise RuntimeError("Trying to break")
with self.assertRaises(RuntimeError):
inner()
assert Switch.objects.get(name='foo').active
def test_restores_after_exception_in_decorator(self):
Switch.objects.create(name='foo', active=True)
@override_switch('foo', active=False)
def inner():
raise RuntimeError("Trying to break")
with self.assertRaises(RuntimeError):
inner()
assert Switch.objects.get(name='foo').active
def req():
r = RequestFactory().get('/')
r.user = AnonymousUser()
return r
class OverrideFlagTests(TransactionTestCase):
def test_flag_existed_and_was_active(self):
Flag.objects.create(name='foo', everyone=True)
with override_flag('foo', active=True):
assert waffle.flag_is_active(req(), 'foo')
with override_flag('foo', active=False):
assert not waffle.flag_is_active(req(), 'foo')
assert Flag.objects.get(name='foo').everyone
def test_flag_existed_and_was_inactive(self):
Flag.objects.create(name='foo', everyone=False)
with override_flag('foo', active=True):
assert waffle.flag_is_active(req(), 'foo')
with override_flag('foo', active=False):
assert not waffle.flag_is_active(req(), 'foo')
assert Flag.objects.get(name='foo').everyone is False
def test_flag_existed_and_was_null(self):
Flag.objects.create(name='foo', everyone=None)
with override_flag('foo', active=True):
assert waffle.flag_is_active(req(), 'foo')
with override_flag('foo', active=False):
assert not waffle.flag_is_active(req(), 'foo')
assert Flag.objects.get(name='foo').everyone is None
def test_flag_did_not_exist(self):
assert not Flag.objects.filter(name='foo').exists()
with override_flag('foo', active=True):
assert waffle.flag_is_active(req(), 'foo')
with override_flag('foo', active=False):
assert not waffle.flag_is_active(req(), 'foo')
assert not Flag.objects.filter(name='foo').exists()
class OverrideSampleTests(TransactionTestCase):
def test_sample_existed_and_was_100(self):
Sample.objects.create(name='foo', percent='100.0')
with override_sample('foo', active=True):
assert waffle.sample_is_active('foo')
with override_sample('foo', active=False):
assert not waffle.sample_is_active('foo')
self.assertEquals(Decimal('100.0'),
Sample.objects.get(name='foo').percent)
def test_sample_existed_and_was_0(self):
Sample.objects.create(name='foo', percent='0.0')
with override_sample('foo', active=True):
assert waffle.sample_is_active('foo')
with override_sample('foo', active=False):
assert not waffle.sample_is_active('foo')
self.assertEquals(Decimal('0.0'),
Sample.objects.get(name='foo').percent)
def test_sample_existed_and_was_50(self):
Sample.objects.create(name='foo', percent='50.0')
with override_sample('foo', active=True):
assert waffle.sample_is_active('foo')
with override_sample('foo', active=False):
assert not waffle.sample_is_active('foo')
self.assertEquals(Decimal('50.0'),
Sample.objects.get(name='foo').percent)
def test_sample_did_not_exist(self):
assert not Sample.objects.filter(name='foo').exists()
with override_sample('foo', active=True):
assert waffle.sample_is_active('foo')
with override_sample('foo', active=False):
assert not waffle.sample_is_active('foo')
assert not Sample.objects.filter(name='foo').exists()
@override_switch('foo', active=False)
class OverrideSwitchOnClassTests(TransactionTestCase):
def setUp(self):
assert not Switch.objects.filter(name='foo').exists()
Switch.objects.create(name='foo', active=True)
def test_undecorated_method_is_set_properly_for_switch(self):
self.assertFalse(waffle.switch_is_active('foo'))
@override_flag('foo', active=False)
class OverrideFlagOnClassTests(TransactionTestCase):
def setUp(self):
assert not Flag.objects.filter(name='foo').exists()
Flag.objects.create(name='foo', everyone=True)
def test_undecorated_method_is_set_properly_for_flag(self):
self.assertFalse(waffle.flag_is_active(req(), 'foo'))
@override_sample('foo', active=False)
class OverrideSampleOnClassTests(TransactionTestCase):
def setUp(self):
assert not Sample.objects.filter(name='foo').exists()
Sample.objects.create(name='foo', percent='100.0')
def test_undecorated_method_is_set_properly_for_sample(self):
self.assertFalse(waffle.sample_is_active('foo'))