Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
21e92a41
Commit
21e92a41
authored
Jan 20, 2017
by
Renzo Lucioni
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add testing utility for toggling Waffle Switches
parent
12c595f8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
2 deletions
+49
-2
openedx/core/djangolib/testing/tests/test_utils.py
+26
-1
openedx/core/djangolib/testing/utils.py
+23
-1
No files found.
openedx/core/djangolib/testing/tests/test_utils.py
View file @
21e92a41
...
...
@@ -5,8 +5,9 @@ from django.contrib.auth import get_user_model
from
django.contrib.auth.models
import
AnonymousUser
from
django.http.request
import
HttpRequest
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
()
...
...
@@ -27,3 +28,27 @@ class TestGetMockRequest(TestCase):
def
test_mock_request_without_user
(
self
):
request
=
get_mock_request
()
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
)
openedx/core/djangolib/testing/utils.py
View file @
21e92a41
...
...
@@ -18,8 +18,8 @@ from django.core.cache import caches
from
django.test
import
RequestFactory
,
TestCase
,
override_settings
from
django.conf
import
settings
from
django.contrib
import
sites
from
nose.plugins
import
Plugin
from
waffle.models
import
Switch
from
request_cache.middleware
import
RequestCache
...
...
@@ -190,3 +190,25 @@ def skip_unless_lms(func):
Only run the decorated test in the LMS test suite
"""
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment