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

Side by Side Diff: sitescripts/notifications/test/notification.py

Issue 4883267715072000: Issue 2276 - Handle groups in notification.json requests (Closed)
Patch Set: Address comments Created June 12, 2015, 11 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/notifications/test/__init__.py ('k') | sitescripts/notifications/web/__init__.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import json
19 import mock
20 import unittest
21
22 import sitescripts.notifications.web.notification as notification
23
24 class TestNotification(unittest.TestCase):
25 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
26 def test_no_group(self, load_notifications_call):
27 load_notifications_call.return_value = [{"id": "1"}]
28 result = json.loads(notification.notification({}, lambda *args: None))
29 self.assertEqual(len(result["notifications"]), 1)
30 self.assertEqual(result["notifications"][0]["id"], "1")
31 self.assertFalse("-" in result["version"])
32
33 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
34 def test_not_in_group(self, load_notifications_call):
35 load_notifications_call.return_value = [
36 {"id": "1"},
37 {
38 "id": "a",
39 "variants": [{}]
40 }
41 ]
42 result = json.loads(notification.notification({
43 "QUERY_STRING": "lastVersion=197001010000-a/0"
44 }, lambda *args: None))
45 self.assertEqual(len(result["notifications"]), 1)
46 self.assertEqual(result["notifications"][0]["id"], "1")
47 self.assertRegexpMatches(result["version"], r"-a/0")
48
49 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
50 def test_in_group(self, load_notifications_call):
51 load_notifications_call.return_value = [
52 {"id": "1"},
53 {
54 "id": "a",
55 "variants": [{}]
56 }
57 ]
58 result = json.loads(notification.notification({
59 "QUERY_STRING": "lastVersion=197001010000-a/1"
60 }, lambda *args: None))
61 self.assertEqual(len(result["notifications"]), 1)
62 self.assertEqual(result["notifications"][0]["id"], "a")
63 self.assertRegexpMatches(result["version"], r"-a/1")
64
65 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
66 def test_not_in_one_of_many_groups(self, load_notifications_call):
67 load_notifications_call.return_value = [
68 {"id": "1"},
69 {
70 "id": "a",
71 "variants": [{}]
72 },
73 {
74 "id": "b",
75 "variants": [{}]
76 },
77 {
78 "id": "c",
79 "variants": [{}]
80 }
81 ]
82 result = json.loads(notification.notification({
83 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0"
84 }, lambda *args: None))
85 self.assertEqual(len(result["notifications"]), 1)
86 self.assertEqual(result["notifications"][0]["id"], "1")
87 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0")
88
89 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
90 def test_in_one_of_many_groups(self, load_notifications_call):
91 load_notifications_call.return_value = [
92 {"id": "1"},
93 {
94 "id": "a",
95 "variants": [{}]
96 },
97 {
98 "id": "b",
99 "variants": [{}]
100 },
101 {
102 "id": "c",
103 "variants": [{}]
104 }
105 ]
106 result = json.loads(notification.notification({
107 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0"
108 }, lambda *args: None))
109 self.assertEqual(len(result["notifications"]), 1)
110 self.assertEqual(result["notifications"][0]["id"], "b")
111 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0")
112
113 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
114 def test_not_put_in_group(self, load_notifications_call):
115 load_notifications_call.return_value = [
116 {"id": "1"},
117 {
118 "id": "a",
119 "variants": [{"sample": 0}]
120 }
121 ]
122 result = json.loads(notification.notification({
123 "QUERY_STRING": "lastVersion=197001010000"
124 }, lambda *args: None))
125 self.assertEqual(len(result["notifications"]), 1)
126 self.assertEqual(result["notifications"][0]["id"], "1")
127 self.assertRegexpMatches(result["version"], r"-a/0")
128
129 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
130 def test_put_in_group(self, load_notifications_call):
131 load_notifications_call.return_value = [
132 {"id": "1"},
133 {
134 "id": "a",
135 "variants": [{"sample": 1}]
136 }
137 ]
138 result = json.loads(notification.notification({
139 "QUERY_STRING": "lastVersion=197001010000"
140 }, lambda *args: None))
141 self.assertEqual(len(result["notifications"]), 1)
142 self.assertEqual(result["notifications"][0]["id"], "a")
143 self.assertRegexpMatches(result["version"], r"-a/1")
144
145 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
146 def test_notification_variant_merged(self, load_notifications_call):
147 load_notifications_call.return_value = [
148 {
149 "id": "a",
150 "title.en-GB": "default",
151 "message.en-GB": "default",
152 "message.de-DE": "vorgabe",
153 "variants": [
154 {
155 "sample": 1,
156 "message.en-GB": "variant"
157 }
158 ]
159 }
160 ]
161 result = json.loads(notification.notification({}, lambda *args: None))
162 self.assertEqual(len(result["notifications"]), 1)
163 self.assertEqual(result["notifications"][0]["id"], "a")
164 self.assertEqual(result["notifications"][0]["title.en-GB"], "default")
165 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant")
166 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe")
167 self.assertFalse("variants" in result["notifications"][0])
168 self.assertFalse("sample" in result["notifications"][0])
169
170 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
171 def test_no_variant_no_notifications(self, load_notifications_call):
172 load_notifications_call.return_value = [
173 {
174 "id": "a",
175 "variants": [{"sample": 0}]
176 }
177 ]
178 result = json.loads(notification.notification({}, lambda *args: None))
179 self.assertEqual(len(result["notifications"]), 0)
180
181 @mock.patch("random.random")
182 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
183 def test_probability_distribution_single_group(
184 self, load_notifications_call, random_call):
185 load_notifications_call.return_value = [
186 {
187 "id": "a",
188 "variants": [
189 {
190 "sample": 0.5,
191 "title.en-GB": "1"
192 },
193 {
194 "sample": 0.25,
195 "title.en-GB": "2"
196 },
197 {
198 "sample": 0.25,
199 "title.en-GB": "3"
200 }
201 ]
202 }
203 ]
204 random_call.return_value = 0
205 result = json.loads(notification.notification({}, lambda *args: None))
206 self.assertEqual(len(result["notifications"]), 1)
207 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
208 self.assertRegexpMatches(result["version"], r"-a/1")
209 random_call.return_value = 0.5
210 result = json.loads(notification.notification({}, lambda *args: None))
211 self.assertEqual(len(result["notifications"]), 1)
212 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
213 self.assertRegexpMatches(result["version"], r"-a/1")
214 random_call.return_value = 0.51
215 result = json.loads(notification.notification({}, lambda *args: None))
216 self.assertEqual(len(result["notifications"]), 1)
217 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
218 self.assertRegexpMatches(result["version"], r"-a/2")
219 random_call.return_value = 0.75
220 result = json.loads(notification.notification({}, lambda *args: None))
221 self.assertEqual(len(result["notifications"]), 1)
222 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
223 self.assertRegexpMatches(result["version"], r"-a/2")
224 random_call.return_value = 0.751
225 result = json.loads(notification.notification({}, lambda *args: None))
226 self.assertEqual(len(result["notifications"]), 1)
227 self.assertEqual(result["notifications"][0]["title.en-GB"], "3")
228 self.assertRegexpMatches(result["version"], r"-a/3")
229 random_call.return_value = 1
230 result = json.loads(notification.notification({}, lambda *args: None))
231 self.assertEqual(len(result["notifications"]), 1)
232 self.assertEqual(result["notifications"][0]["title.en-GB"], "3")
233 self.assertRegexpMatches(result["version"], r"-a/3")
234
235 @mock.patch("random.random")
236 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
237 def test_probability_distribution_multiple_groups(
238 self, load_notifications_call, random_call):
239 load_notifications_call.return_value = [
240 {
241 "id": "a",
242 "variants": [
243 {
244 "sample": 0.25,
245 "title.en-GB": "1"
246 },
247 {
248 "sample": 0.25,
249 "title.en-GB": "2"
250 }
251 ]
252 },
253 {
254 "id": "b",
255 "variants": [
256 {
257 "sample": 0.25,
258 "title.en-GB": "1"
259 },
260 {
261 "sample": 0.25,
262 "title.en-GB": "2"
263 }
264 ]
265 }
266 ]
267 random_call.return_value = 0
268 result = json.loads(notification.notification({}, lambda *args: None))
269 self.assertEqual(len(result["notifications"]), 1)
270 self.assertEqual(result["notifications"][0]["id"], "a")
271 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
272 self.assertRegexpMatches(result["version"], r"-a/1-b/0")
273 random_call.return_value = 0.251
274 result = json.loads(notification.notification({}, lambda *args: None))
275 self.assertEqual(len(result["notifications"]), 1)
276 self.assertEqual(result["notifications"][0]["id"], "a")
277 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
278 self.assertRegexpMatches(result["version"], r"-a/2-b/0")
279 random_call.return_value = 0.51
280 result = json.loads(notification.notification({}, lambda *args: None))
281 self.assertEqual(len(result["notifications"]), 1)
282 self.assertEqual(result["notifications"][0]["id"], "b")
283 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
284 self.assertRegexpMatches(result["version"], r"-a/0-b/1")
285 random_call.return_value = 0.751
286 result = json.loads(notification.notification({}, lambda *args: None))
287 self.assertEqual(len(result["notifications"]), 1)
288 self.assertEqual(result["notifications"][0]["id"], "b")
289 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
290 self.assertRegexpMatches(result["version"], r"-a/0-b/2")
291
292 @mock.patch("sitescripts.notifications.web.notification.load_notifications")
293 def test_invalid_last_version(self, load_notifications_call):
294 load_notifications_call.return_value = []
295 notification.notification({"QUERY_STRING": "lastVersion="},
296 lambda *args: None)
297 notification.notification({"QUERY_STRING": "lastVersion=-"},
298 lambda *args: None)
299 notification.notification({"QUERY_STRING": "lastVersion=-/"},
300 lambda *args: None)
301 notification.notification({"QUERY_STRING": "lastVersion=-//"},
302 lambda *args: None)
303
304 if __name__ == '__main__':
305 unittest.main()
OLDNEW
« no previous file with comments | « sitescripts/notifications/test/__init__.py ('k') | sitescripts/notifications/web/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld