Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/notifications/test/notification.py

Issue 29325919: Issue 2982 - Return all notifications that have title and message (Closed)
Left Patch Set: Created Sept. 4, 2015, 9:03 a.m.
Right Patch Set: Address comments Created Sept. 7, 2015, 7:51 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | sitescripts/notifications/web/notification.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 13 matching lines...) Expand all
24 class TestNotification(unittest.TestCase): 24 class TestNotification(unittest.TestCase):
25 def setUp(self): 25 def setUp(self):
26 self.load_notifications_patcher = mock.patch("sitescripts.notifications.web. notification.load_notifications") 26 self.load_notifications_patcher = mock.patch("sitescripts.notifications.web. notification.load_notifications")
27 self.load_notifications_mock = self.load_notifications_patcher.start() 27 self.load_notifications_mock = self.load_notifications_patcher.start()
28 28
29 def tearDown(self): 29 def tearDown(self):
30 self.load_notifications_patcher.stop() 30 self.load_notifications_patcher.stop()
31 31
32 def test_no_group(self): 32 def test_no_group(self):
33 self.load_notifications_mock.return_value = [ 33 self.load_notifications_mock.return_value = [
34 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}} 34 {"id": "1", "title": {}, "message": {}}
Felix Dahlke 2015/09/04 09:08:40 Adding title and message this way to each notifica
Sebastian Noack 2015/09/04 11:38:12 DUMMY_TEXTS = {"title": {"en-US": ""}, "message":
Felix Dahlke 2015/09/07 07:54:36 Now that I've gotten rid of the "en-US" checking,
35 ] 35 ]
36 result = json.loads(notification.notification({}, lambda *args: None)) 36 result = json.loads(notification.notification({}, lambda *args: None))
37 self.assertEqual(len(result["notifications"]), 1) 37 self.assertEqual(len(result["notifications"]), 1)
38 self.assertEqual(result["notifications"][0]["id"], "1") 38 self.assertEqual(result["notifications"][0]["id"], "1")
39 self.assertFalse("-" in result["version"]) 39 self.assertFalse("-" in result["version"])
40 40
41 def test_not_in_group(self): 41 def test_not_in_group(self):
42 self.load_notifications_mock.return_value = [ 42 self.load_notifications_mock.return_value = [
43 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 43 {"id": "1", "title": {}, "message": {}},
44 { 44 {"id": "a", "variants": [{"title": {}, "message": {}}]}
45 "id": "a",
46 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}]
47 }
48 ] 45 ]
49 result = json.loads(notification.notification({ 46 result = json.loads(notification.notification({
50 "QUERY_STRING": "lastVersion=197001010000-a/0" 47 "QUERY_STRING": "lastVersion=197001010000-a/0"
51 }, lambda *args: None)) 48 }, lambda *args: None))
52 self.assertEqual(len(result["notifications"]), 1) 49 self.assertEqual(len(result["notifications"]), 1)
53 self.assertEqual(result["notifications"][0]["id"], "1") 50 self.assertEqual(result["notifications"][0]["id"], "1")
54 self.assertRegexpMatches(result["version"], r"-a/0") 51 self.assertRegexpMatches(result["version"], r"-a/0")
55 52
56 def test_in_group(self): 53 def test_in_group(self):
57 self.load_notifications_mock.return_value = [ 54 self.load_notifications_mock.return_value = [
58 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 55 {"id": "1", "title": {}, "message": {}},
59 { 56 {"id": "a", "variants": [{"title": {}, "message": {}}]}
60 "id": "a",
61 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}]
62 }
63 ] 57 ]
64 result = json.loads(notification.notification({ 58 result = json.loads(notification.notification({
65 "QUERY_STRING": "lastVersion=197001010000-a/1" 59 "QUERY_STRING": "lastVersion=197001010000-a/1"
66 }, lambda *args: None)) 60 }, lambda *args: None))
67 self.assertEqual(len(result["notifications"]), 1) 61 self.assertEqual(len(result["notifications"]), 1)
68 self.assertEqual(result["notifications"][0]["id"], "a") 62 self.assertEqual(result["notifications"][0]["id"], "a")
69 self.assertRegexpMatches(result["version"], r"-a/1") 63 self.assertRegexpMatches(result["version"], r"-a/1")
70 64
71 def test_not_in_one_of_many_groups(self): 65 def test_not_in_one_of_many_groups(self):
72 self.load_notifications_mock.return_value = [ 66 self.load_notifications_mock.return_value = [
73 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 67 {"id": "1", "title": {}, "message": {}},
74 { 68 {"id": "a", "variants": [{"title": {}, "message": {}}]},
75 "id": "a", 69 {"id": "b", "variants": [{"title": {}, "message": {}}]},
76 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] 70 {"id": "c", "variants": [{"title": {}, "message": {}}]}
77 },
78 {
79 "id": "b",
80 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}]
81 },
82 {
83 "id": "c",
84 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}]
85 }
86 ] 71 ]
87 result = json.loads(notification.notification({ 72 result = json.loads(notification.notification({
88 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" 73 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0"
89 }, lambda *args: None)) 74 }, lambda *args: None))
90 self.assertEqual(len(result["notifications"]), 1) 75 self.assertEqual(len(result["notifications"]), 1)
91 self.assertEqual(result["notifications"][0]["id"], "1") 76 self.assertEqual(result["notifications"][0]["id"], "1")
92 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") 77 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0")
93 78
94 def test_in_one_of_many_groups(self): 79 def test_in_one_of_many_groups(self):
95 self.load_notifications_mock.return_value = [ 80 self.load_notifications_mock.return_value = [
96 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 81 {"id": "1", "title": {}, "message": {}},
97 { 82 {"id": "a", "variants": [{"title": {}, "message": {}}]},
98 "id": "a", 83 {"id": "b", "variants": [{"title": {}, "message": {}}]},
99 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] 84 {"id": "c", "variants": [{"title": {}, "message": {}}]}
100 },
101 {
102 "id": "b",
103 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}]
104 },
105 {
106 "id": "c",
107 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}]
108 }
109 ] 85 ]
110 result = json.loads(notification.notification({ 86 result = json.loads(notification.notification({
111 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" 87 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0"
112 }, lambda *args: None)) 88 }, lambda *args: None))
113 self.assertEqual(len(result["notifications"]), 1) 89 self.assertEqual(len(result["notifications"]), 1)
114 self.assertEqual(result["notifications"][0]["id"], "b") 90 self.assertEqual(result["notifications"][0]["id"], "b")
115 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") 91 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0")
116 92
117 def test_not_put_in_group(self): 93 def test_not_put_in_group(self):
118 self.load_notifications_mock.return_value = [ 94 self.load_notifications_mock.return_value = [
119 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 95 {"id": "1", "title": {}, "message": {}},
120 { 96 {"id": "a", "variants": [{"sample": 0, "title": {}, "message": {}}]}
121 "id": "a",
122 "variants": [
123 {"sample": 0, "title": {"en-US": ""}, "message": {"en-US": ""}}
124 ]
125 }
126 ] 97 ]
127 result = json.loads(notification.notification({ 98 result = json.loads(notification.notification({
128 "QUERY_STRING": "lastVersion=197001010000" 99 "QUERY_STRING": "lastVersion=197001010000"
129 }, lambda *args: None)) 100 }, lambda *args: None))
130 self.assertEqual(len(result["notifications"]), 1) 101 self.assertEqual(len(result["notifications"]), 1)
131 self.assertEqual(result["notifications"][0]["id"], "1") 102 self.assertEqual(result["notifications"][0]["id"], "1")
132 self.assertRegexpMatches(result["version"], r"-a/0") 103 self.assertRegexpMatches(result["version"], r"-a/0")
133 104
134 def test_put_in_group(self): 105 def test_put_in_group(self):
135 self.load_notifications_mock.return_value = [ 106 self.load_notifications_mock.return_value = [
136 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 107 {"id": "1", "title": {}, "message": {}},
137 { 108 {"id": "a", "variants": [{"sample": 1, "title": {}, "message": {}}]}
138 "id": "a",
139 "variants": [
140 {"sample": 1, "title": {"en-US": ""}, "message": {"en-US": ""}}
141 ]
142 }
143 ] 109 ]
144 result = json.loads(notification.notification({ 110 result = json.loads(notification.notification({
145 "QUERY_STRING": "lastVersion=197001010000" 111 "QUERY_STRING": "lastVersion=197001010000"
146 }, lambda *args: None)) 112 }, lambda *args: None))
147 self.assertEqual(len(result["notifications"]), 1) 113 self.assertEqual(len(result["notifications"]), 1)
148 self.assertEqual(result["notifications"][0]["id"], "a") 114 self.assertEqual(result["notifications"][0]["id"], "a")
149 self.assertRegexpMatches(result["version"], r"-a/1") 115 self.assertRegexpMatches(result["version"], r"-a/1")
150 116
151 def test_notification_variant_merged(self): 117 def test_notification_variant_merged(self):
152 self.load_notifications_mock.return_value = [ 118 self.load_notifications_mock.return_value = [
153 { 119 {
154 "id": "a", 120 "id": "a",
155 "title": {"en-US": "default"}, 121 "title": {"en-US": "default"},
Felix Dahlke 2015/09/04 09:08:40 No idea why I had that syntax there, it's not vali
156 "message": {"en-US": "default"}, 122 "message": {"en-US": "default"},
157 "variants": [ 123 "variants": [
158 { 124 {"sample": 1, "message": {"en-US": "variant"}}
159 "sample": 1,
160 "message": {"en-US": "variant"}
161 }
162 ] 125 ]
163 } 126 }
164 ] 127 ]
165 result = json.loads(notification.notification({}, lambda *args: None)) 128 result = json.loads(notification.notification({}, lambda *args: None))
166 self.assertEqual(len(result["notifications"]), 1) 129 self.assertEqual(len(result["notifications"]), 1)
167 self.assertEqual(result["notifications"][0]["id"], "a") 130 self.assertEqual(result["notifications"][0]["id"], "a")
168 self.assertEqual(result["notifications"][0]["title"]["en-US"], "default") 131 self.assertEqual(result["notifications"][0]["title"]["en-US"], "default")
169 self.assertEqual(result["notifications"][0]["message"]["en-US"], "variant") 132 self.assertEqual(result["notifications"][0]["message"]["en-US"], "variant")
170 self.assertFalse("variants" in result["notifications"][0]) 133 self.assertFalse("variants" in result["notifications"][0])
171 self.assertFalse("sample" in result["notifications"][0]) 134 self.assertFalse("sample" in result["notifications"][0])
172 135
173 def test_no_variant_no_notifications(self): 136 def test_no_variant_no_notifications(self):
174 self.load_notifications_mock.return_value = [ 137 self.load_notifications_mock.return_value = [
175 { 138 {"id": "a", "variants": [{"sample": 0}]}
176 "id": "a",
177 "variants": [{"sample": 0}]
178 }
179 ] 139 ]
180 result = json.loads(notification.notification({}, lambda *args: None)) 140 result = json.loads(notification.notification({}, lambda *args: None))
181 self.assertEqual(len(result["notifications"]), 0) 141 self.assertEqual(len(result["notifications"]), 0)
182 142
183 @mock.patch("random.random") 143 @mock.patch("random.random")
184 def test_probability_distribution_single_group(self, random_call): 144 def test_probability_distribution_single_group(self, random_call):
185 self.load_notifications_mock.return_value = [ 145 self.load_notifications_mock.return_value = [
186 { 146 {
187 "id": "a", 147 "id": "a",
188 "variants": [ 148 "variants": [
189 { 149 {"sample": 0.5, "title": {"en-US": "1"}, "message": {}},
190 "sample": 0.5, 150 {"sample": 0.25, "title": {"en-US": "2"}, "message": {}},
191 "title": {"en-US": "1"}, 151 {"sample": 0.25, "title": {"en-US": "3"}, "message": {}}
192 "message": {"en-US": ""}
193 },
194 {
195 "sample": 0.25,
196 "title": {"en-US": "2"},
197 "message": {"en-US": ""}
198 },
199 {
200 "sample": 0.25,
201 "title": {"en-US": "3"},
202 "message": {"en-US": ""}
203 }
204 ] 152 ]
205 } 153 }
206 ] 154 ]
207 random_call.return_value = 0 155 random_call.return_value = 0
208 result = json.loads(notification.notification({}, lambda *args: None)) 156 result = json.loads(notification.notification({}, lambda *args: None))
209 self.assertEqual(len(result["notifications"]), 1) 157 self.assertEqual(len(result["notifications"]), 1)
210 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") 158 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1")
211 self.assertRegexpMatches(result["version"], r"-a/1") 159 self.assertRegexpMatches(result["version"], r"-a/1")
212 random_call.return_value = 0.5 160 random_call.return_value = 0.5
213 result = json.loads(notification.notification({}, lambda *args: None)) 161 result = json.loads(notification.notification({}, lambda *args: None))
(...skipping 20 matching lines...) Expand all
234 self.assertEqual(len(result["notifications"]), 1) 182 self.assertEqual(len(result["notifications"]), 1)
235 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3") 183 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3")
236 self.assertRegexpMatches(result["version"], r"-a/3") 184 self.assertRegexpMatches(result["version"], r"-a/3")
237 185
238 @mock.patch("random.random") 186 @mock.patch("random.random")
239 def test_probability_distribution_multiple_groups(self, random_call): 187 def test_probability_distribution_multiple_groups(self, random_call):
240 self.load_notifications_mock.return_value = [ 188 self.load_notifications_mock.return_value = [
241 { 189 {
242 "id": "a", 190 "id": "a",
243 "variants": [ 191 "variants": [
244 { 192 {"sample": 0.25, "title": {"en-US": "1"}, "message": {}},
245 "sample": 0.25, 193 {"sample": 0.25, "title": {"en-US": "2"}, "message": {}}
246 "title": {"en-US": "1"},
247 "message": {"en-US": ""}
248 },
249 {
250 "sample": 0.25,
251 "title": {"en-US": "2"},
252 "message": {"en-US": ""}
253 }
254 ] 194 ]
255 }, 195 },
256 { 196 {
257 "id": "b", 197 "id": "b",
258 "variants": [ 198 "variants": [
259 { 199 {"sample": 0.25, "title": {"en-US": "1"}, "message": {}},
260 "sample": 0.25, 200 {"sample": 0.25, "title": {"en-US": "2"}, "message": {}}
261 "title": {"en-US": "1"},
262 "message": {"en-US": ""}
263 },
264 {
265 "sample": 0.25,
266 "title": {"en-US": "2"},
267 "message": {"en-US": ""}
268 }
269 ] 201 ]
270 } 202 }
271 ] 203 ]
272 random_call.return_value = 0 204 random_call.return_value = 0
273 result = json.loads(notification.notification({}, lambda *args: None)) 205 result = json.loads(notification.notification({}, lambda *args: None))
274 self.assertEqual(len(result["notifications"]), 1) 206 self.assertEqual(len(result["notifications"]), 1)
275 self.assertEqual(result["notifications"][0]["id"], "a") 207 self.assertEqual(result["notifications"][0]["id"], "a")
276 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") 208 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1")
277 self.assertRegexpMatches(result["version"], r"-a/1-b/0") 209 self.assertRegexpMatches(result["version"], r"-a/1-b/0")
278 random_call.return_value = 0.251 210 random_call.return_value = 0.251
(...skipping 21 matching lines...) Expand all
300 lambda *args: None) 232 lambda *args: None)
301 notification.notification({"QUERY_STRING": "lastVersion=-"}, 233 notification.notification({"QUERY_STRING": "lastVersion=-"},
302 lambda *args: None) 234 lambda *args: None)
303 notification.notification({"QUERY_STRING": "lastVersion=-/"}, 235 notification.notification({"QUERY_STRING": "lastVersion=-/"},
304 lambda *args: None) 236 lambda *args: None)
305 notification.notification({"QUERY_STRING": "lastVersion=-//"}, 237 notification.notification({"QUERY_STRING": "lastVersion=-//"},
306 lambda *args: None) 238 lambda *args: None)
307 239
308 def test_version_header_present(self): 240 def test_version_header_present(self):
309 self.load_notifications_mock.return_value = [ 241 self.load_notifications_mock.return_value = [
310 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}} 242 {"id": "1", "title": {}, "message": {}}
311 ] 243 ]
312 response_header_map = {} 244 response_header_map = {}
313 def start_response(status, response_headers): 245 def start_response(status, response_headers):
314 for name, value in response_headers: 246 for name, value in response_headers:
315 response_header_map[name] = value 247 response_header_map[name] = value
316 result = json.loads(notification.notification({}, start_response)) 248 result = json.loads(notification.notification({}, start_response))
317 self.assertEqual(result["version"], 249 self.assertEqual(result["version"],
318 response_header_map["ABP-Notification-Version"]) 250 response_header_map["ABP-Notification-Version"])
319 251
320 def test_default_group_notification_returned_if_valid(self): 252 def test_default_group_notification_returned_if_valid(self):
321 self.load_notifications_mock.return_value = [ 253 self.load_notifications_mock.return_value = [
322 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 254 {"id": "1", "title": {}, "message": {}},
323 { 255 {
324 "id": "a", 256 "id": "a",
325 "title": {"en-US": "0"}, 257 "title": {"en-US": "0"},
326 "message": {"en-US": "0"}, 258 "message": {"en-US": "0"},
327 "variants": [ 259 "variants": [
328 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} 260 {"title": {"en-US": "1"}, "message": {"en-US": "1"}}
329 ] 261 ]
330 } 262 }
331 ] 263 ]
332 result = json.loads(notification.notification({ 264 result = json.loads(notification.notification({
333 "QUERY_STRING": "lastVersion=197001010000-a/0" 265 "QUERY_STRING": "lastVersion=197001010000-a/0"
334 }, lambda *args: None)) 266 }, lambda *args: None))
335 self.assertEqual(len(result["notifications"]), 2) 267 self.assertEqual(len(result["notifications"]), 2)
336 self.assertEqual(result["notifications"][0]["id"], "1") 268 self.assertEqual(result["notifications"][0]["id"], "1")
337 self.assertEqual(result["notifications"][1]["id"], "a") 269 self.assertEqual(result["notifications"][1]["id"], "a")
338 self.assertEqual(result["notifications"][1]["title"]["en-US"], "0") 270 self.assertEqual(result["notifications"][1]["title"]["en-US"], "0")
339 self.assertNotIn("variants", result["notifications"][1]) 271 self.assertNotIn("variants", result["notifications"][1])
340 self.assertRegexpMatches(result["version"], r"-a/0") 272 self.assertRegexpMatches(result["version"], r"-a/0")
341 273
342 def test_default_group_notification_not_returned_if_invalid(self): 274 def test_default_group_notification_not_returned_if_invalid(self):
343 self.load_notifications_mock.return_value = [ 275 self.load_notifications_mock.return_value = [
344 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 276 {"id": "1", "title": {}, "message": {}},
345 { 277 {
346 "id": "a", 278 "id": "a",
347 "title": {"en-US": "0"}, 279 "title": {"en-US": "0"},
348 "variants": [ 280 "variants": [
349 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} 281 {"title": {"en-US": "1"}, "message": {"en-US": "1"}}
350 ] 282 ]
351 } 283 }
352 ] 284 ]
353 result = json.loads(notification.notification({ 285 result = json.loads(notification.notification({
354 "QUERY_STRING": "lastVersion=197001010000-a/0" 286 "QUERY_STRING": "lastVersion=197001010000-a/0"
355 }, lambda *args: None)) 287 }, lambda *args: None))
356 self.assertEqual(len(result["notifications"]), 1) 288 self.assertEqual(len(result["notifications"]), 1)
357 self.assertEqual(result["notifications"][0]["id"], "1") 289 self.assertEqual(result["notifications"][0]["id"], "1")
358 self.assertRegexpMatches(result["version"], r"-a/0") 290 self.assertRegexpMatches(result["version"], r"-a/0")
359 291
360 def test_invalid_notification_not_returned(self): 292 def test_invalid_notification_not_returned(self):
361 self.load_notifications_mock.return_value = [ 293 self.load_notifications_mock.return_value = [
362 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, 294 {"id": "1", "title": {}, "message": {}},
363 {"id": "2", "title": {"en-US": ""}}, 295 {"id": "2", "title": {}},
364 {"id": "3", "title": {}, "message": {"en-US": ""}}, 296 {"id": "3", "message": {}}
365 {"id": "4", "message": {"en-US": ""}},
366 {"id": "5", "title": {"en-US": ""}, "message": {}},
367 ] 297 ]
368 result = json.loads(notification.notification({}, lambda *args: None)) 298 result = json.loads(notification.notification({}, lambda *args: None))
369 self.assertEqual(len(result["notifications"]), 1) 299 self.assertEqual(len(result["notifications"]), 1)
370 self.assertEqual(result["notifications"][0]["id"], "1") 300 self.assertEqual(result["notifications"][0]["id"], "1")
371 301
372 if __name__ == '__main__': 302 if __name__ == '__main__':
373 unittest.main() 303 unittest.main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld