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 StringIO | |
19 import datetime | |
20 import mock | |
Sebastian Noack
2015/06/23 15:42:44
Nit: Third party module imports go below corelib i
Felix Dahlke
2015/06/23 15:51:43
Done.
| |
21 import tarfile | |
22 import unittest | |
23 | |
24 import sitescripts.notifications.parser as parser | |
25 | |
26 def _create_notification_archive(name, text): | |
27 archive_stream = StringIO.StringIO() | |
28 file_stream = StringIO.StringIO(text) | |
29 tar_info = tarfile.TarInfo(name) | |
30 tar_info.size = len(file_stream.buf) | |
31 with tarfile.open(mode="w", fileobj=archive_stream) as archive: | |
32 archive.addfile(tar_info, file_stream) | |
33 return archive_stream.getvalue() | |
34 | |
35 class TestParser(unittest.TestCase): | |
36 def setUp(self): | |
37 self.call_patcher = mock.patch("subprocess.call") | |
38 self.call_patcher.start() | |
39 self.check_output_patcher = mock.patch("subprocess.check_output") | |
40 check_output_mock = self.check_output_patcher.start() | |
41 def check_output_side_effect(command): | |
42 if "hg" in command and "archive" in command: | |
43 return _create_notification_archive(*self.notification_to_load) | |
44 check_output_mock.side_effect = check_output_side_effect | |
45 | |
46 def tearDown(self): | |
47 self.call_patcher.stop() | |
48 self.check_output_patcher.stop() | |
49 | |
50 def test_typical(self): | |
51 self.notification_to_load = ("1", """ | |
52 severity = information | |
53 title.en-US = The title | |
54 message.en-US = The message | |
55 """) | |
56 notifications = parser.load_notifications() | |
57 self.assertEqual(len(notifications), 1) | |
58 self.assertEqual(notifications[0]["id"], "1") | |
59 self.assertEqual(notifications[0]["severity"], "information") | |
60 self.assertEqual(notifications[0]["title"]["en-US"], "The title") | |
61 self.assertEqual(notifications[0]["message"]["en-US"], "The message") | |
62 | |
63 def test_inactive(self): | |
64 self.notification_to_load = ("1", """ | |
65 inactive = True | |
66 """) | |
67 notifications = parser.load_notifications() | |
68 self.assertEqual(len(notifications), 0) | |
69 | |
70 def test_in_range(self): | |
71 current_time = datetime.datetime.now() | |
72 hour_delta = datetime.timedelta(hours=1) | |
73 start_time = current_time - hour_delta | |
74 end_time = current_time + hour_delta | |
75 self.notification_to_load = ("1", """ | |
76 start = %s | |
77 end = %s | |
78 """ % (start_time.isoformat(), end_time.isoformat())) | |
79 notifications = parser.load_notifications() | |
80 self.assertEqual(len(notifications), 1) | |
81 self.assertEqual(notifications[0]["id"], "1") | |
82 | |
83 def test_after_range(self): | |
84 current_time = datetime.datetime.now() | |
85 start_time = current_time - datetime.timedelta(hours=2) | |
86 end_time = current_time - datetime.timedelta(hours=1) | |
87 self.notification_to_load = ("1", """ | |
88 start = %s | |
89 end = %s | |
90 """ % (start_time.isoformat(), end_time.isoformat())) | |
91 notifications = parser.load_notifications() | |
92 self.assertEqual(len(notifications), 0) | |
93 | |
94 def test_before_range(self): | |
95 current_time = datetime.datetime.now() | |
96 start_time = current_time + datetime.timedelta(hours=1) | |
97 end_time = current_time + datetime.timedelta(hours=2) | |
98 self.notification_to_load = ("1", """ | |
99 start = %s | |
100 end = %s | |
101 """ % (start_time.isoformat(), end_time.isoformat())) | |
102 notifications = parser.load_notifications() | |
103 self.assertEqual(len(notifications), 0) | |
104 | |
105 if __name__ == "__main__": | |
106 unittest.main() | |
OLD | NEW |