OLD | NEW |
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-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 import mock | 21 import mock |
22 | 22 |
23 import sitescripts.notifications.parser as parser | 23 import sitescripts.notifications.parser as parser |
24 | 24 |
25 | 25 |
26 def _create_notification_archive(name, text): | 26 def _create_notification_archive(name, text): |
27 archive_stream = StringIO.StringIO() | 27 archive_stream = StringIO.StringIO() |
28 file_stream = StringIO.StringIO(text) | 28 file_stream = StringIO.StringIO(text) |
29 tar_info = tarfile.TarInfo(name) | 29 tar_info = tarfile.TarInfo(name) |
30 tar_info.size = len(file_stream.buf) | 30 tar_info.size = len(file_stream.buf) |
31 with tarfile.open(mode="w", fileobj=archive_stream) as archive: | 31 with tarfile.open(mode='w', fileobj=archive_stream) as archive: |
32 archive.addfile(tar_info, file_stream) | 32 archive.addfile(tar_info, file_stream) |
33 return archive_stream.getvalue() | 33 return archive_stream.getvalue() |
34 | 34 |
35 | 35 |
36 def _format_time(time): | 36 def _format_time(time): |
37 return datetime.datetime.strftime(time, "%Y-%m-%dT%H:%M") | 37 return datetime.datetime.strftime(time, '%Y-%m-%dT%H:%M') |
38 | 38 |
39 | 39 |
40 class TestParser(unittest.TestCase): | 40 class TestParser(unittest.TestCase): |
41 def setUp(self): | 41 def setUp(self): |
42 self.call_patcher = mock.patch("subprocess.call") | 42 self.call_patcher = mock.patch('subprocess.call') |
43 self.call_patcher.start() | 43 self.call_patcher.start() |
44 self.check_output_patcher = mock.patch("subprocess.check_output") | 44 self.check_output_patcher = mock.patch('subprocess.check_output') |
45 check_output_mock = self.check_output_patcher.start() | 45 check_output_mock = self.check_output_patcher.start() |
46 | 46 |
47 def check_output_side_effect(command): | 47 def check_output_side_effect(command): |
48 if "hg" in command and "archive" in command: | 48 if 'hg' in command and 'archive' in command: |
49 return _create_notification_archive(*self.notification_to_load) | 49 return _create_notification_archive(*self.notification_to_load) |
50 check_output_mock.side_effect = check_output_side_effect | 50 check_output_mock.side_effect = check_output_side_effect |
51 | 51 |
52 def tearDown(self): | 52 def tearDown(self): |
53 self.call_patcher.stop() | 53 self.call_patcher.stop() |
54 self.check_output_patcher.stop() | 54 self.check_output_patcher.stop() |
55 | 55 |
56 def test_typical(self): | 56 def test_typical(self): |
57 self.notification_to_load = ("1", """ | 57 self.notification_to_load = ('1', ''' |
58 severity = information | 58 severity = information |
59 title.en-US = The title | 59 title.en-US = The title |
60 message.en-US = The message | 60 message.en-US = The message |
61 """) | 61 ''') |
62 notifications = parser.load_notifications() | 62 notifications = parser.load_notifications() |
63 self.assertEqual(len(notifications), 1) | 63 self.assertEqual(len(notifications), 1) |
64 self.assertEqual(notifications[0]["id"], "1") | 64 self.assertEqual(notifications[0]['id'], '1') |
65 self.assertEqual(notifications[0]["severity"], "information") | 65 self.assertEqual(notifications[0]['severity'], 'information') |
66 self.assertEqual(notifications[0]["title"]["en-US"], "The title") | 66 self.assertEqual(notifications[0]['title']['en-US'], 'The title') |
67 self.assertEqual(notifications[0]["message"]["en-US"], "The message") | 67 self.assertEqual(notifications[0]['message']['en-US'], 'The message') |
68 self.assertNotIn("inactive", notifications[0]) | 68 self.assertNotIn('inactive', notifications[0]) |
69 | 69 |
70 def test_inactive(self): | 70 def test_inactive(self): |
71 self.notification_to_load = ("1", """ | 71 self.notification_to_load = ('1', ''' |
72 inactive = Yes | 72 inactive = Yes |
73 """) | 73 ''') |
74 notifications = parser.load_notifications() | 74 notifications = parser.load_notifications() |
75 self.assertEqual(len(notifications), 1) | 75 self.assertEqual(len(notifications), 1) |
76 self.assertTrue(notifications[0]["inactive"]) | 76 self.assertTrue(notifications[0]['inactive']) |
77 self.notification_to_load = ("1", """ | 77 self.notification_to_load = ('1', ''' |
78 inactive = No | 78 inactive = No |
79 """) | 79 ''') |
80 notifications = parser.load_notifications() | 80 notifications = parser.load_notifications() |
81 self.assertEqual(len(notifications), 1) | 81 self.assertEqual(len(notifications), 1) |
82 self.assertFalse(notifications[0]["inactive"]) | 82 self.assertFalse(notifications[0]['inactive']) |
83 | 83 |
84 def test_in_range(self): | 84 def test_in_range(self): |
85 current_time = datetime.datetime.now() | 85 current_time = datetime.datetime.now() |
86 hour_delta = datetime.timedelta(hours=1) | 86 hour_delta = datetime.timedelta(hours=1) |
87 start_time = current_time - hour_delta | 87 start_time = current_time - hour_delta |
88 end_time = current_time + hour_delta | 88 end_time = current_time + hour_delta |
89 self.notification_to_load = ("1", """ | 89 self.notification_to_load = ('1', ''' |
90 start = %s | 90 start = %s |
91 end = %s | 91 end = %s |
92 """ % (_format_time(start_time), _format_time(end_time))) | 92 ''' % (_format_time(start_time), _format_time(end_time))) |
93 notifications = parser.load_notifications() | 93 notifications = parser.load_notifications() |
94 self.assertEqual(len(notifications), 1) | 94 self.assertEqual(len(notifications), 1) |
95 self.assertEqual(notifications[0]["id"], "1") | 95 self.assertEqual(notifications[0]['id'], '1') |
96 self.assertNotIn("inactive", notifications[0]) | 96 self.assertNotIn('inactive', notifications[0]) |
97 | 97 |
98 def test_after_range(self): | 98 def test_after_range(self): |
99 current_time = datetime.datetime.now() | 99 current_time = datetime.datetime.now() |
100 start_time = current_time - datetime.timedelta(hours=2) | 100 start_time = current_time - datetime.timedelta(hours=2) |
101 end_time = current_time - datetime.timedelta(hours=1) | 101 end_time = current_time - datetime.timedelta(hours=1) |
102 self.notification_to_load = ("1", """ | 102 self.notification_to_load = ('1', ''' |
103 start = %s | 103 start = %s |
104 end = %s | 104 end = %s |
105 """ % (_format_time(start_time), _format_time(end_time))) | 105 ''' % (_format_time(start_time), _format_time(end_time))) |
106 notifications = parser.load_notifications() | 106 notifications = parser.load_notifications() |
107 self.assertEqual(len(notifications), 1) | 107 self.assertEqual(len(notifications), 1) |
108 self.assertTrue(notifications[0]["inactive"]) | 108 self.assertTrue(notifications[0]['inactive']) |
109 | 109 |
110 def test_before_range(self): | 110 def test_before_range(self): |
111 current_time = datetime.datetime.now() | 111 current_time = datetime.datetime.now() |
112 start_time = current_time + datetime.timedelta(hours=1) | 112 start_time = current_time + datetime.timedelta(hours=1) |
113 end_time = current_time + datetime.timedelta(hours=2) | 113 end_time = current_time + datetime.timedelta(hours=2) |
114 self.notification_to_load = ("1", """ | 114 self.notification_to_load = ('1', ''' |
115 start = %s | 115 start = %s |
116 end = %s | 116 end = %s |
117 """ % (_format_time(start_time), _format_time(end_time))) | 117 ''' % (_format_time(start_time), _format_time(end_time))) |
118 notifications = parser.load_notifications() | 118 notifications = parser.load_notifications() |
119 self.assertEqual(len(notifications), 1) | 119 self.assertEqual(len(notifications), 1) |
120 self.assertTrue(notifications[0]["inactive"]) | 120 self.assertTrue(notifications[0]['inactive']) |
121 | 121 |
122 def test_start_and_end_not_present(self): | 122 def test_start_and_end_not_present(self): |
123 current_time = datetime.datetime.now() | 123 current_time = datetime.datetime.now() |
124 hour_delta = datetime.timedelta(hours=1) | 124 hour_delta = datetime.timedelta(hours=1) |
125 start_time = current_time - hour_delta | 125 start_time = current_time - hour_delta |
126 end_time = current_time + hour_delta | 126 end_time = current_time + hour_delta |
127 self.notification_to_load = ("1", """ | 127 self.notification_to_load = ('1', ''' |
128 start = %s | 128 start = %s |
129 end = %s | 129 end = %s |
130 """ % (_format_time(start_time), _format_time(end_time))) | 130 ''' % (_format_time(start_time), _format_time(end_time))) |
131 notifications = parser.load_notifications() | 131 notifications = parser.load_notifications() |
132 self.assertEqual(len(notifications), 1) | 132 self.assertEqual(len(notifications), 1) |
133 self.assertNotIn("inactive", notifications[0]) | 133 self.assertNotIn('inactive', notifications[0]) |
134 self.assertNotIn("start", notifications[0]) | 134 self.assertNotIn('start', notifications[0]) |
135 self.assertNotIn("end", notifications[0]) | 135 self.assertNotIn('end', notifications[0]) |
136 | 136 |
137 if __name__ == "__main__": | 137 if __name__ == '__main__': |
138 unittest.main() | 138 unittest.main() |
OLD | NEW |