From 783f914f080dcbd861f3b032946e6499283fa479 Mon Sep 17 00:00:00 2001 From: "Gavin M. Roy" Date: Wed, 15 Oct 2014 18:38:20 -0400 Subject: [PATCH] Work in progress update - Add stub classes for testing - Change how testing will work - Make the logger used in logging configuration based upon a class level constant/attribute --- sprockets/cli/__init__.py | 10 +++---- sstubs/__init__.py | 1 + sstubs/app.py | 8 ++++++ sstubs/controller.py | 12 +++++++++ sstubs/plugin.py | 21 +++++++++++++++ tests.py | 57 +++++++++++++++++---------------------- 6 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 sstubs/__init__.py create mode 100644 sstubs/app.py create mode 100644 sstubs/controller.py create mode 100644 sstubs/plugin.py diff --git a/sprockets/cli/__init__.py b/sprockets/cli/__init__.py index ae2b0db..f1f95a0 100644 --- a/sprockets/cli/__init__.py +++ b/sprockets/cli/__init__.py @@ -110,6 +110,7 @@ class CLI(object): """ CONTROLLERS = 'sprockets.controller' + LOGGER = 'sprockets' PLUGINS = 'sprockets.plugin' def __init__(self): @@ -251,8 +252,7 @@ class CLI(object): metavar='APP', help='Application to run') - @staticmethod - def _configure_logging(application, verbosity=0, syslog=False): + def _configure_logging(self, application, verbosity=0, syslog=False): """Configure logging for the application, setting the appropriate verbosity and adding syslog if it's enabled. @@ -266,13 +266,13 @@ class CLI(object): # Increase the logging verbosity if verbosity == 1: - config['loggers']['sprockets']['level'] = logging.INFO + config['loggers'][self.LOGGER]['level'] = logging.INFO elif verbosity == 2: - config['loggers']['sprockets']['level'] = logging.DEBUG + config['loggers'][self.LOGGER]['level'] = logging.DEBUG # Add syslog if it's enabled if syslog: - config['loggers']['sprockets']['handlers'].append('syslog') + config['loggers'][self.LOGGER]['handlers'].append('syslog') # Copy the sprockets logger to the application config['loggers'][application] = dict(config['loggers']['sprockets']) diff --git a/sstubs/__init__.py b/sstubs/__init__.py new file mode 100644 index 0000000..af04ef8 --- /dev/null +++ b/sstubs/__init__.py @@ -0,0 +1 @@ +__author__ = 'gavinr' diff --git a/sstubs/app.py b/sstubs/app.py new file mode 100644 index 0000000..2806de4 --- /dev/null +++ b/sstubs/app.py @@ -0,0 +1,8 @@ +""" +Stub Controller for testing + +""" + + +def run(): + pass diff --git a/sstubs/controller.py b/sstubs/controller.py new file mode 100644 index 0000000..2634dd8 --- /dev/null +++ b/sstubs/controller.py @@ -0,0 +1,12 @@ +""" +Stub Controller for testing + +""" + + +def add_cli_arguments(parser): + pass + + +def main(app, args): + pass diff --git a/sstubs/plugin.py b/sstubs/plugin.py new file mode 100644 index 0000000..69888db --- /dev/null +++ b/sstubs/plugin.py @@ -0,0 +1,21 @@ +""" +Stub Plugin Module + +""" +PRIORITY = 50 + + +def add_cli_arguments(parser): + pass + + +def initialize(controller): + pass + + +def on_start(controller): + pass + + +def on_shutdown(controller): + pass diff --git a/tests.py b/tests.py index aeae903..7bab6f5 100644 --- a/tests.py +++ b/tests.py @@ -12,46 +12,34 @@ import mock from sprockets import cli +class Package(object): + + def __init__(self, name, module_name): + self.name = name + self.module_name = module_name + + class InitializationTests(unittest.TestCase): @mock.patch('argparse.ArgumentParser.parse_args') @mock.patch('pkg_resources.iter_entry_points') - @mock.patch('importlib.import_module') - def setUp(self, import_module, iter_entry_points, parse_args): - self.import_module = import_module + def setUp(self, iter_entry_points, parse_args): self.iter_entry_points = iter_entry_points self.parse_args = parse_args - self.app_points = [mock.Mock(name='test_app', - module_name='mock_app')] - self.ctrl_points = [mock.Mock(name='test_http', - module_name='mock_http')] - self.plugin_points = [mock.Mock(name='test_plugin', - module_name='mock_plugin')] - - self.mock_app = mock.Mock() - self.mock_controller = mock.Mock() - self.mock_controller.add_cli_arguments = self.add_cli_arguments = \ - mock.Mock() - self.mock_plugin = mock.Mock() + self.app_points = [Package('tapp', 'sstubs.app')] + self.ctrl_points = [Package('tcontroller', 'sstubs.controller')] + self.plugin_points = [Package('tplugin', 'sstubs.plugin')] def entry_point_side_effect(*args, **kwargs): if kwargs.get('group') == 'sprockets.controller': - return self.ctrl_points + return iter(self.ctrl_points) elif kwargs.get('group') == 'sprockets.plugin': - return self.plugin_points + return iter(self.plugin_points) elif kwargs.get('group') == 'sprockets.test_http.app': - return self.app_points - self.iter_entry_points.side_effect = entry_point_side_effect + return iter(self.app_points) - def import_module_side_effect(*args, **kwargs): - if args[0] == 'mock_app': - return self.mock_app - elif args[0] == 'mock_http': - return self.mock_controller - elif args[0] == 'mock_plugin': - return self.mock_plugin - self.import_module.side_effect = import_module_side_effect + self.iter_entry_points.side_effect = entry_point_side_effect self.obj = cli.CLI() def test_pkg_resources_iterated(self): @@ -59,9 +47,14 @@ class InitializationTests(unittest.TestCase): mock.call(group='sprockets.plugin')] self.iter_entry_points.assert_has_calls(calls) - def test_controller_packages_imported(self): - self.import_module.assert_has_calls([mock.call('mock_http'), - mock.call('mock_plugin')]) + def test_controller_imported(self): + for attr in ['add_cli_arguments', 'main']: + self.assertTrue(hasattr(self.obj._controllers.get('tcontroller'), + attr)) + + def test_plugin_is_imported(self): + for attr in ['initialize', 'on_start', 'on_shutdown']: + self.assertTrue(hasattr(self.obj._plugins.get('tplugin'), attr)) + + - #def test_controller_argparse_method_invoked(self): - # self.add_cli_arguments.assert_called_once_with(self.obj.arg_parser)