Skip to content

Commit 8eaa914

Browse files
committed
test: add unit tests for toggles module
1 parent b4be7e9 commit 8eaa914

File tree

2 files changed

+173
-3
lines changed

2 files changed

+173
-3
lines changed

openedx_user_groups/toggles.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
within the Open edX platform. These toggles allow for dynamic control of features without requiring code changes.
55
"""
66

7-
from unittest.mock import Mock
8-
97
from opaque_keys.edx.keys import CourseKey
108

119
try:
1210
from openedx.core.djangoapps.waffle_utils import CourseWaffleFlag
1311
except ImportError:
14-
CourseWaffleFlag = Mock()
12+
13+
class CourseWaffleFlag:
14+
"""Mock CourseWaffleFlag class."""
15+
16+
def __init__(self, name, module_name):
17+
"""Initialize the CourseWaffleFlag."""
18+
self.name = name
19+
self.module_name = module_name
1520

1621

1722
# Namespace for all user group related waffle flags

tests/test_toggles.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
"""
2+
Test Suite for the User Group toggles.
3+
4+
This test suite covers all toggle functionality defined in toggles.py.
5+
"""
6+
7+
from unittest.mock import Mock, patch
8+
9+
from django.test import TestCase
10+
from opaque_keys.edx.keys import CourseKey
11+
12+
from openedx_user_groups.toggles import ENABLE_USER_GROUPS, WAFFLE_FLAG_NAMESPACE, is_user_groups_enabled
13+
14+
15+
class TestToggles(TestCase):
16+
"""Test toggle functionality and configuration."""
17+
18+
def test_waffle_flag_namespace(self):
19+
"""Test that the waffle flag namespace is correctly defined.
20+
21+
Expected Results:
22+
- The namespace should be "user_groups".
23+
"""
24+
self.assertEqual(WAFFLE_FLAG_NAMESPACE, "user_groups")
25+
26+
def test_enable_user_groups_flag_creation(self):
27+
"""Test that the ENABLE_USER_GROUPS flag is created with correct parameters.
28+
29+
Expected Results:
30+
- The flag should be created with the correct namespace and module name.
31+
"""
32+
expected_flag_name = f"{WAFFLE_FLAG_NAMESPACE}.enable_user_groups"
33+
self.assertEqual(ENABLE_USER_GROUPS.name, expected_flag_name)
34+
self.assertEqual(ENABLE_USER_GROUPS.module_name, "openedx_user_groups.toggles")
35+
36+
@patch("openedx_user_groups.toggles.ENABLE_USER_GROUPS")
37+
def test_is_user_groups_enabled_when_flag_enabled(self, mock_flag: Mock):
38+
"""Test is_user_groups_enabled returns True when flag is enabled.
39+
40+
Expected Results:
41+
- The function should return True when the waffle flag is enabled.
42+
"""
43+
course_key = CourseKey.from_string("course-v1:edX+Demo+Course")
44+
mock_flag.is_enabled.return_value = True
45+
46+
result = is_user_groups_enabled(course_key)
47+
48+
self.assertTrue(result)
49+
mock_flag.is_enabled.assert_called_once_with(course_key)
50+
51+
@patch("openedx_user_groups.toggles.ENABLE_USER_GROUPS")
52+
def test_is_user_groups_enabled_when_flag_disabled(self, mock_flag: Mock):
53+
"""Test is_user_groups_enabled returns False when flag is disabled.
54+
55+
Expected Results:
56+
- The function should return False when the waffle flag is disabled.
57+
"""
58+
course_key = CourseKey.from_string("course-v1:edX+Demo+Course")
59+
mock_flag.is_enabled.return_value = False
60+
61+
result = is_user_groups_enabled(course_key)
62+
63+
self.assertFalse(result)
64+
mock_flag.is_enabled.assert_called_once_with(course_key)
65+
66+
@patch("openedx_user_groups.toggles.ENABLE_USER_GROUPS")
67+
def test_is_user_groups_enabled_with_different_course_keys(self, mock_flag: Mock):
68+
"""Test is_user_groups_enabled works with different course key formats.
69+
70+
Expected Results:
71+
- The function should work correctly with different course key formats.
72+
"""
73+
course_keys = [
74+
CourseKey.from_string("course-v1:edX+Demo+Course"),
75+
CourseKey.from_string("course-v1:TestOrg+CS101+2024"),
76+
CourseKey.from_string("course-v1:AnotherOrg+Math101+Fall2024"),
77+
]
78+
mock_flag.is_enabled.return_value = True
79+
80+
for course_key in course_keys:
81+
result = is_user_groups_enabled(course_key)
82+
self.assertTrue(result)
83+
mock_flag.is_enabled.assert_called_with(course_key)
84+
85+
@patch("openedx_user_groups.toggles.ENABLE_USER_GROUPS")
86+
def test_is_user_groups_enabled_flag_called_with_correct_arguments(self, mock_flag: Mock):
87+
"""Test that the flag is called with the correct course key argument.
88+
89+
Expected Results:
90+
- The waffle flag should be called with the exact course key passed to the function.
91+
"""
92+
course_key = CourseKey.from_string("course-v1:edX+Demo+Course")
93+
mock_flag.is_enabled.return_value = True
94+
95+
is_user_groups_enabled(course_key)
96+
97+
mock_flag.is_enabled.assert_called_once_with(course_key)
98+
# Verify the course key is the same object, not just equal
99+
called_course_key = mock_flag.is_enabled.call_args[0][0]
100+
self.assertEqual(called_course_key, course_key)
101+
102+
103+
class TestToggleIntegration(TestCase):
104+
"""Test toggle integration with course keys and real scenarios."""
105+
106+
def setUp(self):
107+
"""Set up test data."""
108+
self.course_key = CourseKey.from_string("course-v1:edX+Demo+Course")
109+
110+
@patch("openedx_user_groups.toggles.ENABLE_USER_GROUPS")
111+
def test_toggle_with_real_course_key_objects(self, mock_flag: Mock):
112+
"""Test toggle functionality with real CourseKey objects.
113+
114+
Expected Results:
115+
- The toggle should work correctly with real CourseKey objects.
116+
"""
117+
mock_flag.is_enabled.return_value = True
118+
119+
result = is_user_groups_enabled(self.course_key)
120+
121+
self.assertTrue(result)
122+
mock_flag.is_enabled.assert_called_once_with(self.course_key)
123+
124+
@patch("openedx_user_groups.toggles.ENABLE_USER_GROUPS")
125+
def test_toggle_behavior_consistency(self, mock_flag: Mock):
126+
"""Test that toggle behavior is consistent across multiple calls.
127+
128+
Expected Results:
129+
- Multiple calls with the same course key should return the same result.
130+
"""
131+
mock_flag.is_enabled.return_value = True
132+
133+
result1 = is_user_groups_enabled(self.course_key)
134+
result2 = is_user_groups_enabled(self.course_key)
135+
result3 = is_user_groups_enabled(self.course_key)
136+
137+
self.assertTrue(result1)
138+
self.assertTrue(result2)
139+
self.assertTrue(result3)
140+
self.assertEqual(mock_flag.is_enabled.call_count, 3)
141+
142+
@patch("openedx_user_groups.toggles.ENABLE_USER_GROUPS")
143+
def test_toggle_with_different_course_keys_returns_different_results(self, mock_flag: Mock):
144+
"""Test that different course keys can have different toggle states.
145+
146+
Expected Results:
147+
- Different course keys can have different toggle states.
148+
"""
149+
course_key1 = CourseKey.from_string("course-v1:edX+Demo+Course")
150+
course_key2 = CourseKey.from_string("course-v1:TestOrg+CS101+2024")
151+
152+
# Configure mock to return different values for different course keys
153+
def mock_is_enabled(course_key):
154+
if str(course_key) == "course-v1:edX+Demo+Course":
155+
return True
156+
return False
157+
158+
mock_flag.is_enabled.side_effect = mock_is_enabled
159+
160+
result1 = is_user_groups_enabled(course_key1)
161+
result2 = is_user_groups_enabled(course_key2)
162+
163+
self.assertTrue(result1)
164+
self.assertFalse(result2)
165+
self.assertEqual(mock_flag.is_enabled.call_count, 2)

0 commit comments

Comments
 (0)