Skip to content

Commit 8bd6d27

Browse files
committed
test: add unit tests for partitions and processors module
1 parent 9ff8d3a commit 8bd6d27

5 files changed

Lines changed: 937 additions & 17 deletions

File tree

openedx_user_groups/partitions/user_group_partition_scheme.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,46 @@
33
"""
44

55
import logging
6+
from unittest.mock import Mock
67

78
from django.utils.translation import gettext_lazy as _
8-
from lms.djangoapps.courseware.masquerade import (
9-
get_course_masquerade,
10-
get_masquerading_user_group,
11-
is_masquerading_as_specific_student,
12-
)
9+
10+
try:
11+
from lms.djangoapps.courseware.masquerade import (
12+
get_course_masquerade,
13+
get_masquerading_user_group,
14+
is_masquerading_as_specific_student,
15+
)
16+
from openedx.core import types
17+
from xmodule.partitions.partitions import Group, UserPartition, UserPartitionError
18+
except ImportError:
19+
get_course_masquerade = Mock()
20+
get_masquerading_user_group = Mock()
21+
is_masquerading_as_specific_student = Mock()
22+
types = Mock()
23+
Group = Mock()
24+
25+
class UserPartitionError(Exception):
26+
"""Mock UserPartitionError class for testing."""
27+
28+
class UserPartition:
29+
"""Mock UserPartition class for testing."""
30+
31+
# pylint: disable=redefined-builtin, too-many-positional-arguments, unused-argument
32+
def __init__(self, id, name, description, groups, scheme, parameters, active=True):
33+
self.parameters = parameters
34+
self.scheme = scheme
35+
self.id = id
36+
self.name = name
37+
self.description = description
38+
self.active = active
39+
40+
@property
41+
def groups(self):
42+
return self.groups
43+
44+
1345
from opaque_keys.edx.keys import CourseKey
14-
from openedx.core import types
15-
from xmodule.partitions.partitions import Group, UserPartition, UserPartitionError
1646

1747
from openedx_user_groups.models import UserGroup, UserGroupMembership
1848
from openedx_user_groups.toggles import is_user_groups_enabled
@@ -31,7 +61,7 @@ class UserGroupPartition(UserPartition):
3161
"""
3262

3363
@property
34-
def groups(self):
64+
def groups(self) -> list[Group]:
3565
"""
3666
Dynamically generate groups (based on user groups) for this partition.
3767
"""
@@ -98,7 +128,7 @@ def get_group_for_user(
98128

99129
return user_groups
100130

101-
# pylint: disable=redefined-builtin, invalid-name
131+
# pylint: disable=redefined-builtin, invalid-name, too-many-positional-arguments
102132
@classmethod
103133
def create_user_partition(
104134
cls,
@@ -152,7 +182,7 @@ def create_user_partition(
152182
return user_group_partition
153183

154184

155-
def create_user_group_partition_with_course_id(course_id):
185+
def create_user_group_partition_with_course_id(course_id: CourseKey) -> UserPartition | None:
156186
"""
157187
Create and return the user group partition based only on course_id.
158188
If it cannot be created, None is returned.

openedx_user_groups/processors/user_group_partition_groups.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@
33
"""
44

55
from datetime import datetime
6-
from typing import Dict, Set
6+
from typing import Dict, List, Set
7+
from unittest.mock import Mock
78

89
from opaque_keys.edx.keys import CourseKey
9-
from openedx.core import types
10-
from openedx.core.djangoapps.content.learning_sequences.api.processors.base import OutlineProcessor
11-
from xmodule.partitions.partitions import Group
12-
from xmodule.partitions.partitions_service import get_user_partition_groups
10+
11+
try:
12+
from openedx.core import types
13+
from openedx.core.djangoapps.content.learning_sequences.api.processors.base import OutlineProcessor
14+
from xmodule.partitions.partitions import Group
15+
from xmodule.partitions.partitions_service import get_user_partition_groups
16+
except ImportError:
17+
types = Mock()
18+
Group = Mock()
19+
get_user_partition_groups = Mock()
20+
21+
class OutlineProcessor:
22+
"""Mock OutlineProcessor class."""
23+
24+
def __init__(self, course_key, user, at_time):
25+
"""Initialize the OutlineProcessor."""
26+
self.course_key = course_key
27+
self.user = user
28+
self.at_time = at_time
29+
1330

1431
from openedx_user_groups.partitions.user_group_partition_scheme import (
1532
USER_GROUP_PARTITION_ID,
@@ -37,7 +54,7 @@ def __init__(self, course_key: CourseKey, user: types.User, at_time: datetime):
3754
at_time (datetime): The time at which the data is loaded.
3855
"""
3956
super().__init__(course_key, user, at_time)
40-
self.user_groups: Dict[str, Group] = {}
57+
self.user_groups: List[Group] = []
4158

4259
def load_data(self, _) -> None:
4360
"""

openedx_user_groups/toggles.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
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+
79
from opaque_keys.edx.keys import CourseKey
810

911
try:
1012
from openedx.core.djangoapps.waffle_utils import CourseWaffleFlag
1113
except ImportError:
12-
CourseWaffleFlag = None
14+
CourseWaffleFlag = Mock()
15+
1316

1417
# Namespace for all user group related waffle flags
1518
WAFFLE_FLAG_NAMESPACE = "user_groups"

0 commit comments

Comments
 (0)