Skip to content

Commit 5eb865f

Browse files
refactor aggregation method
1 parent bd70ce7 commit 5eb865f

File tree

3 files changed

+136
-135
lines changed

3 files changed

+136
-135
lines changed

openedx/core/djangoapps/notifications/tests/test_utils.py

Lines changed: 131 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_empty_configs_list_returns_default(self):
1515
"""
1616
If the configs list is empty, the default config should be returned.
1717
"""
18-
default_config = {
18+
default_config = [{
1919
"grading": {
2020
"enabled": False,
2121
"non_editable": {},
@@ -28,45 +28,46 @@ def test_empty_configs_list_returns_default(self):
2828
}
2929
}
3030
}
31-
}
31+
}]
3232

33-
result = aggregate_notification_configs(default_config, [])
34-
assert result == default_config
33+
result = aggregate_notification_configs(default_config)
34+
assert result == default_config[0]
3535

3636
def test_enable_notification_type(self):
3737
"""
3838
If a config enables a notification type, it should be enabled in the result.
3939
"""
40-
default_config = {
41-
"grading": {
42-
"enabled": False,
43-
"non_editable": {},
44-
"notification_types": {
45-
"core": {
46-
"web": False,
47-
"push": False,
48-
"email": False,
49-
"email_cadence": "Weekly"
40+
41+
config_list = [
42+
{
43+
"grading": {
44+
"enabled": False,
45+
"non_editable": {},
46+
"notification_types": {
47+
"core": {
48+
"web": False,
49+
"push": False,
50+
"email": False,
51+
"email_cadence": "Weekly"
52+
}
5053
}
5154
}
52-
}
53-
}
54-
55-
config_list = [{
56-
"grading": {
57-
"enabled": True,
58-
"notification_types": {
59-
"core": {
60-
"web": True,
61-
"push": True,
62-
"email": True,
63-
"email_cadence": "Weekly"
55+
},
56+
{
57+
"grading": {
58+
"enabled": True,
59+
"notification_types": {
60+
"core": {
61+
"web": True,
62+
"push": True,
63+
"email": True,
64+
"email_cadence": "Weekly"
65+
}
6466
}
6567
}
66-
}
67-
}]
68+
}]
6869

69-
result = aggregate_notification_configs(default_config, config_list)
70+
result = aggregate_notification_configs(config_list)
7071
assert result["grading"]["enabled"] is True
7172
assert result["grading"]["notification_types"]["core"]["web"] is True
7273
assert result["grading"]["notification_types"]["core"]["push"] is True
@@ -78,21 +79,23 @@ def test_merge_core_notification_types(self):
7879
"""
7980
Core notification types should be merged across configs.
8081
"""
81-
default_config = {
82-
"discussion": {
83-
"enabled": True,
84-
"core_notification_types": ["new_comment"],
85-
"notification_types": {}
86-
}
87-
}
8882

89-
config_list = [{
90-
"discussion": {
91-
"core_notification_types": ["new_response", "new_comment"]
92-
}
93-
}]
83+
config_list = [
84+
{
85+
"discussion": {
86+
"enabled": True,
87+
"core_notification_types": ["new_comment"],
88+
"notification_types": {}
89+
}
90+
},
91+
{
92+
"discussion": {
93+
"core_notification_types": ["new_response", "new_comment"]
94+
}
9495

95-
result = aggregate_notification_configs(default_config, config_list)
96+
}]
97+
98+
result = aggregate_notification_configs(config_list)
9699
assert set(result["discussion"]["core_notification_types"]) == {
97100
"new_comment", "new_response"
98101
}
@@ -101,21 +104,21 @@ def test_multiple_configs_aggregate(self):
101104
"""
102105
Multiple configs should be aggregated together.
103106
"""
104-
default_config = {
105-
"updates": {
106-
"enabled": False,
107-
"notification_types": {
108-
"course_updates": {
109-
"web": False,
110-
"push": False,
111-
"email": False,
112-
"email_cadence": "Weekly"
113-
}
114-
}
115-
}
116-
}
117107

118108
config_list = [
109+
{
110+
"updates": {
111+
"enabled": False,
112+
"notification_types": {
113+
"course_updates": {
114+
"web": False,
115+
"push": False,
116+
"email": False,
117+
"email_cadence": "Weekly"
118+
}
119+
}
120+
}
121+
},
119122
{
120123
"updates": {
121124
"enabled": True,
@@ -139,7 +142,7 @@ def test_multiple_configs_aggregate(self):
139142
}
140143
]
141144

142-
result = aggregate_notification_configs(default_config, config_list)
145+
result = aggregate_notification_configs(config_list)
143146
assert result["updates"]["enabled"] is True
144147
assert result["updates"]["notification_types"]["course_updates"]["web"] is True
145148
assert result["updates"]["notification_types"]["course_updates"]["push"] is True
@@ -151,86 +154,89 @@ def test_ignore_unknown_notification_types(self):
151154
"""
152155
Unknown notification types should be ignored.
153156
"""
154-
default_config = {
155-
"grading": {
156-
"enabled": False,
157-
"notification_types": {
158-
"core": {
159-
"web": False,
160-
"push": False,
161-
"email": False,
162-
"email_cadence": "Daily"
157+
config_list = [
158+
{
159+
"grading": {
160+
"enabled": False,
161+
"notification_types": {
162+
"core": {
163+
"web": False,
164+
"push": False,
165+
"email": False,
166+
"email_cadence": "Daily"
167+
}
163168
}
164169
}
165-
}
166-
}
167-
168-
config_list = [{
169-
"grading": {
170-
"notification_types": {
171-
"unknown_type": {
172-
"web": True,
173-
"push": True,
174-
"email": True
170+
},
171+
{
172+
"grading": {
173+
"notification_types": {
174+
"unknown_type": {
175+
"web": True,
176+
"push": True,
177+
"email": True
178+
}
175179
}
176180
}
177-
}
178-
}]
181+
}]
179182

180-
result = aggregate_notification_configs(default_config, config_list)
183+
result = aggregate_notification_configs(config_list)
181184
assert "unknown_type" not in result["grading"]["notification_types"]
182185
assert result["grading"]["notification_types"]["core"]["web"] is False
183186

184187
def test_ignore_unknown_categories(self):
185188
"""
186189
Unknown categories should be ignored.
187190
"""
188-
default_config = {
189-
"grading": {
190-
"enabled": False,
191-
"notification_types": {}
192-
}
193-
}
194191

195-
config_list = [{
196-
"unknown_category": {
197-
"enabled": True,
198-
"notification_types": {}
199-
}
200-
}]
192+
config_list = [
193+
{
194+
"grading": {
195+
"enabled": False,
196+
"notification_types": {}
197+
}
198+
},
199+
{
200+
"unknown_category": {
201+
"enabled": True,
202+
"notification_types": {}
203+
}
204+
}]
201205

202-
result = aggregate_notification_configs(default_config, config_list)
206+
result = aggregate_notification_configs(config_list)
203207
assert "unknown_category" not in result
204208
assert result["grading"]["enabled"] is False
205209

206210
def test_preserves_default_structure(self):
207211
"""
208212
The resulting config should have the same structure as the default config.
209213
"""
210-
default_config = {
211-
"discussion": {
212-
"enabled": False,
213-
"non_editable": {"core": ["web"]},
214-
"notification_types": {
215-
"core": {
216-
"web": False,
217-
"push": False,
218-
"email": False,
219-
"email_cadence": "Weekly"
220-
}
221-
},
222-
"core_notification_types": []
223-
}
224-
}
225214

226-
config_list = [{
227-
"discussion": {
228-
"enabled": True,
229-
"extra_field": "should_not_appear"
215+
config_list = [
216+
{
217+
"discussion": {
218+
"enabled": False,
219+
"non_editable": {"core": ["web"]},
220+
"notification_types": {
221+
"core": {
222+
"web": False,
223+
"push": False,
224+
"email": False,
225+
"email_cadence": "Weekly"
226+
}
227+
},
228+
"core_notification_types": []
229+
}
230+
},
231+
{
232+
"discussion": {
233+
"enabled": True,
234+
"extra_field": "should_not_appear"
235+
}
230236
}
231-
}]
237+
]
232238

233-
result = aggregate_notification_configs(default_config, config_list)
239+
result = aggregate_notification_configs(config_list)
234240
assert set(result["discussion"].keys()) == {
235241
"enabled", "non_editable", "notification_types", "core_notification_types"
236242
}
@@ -240,21 +246,20 @@ def test_if_email_cadence_has_diff_set_mix_as_value(self):
240246
"""
241247
If email_cadence is different in the configs, set it to "Mixed".
242248
"""
243-
default_config = {
244-
"grading": {
245-
"enabled": False,
246-
"notification_types": {
247-
"core": {
248-
"web": False,
249-
"push": False,
250-
"email": False,
251-
"email_cadence": "Daily"
249+
config_list = [
250+
{
251+
"grading": {
252+
"enabled": False,
253+
"notification_types": {
254+
"core": {
255+
"web": False,
256+
"push": False,
257+
"email": False,
258+
"email_cadence": "Daily"
259+
}
252260
}
253261
}
254-
}
255-
}
256-
257-
config_list = [
262+
},
258263
{
259264
"grading": {
260265
"enabled": True,
@@ -279,5 +284,5 @@ def test_if_email_cadence_has_diff_set_mix_as_value(self):
279284
}
280285
]
281286

282-
result = aggregate_notification_configs(default_config, config_list)
287+
result = aggregate_notification_configs(config_list)
283288
assert result["grading"]["notification_types"]["core"]["email_cadence"] == "Mixed"

0 commit comments

Comments
 (0)