forked from jazzband/django-waffle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_waffle.py
443 lines (355 loc) · 17.1 KB
/
test_waffle.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from __future__ import unicode_literals
import random
import threading
import unittest
from django.conf import settings
from django.contrib.auth.models import AnonymousUser, Group, User
from django.db import connection, transaction
from django.test import RequestFactory, TransactionTestCase
from django.test.utils import override_settings
import mock
import waffle
from test_app import views
from waffle.middleware import WaffleMiddleware
from waffle.models import Flag, Sample, Switch
from waffle.tests.base import TestCase
def get(**kw):
request = RequestFactory().get('/foo', data=kw)
request.user = AnonymousUser()
return request
def process_request(request, view):
response = view(request)
return WaffleMiddleware().process_response(request, response)
class WaffleTests(TestCase):
def test_persist_active_flag(self):
Flag.objects.create(name='myflag', percent='0.1')
request = get()
# Flag stays on.
request.COOKIES['dwf_myflag'] = 'True'
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' in response.cookies
self.assertEqual('True', response.cookies['dwf_myflag'].value)
def test_persist_inactive_flag(self):
Flag.objects.create(name='myflag', percent='99.9')
request = get()
# Flag stays off.
request.COOKIES['dwf_myflag'] = 'False'
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' in response.cookies
self.assertEqual('False', response.cookies['dwf_myflag'].value)
def test_no_set_unused_flag(self):
"""An unused flag shouldn't have its cookie reset."""
request = get()
request.COOKIES['dwf_unused'] = 'True'
response = process_request(request, views.flag_in_view)
assert 'dwf_unused' not in response.cookies
def test_superuser(self):
"""Test the superuser switch."""
Flag.objects.create(name='myflag', superusers=True)
request = get()
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
superuser = User(username='foo', is_superuser=True)
request.user = superuser
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies
non_superuser = User(username='bar', is_superuser=False)
non_superuser.save()
request.user = non_superuser
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
def test_staff(self):
"""Test the staff switch."""
Flag.objects.create(name='myflag', staff=True)
request = get()
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
staff = User(username='foo', is_staff=True)
request.user = staff
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies
non_staff = User(username='foo', is_staff=False)
non_staff.save()
request.user = non_staff
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
def test_languages(self):
Flag.objects.create(name='myflag', languages='en,fr')
request = get()
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
request.LANGUAGE_CODE = 'en'
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
request.LANGUAGE_CODE = 'de'
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
def test_user(self):
"""Test the per-user switch."""
user = User.objects.create(username='foo')
flag = Flag.objects.create(name='myflag')
flag.users.add(user)
request = get()
request.user = user
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies
request.user = User.objects.create(username='someone_else')
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
def test_group(self):
"""Test the per-group switch."""
group = Group.objects.create(name='foo')
user = User.objects.create(username='bar')
user.groups.add(group)
flag = Flag.objects.create(name='myflag')
flag.groups.add(group)
request = get()
request.user = user
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies
request.user = User(username='someone_else')
request.user.save()
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
def test_authenticated(self):
"""Test the authenticated/anonymous switch."""
Flag.objects.create(name='myflag', authenticated=True)
request = get()
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
request.user = User(username='foo')
assert request.user.is_authenticated
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies
def test_everyone_on(self):
"""Test the 'everyone' switch on."""
Flag.objects.create(name='myflag', everyone=True)
request = get()
request.COOKIES['dwf_myflag'] = 'False'
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies
request.user = User(username='foo')
assert request.user.is_authenticated
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
assert 'dwf_myflag' not in response.cookies
def test_everyone_off(self):
"""Test the 'everyone' switch off."""
Flag.objects.create(name='myflag', everyone=False,
authenticated=True)
request = get()
request.COOKIES['dwf_myflag'] = 'True'
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
request.user = User(username='foo')
assert request.user.is_authenticated
response = process_request(request, views.flag_in_view)
self.assertEqual(b'off', response.content)
assert 'dwf_myflag' not in response.cookies
def test_percent(self):
"""If you have no cookie, you get a cookie!"""
Flag.objects.create(name='myflag', percent='50.0')
request = get()
response = process_request(request, views.flag_in_view)
assert 'dwf_myflag' in response.cookies
@mock.patch.object(random, 'uniform')
def test_reroll(self, uniform):
"""Even without a cookie, calling flag_is_active twice should return
the same value."""
Flag.objects.create(name='myflag', percent='50.0')
# Make sure we're not really random.
request = get() # Create a clean request.
assert not hasattr(request, 'waffles')
uniform.return_value = '10' # < 50. Flag is True.
assert waffle.flag_is_active(request, 'myflag')
assert hasattr(request, 'waffles') # We should record this flag.
assert 'myflag' in request.waffles
assert request.waffles['myflag'][0]
uniform.return_value = '70' # > 50. Normally, Flag would be False.
assert waffle.flag_is_active(request, 'myflag')
assert request.waffles['myflag'][0]
def test_undefined(self):
"""Undefined flags are always false."""
request = get()
assert not waffle.flag_is_active(request, 'foo')
@override_settings(WAFFLE_FLAG_DEFAULT=True)
def test_undefined_default(self):
"""WAFFLE_FLAG_DEFAULT controls undefined flags."""
request = get()
assert waffle.flag_is_active(request, 'foo')
@override_settings(WAFFLE_OVERRIDE=True)
def test_override(self):
request = get(foo='1')
Flag.objects.create(name='foo') # Off for everyone.
assert waffle.flag_is_active(request, 'foo')
def test_testing_flag(self):
Flag.objects.create(name='foo', testing=True)
request = get(dwft_foo='1')
assert waffle.flag_is_active(request, 'foo')
assert 'foo' in request.waffle_tests
assert request.waffle_tests['foo']
# GET param should override cookie
request = get(dwft_foo='0')
request.COOKIES['dwft_foo'] = 'True'
assert not waffle.flag_is_active(request, 'foo')
assert 'foo' in request.waffle_tests
assert not request.waffle_tests['foo']
def test_testing_disabled_flag(self):
Flag.objects.create(name='foo')
request = get(dwft_foo='1')
assert not waffle.flag_is_active(request, 'foo')
assert not hasattr(request, 'waffle_tests')
request = get(dwft_foo='0')
assert not waffle.flag_is_active(request, 'foo')
assert not hasattr(request, 'waffle_tests')
def test_set_then_unset_testing_flag(self):
Flag.objects.create(name='myflag', testing=True)
response = self.client.get('/flag_in_view?dwft_myflag=1')
self.assertEqual(b'on', response.content)
response = self.client.get('/flag_in_view')
self.assertEqual(b'on', response.content)
response = self.client.get('/flag_in_view?dwft_myflag=0')
self.assertEqual(b'off', response.content)
response = self.client.get('/flag_in_view')
self.assertEqual(b'off', response.content)
response = self.client.get('/flag_in_view?dwft_myflag=1')
self.assertEqual(b'on', response.content)
@override_settings(DATABASE_ROUTERS=['waffle.tests.base.ReplicationRouter'])
def test_everyone_on_read_from_write_db(self):
flag = Flag.objects.create(name='myflag', everyone=True)
request = get()
response = process_request(request, views.flag_in_view)
# By default, flag_is_active should hit whatever it configured as the
# read DB (so values will be stale if replication is lagged).
self.assertEqual(b'off', response.content)
with override_settings(WAFFLE_READ_FROM_WRITE_DB=True):
# Save the flag again to flush the cache.
flag.save()
# The next read should now be directed to the write DB, ensuring
# the cache and DB are in sync.
response = process_request(request, views.flag_in_view)
self.assertEqual(b'on', response.content)
class SwitchTests(TestCase):
def test_switch_active(self):
switch = Switch.objects.create(name='myswitch', active=True)
assert waffle.switch_is_active(switch.name)
def test_switch_inactive(self):
switch = Switch.objects.create(name='myswitch', active=False)
assert not waffle.switch_is_active(switch.name)
def test_switch_active_from_cache(self):
"""Do not make two queries for an existing active switch."""
switch = Switch.objects.create(name='myswitch', active=True)
# Get the value once so that it will be put into the cache
assert waffle.switch_is_active(switch.name)
queries = len(connection.queries)
assert waffle.switch_is_active(switch.name)
self.assertEqual(queries, len(connection.queries))
def test_switch_inactive_from_cache(self):
"""Do not make two queries for an existing inactive switch."""
switch = Switch.objects.create(name='myswitch', active=False)
# Get the value once so that it will be put into the cache
assert not waffle.switch_is_active(switch.name)
queries = len(connection.queries)
assert not waffle.switch_is_active(switch.name)
self.assertEqual(queries, len(connection.queries))
def test_undefined(self):
assert not waffle.switch_is_active('foo')
@override_settings(WAFFLE_SWITCH_DEFAULT=True)
def test_undefined_default(self):
assert waffle.switch_is_active('foo')
@override_settings(DEBUG=True)
def test_no_query(self):
"""Do not make two queries for a non-existent switch."""
assert not Switch.objects.filter(name='foo').exists()
queries = len(connection.queries)
assert not waffle.switch_is_active('foo')
assert len(connection.queries) > queries
queries = len(connection.queries)
assert not waffle.switch_is_active('foo')
self.assertEqual(queries, len(connection.queries))
@override_settings(DATABASE_ROUTERS=['waffle.tests.base.ReplicationRouter'])
def test_read_from_write_db(self):
switch = Switch.objects.create(name='switch', active=True)
# By default, switch_is_active should hit whatever it configured as the
# read DB (so values will be stale if replication is lagged).
assert not waffle.switch_is_active(switch.name)
with override_settings(WAFFLE_READ_FROM_WRITE_DB=True):
# Save the switch again to flush the cache.
switch.save()
# The next read should now be directed to the write DB, ensuring
# the cache and DB are in sync.
assert waffle.switch_is_active(switch.name)
class SampleTests(TestCase):
def test_sample_100(self):
sample = Sample.objects.create(name='sample', percent='100.0')
assert waffle.sample_is_active(sample.name)
def test_sample_0(self):
sample = Sample.objects.create(name='sample', percent='0.0')
assert not waffle.sample_is_active(sample.name)
def test_undefined(self):
assert not waffle.sample_is_active('foo')
@override_settings(WAFFLE_SAMPLE_DEFAULT=True)
def test_undefined_default(self):
assert waffle.sample_is_active('foo')
@override_settings(DATABASE_ROUTERS=['waffle.tests.base.ReplicationRouter'])
def test_read_from_write_db(self):
sample = Sample.objects.create(name='sample', percent='100.0')
# By default, sample_is_active should hit whatever it configured as the
# read DB (so values will be stale if replication is lagged).
assert not waffle.sample_is_active(sample.name)
with override_settings(WAFFLE_READ_FROM_WRITE_DB=True):
# Save the sample again to flush the cache.
sample.save()
# The next read should now be directed to the write DB, ensuring
# the cache and DB are in sync.
assert waffle.sample_is_active(sample.name)
class TransactionTests(TransactionTestCase):
@unittest.skipIf('sqlite3' in settings.DATABASES['default']['ENGINE'],
'This test uses threads, which the sqlite3 DB engine '
'does not support.')
def test_update_switch_in_transaction(self):
"""Wait to invalidate the cache until after the current transaction."""
switch_name = 'transaction-switch-name'
switch = Switch.objects.create(name=switch_name, active=False)
self.addCleanup(switch.delete)
switch_written_in_background_thread = threading.Event()
switch_read_in_main_thread = threading.Event()
@transaction.atomic
def update_switch():
switch.active = True
switch.save()
# Signal to the main thread that the switch has been updated, but
# the transaction is not yet committed.
switch_written_in_background_thread.set()
# Pause here to allow the main thread to make an assertion.
switch_read_in_main_thread.wait(timeout=1)
# Start a background thread to update the switch in a transaction.
t = threading.Thread(target=update_switch)
t.daemon = True
t.start()
# After the switch is updated but before the transaction is committed,
# the cache will still have the previous value.
switch_written_in_background_thread.wait(timeout=1)
assert not waffle.switch_is_active(switch_name)
# After the transaction is committed, the cache should have been
# invalidated, hence the next call to switch_is_active should have the
# correct value.
switch_read_in_main_thread.set()
t.join(timeout=1)
assert waffle.switch_is_active(switch_name)