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