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

Delta Between Two Patch Sets: sitescripts/notifications/test/notification.py

Issue 29570562: Issue 5827 - Assign new groups even if prior groups are present (Closed)
Left Patch Set: Created Oct. 9, 2017, 8:56 a.m.
Right Patch Set: Removed return to make in-place editing more apparent Created Oct. 10, 2017, 2:46 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | sitescripts/notifications/web/notification.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 import os
17 import json 16 import json
18 import mock 17 import mock
19 import unittest 18 import unittest
20 import tempfile
21 import shutil
22 import time
23 import subprocess
24
25 from sitescripts.utils import get_config
26 19
27 import sitescripts.notifications.web.notification as notification 20 import sitescripts.notifications.web.notification as notification
28
29 mock_time = mock.Mock()
30 mock_time.return_value = time.gmtime(0)
31 21
32 22
33 class TestNotification(unittest.TestCase): 23 class TestNotification(unittest.TestCase):
34 def setUp(self): 24 def setUp(self):
35 self.repo_dir = tempfile.mkdtemp('md_repo') 25 self.load_notifications_patcher = mock.patch('sitescripts.notifications. web.notification.load_notifications')
wspee 2017/10/09 09:12:03 As discussed: This created a new repository for ev
Vasily Kuznetsov 2017/10/09 22:29:11 Hm. Now after I looked at the whole test suite it
wspee 2017/10/10 09:03:53 No worries. Half of it was based on my assumptions
36 subprocess.check_call(['hg', 'init'], cwd=self.repo_dir) 26 self.load_notifications_mock = self.load_notifications_patcher.start()
37
38 open(os.path.join(self.repo_dir, '.hgignore'), 'w+').close()
39
40 subprocess.check_call(
41 ['hg', 'commit', '-q', '-m "Initial commit"', '-A'],
42 cwd=self.repo_dir)
43
44 config = get_config()
45 self._notification_repository_orig = config.get(
46 'notifications', 'repository')
47 get_config().set('notifications', 'repository', self.repo_dir)
48 27
49 def tearDown(self): 28 def tearDown(self):
50 shutil.rmtree(self.repo_dir) 29 self.load_notifications_patcher.stop()
51 get_config().set(
52 'notifications', 'repository', self._notification_repository_orig)
53
54 def add_notifications(self, notifications):
55 for id_, content in notifications:
56 with open(os.path.join(self.repo_dir, id_), 'w+') as f:
57 f.write(content)
58 f.flush()
59
60 subprocess.check_call(
61 ['hg', 'commit', '-q', '-m', id_, '-A'],
62 cwd=self.repo_dir)
63 30
64 def test_no_group(self): 31 def test_no_group(self):
65 self.add_notifications([ 32 self.load_notifications_mock.return_value = [
66 ('1', 'title.en-US = 1\nmessage.en-US = 1\n')]) 33 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}
67 34 ]
68 result = json.loads(notification.notification({}, lambda *args: None)) 35 result = json.loads(notification.notification({}, lambda *args: None))
69 self.assertEqual(len(result['notifications']), 1) 36 self.assertEqual(len(result['notifications']), 1)
70 self.assertEqual(result['notifications'][0]['id'], '1') 37 self.assertEqual(result['notifications'][0]['id'], '1')
71 self.assertFalse('-' in result['version']) 38 self.assertFalse('-' in result['version'])
72 39
73 def test_not_in_group(self): 40 def test_not_in_group(self):
74 self.add_notifications([ 41 self.load_notifications_mock.return_value = [
75 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 42 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
76 ('2', '[1]\ntitle.en-US = 2\nmessage.en-US = 2\nsample = 1'), 43 {'id': 'a', 'variants': [
77 ]) 44 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
78 45 ]}
79 result = json.loads(notification.notification({ 46 ]
80 'QUERY_STRING': 'lastVersion=197001010000-2/0' 47 result = json.loads(notification.notification({
81 }, lambda *args: None)) 48 'QUERY_STRING': 'lastVersion=197001010000-a/0'
82 self.assertEqual(len(result['notifications']), 1) 49 }, lambda *args: None))
83 self.assertEqual(result['notifications'][0]['id'], '1') 50 self.assertEqual(len(result['notifications']), 1)
84 self.assertRegexpMatches(result['version'], r'-2/0') 51 self.assertEqual(result['notifications'][0]['id'], '1')
52 self.assertRegexpMatches(result['version'], r'-a/0')
85 53
86 def test_in_group(self): 54 def test_in_group(self):
87 self.add_notifications([ 55 self.load_notifications_mock.return_value = [
88 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 56 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
89 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), 57 {'id': 'a', 'variants': [
90 ]) 58 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
91 59 ]}
92 result = json.loads(notification.notification({ 60 ]
93 'QUERY_STRING': 'lastVersion=197001010000-2/1' 61 result = json.loads(notification.notification({
94 }, lambda *args: None)) 62 'QUERY_STRING': 'lastVersion=197001010000-a/1'
95 self.assertEqual(len(result['notifications']), 1) 63 }, lambda *args: None))
96 self.assertEqual(result['notifications'][0]['id'], '2') 64 self.assertEqual(len(result['notifications']), 1)
97 self.assertRegexpMatches(result['version'], r'-2/1') 65 self.assertEqual(result['notifications'][0]['id'], 'a')
66 self.assertRegexpMatches(result['version'], r'-a/1')
98 67
99 def test_not_in_one_of_many_groups(self): 68 def test_not_in_one_of_many_groups(self):
100 self.add_notifications([ 69 self.load_notifications_mock.return_value = [
101 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 70 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
102 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), 71 {'id': 'a', 'variants': [
103 ('3', '[1]\ntitle.en-US = 3.1\nmessage.en-US = 3.1\nsample = 1'), 72 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
104 ('4', '[1]\ntitle.en-US = 4.1\nmessage.en-US = 4.1\nsample = 1'), 73 ]},
105 ]) 74 {'id': 'b', 'variants': [
106 75 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
107 result = json.loads(notification.notification({ 76 ]},
108 'QUERY_STRING': 'lastVersion=197001010000-2/0-3/0-4/0' 77 {'id': 'c', 'variants': [
109 }, lambda *args: None)) 78 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
110 self.assertEqual(len(result['notifications']), 1) 79 ]}
111 self.assertEqual(result['notifications'][0]['id'], '1') 80 ]
112 self.assertRegexpMatches(result['version'], r'-2/0-3/0-4/0') 81 result = json.loads(notification.notification({
82 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/0-c/0'
83 }, lambda *args: None))
84 self.assertEqual(len(result['notifications']), 1)
85 self.assertEqual(result['notifications'][0]['id'], '1')
86 self.assertRegexpMatches(result['version'], r'-a/0-b/0-c/0')
113 87
114 def test_in_one_of_many_groups(self): 88 def test_in_one_of_many_groups(self):
115 self.add_notifications([ 89 self.load_notifications_mock.return_value = [
116 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 90 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
117 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), 91 {'id': 'a', 'variants': [
118 ('3', '[1]\ntitle.en-US = 3.1\nmessage.en-US = 3.1\nsample = 1'), 92 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
119 ('4', '[1]\ntitle.en-US = 4.1\nmessage.en-US = 4.1\nsample = 1'), 93 ]},
120 ]) 94 {'id': 'b', 'variants': [
121 95 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
122 result = json.loads(notification.notification({ 96 ]},
123 'QUERY_STRING': 'lastVersion=197001010000-2/0-3/1-4/0' 97 {'id': 'c', 'variants': [
124 }, lambda *args: None)) 98 {'title': {'en-US': ''}, 'message': {'en-US': ''}}
125 self.assertEqual(len(result['notifications']), 1) 99 ]}
126 self.assertEqual(result['notifications'][0]['id'], '3') 100 ]
127 self.assertRegexpMatches(result['version'], r'-2/0-3/1-4/0') 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')
128 107
129 def test_not_put_in_group(self): 108 def test_not_put_in_group(self):
130 self.add_notifications([ 109 self.load_notifications_mock.return_value = [
Vasily Kuznetsov 2017/10/09 22:29:11 There is and was a lot of code duplication in this
wspee 2017/10/10 09:03:53 Acknowledged.
131 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 110 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
132 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 0'), 111 {'id': 'a', 'variants': [
133 ]) 112 {'sample': 0, 'title': {'en-US': ''}, 'message': {'en-US': ''}}
134 113 ]}
114 ]
135 result = json.loads(notification.notification({ 115 result = json.loads(notification.notification({
136 'QUERY_STRING': 'lastVersion=197001010000' 116 'QUERY_STRING': 'lastVersion=197001010000'
137 }, lambda *args: None)) 117 }, lambda *args: None))
138 self.assertEqual(len(result['notifications']), 1) 118 self.assertEqual(len(result['notifications']), 1)
139 self.assertEqual(result['notifications'][0]['id'], '1') 119 self.assertEqual(result['notifications'][0]['id'], '1')
140 self.assertRegexpMatches(result['version'], r'-2/0') 120 self.assertRegexpMatches(result['version'], r'-a/0')
141 121
142 def test_put_in_group(self): 122 def test_put_in_group(self):
143 self.add_notifications([ 123 self.load_notifications_mock.return_value = [
144 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 124 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
145 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), 125 {'id': 'a', 'variants': [
146 ]) 126 {'sample': 1, 'title': {'en-US': ''}, 'message': {'en-US': ''}}
147 127 ]}
128 ]
148 result = json.loads(notification.notification({ 129 result = json.loads(notification.notification({
149 'QUERY_STRING': 'lastVersion=197001010000' 130 'QUERY_STRING': 'lastVersion=197001010000'
150 }, lambda *args: None)) 131 }, lambda *args: None))
151 self.assertEqual(len(result['notifications']), 1) 132 self.assertEqual(len(result['notifications']), 1)
152 self.assertEqual(result['notifications'][0]['id'], '2') 133 self.assertEqual(result['notifications'][0]['id'], 'a')
153 self.assertRegexpMatches(result['version'], r'-2/1') 134 self.assertRegexpMatches(result['version'], r'-a/1')
135
136 def test_notification_variant_merged(self):
137 self.load_notifications_mock.return_value = [
138 {
139 'id': 'a',
140 'title': {'en-US': 'default'},
141 'message': {'en-US': 'default'},
142 'variants': [
143 {'sample': 1, 'message': {'en-US': 'variant'}}
144 ]
145 }
146 ]
147 result = json.loads(notification.notification({}, lambda *args: None))
148 self.assertEqual(len(result['notifications']), 1)
149 self.assertEqual(result['notifications'][0]['id'], 'a')
150 self.assertEqual(result['notifications'][0]['title']['en-US'], 'default' )
151 self.assertEqual(result['notifications'][0]['message']['en-US'], 'varian t')
152 self.assertFalse('variants' in result['notifications'][0])
153 self.assertFalse('sample' in result['notifications'][0])
154 154
155 def test_no_variant_no_notifications(self): 155 def test_no_variant_no_notifications(self):
156 self.add_notifications([ 156 self.load_notifications_mock.return_value = [
157 ('1', '[1]\ntitle.en-US = 1\nmessage.en-US = 1\nsample = 0'), 157 {'id': 'a', 'variants': [{'sample': 0}]}
158 ]) 158 ]
159
160 result = json.loads(notification.notification({}, lambda *args: None)) 159 result = json.loads(notification.notification({}, lambda *args: None))
161 self.assertEqual(len(result['notifications']), 0) 160 self.assertEqual(len(result['notifications']), 0)
162 161
163 @mock.patch('random.random') 162 @mock.patch('random.random')
164 def test_probability_distribution_single_group(self, random_call): 163 def test_probability_distribution_single_group(self, random_call):
165 self.add_notifications([ 164 self.load_notifications_mock.return_value = [
166 ('1', '[1]\n' 165 {
167 'title.en-US = 1.1\n' 166 'id': 'a',
168 'message.en-US = 1.1\n' 167 'variants': [
169 'sample = 0.5\n' 168 {'sample': 0.5, 'title': {'en-US': '1'}, 'message': {'en-US' : ''}},
170 '[2]\n' 169 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US ': ''}},
171 'title.en-US = 1.2\n' 170 {'sample': 0.25, 'title': {'en-US': '3'}, 'message': {'en-US ': ''}}
172 'message.en-US = 1.2\n' 171 ]
173 'sample = 0.25\n' 172 }
174 '[3]\n' 173 ]
175 'title.en-US = 1.3\n'
176 'message.en-US = 1.3\n'
177 'sample = 0.25\n')
178 ])
179
180 random_call.return_value = 0 174 random_call.return_value = 0
181 result = json.loads(notification.notification({}, lambda *args: None)) 175 result = json.loads(notification.notification({}, lambda *args: None))
182 self.assertEqual(len(result['notifications']), 1) 176 self.assertEqual(len(result['notifications']), 1)
183 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.1') 177 self.assertEqual(result['notifications'][0]['title']['en-US'], '1')
184 self.assertRegexpMatches(result['version'], r'-1/1') 178 self.assertRegexpMatches(result['version'], r'-a/1')
185 random_call.return_value = 0.5 179 random_call.return_value = 0.5
186 result = json.loads(notification.notification({}, lambda *args: None)) 180 result = json.loads(notification.notification({}, lambda *args: None))
187 self.assertEqual(len(result['notifications']), 1) 181 self.assertEqual(len(result['notifications']), 1)
188 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.1') 182 self.assertEqual(result['notifications'][0]['title']['en-US'], '1')
189 self.assertRegexpMatches(result['version'], r'-1/1') 183 self.assertRegexpMatches(result['version'], r'-a/1')
190 random_call.return_value = 0.51 184 random_call.return_value = 0.51
191 result = json.loads(notification.notification({}, lambda *args: None)) 185 result = json.loads(notification.notification({}, lambda *args: None))
192 self.assertEqual(len(result['notifications']), 1) 186 self.assertEqual(len(result['notifications']), 1)
193 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.2') 187 self.assertEqual(result['notifications'][0]['title']['en-US'], '2')
194 self.assertRegexpMatches(result['version'], r'-1/2') 188 self.assertRegexpMatches(result['version'], r'-a/2')
195 random_call.return_value = 0.75 189 random_call.return_value = 0.75
196 result = json.loads(notification.notification({}, lambda *args: None)) 190 result = json.loads(notification.notification({}, lambda *args: None))
197 self.assertEqual(len(result['notifications']), 1) 191 self.assertEqual(len(result['notifications']), 1)
198 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.2') 192 self.assertEqual(result['notifications'][0]['title']['en-US'], '2')
199 self.assertRegexpMatches(result['version'], r'-1/2') 193 self.assertRegexpMatches(result['version'], r'-a/2')
200 random_call.return_value = 0.751 194 random_call.return_value = 0.751
201 result = json.loads(notification.notification({}, lambda *args: None)) 195 result = json.loads(notification.notification({}, lambda *args: None))
202 self.assertEqual(len(result['notifications']), 1) 196 self.assertEqual(len(result['notifications']), 1)
203 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.3') 197 self.assertEqual(result['notifications'][0]['title']['en-US'], '3')
204 self.assertRegexpMatches(result['version'], r'-1/3') 198 self.assertRegexpMatches(result['version'], r'-a/3')
205 random_call.return_value = 1 199 random_call.return_value = 1
206 result = json.loads(notification.notification({}, lambda *args: None)) 200 result = json.loads(notification.notification({}, lambda *args: None))
207 self.assertEqual(len(result['notifications']), 1) 201 self.assertEqual(len(result['notifications']), 1)
208 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.3') 202 self.assertEqual(result['notifications'][0]['title']['en-US'], '3')
209 self.assertRegexpMatches(result['version'], r'-1/3') 203 self.assertRegexpMatches(result['version'], r'-a/3')
210 204
211 @mock.patch('random.random') 205 @mock.patch('random.random')
212 def test_probability_distribution_multiple_groups(self, random_call): 206 def test_probability_distribution_multiple_groups(self, random_call):
213 self.add_notifications([ 207 self.load_notifications_mock.return_value = [
214 ('1', '[1]\n' 208 {
215 'title.en-US = 1.1\n' 209 'id': 'a',
216 'message.en-US = 1.1\n' 210 'variants': [
217 'sample = 0.25\n' 211 {'sample': 0.25, 'title': {'en-US': '1'}, 'message': {'en-US ': ''}},
218 '[2]\n' 212 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US ': ''}}
219 'title.en-US = 1.2\n' 213 ]
220 'message.en-US = 1.2\n' 214 },
221 'sample = 0.25\n'), 215 {
222 ('2', '[1]\n' 216 'id': 'b',
223 'title.en-US = 2.1\n' 217 'variants': [
224 'message.en-US = 2.1\n' 218 {'sample': 0.25, 'title': {'en-US': '1'}, 'message': {'en-US ': ''}},
225 'sample = 0.25\n' 219 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US ': ''}}
226 '[2]\n' 220 ]
227 'title.en-US = 2.2\n' 221 }
228 'message.en-US = 2.1\n' 222 ]
229 'sample = 0.25\n')
230 ])
231
232 random_call.return_value = 0 223 random_call.return_value = 0
233 result = json.loads(notification.notification({}, lambda *args: None)) 224 result = json.loads(notification.notification({}, lambda *args: None))
234 self.assertEqual(len(result['notifications']), 1) 225 self.assertEqual(len(result['notifications']), 1)
235 self.assertEqual(result['notifications'][0]['id'], '1') 226 self.assertEqual(result['notifications'][0]['id'], 'a')
236 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.1') 227 self.assertEqual(result['notifications'][0]['title']['en-US'], '1')
237 self.assertRegexpMatches(result['version'], r'-1/1-2/0') 228 self.assertRegexpMatches(result['version'], r'-a/1-b/0')
238 random_call.return_value = 0.251 229 random_call.return_value = 0.251
239 result = json.loads(notification.notification({}, lambda *args: None)) 230 result = json.loads(notification.notification({}, lambda *args: None))
240 self.assertEqual(len(result['notifications']), 1) 231 self.assertEqual(len(result['notifications']), 1)
241 self.assertEqual(result['notifications'][0]['id'], '1') 232 self.assertEqual(result['notifications'][0]['id'], 'a')
242 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.2') 233 self.assertEqual(result['notifications'][0]['title']['en-US'], '2')
243 self.assertRegexpMatches(result['version'], r'-1/2-2/0') 234 self.assertRegexpMatches(result['version'], r'-a/2-b/0')
244 random_call.return_value = 0.51 235 random_call.return_value = 0.51
245 result = json.loads(notification.notification({}, lambda *args: None)) 236 result = json.loads(notification.notification({}, lambda *args: None))
246 self.assertEqual(len(result['notifications']), 1) 237 self.assertEqual(len(result['notifications']), 1)
247 self.assertEqual(result['notifications'][0]['id'], '2') 238 self.assertEqual(result['notifications'][0]['id'], 'b')
248 self.assertEqual(result['notifications'][0]['title']['en-US'], '2.1') 239 self.assertEqual(result['notifications'][0]['title']['en-US'], '1')
249 self.assertRegexpMatches(result['version'], r'-1/0-2/1') 240 self.assertRegexpMatches(result['version'], r'-a/0-b/1')
250 random_call.return_value = 0.751 241 random_call.return_value = 0.751
251 result = json.loads(notification.notification({}, lambda *args: None)) 242 result = json.loads(notification.notification({}, lambda *args: None))
252 self.assertEqual(len(result['notifications']), 1) 243 self.assertEqual(len(result['notifications']), 1)
253 self.assertEqual(result['notifications'][0]['id'], '2') 244 self.assertEqual(result['notifications'][0]['id'], 'b')
254 self.assertEqual(result['notifications'][0]['title']['en-US'], '2.2') 245 self.assertEqual(result['notifications'][0]['title']['en-US'], '2')
255 self.assertRegexpMatches(result['version'], r'-1/0-2/2') 246 self.assertRegexpMatches(result['version'], r'-a/0-b/2')
256 247
257 @mock.patch('time.gmtime', mock_time)
258 def test_invalid_last_version(self): 248 def test_invalid_last_version(self):
249 self.load_notifications_mock.return_value = []
259 notification.notification({'QUERY_STRING': 'lastVersion='}, 250 notification.notification({'QUERY_STRING': 'lastVersion='},
260 lambda *args: None) 251 lambda *args: None)
261 result = json.loads(notification.notification({}, lambda *args: None))
262 self.assertEqual(result['version'], '197001010000')
263
264 notification.notification({'QUERY_STRING': 'lastVersion=-'}, 252 notification.notification({'QUERY_STRING': 'lastVersion=-'},
265 lambda *args: None) 253 lambda *args: None)
266 result = json.loads(notification.notification({}, lambda *args: None))
267 self.assertEqual(result['version'], '197001010000')
268
269 notification.notification({'QUERY_STRING': 'lastVersion=-/'}, 254 notification.notification({'QUERY_STRING': 'lastVersion=-/'},
270 lambda *args: None) 255 lambda *args: None)
271 result = json.loads(notification.notification({}, lambda *args: None))
272 self.assertEqual(result['version'], '197001010000')
273
274 notification.notification({'QUERY_STRING': 'lastVersion=-//'}, 256 notification.notification({'QUERY_STRING': 'lastVersion=-//'},
275 lambda *args: None) 257 lambda *args: None)
276 result = json.loads(notification.notification({}, lambda *args: None))
277 self.assertEqual(result['version'], '197001010000')
278 258
279 def test_version_header_present(self): 259 def test_version_header_present(self):
280 self.add_notifications([ 260 self.load_notifications_mock.return_value = [
281 ('1', 'title.en-US = 1\nmessage.en-US = 1\n') 261 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}
282 ]) 262 ]
283 response_header_map = {} 263 response_header_map = {}
284 264
285 def start_response(status, response_headers): 265 def start_response(status, response_headers):
286 for name, value in response_headers: 266 for name, value in response_headers:
287 response_header_map[name] = value 267 response_header_map[name] = value
288 result = json.loads(notification.notification({}, start_response)) 268 result = json.loads(notification.notification({}, start_response))
289 self.assertEqual(result['version'], 269 self.assertEqual(result['version'],
290 response_header_map['ABP-Notification-Version']) 270 response_header_map['ABP-Notification-Version'])
291 271
292 def test_default_group_notification_returned_if_valid(self): 272 def test_default_group_notification_returned_if_valid(self):
293 self.add_notifications([ 273 self.load_notifications_mock.return_value = [
294 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 274 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
295 ('2', 'title.en-US = 2\nmessage.en-US = 2\n' 275 {
296 '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1'), 276 'id': 'a',
297 ]) 277 'title': {'en-US': '0'},
298 278 'message': {'en-US': '0'},
299 result = json.loads(notification.notification({ 279 'variants': [
300 'QUERY_STRING': 'lastVersion=197001010000-2/0' 280 {'title': {'en-US': '1'}, 'message': {'en-US': '1'}}
281 ]
282 }
283 ]
284 result = json.loads(notification.notification({
285 'QUERY_STRING': 'lastVersion=197001010000-a/0'
301 }, lambda *args: None)) 286 }, lambda *args: None))
302 self.assertEqual(len(result['notifications']), 2) 287 self.assertEqual(len(result['notifications']), 2)
303 self.assertEqual(result['notifications'][0]['id'], '1') 288 self.assertEqual(result['notifications'][0]['id'], '1')
304 self.assertEqual(result['notifications'][1]['id'], '2') 289 self.assertEqual(result['notifications'][1]['id'], 'a')
305 self.assertEqual(result['notifications'][1]['title']['en-US'], '2') 290 self.assertEqual(result['notifications'][1]['title']['en-US'], '0')
306 self.assertNotIn('variants', result['notifications'][1]) 291 self.assertNotIn('variants', result['notifications'][1])
307 self.assertRegexpMatches(result['version'], r'-2/0') 292 self.assertRegexpMatches(result['version'], r'-a/0')
308 293
309 def test_default_group_notification_not_returned_if_invalid(self): 294 def test_default_group_notification_not_returned_if_invalid(self):
310 self.add_notifications([ 295 self.load_notifications_mock.return_value = [
311 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 296 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
312 ('2', 'title.en-US = 2\n' 297 {
313 '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1'), 298 'id': 'a',
314 ]) 299 'title': {'en-US': '0'},
315 result = json.loads(notification.notification({ 300 'variants': [
316 'QUERY_STRING': 'lastVersion=197001010000-2/0' 301 {'title': {'en-US': '1'}, 'message': {'en-US': '1'}}
317 }, lambda *args: None)) 302 ]
318 self.assertEqual(len(result['notifications']), 1) 303 }
319 self.assertEqual(result['notifications'][0]['id'], '1') 304 ]
320 self.assertRegexpMatches(result['version'], r'-2/0') 305 result = json.loads(notification.notification({
306 'QUERY_STRING': 'lastVersion=197001010000-a/0'
307 }, lambda *args: None))
308 self.assertEqual(len(result['notifications']), 1)
309 self.assertEqual(result['notifications'][0]['id'], '1')
310 self.assertRegexpMatches(result['version'], r'-a/0')
321 311
322 def test_invalid_notification_not_returned(self): 312 def test_invalid_notification_not_returned(self):
323 self.add_notifications([ 313 self.load_notifications_mock.return_value = [
324 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), 314 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}},
325 ('2', 'title.en-US = 2\n'), 315 {'id': '2', 'title': {'en-US': ''}, 'message': {}},
326 ('3', 'title.en-US = \n'), 316 {'id': '3', 'title': {}, 'message': {'en-US': ''}},
327 ('4', 'message.en-US = \n'), 317 {'id': '4', 'title': {}},
328 ('5', '\n'), 318 {'id': '5', 'message': {}},
329 ]) 319 {'id': '6'}
330 320 ]
331 result = json.loads(notification.notification({}, lambda *args: None)) 321 result = json.loads(notification.notification({}, lambda *args: None))
332 self.assertEqual(len(result['notifications']), 1) 322 self.assertEqual(len(result['notifications']), 1)
333 self.assertEqual(result['notifications'][0]['id'], '1') 323 self.assertEqual(result['notifications'][0]['id'], '1')
334 324
335 def test_stays_in_group_when_notification_present(self): 325 def test_stays_in_group_when_notification_present(self):
336 self.add_notifications([ 326 self.load_notifications_mock.return_value = [
337 ('1', '\n'), 327 {'id': 'a'}
338 ]) 328 ]
339 result = json.loads(notification.notification({ 329 result = json.loads(notification.notification({
340 'QUERY_STRING': 'lastVersion=197001010000-1/0-2/1' 330 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1'
341 }, lambda *args: None)) 331 }, lambda *args: None))
342 self.assertEqual(len(result['notifications']), 0) 332 self.assertEqual(len(result['notifications']), 0)
343 self.assertRegexpMatches(result['version'], r'-1/0') 333 self.assertRegexpMatches(result['version'], r'-a/0')
344 334
345 def test_leaves_group_when_notification_absent(self): 335 def test_leaves_group_when_notification_absent(self):
346 result = json.loads(notification.notification({ 336 self.load_notifications_mock.return_value = []
347 'QUERY_STRING': 'lastVersion=197001010000-1/0-2/1' 337 result = json.loads(notification.notification({
338 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1'
348 }, lambda *args: None)) 339 }, lambda *args: None))
349 self.assertEqual(len(result['notifications']), 0) 340 self.assertEqual(len(result['notifications']), 0)
350 self.assertRegexpMatches(result['version'], r'[^-]*') 341 self.assertRegexpMatches(result['version'], r'[^-]*')
351 342
352 def test_stays_in_group_when_notification_inactive(self): 343 def test_stays_in_group_when_notification_inactive(self):
353 self.add_notifications( 344 self.load_notifications_mock.return_value = [
354 [('1', 'inactive = true\ntitle.en-US = 1\nmessage.en-US = 1\n')]) 345 {'id': 'a', 'inactive': True}
355 result = json.loads(notification.notification({ 346 ]
356 'QUERY_STRING': 'lastVersion=197001010000-1/0-2/1' 347 result = json.loads(notification.notification({
357 }, lambda *args: None)) 348 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1'
358 self.assertEqual(len(result['notifications']), 0) 349 }, lambda *args: None))
359 self.assertRegexpMatches(result['version'], r'-1/0') 350 self.assertEqual(len(result['notifications']), 0)
351 self.assertRegexpMatches(result['version'], r'-a/0')
360 352
361 def test_stays_in_group_when_notification_inactive_assign_new_group(self): 353 def test_stays_in_group_when_notification_inactive_assign_new_group(self):
362 # See: https://issues.adblockplus.org/ticket/5827 354 # See: https://issues.adblockplus.org/ticket/5827
363 self.add_notifications([ 355 self.load_notifications_mock.return_value = [
364 ('1', 'inactive = true\ntitle.en-US = 1.1\nmessage.en-US = 1.1'), 356 {'id': '1', 'inactive': True},
365 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1\n'), 357 {'id': '2', 'variants': [
366 ]) 358 {'sample': 1, 'title': {'en-US': '2.1'}, 'message': {'en-US': '2 .1'}},
359 ]},
360 ]
367 result = json.loads(notification.notification({ 361 result = json.loads(notification.notification({
368 'QUERY_STRING': 'lastVersion=197001010000-1/0' 362 'QUERY_STRING': 'lastVersion=197001010000-1/0'
369 }, lambda *args: None)) 363 }, lambda *args: None))
370
371 self.assertEqual(len(result['notifications']), 1) 364 self.assertEqual(len(result['notifications']), 1)
372 self.assertRegexpMatches(result['version'], r'-1/0-2/1') 365 self.assertRegexpMatches(result['version'], r'-1/0-2/1')
373 366
374 def test_inactive_notifications_not_returned(self): 367 def test_inactive_notifications_not_returned(self):
375 self.add_notifications([ 368 self.load_notifications_mock.return_value = [
376 ('1', 'inactive = true\ntitle.en-US = 1\nmessage.en-US = 1\n'), 369 {'id': 'a', 'title': {'en-US': ''}, 'message': {'en-US': ''}, 'inact ive': True},
377 ('2', 'inactive = false\ntitle.en-US = 2\nmessage.en-US = 2\n'), 370 {'id': 'b', 'title': {'en-US': ''}, 'message': {'en-US': ''}, 'inact ive': False},
378 ('3', 'title.en-US = 3\nmessage.en-US = 3\n'), 371 {'id': 'c', 'title': {'en-US': ''}, 'message': {'en-US': ''}}
379 ]) 372 ]
380
381 result = json.loads(notification.notification({}, lambda *args: None)) 373 result = json.loads(notification.notification({}, lambda *args: None))
382 self.assertEqual(len(result['notifications']), 2) 374 self.assertEqual(len(result['notifications']), 2)
383 self.assertEqual(result['notifications'][0]['id'], '2') 375 self.assertEqual(result['notifications'][0]['id'], 'b')
384 self.assertEqual(result['notifications'][1]['id'], '3') 376 self.assertEqual(result['notifications'][1]['id'], 'c')
385 377
386 def test_inactive_notification_variant_not_returned(self): 378 def test_inactive_notification_variant_not_returned(self):
387 self.add_notifications([ 379 self.load_notifications_mock.return_value = [
388 ('1', 'inactive = true\ntitle.en-US = 1\nmessage.en-US = 1\n'), 380 {'id': 'a', 'inactive': True}
389 ]) 381 ]
390 result = json.loads(notification.notification({ 382 result = json.loads(notification.notification({
391 'QUERY_STRING': 'lastVersion=197001010000-a/1' 383 'QUERY_STRING': 'lastVersion=197001010000-a/1'
392 }, lambda *args: None)) 384 }, lambda *args: None))
393 self.assertEqual(len(result['notifications']), 0) 385 self.assertEqual(len(result['notifications']), 0)
386
394 387
395 if __name__ == '__main__': 388 if __name__ == '__main__':
396 unittest.main() 389 unittest.main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld