[b5d87e0] | 1 | from unittest.mock import Mock, patch |
---|
| 2 | |
---|
| 3 | import pytest |
---|
| 4 | |
---|
| 5 | from pyramid.testing import DummyRequest |
---|
| 6 | |
---|
| 7 | from ow.tasks.manager import TasksManager |
---|
| 8 | from ow.tasks.run import command_line |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | @patch('ow.tasks.manager.setup_logging') |
---|
| 12 | @patch('ow.tasks.manager.bootstrap') |
---|
| 13 | @patch('ow.tasks.run.sys') |
---|
| 14 | @patch('ow.tasks.run.TasksManager') |
---|
| 15 | class TestCommandLine(object): |
---|
| 16 | """ |
---|
| 17 | Tests covering the command_line method that allows users to execute the |
---|
| 18 | tasks manager from a CLI. |
---|
| 19 | """ |
---|
| 20 | |
---|
| 21 | @pytest.fixture |
---|
| 22 | def registry(self): |
---|
| 23 | """ |
---|
| 24 | Mock a pyramid registry |
---|
| 25 | """ |
---|
| 26 | registry = Mock() |
---|
| 27 | registry.settings = {} |
---|
| 28 | return registry |
---|
| 29 | |
---|
| 30 | @pytest.fixture |
---|
| 31 | def env(self, registry): |
---|
| 32 | env = {'request': DummyRequest(), |
---|
| 33 | 'root': {}, |
---|
| 34 | 'closer': Mock(), |
---|
| 35 | 'registry': registry} |
---|
| 36 | return env |
---|
| 37 | |
---|
| 38 | @pytest.fixture |
---|
| 39 | def manager(self): |
---|
| 40 | tm = TasksManager() |
---|
| 41 | tm.usage = Mock() |
---|
| 42 | tm.usage.return_value = 'faked manager usage' |
---|
| 43 | return tm |
---|
| 44 | |
---|
| 45 | def test_wrong_params(self, tm, mysys, b, manager, env): |
---|
| 46 | mysys.argv = ['script', 'one', 'two', 'three'] |
---|
| 47 | mysys.exit.side_effect = ValueError('sysexit') |
---|
| 48 | tm.return_value = manager |
---|
| 49 | b.return_value = env |
---|
| 50 | with pytest.raises(ValueError): |
---|
| 51 | command_line() |
---|
| 52 | mysys.exit.assert_called_with(1) |
---|
| 53 | |
---|
| 54 | @patch('ow.tasks.run.queue_processor') |
---|
| 55 | def test_right_params_send_emails( |
---|
| 56 | self, qp, tm, mysys, b, sl, manager, env): |
---|
| 57 | mysys.argv = ['tasks-script', 'tests.ini', 'send_emails'] |
---|
| 58 | qp.return_value = None |
---|
| 59 | tm.return_value = manager |
---|
| 60 | b.return_value = env |
---|
| 61 | command_line() |
---|
| 62 | qp.assert_called_with(env) |
---|