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: Test that translations are mixed properly Created April 11, 2015, 9:10 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 "message.de-DE": "vorgabe",
145 "variants": [
146 {
147 "sample": 1,
148 "message.en-GB": "variant"
149 }
150 ]
151 }
152 ]
153 result = json.loads(notification.notification({}, lambda *args: None))
154 self.assertEqual(len(result["notifications"]), 1)
155 self.assertEqual(result["notifications"][0]["id"], "a")
156 self.assertEqual(result["notifications"][0]["title.en-GB"], "default")
157 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant")
158 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe")
159
160 def test_no_variant_no_notifications(self):
161 notification.load_notifications = lambda: [
162 {
163 "id": "a",
164 "variants": [{"sample": 0}]
165 }
166 ]
167 result = json.loads(notification.notification({}, lambda *args: None))
168 self.assertEqual(len(result["notifications"]), 0)
169
170 @mock.patch("random.random")
171 def test_probability_distribution_single_group(self, random_call):
172 notification.load_notifications = lambda: [
173 {
174 "id": "a",
175 "variants": [
176 {
177 "sample": 0.5,
178 "title.en-GB": "1"
179 },
180 {
181 "sample": 0.25,
182 "title.en-GB": "2"
183 },
184 {
185 "sample": 0.25,
186 "title.en-GB": "3"
187 }
188 ]
189 }
190 ]
191 random_call.return_value = 0
192 result = json.loads(notification.notification({}, lambda *args: None))
193 self.assertEqual(len(result["notifications"]), 1)
194 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
195 self.assertRegexpMatches(result["version"], r"-a/1")
196 random_call.return_value = 0.5
197 result = json.loads(notification.notification({}, lambda *args: None))
198 self.assertEqual(len(result["notifications"]), 1)
199 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
200 self.assertRegexpMatches(result["version"], r"-a/1")
201 random_call.return_value = 0.51
202 result = json.loads(notification.notification({}, lambda *args: None))
203 self.assertEqual(len(result["notifications"]), 1)
204 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
205 self.assertRegexpMatches(result["version"], r"-a/2")
206 random_call.return_value = 0.75
207 result = json.loads(notification.notification({}, lambda *args: None))
208 self.assertEqual(len(result["notifications"]), 1)
209 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
210 self.assertRegexpMatches(result["version"], r"-a/2")
211 random_call.return_value = 0.751
212 result = json.loads(notification.notification({}, lambda *args: None))
213 self.assertEqual(len(result["notifications"]), 1)
214 self.assertEqual(result["notifications"][0]["title.en-GB"], "3")
215 self.assertRegexpMatches(result["version"], r"-a/3")
216 random_call.return_value = 1
217 result = json.loads(notification.notification({}, lambda *args: None))
218 self.assertEqual(len(result["notifications"]), 1)
219 self.assertEqual(result["notifications"][0]["title.en-GB"], "3")
220 self.assertRegexpMatches(result["version"], r"-a/3")
221
222 @mock.patch("random.random")
223 def test_probability_distribution_multiple_groups(self, random_call):
224 notification.load_notifications = lambda: [
225 {
226 "id": "a",
227 "variants": [
228 {
229 "sample": 0.25,
230 "title.en-GB": "1"
231 },
232 {
233 "sample": 0.25,
234 "title.en-GB": "2"
235 }
236 ]
237 },
238 {
239 "id": "b",
240 "variants": [
241 {
242 "sample": 0.25,
243 "title.en-GB": "1"
244 },
245 {
246 "sample": 0.25,
247 "title.en-GB": "2"
248 }
249 ]
250 }
251 ]
252 random_call.return_value = 0
253 result = json.loads(notification.notification({}, lambda *args: None))
254 self.assertEqual(len(result["notifications"]), 1)
255 self.assertEqual(result["notifications"][0]["id"], "a")
256 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
257 self.assertRegexpMatches(result["version"], r"-a/1-b/0")
258 random_call.return_value = 0.251
259 result = json.loads(notification.notification({}, lambda *args: None))
260 self.assertEqual(len(result["notifications"]), 1)
261 self.assertEqual(result["notifications"][0]["id"], "a")
262 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
263 self.assertRegexpMatches(result["version"], r"-a/2-b/0")
264 random_call.return_value = 0.51
265 result = json.loads(notification.notification({}, lambda *args: None))
266 self.assertEqual(len(result["notifications"]), 1)
267 self.assertEqual(result["notifications"][0]["id"], "b")
268 self.assertEqual(result["notifications"][0]["title.en-GB"], "1")
269 self.assertRegexpMatches(result["version"], r"-a/0-b/1")
270 random_call.return_value = 0.751
271 result = json.loads(notification.notification({}, lambda *args: None))
272 self.assertEqual(len(result["notifications"]), 1)
273 self.assertEqual(result["notifications"][0]["id"], "b")
274 self.assertEqual(result["notifications"][0]["title.en-GB"], "2")
275 self.assertRegexpMatches(result["version"], r"-a/0-b/2")
276
277 def test_invalid_last_version(self):
278 notification.load_notifications = lambda: []
279 notification.notification({"QUERY_STRING": "lastVersion="},
280 lambda *args: None)
281 notification.notification({"QUERY_STRING": "lastVersion=-"},
282 lambda *args: None)
283 notification.notification({"QUERY_STRING": "lastVersion=-/"},
284 lambda *args: None)
285
286 if __name__ == '__main__':
287 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