OLD | NEW |
(Empty) | |
| 1 # This file is part of the Juju Quickstart Plugin, which lets users set up a |
| 2 # Juju environment in very few steps (https://launchpad.net/juju-quickstart). |
| 3 # Copyright (C) 2013 Canonical Ltd. |
| 4 # |
| 5 # This program is free software: you can redistribute it and/or modify it under |
| 6 # the terms of the GNU Affero General Public License version 3, as published by |
| 7 # the Free Software Foundation. |
| 8 # |
| 9 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
| 11 # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 # Affero General Public License for more details. |
| 13 # |
| 14 # You should have received a copy of the GNU Affero General Public License |
| 15 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
| 17 """Tests for the Juju Quickstart CLI application views.""" |
| 18 |
| 19 from __future__ import unicode_literals |
| 20 |
| 21 from contextlib import contextmanager |
| 22 import unittest |
| 23 |
| 24 import mock |
| 25 |
| 26 from quickstart.cli import ( |
| 27 ui, |
| 28 views, |
| 29 ) |
| 30 |
| 31 |
| 32 class TestShow(unittest.TestCase): |
| 33 |
| 34 @contextmanager |
| 35 def patch_setup_urwid_app(self, run_side_effect=None): |
| 36 """Patch the base.setup_urwid_app function. |
| 37 |
| 38 The context manager returns a tuple (mock_loop, mock_app) containing |
| 39 the two mock objects returned by the mock call. |
| 40 |
| 41 The run_side_effect argument can be provided to specify the side |
| 42 effects of the mock_loop.run call. |
| 43 """ |
| 44 mock_loop = mock.Mock() |
| 45 mock_loop.run = mock.Mock(side_effect=run_side_effect) |
| 46 mock_setup_urwid_app = mock.Mock(return_value=(mock_loop, mock.Mock())) |
| 47 setup_urwid_app_path = 'quickstart.cli.views.base.setup_urwid_app' |
| 48 with mock.patch(setup_urwid_app_path, mock_setup_urwid_app): |
| 49 yield mock_setup_urwid_app() |
| 50 |
| 51 def test_show_view(self): |
| 52 # The loop and app objects are properly used by the show function: |
| 53 # the loop is run and the app is passed to the view. |
| 54 view = mock.Mock() |
| 55 with self.patch_setup_urwid_app() as (mock_loop, mock_app): |
| 56 views.show(view) |
| 57 view.assert_called_once_with(mock_app) |
| 58 mock_loop.run.assert_called_once_with() |
| 59 |
| 60 def test_view_exit(self): |
| 61 # An ui.AppExit correctly quits the application. The return value |
| 62 # encapsulated on the exception is also returned by the show function. |
| 63 view = mock.Mock() |
| 64 run_side_effect = ui.AppExit('bad wolf') |
| 65 with self.patch_setup_urwid_app(run_side_effect=run_side_effect): |
| 66 return_value = views.show(view) |
| 67 self.assertEqual('bad wolf', return_value) |
| 68 |
| 69 def test_unhandled_input(self): |
| 70 # The unhandled_input callable is properly set up and registered to |
| 71 # the main loop. |
| 72 view = mock.Mock(return_value=42) |
| 73 with self.patch_setup_urwid_app() as (mock_loop, mock_app): |
| 74 views.show(view) |
| 75 unhandled_input = mock_loop.set_unhandled_input.call_args[0][0] |
| 76 with self.assertRaises(ui.AppExit) as context_manager: |
| 77 unhandled_input(ui.EXIT_KEY) |
| 78 self.assertEqual(42, context_manager.exception.return_value) |
| 79 |
| 80 def test_view_arguments(self): |
| 81 # The view is invoked passing the app and all the optional show |
| 82 # function arguments. |
| 83 view = mock.Mock() |
| 84 with self.patch_setup_urwid_app() as (mock_loop, mock_app): |
| 85 views.show(view, 'arg1', 'arg2') |
| 86 view.assert_called_once_with(mock_app, 'arg1', 'arg2') |
OLD | NEW |