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