-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathtest_partition_scheme.py
211 lines (187 loc) · 8.46 KB
/
test_partition_scheme.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
"""
Test the partitions and partitions services. The partitions tested
in this file are the following:
- TeamPartitionScheme
"""
from unittest.mock import MagicMock, patch
from common.djangoapps.student.tests.factories import UserFactory
from lms.djangoapps.teams.tests.factories import CourseTeamFactory
from lms.djangoapps.teams.team_partition_scheme import TeamPartitionScheme
from openedx.core.lib.teams_config import create_team_set_partitions_with_course_id
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.factories import ToyCourseFactory
from xmodule.modulestore.django import modulestore
from xmodule.partitions.partitions import Group
@patch(
"lms.djangoapps.teams.team_partition_scheme.CONTENT_GROUPS_FOR_TEAMS.is_enabled",
lambda _: True
)
class TestTeamPartitionScheme(ModuleStoreTestCase):
"""
Test the TeamPartitionScheme partition scheme and its related functions.
"""
def setUp(self):
"""
Regenerate a course with teams configuration, partition and groups,
and a student for each test.
"""
super().setUp()
self.course_key = ToyCourseFactory.create().id
self.course = modulestore().get_course(self.course_key)
self.student = UserFactory.create()
self.student.courseenrollment_set.create(course_id=self.course_key, is_active=True)
self.team_sets = [
MagicMock(name="1st TeamSet", teamset_id=1, user_partition_id=51),
MagicMock(name="2nd TeamSet", teamset_id=2, user_partition_id=52),
]
@patch("lms.djangoapps.teams.team_partition_scheme.TeamsConfigurationService")
def test_create_user_partition_with_course_id(self, mock_teams_configuration_service):
"""
Test that create_user_partition returns the correct user partitions for the input data.
Expected result:
- There's a user partition matching the ID given.
"""
mock_teams_configuration_service().get_teams_configuration.return_value.teamsets = self.team_sets
partition = TeamPartitionScheme.create_user_partition(
id=self.team_sets[0].user_partition_id,
name=f"Team Group: {self.team_sets[0].name}",
description="Partition for segmenting users by team-set",
parameters={
"course_id": str(self.course_key),
"team_set_id": self.team_sets[0].teamset_id,
}
)
assert partition.id == self.team_sets[0].user_partition_id
def test_team_partition_generator(self):
"""
Test that create_team_set_partition returns the correct user partitions for the input data.
Expected result:
- The user partitions are created based on the team sets.
"""
partitions = create_team_set_partitions_with_course_id(self.course_key, self.team_sets)
assert partitions == [
TeamPartitionScheme.create_user_partition(
id=self.team_sets[0].user_partition_id,
name=f"Team Group: {self.team_sets[0].name}",
description="Partition for segmenting users by team-set",
parameters={
"course_id": str(self.course_key),
"team_set_id": self.team_sets[0].teamset_id,
}
),
TeamPartitionScheme.create_user_partition(
id=self.team_sets[1].user_partition_id,
name=f"Team Group: {self.team_sets[1].name}",
description="Partition for segmenting users by team-set",
parameters={
"course_id": str(self.course_key),
"team_set_id": self.team_sets[1].teamset_id,
}
),
]
@patch("lms.djangoapps.teams.team_partition_scheme.TeamsConfigurationService")
def test_get_partition_groups(self, mock_teams_configuration_service):
"""
Test that the TeamPartitionScheme returns the correct groups for a team set.
Expected result:
- The groups in the partition match the teams in the team set.
"""
mock_teams_configuration_service().get_teams_configuration.return_value.teamsets = self.team_sets
team_1 = CourseTeamFactory.create(
name="Team 1 in TeamSet",
course_id=self.course_key,
topic_id=self.team_sets[0].teamset_id,
)
team_2 = CourseTeamFactory.create(
name="Team 2 in TeamSet",
course_id=self.course_key,
topic_id=self.team_sets[0].teamset_id,
)
team_partition_scheme = TeamPartitionScheme.create_user_partition(
id=self.team_sets[0].user_partition_id,
name=f"Team Group: {self.team_sets[0].name}",
description="Partition for segmenting users by team-set",
parameters={
"course_id": str(self.course_key),
"team_set_id": self.team_sets[0].teamset_id,
}
)
assert team_partition_scheme.groups == [
Group(team_1.id, str(team_1.name)),
Group(team_2.id, str(team_2.name)),
]
@patch("lms.djangoapps.teams.team_partition_scheme.TeamsConfigurationService")
def test_get_group_for_user(self, mock_teams_configuration_service):
"""
Test that the TeamPartitionScheme returns the correct group for a
student in a team when the team is linked to a partition group.
Expected result:
- The group returned matches the team the student is in.
"""
mock_teams_configuration_service().get_teams_configuration.return_value.teamsets = self.team_sets
team = CourseTeamFactory.create(
name="Team in 1st TeamSet",
course_id=self.course_key,
topic_id=self.team_sets[0].teamset_id,
)
team.add_user(self.student)
team_partition_scheme = TeamPartitionScheme.create_user_partition(
id=self.team_sets[0].user_partition_id,
name=f"Team Group: {self.team_sets[0].name}",
description="Partition for segmenting users by team-set",
parameters={
"course_id": str(self.course_key),
"team_set_id": self.team_sets[0].teamset_id,
}
)
assert TeamPartitionScheme.get_group_for_user(
self.course_key, self.student, team_partition_scheme
) == team_partition_scheme.groups[0]
def test_get_group_for_user_no_team(self):
"""
Test that the TeamPartitionScheme returns None for a student not in a team.
Expected result:
- The group returned is None.
"""
team_partition_scheme = TeamPartitionScheme.create_user_partition(
id=51,
name="Team Group: 1st TeamSet",
description="Partition for segmenting users by team-set",
parameters={
"course_id": str(self.course_key),
"team_set_id": 1,
}
)
assert TeamPartitionScheme.get_group_for_user(
self.course_key, self.student, team_partition_scheme
) is None
@patch("lms.djangoapps.teams.team_partition_scheme.get_course_masquerade")
@patch("lms.djangoapps.teams.team_partition_scheme.get_masquerading_user_group")
@patch("lms.djangoapps.teams.team_partition_scheme.is_masquerading_as_specific_student")
def test_group_for_user_masquerading(
self,
mock_is_masquerading_as_specific_student,
mock_get_masquerading_user_group,
mock_get_course_masquerade
):
"""
Test that the TeamPartitionScheme calls the masquerading functions when
the user is masquerading.
Expected result:
- The masquerading functions are called.
"""
team_partition_scheme = TeamPartitionScheme.create_user_partition(
id=51,
name="Team Group: 1st TeamSet",
description="Partition for segmenting users by team-set",
parameters={
"course_id": str(self.course_key),
"team_set_id": 1,
}
)
mock_get_course_masquerade.return_value = True
mock_is_masquerading_as_specific_student.return_value = False
TeamPartitionScheme.get_group_for_user(
self.course_key, self.student, team_partition_scheme
)
mock_get_masquerading_user_group.assert_called_once_with(self.course_key, self.student, team_partition_scheme)