Commit 21e92a41 by Renzo Lucioni

Add testing utility for toggling Waffle Switches

parent 12c595f8
...@@ -5,8 +5,9 @@ from django.contrib.auth import get_user_model ...@@ -5,8 +5,9 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django.test import TestCase from django.test import TestCase
from waffle.models import Switch
from ..utils import get_mock_request from ..utils import get_mock_request, toggle_switch
USER_MODEL = get_user_model() USER_MODEL = get_user_model()
...@@ -27,3 +28,27 @@ class TestGetMockRequest(TestCase): ...@@ -27,3 +28,27 @@ class TestGetMockRequest(TestCase):
def test_mock_request_without_user(self): def test_mock_request_without_user(self):
request = get_mock_request() request = get_mock_request()
self.assertIsInstance(request.user, AnonymousUser) self.assertIsInstance(request.user, AnonymousUser)
class TestToggleSwitch(TestCase):
"""
Verify that the toggle_switch utility can be used to turn Waffle Switches
on and off.
"""
def test_toggle_switch(self):
"""Verify that a new switch can be turned on and off."""
name = 'foo'
switch = toggle_switch(name)
# Verify that the switch was saved.
self.assertEqual(switch, Switch.objects.get())
# Verify that the switch has the right name and is active.
self.assertEqual(switch.name, name)
self.assertTrue(switch.active)
switch = toggle_switch(name)
# Verify that the switch has been turned off.
self.assertFalse(switch.active)
...@@ -18,8 +18,8 @@ from django.core.cache import caches ...@@ -18,8 +18,8 @@ from django.core.cache import caches
from django.test import RequestFactory, TestCase, override_settings from django.test import RequestFactory, TestCase, override_settings
from django.conf import settings from django.conf import settings
from django.contrib import sites from django.contrib import sites
from nose.plugins import Plugin from nose.plugins import Plugin
from waffle.models import Switch
from request_cache.middleware import RequestCache from request_cache.middleware import RequestCache
...@@ -190,3 +190,25 @@ def skip_unless_lms(func): ...@@ -190,3 +190,25 @@ def skip_unless_lms(func):
Only run the decorated test in the LMS test suite Only run the decorated test in the LMS test suite
""" """
return skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in LMS')(func) return skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in LMS')(func)
def toggle_switch(name, active=True):
"""
Activate or deactivate a Waffle switch. The switch is created if it does not exist.
Arguments:
name (str): Name of the switch to be toggled.
Keyword Arguments:
active (bool): Whether a newly created switch should be on or off.
Returns:
Switch
"""
switch, created = Switch.objects.get_or_create(name=name, defaults={'active': active})
if not created:
switch.active = not switch.active
switch.save()
return switch
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment