Left: | ||
Right: |
OLD | NEW |
---|---|
(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: [ | |
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.
| |
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 self.assertFalse("variants" in result["notifications"][0]) | |
160 self.assertFalse("sample" in result["notifications"][0]) | |
161 | |
162 def test_no_variant_no_notifications(self): | |
163 notification.load_notifications = lambda: [ | |
164 { | |
165 "id": "a", | |
166 "variants": [{"sample": 0}] | |
167 } | |
168 ] | |
169 result = json.loads(notification.notification({}, lambda *args: None)) | |
170 self.assertEqual(len(result["notifications"]), 0) | |
171 | |
172 @mock.patch("random.random") | |
173 def test_probability_distribution_single_group(self, random_call): | |
174 notification.load_notifications = lambda: [ | |
175 { | |
176 "id": "a", | |
177 "variants": [ | |
178 { | |
179 "sample": 0.5, | |
180 "title.en-GB": "1" | |
181 }, | |
182 { | |
183 "sample": 0.25, | |
184 "title.en-GB": "2" | |
185 }, | |
186 { | |
187 "sample": 0.25, | |
188 "title.en-GB": "3" | |
189 } | |
190 ] | |
191 } | |
192 ] | |
193 random_call.return_value = 0 | |
194 result = json.loads(notification.notification({}, lambda *args: None)) | |
195 self.assertEqual(len(result["notifications"]), 1) | |
196 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | |
197 self.assertRegexpMatches(result["version"], r"-a/1") | |
198 random_call.return_value = 0.5 | |
199 result = json.loads(notification.notification({}, lambda *args: None)) | |
200 self.assertEqual(len(result["notifications"]), 1) | |
201 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | |
202 self.assertRegexpMatches(result["version"], r"-a/1") | |
203 random_call.return_value = 0.51 | |
204 result = json.loads(notification.notification({}, lambda *args: None)) | |
205 self.assertEqual(len(result["notifications"]), 1) | |
206 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | |
207 self.assertRegexpMatches(result["version"], r"-a/2") | |
208 random_call.return_value = 0.75 | |
209 result = json.loads(notification.notification({}, lambda *args: None)) | |
210 self.assertEqual(len(result["notifications"]), 1) | |
211 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | |
212 self.assertRegexpMatches(result["version"], r"-a/2") | |
213 random_call.return_value = 0.751 | |
214 result = json.loads(notification.notification({}, lambda *args: None)) | |
215 self.assertEqual(len(result["notifications"]), 1) | |
216 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | |
217 self.assertRegexpMatches(result["version"], r"-a/3") | |
218 random_call.return_value = 1 | |
219 result = json.loads(notification.notification({}, lambda *args: None)) | |
220 self.assertEqual(len(result["notifications"]), 1) | |
221 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | |
222 self.assertRegexpMatches(result["version"], r"-a/3") | |
223 | |
224 @mock.patch("random.random") | |
225 def test_probability_distribution_multiple_groups(self, random_call): | |
226 notification.load_notifications = lambda: [ | |
227 { | |
228 "id": "a", | |
229 "variants": [ | |
230 { | |
231 "sample": 0.25, | |
232 "title.en-GB": "1" | |
233 }, | |
234 { | |
235 "sample": 0.25, | |
236 "title.en-GB": "2" | |
237 } | |
238 ] | |
239 }, | |
240 { | |
241 "id": "b", | |
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 random_call.return_value = 0 | |
255 result = json.loads(notification.notification({}, lambda *args: None)) | |
256 self.assertEqual(len(result["notifications"]), 1) | |
257 self.assertEqual(result["notifications"][0]["id"], "a") | |
258 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | |
259 self.assertRegexpMatches(result["version"], r"-a/1-b/0") | |
260 random_call.return_value = 0.251 | |
261 result = json.loads(notification.notification({}, lambda *args: None)) | |
262 self.assertEqual(len(result["notifications"]), 1) | |
263 self.assertEqual(result["notifications"][0]["id"], "a") | |
264 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | |
265 self.assertRegexpMatches(result["version"], r"-a/2-b/0") | |
266 random_call.return_value = 0.51 | |
267 result = json.loads(notification.notification({}, lambda *args: None)) | |
268 self.assertEqual(len(result["notifications"]), 1) | |
269 self.assertEqual(result["notifications"][0]["id"], "b") | |
270 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | |
271 self.assertRegexpMatches(result["version"], r"-a/0-b/1") | |
272 random_call.return_value = 0.751 | |
273 result = json.loads(notification.notification({}, lambda *args: None)) | |
274 self.assertEqual(len(result["notifications"]), 1) | |
275 self.assertEqual(result["notifications"][0]["id"], "b") | |
276 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | |
277 self.assertRegexpMatches(result["version"], r"-a/0-b/2") | |
278 | |
279 def test_invalid_last_version(self): | |
280 notification.load_notifications = lambda: [] | |
281 notification.notification({"QUERY_STRING": "lastVersion="}, | |
282 lambda *args: None) | |
283 notification.notification({"QUERY_STRING": "lastVersion=-"}, | |
284 lambda *args: None) | |
285 notification.notification({"QUERY_STRING": "lastVersion=-/"}, | |
286 lambda *args: None) | |
287 | |
288 if __name__ == '__main__': | |
289 unittest.main() | |
OLD | NEW |