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: Created April 6, 2015, 10:02 p.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 def test_no_group(self):
26 notification.load_notifications = lambda: [{"id": "1"}]
27 result = json.loads(notification.notification({}, lambda *args: None))
28 self.assertEqual(len(result["notifications"]), 1)
29 self.assertEqual(result["notifications"][0]["id"], "1")
30 self.assertFalse("-" in result["version"])
31
32 def test_not_in_group(self):
33 notification.load_notifications = lambda: [
34 {"id": "1"},
35 {
36 "id": "a",
37 "variants": [{}]
38 }
39 ]
40 result = json.loads(notification.notification({
41 "QUERY_STRING": "lastVersion=197001010000-a/0"
42 }, lambda *args: None))
43 self.assertEqual(len(result["notifications"]), 1)
44 self.assertEqual(result["notifications"][0]["id"], "1")
45 self.assertRegexpMatches(result["version"], r"-a/0")
46
47 def test_in_group(self):
48 notification.load_notifications = lambda: [
49 {"id": "1"},
50 {
51 "id": "a",
52 "variants": [{}]
53 }
54 ]
55 result = json.loads(notification.notification({
56 "QUERY_STRING": "lastVersion=197001010000-a/1"
57 }, lambda *args: None))
58 self.assertEqual(len(result["notifications"]), 1)
59 self.assertEqual(result["notifications"][0]["id"], "a")
60 self.assertRegexpMatches(result["version"], r"-a/1")
61
62 def test_not_in_one_of_many_groups(self):
63 notification.load_notifications = lambda: [
64 {"id": "1"},
65 {
66 "id": "a",
67 "variants": [{}]
68 },
69 {
70 "id": "b",
71 "variants": [{}]
72 },
73 {
74 "id": "c",
75 "variants": [{}]
76 }
77 ]
78 result = json.loads(notification.notification({
79 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0"
80 }, lambda *args: None))
81 self.assertEqual(len(result["notifications"]), 1)
82 self.assertEqual(result["notifications"][0]["id"], "1")
83 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0")
84
85 def test_in_one_of_many_groups(self):
86 notification.load_notifications = lambda: [
87 {"id": "1"},
88 {
89 "id": "a",
90 "variants": [{}]
91 },
92 {
93 "id": "b",
94 "variants": [{}]
95 },
96 {
97 "id": "c",
98 "variants": [{}]
99 }
100 ]
101 result = json.loads(notification.notification({
102 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0"
103 }, lambda *args: None))
104 self.assertEqual(len(result["notifications"]), 1)
105 self.assertEqual(result["notifications"][0]["id"], "b")
106 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0")
107
108 def test_not_put_in_group(self):
109 notification.load_notifications = lambda: [
110 {"id": "1"},
111 {
112 "id": "a",
113 "variants": [{"sample": 0}]
114 }
115 ]
116 result = json.loads(notification.notification({
117 "QUERY_STRING": "lastVersion=197001010000"
118 }, lambda *args: None))
119 self.assertEqual(len(result["notifications"]), 1)
120 self.assertEqual(result["notifications"][0]["id"], "1")
121 self.assertRegexpMatches(result["version"], r"-a/0")
122
123 def test_put_in_group(self):
124 notification.load_notifications = lambda: [
125 {"id": "1"},
126 {
127 "id": "a",
128 "variants": [{"sample": 1}]
129 }
130 ]
131 result = json.loads(notification.notification({
132 "QUERY_STRING": "lastVersion=197001010000"
133 }, lambda *args: None))
134 self.assertEqual(len(result["notifications"]), 1)
135 self.assertEqual(result["notifications"][0]["id"], "a")
136 self.assertRegexpMatches(result["version"], r"-a/1")
137
138 def test_notification_variant_merged(self):
139 notification.load_notifications = lambda: [
140 {
141 "id": "a",
142 "title.en-GB": "default",
143 "message.en-GB": "default",
144 "variants": [
145 {
146 "sample": 1,
147 "message.en-GB": "variant"
148 }
149 ]
150 }
151 ]
152 result = json.loads(notification.notification({}, lambda *args: None))
153 self.assertEqual(len(result["notifications"]), 1)
154 self.assertEqual(result["notifications"][0]["id"], "a")
155 self.assertEqual(result["notifications"][0]["title.en-GB"], "default")
156 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant")
157
158 def test_no_variant_no_notifications(self):
159 notification.load_notifications = lambda: [
160 {
161 "id": "a",
162 "variants": [{"sample": 0}]
163 }
164 ]
165 result = json.loads(notification.notification({}, lambda *args: None))
166 self.assertEqual(len(result["notifications"]), 0)
167
168 @mock.patch("random.random")
169 def test_probability_distribution_single_group(self, random_call):
170 notification.load_notifications = lambda: [
171 {
172 "id": "a",
173 "variants": [
174 {
175 "sample": 0.5,
176 "title.en-GB": "1"
177 },
178 {
179 "sample": 0.25,
180 "title.en-GB": "2"
181 },
182 {
183 "sample": 0.25,
184 "title.en-GB": "3"
185 }
186 ]
187 }
188 ]
189 random_call.return_value = 0
190 result = json.loads(notification.notification({}, lambda *args: None))
191 self.assertEqual(len(result["notifications"]), 1)
192 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
193 self.assertRegexpMatches(result["version"], r"-a/1")
194 random_call.return_value = 0.5
195 result = json.loads(notification.notification({}, lambda *args: None))
196 self.assertEqual(len(result["notifications"]), 1)
197 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
198 self.assertRegexpMatches(result["version"], r"-a/1")
199 random_call.return_value = 0.51
200 result = json.loads(notification.notification({}, lambda *args: None))
201 self.assertEqual(len(result["notifications"]), 1)
202 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
203 self.assertRegexpMatches(result["version"], r"-a/2")
204 random_call.return_value = 0.75
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"], "2")
208 self.assertRegexpMatches(result["version"], r"-a/2")
209 random_call.return_value = 0.751
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"], "3")
213 self.assertRegexpMatches(result["version"], r"-a/3")
214 random_call.return_value = 1
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"], "3")
218 self.assertRegexpMatches(result["version"], r"-a/3")
219
220 @mock.patch("random.random")
221 def test_probability_distribution_multiple_groups(self, random_call):
222 notification.load_notifications = lambda: [
223 {
224 "id": "a",
225 "variants": [
226 {
227 "sample": 0.25,
228 "title.en-GB": "1"
229 },
230 {
231 "sample": 0.25,
232 "title.en-GB": "2"
233 }
234 ]
235 },
236 {
237 "id": "b",
238 "variants": [
239 {
240 "sample": 0.25,
241 "title.en-GB": "1"
242 },
243 {
244 "sample": 0.25,
245 "title.en-GB": "2"
246 }
247 ]
248 }
249 ]
250 random_call.return_value = 0
251 result = json.loads(notification.notification({}, lambda *args: None))
252 self.assertEqual(len(result["notifications"]), 1)
253 self.assertEqual(result["notifications"][0]["id"], "a")
254 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
255 self.assertRegexpMatches(result["version"], r"-a/1-b/0")
256 random_call.return_value = 0.251
257 result = json.loads(notification.notification({}, lambda *args: None))
258 self.assertEqual(len(result["notifications"]), 1)
259 self.assertEqual(result["notifications"][0]["id"], "a")
260 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
261 self.assertRegexpMatches(result["version"], r"-a/2-b/0")
262 random_call.return_value = 0.51
263 result = json.loads(notification.notification({}, lambda *args: None))
264 self.assertEqual(len(result["notifications"]), 1)
265 self.assertEqual(result["notifications"][0]["id"], "b")
266 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
267 self.assertRegexpMatches(result["version"], r"-a/0-b/1")
268 random_call.return_value = 0.751
269 result = json.loads(notification.notification({}, lambda *args: None))
270 self.assertEqual(len(result["notifications"]), 1)
271 self.assertEqual(result["notifications"][0]["id"], "b")
272 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
273 self.assertRegexpMatches(result["version"], r"-a/0-b/2")
274
275 def test_invalid_last_version(self):
276 notification.load_notifications = lambda: []
277 notification.notification({"QUERY_STRING": "lastVersion="},
278 lambda *args: None)
279 notification.notification({"QUERY_STRING": "lastVersion=-"},
280 lambda *args: None)
281 notification.notification({"QUERY_STRING": "lastVersion=-/"},
282 lambda *args: None)
283
284 if __name__ == '__main__':
285 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