Refactor basetest and fixtures
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/c538654a Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/c538654a Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/c538654a Branch: refs/heads/cli-tests Commit: c538654a59cf658923530ca852d7b599185683d2 Parents: 2e9d81a Author: Avia Efrat <[email protected]> Authored: Tue Apr 11 18:48:42 2017 +0300 Committer: Avia Efrat <[email protected]> Committed: Sat Apr 15 15:51:54 2017 +0300 ---------------------------------------------------------------------- tests/cli/base_test.py | 10 ++-- tests/cli/test_service_templates.py | 79 ++++++-------------------------- tests/cli/utils.py | 43 +++++++++++++++++ tests/conftest.py | 5 ++ 4 files changed, 70 insertions(+), 67 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c538654a/tests/cli/base_test.py ---------------------------------------------------------------------- diff --git a/tests/cli/base_test.py b/tests/cli/base_test.py index 55da476..430781a 100644 --- a/tests/cli/base_test.py +++ b/tests/cli/base_test.py @@ -1,11 +1,16 @@ from StringIO import StringIO import logging -# + import runner -from utils import setup_logger +from utils import setup_logger, MockStorage import pytest [email protected]() +def mock_storage(): + return MockStorage() + + @pytest.mark.usefixtures("redirect_logger") class TestCliBase(object): @@ -54,4 +59,3 @@ def get_default_logger_config(): 'level': logger.level} _default_logger_config = get_default_logger_config() - http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c538654a/tests/cli/test_service_templates.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_service_templates.py b/tests/cli/test_service_templates.py index 7cf6baf..61999db 100644 --- a/tests/cli/test_service_templates.py +++ b/tests/cli/test_service_templates.py @@ -4,87 +4,38 @@ from aria.cli.exceptions import AriaCliError from aria.core import Core from aria.exceptions import AriaException from aria.storage import exceptions as storage_exceptions -from tests.cli.base_test import TestCliBase, assert_exception_raised, raise_exception -from tests.mock import models - -import pytest - - [email protected] -def mock_object(mocker): - return mocker.MagicMock() - - -class MockStorage(object): - - def __init__(self): - self.service_template = MockServiceTemplateStorage - - -class MockServiceTemplateStorage(object): - - @staticmethod - def list(**_): - return [models.create_service_template('test_st'), - models.create_service_template('test_st2')] - - @staticmethod - def get(id): - st = models.create_service_template('test_st') - if id == '1': # no services and no description. - st.services = [] - if id == '2': # no services, but an description - st.description = 'test_description' - st.services = [] - if id == '3': # one service, and a description - service = models.create_service(st, 'test_s') - st.description = 'test_description' - st.services = [service] - if id == '4': # one service, and a description - service = models.create_service(st, 'test_s') - st.services = [service] - return st - - @staticmethod - def get_by_name(name): - st = models.create_service_template('test_st') - if name == 'with_inputs': - input = models.create_input(name='input1', value='value1') - st.inputs = {'input1': input} - if name == 'without_inputs': - st.inputs = {} - return st +from tests.cli.base_test import TestCliBase, assert_exception_raised, raise_exception, mock_storage class TestServiceTemplatesShow(TestCliBase): - def test_show_no_services_no_description(self, monkeypatch): + def test_show_no_services_no_description(self, monkeypatch, mock_storage): - monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + monkeypatch.setattr(Environment, 'model_storage', mock_storage) self.invoke('service_templates show 1') assert 'Description:' not in self.logger_output_string assert 'Existing services:\n[]' in self.logger_output_string - def test_show_no_services_yes_description(self, monkeypatch): + def test_show_no_services_yes_description(self, monkeypatch, mock_storage): - monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + monkeypatch.setattr(Environment, 'model_storage', mock_storage) self.invoke('service_templates show 2') assert 'Description:\ntest_description' in self.logger_output_string assert 'Existing services:\n[]' in self.logger_output_string - def test_show_one_service_yes_description(self, monkeypatch): + def test_show_one_service_yes_description(self, monkeypatch, mock_storage): - monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + monkeypatch.setattr(Environment, 'model_storage', mock_storage) self.invoke('service_templates show 3') assert 'Description:\ntest_description' in self.logger_output_string assert "Existing services:\n['test_s']" in self.logger_output_string - def test_show_one_service_no_description(self, monkeypatch): + def test_show_one_service_no_description(self, monkeypatch, mock_storage): - monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + monkeypatch.setattr(Environment, 'model_storage', mock_storage) self.invoke('service_templates show 4') assert 'Description:' not in self.logger_output_string @@ -102,9 +53,9 @@ class TestServiceTemplatesShow(TestCliBase): class TestServiceTemplatesList(TestCliBase): - def test_list_one_service_template(self, monkeypatch): + def test_list_one_service_template(self, monkeypatch, mock_storage): - monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + monkeypatch.setattr(Environment, 'model_storage', mock_storage) self.invoke('service_templates list') assert 'test_st' in self.logger_output_string assert 'test_st2' in self.logger_output_string @@ -187,13 +138,13 @@ class TestServiceTemplatesDelete(TestCliBase): class TestServiceTemplatesInputs(TestCliBase): - def test_inputs_existing_inputs(self, monkeypatch): - monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + def test_inputs_existing_inputs(self, monkeypatch, mock_storage): + monkeypatch.setattr(Environment, 'model_storage', mock_storage) self.invoke('service_templates inputs with_inputs') assert 'input1' in self.logger_output_string and 'value1' in self.logger_output_string - def test_inputs_no_inputs(self, monkeypatch): - monkeypatch.setattr(Environment, 'model_storage', MockStorage()) + def test_inputs_no_inputs(self, monkeypatch, mock_storage): + monkeypatch.setattr(Environment, 'model_storage', mock_storage) self.invoke('service_templates inputs without_inputs') assert 'No inputs' in self.logger_output_string http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c538654a/tests/cli/utils.py ---------------------------------------------------------------------- diff --git a/tests/cli/utils.py b/tests/cli/utils.py index 7093e34..c046ed3 100644 --- a/tests/cli/utils.py +++ b/tests/cli/utils.py @@ -1,5 +1,7 @@ import logging +from tests.mock import models + def setup_logger(logger_name, level=logging.INFO, @@ -38,3 +40,44 @@ def setup_logger(logger_name, logger.propagate = False return logger + + +class MockStorage(object): + + def __init__(self): + self.service_template = MockServiceTemplateStorage + + +class MockServiceTemplateStorage(object): + + @staticmethod + def list(**_): + return [models.create_service_template('test_st'), + models.create_service_template('test_st2')] + + @staticmethod + def get(id): + st = models.create_service_template('test_st') + if id == '1': # no services and no description. + st.services = [] + if id == '2': # no services, but an description + st.description = 'test_description' + st.services = [] + if id == '3': # one service, and a description + service = models.create_service(st, 'test_s') + st.description = 'test_description' + st.services = [service] + if id == '4': # one service, and a description + service = models.create_service(st, 'test_s') + st.services = [service] + return st + + @staticmethod + def get_by_name(name): + st = models.create_service_template('test_st') + if name == 'with_inputs': + input = models.create_input(name='input1', value='value1') + st.inputs = {'input1': input} + if name == 'without_inputs': + st.inputs = {} + return st \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c538654a/tests/conftest.py ---------------------------------------------------------------------- diff --git a/tests/conftest.py b/tests/conftest.py index 312ee0b..8f2c273 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -40,3 +40,8 @@ def logging_handler_cleanup(request): def clear_logging_handlers(): logging.getLogger(logger.TASK_LOGGER_NAME).handlers = [] request.addfinalizer(clear_logging_handlers) + + [email protected] +def mock_object(mocker): + return mocker.MagicMock()
