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

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

Issue 4883267715072000: Issue 2276 - Handle groups in notification.json requests (Closed)
Left Patch Set: Refactor Created April 17, 2015, 3 a.m.
Right Patch Set: Extract group parsing Created June 12, 2015, 12:11 p.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 | « sitescripts/notifications/test/__init__.py ('k') | sitescripts/notifications/web/__init__.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,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import json 18 import json
19 import mock 19 import mock
20 import unittest 20 import unittest
21 21
22 import sitescripts.notifications.web.notification as notification 22 import sitescripts.notifications.web.notification as notification
23 23
24 class TestNotification(unittest.TestCase): 24 class TestNotification(unittest.TestCase):
25 def test_no_group(self): 25 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
26 notification.load_notifications = lambda: [{"id": "1"}] 26 def test_no_group(self, load_notifications_call):
27 load_notifications_call.return_value = [{"id": "1"}]
27 result = json.loads(notification.notification({}, lambda *args: None)) 28 result = json.loads(notification.notification({}, lambda *args: None))
28 self.assertEqual(len(result["notifications"]), 1) 29 self.assertEqual(len(result["notifications"]), 1)
29 self.assertEqual(result["notifications"][0]["id"], "1") 30 self.assertEqual(result["notifications"][0]["id"], "1")
30 self.assertFalse("-" in result["version"]) 31 self.assertFalse("-" in result["version"])
31 32
32 def test_not_in_group(self): 33 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
33 notification.load_notifications = lambda: [ 34 def test_not_in_group(self, load_notifications_call):
35 load_notifications_call.return_value = [
34 {"id": "1"}, 36 {"id": "1"},
35 { 37 {
36 "id": "a", 38 "id": "a",
37 "variants": [{}] 39 "variants": [{}]
38 } 40 }
39 ] 41 ]
40 result = json.loads(notification.notification({ 42 result = json.loads(notification.notification({
41 "QUERY_STRING": "lastVersion=197001010000-a/0" 43 "QUERY_STRING": "lastVersion=197001010000-a/0"
42 }, lambda *args: None)) 44 }, lambda *args: None))
43 self.assertEqual(len(result["notifications"]), 1) 45 self.assertEqual(len(result["notifications"]), 1)
44 self.assertEqual(result["notifications"][0]["id"], "1") 46 self.assertEqual(result["notifications"][0]["id"], "1")
45 self.assertRegexpMatches(result["version"], r"-a/0") 47 self.assertRegexpMatches(result["version"], r"-a/0")
46 48
47 def test_in_group(self): 49 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
48 notification.load_notifications = lambda: [ 50 def test_in_group(self, load_notifications_call):
51 load_notifications_call.return_value = [
49 {"id": "1"}, 52 {"id": "1"},
50 { 53 {
51 "id": "a", 54 "id": "a",
52 "variants": [{}] 55 "variants": [{}]
53 } 56 }
54 ] 57 ]
55 result = json.loads(notification.notification({ 58 result = json.loads(notification.notification({
56 "QUERY_STRING": "lastVersion=197001010000-a/1" 59 "QUERY_STRING": "lastVersion=197001010000-a/1"
57 }, lambda *args: None)) 60 }, lambda *args: None))
58 self.assertEqual(len(result["notifications"]), 1) 61 self.assertEqual(len(result["notifications"]), 1)
59 self.assertEqual(result["notifications"][0]["id"], "a") 62 self.assertEqual(result["notifications"][0]["id"], "a")
60 self.assertRegexpMatches(result["version"], r"-a/1") 63 self.assertRegexpMatches(result["version"], r"-a/1")
61 64
62 def test_not_in_one_of_many_groups(self): 65 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
63 notification.load_notifications = lambda: [ 66 def test_not_in_one_of_many_groups(self, load_notifications_call):
67 load_notifications_call.return_value = [
64 {"id": "1"}, 68 {"id": "1"},
65 { 69 {
66 "id": "a", 70 "id": "a",
67 "variants": [{}] 71 "variants": [{}]
68 }, 72 },
69 { 73 {
70 "id": "b", 74 "id": "b",
71 "variants": [{}] 75 "variants": [{}]
72 }, 76 },
73 { 77 {
74 "id": "c", 78 "id": "c",
75 "variants": [{}] 79 "variants": [{}]
76 } 80 }
77 ] 81 ]
78 result = json.loads(notification.notification({ 82 result = json.loads(notification.notification({
79 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" 83 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0"
80 }, lambda *args: None)) 84 }, lambda *args: None))
81 self.assertEqual(len(result["notifications"]), 1) 85 self.assertEqual(len(result["notifications"]), 1)
82 self.assertEqual(result["notifications"][0]["id"], "1") 86 self.assertEqual(result["notifications"][0]["id"], "1")
83 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") 87 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0")
84 88
85 def test_in_one_of_many_groups(self): 89 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
86 notification.load_notifications = lambda: [ 90 def test_in_one_of_many_groups(self, load_notifications_call):
Sebastian Noack 2015/04/20 14:44:39 You should restore the orginal value. Feel free to
Felix Dahlke 2015/04/23 22:15:18 Done.
91 load_notifications_call.return_value = [
87 {"id": "1"}, 92 {"id": "1"},
88 { 93 {
89 "id": "a", 94 "id": "a",
90 "variants": [{}] 95 "variants": [{}]
91 }, 96 },
92 { 97 {
93 "id": "b", 98 "id": "b",
94 "variants": [{}] 99 "variants": [{}]
95 }, 100 },
96 { 101 {
97 "id": "c", 102 "id": "c",
98 "variants": [{}] 103 "variants": [{}]
99 } 104 }
100 ] 105 ]
101 result = json.loads(notification.notification({ 106 result = json.loads(notification.notification({
102 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" 107 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0"
103 }, lambda *args: None)) 108 }, lambda *args: None))
104 self.assertEqual(len(result["notifications"]), 1) 109 self.assertEqual(len(result["notifications"]), 1)
105 self.assertEqual(result["notifications"][0]["id"], "b") 110 self.assertEqual(result["notifications"][0]["id"], "b")
106 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") 111 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0")
107 112
108 def test_not_put_in_group(self): 113 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
109 notification.load_notifications = lambda: [ 114 def test_not_put_in_group(self, load_notifications_call):
115 load_notifications_call.return_value = [
110 {"id": "1"}, 116 {"id": "1"},
111 { 117 {
112 "id": "a", 118 "id": "a",
113 "variants": [{"sample": 0}] 119 "variants": [{"sample": 0}]
114 } 120 }
115 ] 121 ]
116 result = json.loads(notification.notification({ 122 result = json.loads(notification.notification({
117 "QUERY_STRING": "lastVersion=197001010000" 123 "QUERY_STRING": "lastVersion=197001010000"
118 }, lambda *args: None)) 124 }, lambda *args: None))
119 self.assertEqual(len(result["notifications"]), 1) 125 self.assertEqual(len(result["notifications"]), 1)
120 self.assertEqual(result["notifications"][0]["id"], "1") 126 self.assertEqual(result["notifications"][0]["id"], "1")
121 self.assertRegexpMatches(result["version"], r"-a/0") 127 self.assertRegexpMatches(result["version"], r"-a/0")
122 128
123 def test_put_in_group(self): 129 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
124 notification.load_notifications = lambda: [ 130 def test_put_in_group(self, load_notifications_call):
131 load_notifications_call.return_value = [
125 {"id": "1"}, 132 {"id": "1"},
126 { 133 {
127 "id": "a", 134 "id": "a",
128 "variants": [{"sample": 1}] 135 "variants": [{"sample": 1}]
129 } 136 }
130 ] 137 ]
131 result = json.loads(notification.notification({ 138 result = json.loads(notification.notification({
132 "QUERY_STRING": "lastVersion=197001010000" 139 "QUERY_STRING": "lastVersion=197001010000"
133 }, lambda *args: None)) 140 }, lambda *args: None))
134 self.assertEqual(len(result["notifications"]), 1) 141 self.assertEqual(len(result["notifications"]), 1)
135 self.assertEqual(result["notifications"][0]["id"], "a") 142 self.assertEqual(result["notifications"][0]["id"], "a")
136 self.assertRegexpMatches(result["version"], r"-a/1") 143 self.assertRegexpMatches(result["version"], r"-a/1")
137 144
138 def test_notification_variant_merged(self): 145 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
139 notification.load_notifications = lambda: [ 146 def test_notification_variant_merged(self, load_notifications_call):
147 load_notifications_call.return_value = [
140 { 148 {
141 "id": "a", 149 "id": "a",
142 "title.en-GB": "default", 150 "title.en-GB": "default",
143 "message.en-GB": "default", 151 "message.en-GB": "default",
144 "message.de-DE": "vorgabe", 152 "message.de-DE": "vorgabe",
145 "variants": [ 153 "variants": [
146 { 154 {
147 "sample": 1, 155 "sample": 1,
148 "message.en-GB": "variant" 156 "message.en-GB": "variant"
149 } 157 }
150 ] 158 ]
151 } 159 }
152 ] 160 ]
153 result = json.loads(notification.notification({}, lambda *args: None)) 161 result = json.loads(notification.notification({}, lambda *args: None))
154 self.assertEqual(len(result["notifications"]), 1) 162 self.assertEqual(len(result["notifications"]), 1)
155 self.assertEqual(result["notifications"][0]["id"], "a") 163 self.assertEqual(result["notifications"][0]["id"], "a")
156 self.assertEqual(result["notifications"][0]["title.en-GB"], "default") 164 self.assertEqual(result["notifications"][0]["title.en-GB"], "default")
157 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant") 165 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant")
158 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe") 166 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe")
159 self.assertFalse("variants" in result["notifications"][0]) 167 self.assertFalse("variants" in result["notifications"][0])
160 self.assertFalse("sample" in result["notifications"][0]) 168 self.assertFalse("sample" in result["notifications"][0])
161 169
162 def test_no_variant_no_notifications(self): 170 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
163 notification.load_notifications = lambda: [ 171 def test_no_variant_no_notifications(self, load_notifications_call):
172 load_notifications_call.return_value = [
164 { 173 {
165 "id": "a", 174 "id": "a",
166 "variants": [{"sample": 0}] 175 "variants": [{"sample": 0}]
167 } 176 }
168 ] 177 ]
169 result = json.loads(notification.notification({}, lambda *args: None)) 178 result = json.loads(notification.notification({}, lambda *args: None))
170 self.assertEqual(len(result["notifications"]), 0) 179 self.assertEqual(len(result["notifications"]), 0)
171 180
172 @mock.patch("random.random") 181 @mock.patch("random.random")
173 def test_probability_distribution_single_group(self, random_call): 182 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
174 notification.load_notifications = lambda: [ 183 def test_probability_distribution_single_group(
184 self, load_notifications_call, random_call):
185 load_notifications_call.return_value = [
175 { 186 {
176 "id": "a", 187 "id": "a",
177 "variants": [ 188 "variants": [
178 { 189 {
179 "sample": 0.5, 190 "sample": 0.5,
180 "title.en-GB": "1" 191 "title.en-GB": "1"
181 }, 192 },
182 { 193 {
183 "sample": 0.25, 194 "sample": 0.25,
184 "title.en-GB": "2" 195 "title.en-GB": "2"
(...skipping 30 matching lines...) Expand all
215 self.assertEqual(len(result["notifications"]), 1) 226 self.assertEqual(len(result["notifications"]), 1)
216 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") 227 self.assertEqual(result["notifications"][0]["title.en-GB"], "3")
217 self.assertRegexpMatches(result["version"], r"-a/3") 228 self.assertRegexpMatches(result["version"], r"-a/3")
218 random_call.return_value = 1 229 random_call.return_value = 1
219 result = json.loads(notification.notification({}, lambda *args: None)) 230 result = json.loads(notification.notification({}, lambda *args: None))
220 self.assertEqual(len(result["notifications"]), 1) 231 self.assertEqual(len(result["notifications"]), 1)
221 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") 232 self.assertEqual(result["notifications"][0]["title.en-GB"], "3")
222 self.assertRegexpMatches(result["version"], r"-a/3") 233 self.assertRegexpMatches(result["version"], r"-a/3")
223 234
224 @mock.patch("random.random") 235 @mock.patch("random.random")
225 def test_probability_distribution_multiple_groups(self, random_call): 236 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
226 notification.load_notifications = lambda: [ 237 def test_probability_distribution_multiple_groups(
238 self, load_notifications_call, random_call):
239 load_notifications_call.return_value = [
227 { 240 {
228 "id": "a", 241 "id": "a",
229 "variants": [ 242 "variants": [
230 { 243 {
231 "sample": 0.25, 244 "sample": 0.25,
232 "title.en-GB": "1" 245 "title.en-GB": "1"
233 }, 246 },
234 { 247 {
235 "sample": 0.25, 248 "sample": 0.25,
236 "title.en-GB": "2" 249 "title.en-GB": "2"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 self.assertEqual(result["notifications"][0]["id"], "b") 282 self.assertEqual(result["notifications"][0]["id"], "b")
270 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") 283 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
271 self.assertRegexpMatches(result["version"], r"-a/0-b/1") 284 self.assertRegexpMatches(result["version"], r"-a/0-b/1")
272 random_call.return_value = 0.751 285 random_call.return_value = 0.751
273 result = json.loads(notification.notification({}, lambda *args: None)) 286 result = json.loads(notification.notification({}, lambda *args: None))
274 self.assertEqual(len(result["notifications"]), 1) 287 self.assertEqual(len(result["notifications"]), 1)
275 self.assertEqual(result["notifications"][0]["id"], "b") 288 self.assertEqual(result["notifications"][0]["id"], "b")
276 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") 289 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
277 self.assertRegexpMatches(result["version"], r"-a/0-b/2") 290 self.assertRegexpMatches(result["version"], r"-a/0-b/2")
278 291
279 def test_invalid_last_version(self): 292 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
280 notification.load_notifications = lambda: [] 293 def test_invalid_last_version(self, load_notifications_call):
294 load_notifications_call.return_value = []
281 notification.notification({"QUERY_STRING": "lastVersion="}, 295 notification.notification({"QUERY_STRING": "lastVersion="},
282 lambda *args: None) 296 lambda *args: None)
283 notification.notification({"QUERY_STRING": "lastVersion=-"}, 297 notification.notification({"QUERY_STRING": "lastVersion=-"},
284 lambda *args: None) 298 lambda *args: None)
285 notification.notification({"QUERY_STRING": "lastVersion=-/"}, 299 notification.notification({"QUERY_STRING": "lastVersion=-/"},
286 lambda *args: None) 300 lambda *args: None)
301 notification.notification({"QUERY_STRING": "lastVersion=-//"},
302 lambda *args: None)
287 303
288 if __name__ == '__main__': 304 if __name__ == '__main__':
289 unittest.main() 305 unittest.main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld