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, |
(...skipping 14 matching lines...) Expand all Loading... |
25 import sitescripts.notifications.parser as parser | 25 import sitescripts.notifications.parser as parser |
26 | 26 |
27 def _create_notification_archive(name, text): | 27 def _create_notification_archive(name, text): |
28 archive_stream = StringIO.StringIO() | 28 archive_stream = StringIO.StringIO() |
29 file_stream = StringIO.StringIO(text) | 29 file_stream = StringIO.StringIO(text) |
30 tar_info = tarfile.TarInfo(name) | 30 tar_info = tarfile.TarInfo(name) |
31 tar_info.size = len(file_stream.buf) | 31 tar_info.size = len(file_stream.buf) |
32 with tarfile.open(mode="w", fileobj=archive_stream) as archive: | 32 with tarfile.open(mode="w", fileobj=archive_stream) as archive: |
33 archive.addfile(tar_info, file_stream) | 33 archive.addfile(tar_info, file_stream) |
34 return archive_stream.getvalue() | 34 return archive_stream.getvalue() |
| 35 |
| 36 def _format_time(time): |
| 37 return datetime.datetime.strftime(time, "%Y-%m-%dT%H:%M") |
35 | 38 |
36 class TestParser(unittest.TestCase): | 39 class TestParser(unittest.TestCase): |
37 def setUp(self): | 40 def setUp(self): |
38 self.call_patcher = mock.patch("subprocess.call") | 41 self.call_patcher = mock.patch("subprocess.call") |
39 self.call_patcher.start() | 42 self.call_patcher.start() |
40 self.check_output_patcher = mock.patch("subprocess.check_output") | 43 self.check_output_patcher = mock.patch("subprocess.check_output") |
41 check_output_mock = self.check_output_patcher.start() | 44 check_output_mock = self.check_output_patcher.start() |
42 def check_output_side_effect(command): | 45 def check_output_side_effect(command): |
43 if "hg" in command and "archive" in command: | 46 if "hg" in command and "archive" in command: |
44 return _create_notification_archive(*self.notification_to_load) | 47 return _create_notification_archive(*self.notification_to_load) |
(...skipping 24 matching lines...) Expand all Loading... |
69 self.assertEqual(len(notifications), 0) | 72 self.assertEqual(len(notifications), 0) |
70 | 73 |
71 def test_in_range(self): | 74 def test_in_range(self): |
72 current_time = datetime.datetime.now() | 75 current_time = datetime.datetime.now() |
73 hour_delta = datetime.timedelta(hours=1) | 76 hour_delta = datetime.timedelta(hours=1) |
74 start_time = current_time - hour_delta | 77 start_time = current_time - hour_delta |
75 end_time = current_time + hour_delta | 78 end_time = current_time + hour_delta |
76 self.notification_to_load = ("1", """ | 79 self.notification_to_load = ("1", """ |
77 start = %s | 80 start = %s |
78 end = %s | 81 end = %s |
79 """ % (start_time.isoformat(), end_time.isoformat())) | 82 """ % (_format_time(start_time), _format_time(end_time))) |
80 notifications = parser.load_notifications() | 83 notifications = parser.load_notifications() |
81 self.assertEqual(len(notifications), 1) | 84 self.assertEqual(len(notifications), 1) |
82 self.assertEqual(notifications[0]["id"], "1") | 85 self.assertEqual(notifications[0]["id"], "1") |
83 | 86 |
84 def test_after_range(self): | 87 def test_after_range(self): |
85 current_time = datetime.datetime.now() | 88 current_time = datetime.datetime.now() |
86 start_time = current_time - datetime.timedelta(hours=2) | 89 start_time = current_time - datetime.timedelta(hours=2) |
87 end_time = current_time - datetime.timedelta(hours=1) | 90 end_time = current_time - datetime.timedelta(hours=1) |
88 self.notification_to_load = ("1", """ | 91 self.notification_to_load = ("1", """ |
89 start = %s | 92 start = %s |
90 end = %s | 93 end = %s |
91 """ % (start_time.isoformat(), end_time.isoformat())) | 94 """ % (_format_time(start_time), _format_time(end_time))) |
92 notifications = parser.load_notifications() | 95 notifications = parser.load_notifications() |
93 self.assertEqual(len(notifications), 0) | 96 self.assertEqual(len(notifications), 0) |
94 | 97 |
95 def test_before_range(self): | 98 def test_before_range(self): |
96 current_time = datetime.datetime.now() | 99 current_time = datetime.datetime.now() |
97 start_time = current_time + datetime.timedelta(hours=1) | 100 start_time = current_time + datetime.timedelta(hours=1) |
98 end_time = current_time + datetime.timedelta(hours=2) | 101 end_time = current_time + datetime.timedelta(hours=2) |
99 self.notification_to_load = ("1", """ | 102 self.notification_to_load = ("1", """ |
100 start = %s | 103 start = %s |
101 end = %s | 104 end = %s |
102 """ % (start_time.isoformat(), end_time.isoformat())) | 105 """ % (_format_time(start_time), _format_time(end_time))) |
103 notifications = parser.load_notifications() | 106 notifications = parser.load_notifications() |
104 self.assertEqual(len(notifications), 0) | 107 self.assertEqual(len(notifications), 0) |
105 | 108 |
106 if __name__ == "__main__": | 109 if __name__ == "__main__": |
107 unittest.main() | 110 unittest.main() |
LEFT | RIGHT |