LEFT | RIGHT |
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): |
| 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", |
| 152 "message.de-DE": "vorgabe", |
144 "variants": [ | 153 "variants": [ |
145 { | 154 { |
146 "sample": 1, | 155 "sample": 1, |
147 "message.en-GB": "variant" | 156 "message.en-GB": "variant" |
148 } | 157 } |
149 ] | 158 ] |
150 } | 159 } |
151 ] | 160 ] |
152 result = json.loads(notification.notification({}, lambda *args: None)) | 161 result = json.loads(notification.notification({}, lambda *args: None)) |
153 self.assertEqual(len(result["notifications"]), 1) | 162 self.assertEqual(len(result["notifications"]), 1) |
154 self.assertEqual(result["notifications"][0]["id"], "a") | 163 self.assertEqual(result["notifications"][0]["id"], "a") |
155 self.assertEqual(result["notifications"][0]["title.en-GB"], "default") | 164 self.assertEqual(result["notifications"][0]["title.en-GB"], "default") |
156 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant") | 165 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant") |
157 | 166 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe") |
158 def test_no_variant_no_notifications(self): | 167 self.assertFalse("variants" in result["notifications"][0]) |
159 notification.load_notifications = lambda: [ | 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 = [ |
160 { | 173 { |
161 "id": "a", | 174 "id": "a", |
162 "variants": [{"sample": 0}] | 175 "variants": [{"sample": 0}] |
163 } | 176 } |
164 ] | 177 ] |
165 result = json.loads(notification.notification({}, lambda *args: None)) | 178 result = json.loads(notification.notification({}, lambda *args: None)) |
166 self.assertEqual(len(result["notifications"]), 0) | 179 self.assertEqual(len(result["notifications"]), 0) |
167 | 180 |
168 @mock.patch("random.random") | 181 @mock.patch("random.random") |
169 def test_probability_distribution_single_group(self, random_call): | 182 @mock.patch("sitescripts.notifications.web.notification.load_notifications") |
170 notification.load_notifications = lambda: [ | 183 def test_probability_distribution_single_group( |
| 184 self, load_notifications_call, random_call): |
| 185 load_notifications_call.return_value = [ |
171 { | 186 { |
172 "id": "a", | 187 "id": "a", |
173 "variants": [ | 188 "variants": [ |
174 { | 189 { |
175 "sample": 0.5, | 190 "sample": 0.5, |
176 "title.en-GB": "1" | 191 "title.en-GB": "1" |
177 }, | 192 }, |
178 { | 193 { |
179 "sample": 0.25, | 194 "sample": 0.25, |
180 "title.en-GB": "2" | 195 "title.en-GB": "2" |
(...skipping 30 matching lines...) Expand all Loading... |
211 self.assertEqual(len(result["notifications"]), 1) | 226 self.assertEqual(len(result["notifications"]), 1) |
212 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | 227 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") |
213 self.assertRegexpMatches(result["version"], r"-a/3") | 228 self.assertRegexpMatches(result["version"], r"-a/3") |
214 random_call.return_value = 1 | 229 random_call.return_value = 1 |
215 result = json.loads(notification.notification({}, lambda *args: None)) | 230 result = json.loads(notification.notification({}, lambda *args: None)) |
216 self.assertEqual(len(result["notifications"]), 1) | 231 self.assertEqual(len(result["notifications"]), 1) |
217 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | 232 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") |
218 self.assertRegexpMatches(result["version"], r"-a/3") | 233 self.assertRegexpMatches(result["version"], r"-a/3") |
219 | 234 |
220 @mock.patch("random.random") | 235 @mock.patch("random.random") |
221 def test_probability_distribution_multiple_groups(self, random_call): | 236 @mock.patch("sitescripts.notifications.web.notification.load_notifications") |
222 notification.load_notifications = lambda: [ | 237 def test_probability_distribution_multiple_groups( |
| 238 self, load_notifications_call, random_call): |
| 239 load_notifications_call.return_value = [ |
223 { | 240 { |
224 "id": "a", | 241 "id": "a", |
225 "variants": [ | 242 "variants": [ |
226 { | 243 { |
227 "sample": 0.25, | 244 "sample": 0.25, |
228 "title.en-GB": "1" | 245 "title.en-GB": "1" |
229 }, | 246 }, |
230 { | 247 { |
231 "sample": 0.25, | 248 "sample": 0.25, |
232 "title.en-GB": "2" | 249 "title.en-GB": "2" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 self.assertEqual(result["notifications"][0]["id"], "b") | 282 self.assertEqual(result["notifications"][0]["id"], "b") |
266 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | 283 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") |
267 self.assertRegexpMatches(result["version"], r"-a/0-b/1") | 284 self.assertRegexpMatches(result["version"], r"-a/0-b/1") |
268 random_call.return_value = 0.751 | 285 random_call.return_value = 0.751 |
269 result = json.loads(notification.notification({}, lambda *args: None)) | 286 result = json.loads(notification.notification({}, lambda *args: None)) |
270 self.assertEqual(len(result["notifications"]), 1) | 287 self.assertEqual(len(result["notifications"]), 1) |
271 self.assertEqual(result["notifications"][0]["id"], "b") | 288 self.assertEqual(result["notifications"][0]["id"], "b") |
272 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | 289 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") |
273 self.assertRegexpMatches(result["version"], r"-a/0-b/2") | 290 self.assertRegexpMatches(result["version"], r"-a/0-b/2") |
274 | 291 |
275 def test_invalid_last_version(self): | 292 @mock.patch("sitescripts.notifications.web.notification.load_notifications") |
276 notification.load_notifications = lambda: [] | 293 def test_invalid_last_version(self, load_notifications_call): |
| 294 load_notifications_call.return_value = [] |
277 notification.notification({"QUERY_STRING": "lastVersion="}, | 295 notification.notification({"QUERY_STRING": "lastVersion="}, |
278 lambda *args: None) | 296 lambda *args: None) |
279 notification.notification({"QUERY_STRING": "lastVersion=-"}, | 297 notification.notification({"QUERY_STRING": "lastVersion=-"}, |
280 lambda *args: None) | 298 lambda *args: None) |
281 notification.notification({"QUERY_STRING": "lastVersion=-/"}, | 299 notification.notification({"QUERY_STRING": "lastVersion=-/"}, |
282 lambda *args: None) | 300 lambda *args: None) |
| 301 notification.notification({"QUERY_STRING": "lastVersion=-//"}, |
| 302 lambda *args: None) |
283 | 303 |
284 if __name__ == '__main__': | 304 if __name__ == '__main__': |
285 unittest.main() | 305 unittest.main() |
LEFT | RIGHT |