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 def _format_time(time): |
| 37 return datetime.datetime.strftime(time, "%Y-%m-%dT%H:%M") |
| 38 |
| 39 class TestParser(unittest.TestCase): |
| 40 def setUp(self): |
| 41 self.call_patcher = mock.patch("subprocess.call") |
| 42 self.call_patcher.start() |
| 43 self.check_output_patcher = mock.patch("subprocess.check_output") |
| 44 check_output_mock = self.check_output_patcher.start() |
| 45 def check_output_side_effect(command): |
| 46 if "hg" in command and "archive" in command: |
| 47 return _create_notification_archive(*self.notification_to_load) |
| 48 check_output_mock.side_effect = check_output_side_effect |
| 49 |
| 50 def tearDown(self): |
| 51 self.call_patcher.stop() |
| 52 self.check_output_patcher.stop() |
| 53 |
| 54 def test_typical(self): |
| 55 self.notification_to_load = ("1", """ |
| 56 severity = information |
| 57 title.en-US = The title |
| 58 message.en-US = The message |
| 59 """) |
| 60 notifications = parser.load_notifications() |
| 61 self.assertEqual(len(notifications), 1) |
| 62 self.assertEqual(notifications[0]["id"], "1") |
| 63 self.assertEqual(notifications[0]["severity"], "information") |
| 64 self.assertEqual(notifications[0]["title"]["en-US"], "The title") |
| 65 self.assertEqual(notifications[0]["message"]["en-US"], "The message") |
| 66 |
| 67 def test_inactive(self): |
| 68 self.notification_to_load = ("1", """ |
| 69 inactive = True |
| 70 """) |
| 71 notifications = parser.load_notifications() |
| 72 self.assertEqual(len(notifications), 0) |
| 73 |
| 74 def test_in_range(self): |
| 75 current_time = datetime.datetime.now() |
| 76 hour_delta = datetime.timedelta(hours=1) |
| 77 start_time = current_time - hour_delta |
| 78 end_time = current_time + hour_delta |
| 79 self.notification_to_load = ("1", """ |
| 80 start = %s |
| 81 end = %s |
| 82 """ % (_format_time(start_time), _format_time(end_time))) |
| 83 notifications = parser.load_notifications() |
| 84 self.assertEqual(len(notifications), 1) |
| 85 self.assertEqual(notifications[0]["id"], "1") |
| 86 |
| 87 def test_after_range(self): |
| 88 current_time = datetime.datetime.now() |
| 89 start_time = current_time - datetime.timedelta(hours=2) |
| 90 end_time = current_time - datetime.timedelta(hours=1) |
| 91 self.notification_to_load = ("1", """ |
| 92 start = %s |
| 93 end = %s |
| 94 """ % (_format_time(start_time), _format_time(end_time))) |
| 95 notifications = parser.load_notifications() |
| 96 self.assertEqual(len(notifications), 0) |
| 97 |
| 98 def test_before_range(self): |
| 99 current_time = datetime.datetime.now() |
| 100 start_time = current_time + datetime.timedelta(hours=1) |
| 101 end_time = current_time + datetime.timedelta(hours=2) |
| 102 self.notification_to_load = ("1", """ |
| 103 start = %s |
| 104 end = %s |
| 105 """ % (_format_time(start_time), _format_time(end_time))) |
| 106 notifications = parser.load_notifications() |
| 107 self.assertEqual(len(notifications), 0) |
| 108 |
| 109 if __name__ == "__main__": |
| 110 unittest.main() |
OLD | NEW |