diff options
author | markmcclain <mark.mcclain@dreamhost.com> | 2013-03-13 10:32:25 -0700 |
---|---|---|
committer | markmcclain <mark.mcclain@dreamhost.com> | 2013-03-13 10:32:25 -0700 |
commit | 7553f19f0af8bd6e41c2f5482945278dc12bc418 (patch) | |
tree | 32e3361abc18c0e527559d4d7dda3e51cb354cbb | |
parent | e98ead72c47abe52134677a399c49a9cdb90f48f (diff) | |
parent | 1bf1debce68d6196cad058b5a19d2c9a04233199 (diff) | |
download | pecan-7553f19f0af8bd6e41c2f5482945278dc12bc418.tar.gz |
Merge pull request #201 from ryanpetrello/next
Improve runtime configuration cleanup for tests.
-rw-r--r-- | pecan/tests/__init__.py | 20 | ||||
-rw-r--r-- | pecan/tests/middleware/test_debug.py | 6 | ||||
-rw-r--r-- | pecan/tests/middleware/test_errordocument.py | 5 | ||||
-rw-r--r-- | pecan/tests/middleware/test_recursive.py | 4 | ||||
-rw-r--r-- | pecan/tests/middleware/test_static.py | 6 | ||||
-rw-r--r-- | pecan/tests/test_base.py | 41 | ||||
-rw-r--r-- | pecan/tests/test_commands.py | 8 | ||||
-rw-r--r-- | pecan/tests/test_conf.py | 15 | ||||
-rw-r--r-- | pecan/tests/test_generic.py | 7 | ||||
-rw-r--r-- | pecan/tests/test_hooks.py | 12 | ||||
-rw-r--r-- | pecan/tests/test_jsonify.py | 15 | ||||
-rw-r--r-- | pecan/tests/test_rest.py | 9 | ||||
-rw-r--r-- | pecan/tests/test_scaffolds.py | 9 | ||||
-rw-r--r-- | pecan/tests/test_secure.py | 11 | ||||
-rw-r--r-- | pecan/tests/test_templating.py | 9 |
15 files changed, 107 insertions, 70 deletions
diff --git a/pecan/tests/__init__.py b/pecan/tests/__init__.py index e69de29..4d2df74 100644 --- a/pecan/tests/__init__.py +++ b/pecan/tests/__init__.py @@ -0,0 +1,20 @@ +import sys +import os +if sys.version_info < (2, 7): + from unittest2 import TestCase # pragma: nocover +else: + from unittest import TestCase # pragma: nocover + + +class PecanTestCase(TestCase): + + def setUp(self): + self.addCleanup(self._reset_global_config) + + def _reset_global_config(self): + from pecan import configuration + configuration.set_config( + dict(configuration.initconf()), + overwrite=True + ) + os.environ.pop('PECAN_CONFIG', None) diff --git a/pecan/tests/middleware/test_debug.py b/pecan/tests/middleware/test_debug.py index a4a9608..5358333 100644 --- a/pecan/tests/middleware/test_debug.py +++ b/pecan/tests/middleware/test_debug.py @@ -1,9 +1,9 @@ -from unittest import TestCase from wsgiref.util import setup_testing_defaults from webtest import TestApp from pecan.middleware.debug import DebugMiddleware +from pecan.tests import PecanTestCase class StripPasteVar(object): @@ -15,9 +15,11 @@ class StripPasteVar(object): return self.app(environ, start_response) -class TestDebugMiddleware(TestCase): +class TestDebugMiddleware(PecanTestCase): def setUp(self): + super(TestDebugMiddleware, self).setUp() + def conditional_error_app(environ, start_response): setup_testing_defaults(environ) if environ['PATH_INFO'] == '/error': diff --git a/pecan/tests/middleware/test_errordocument.py b/pecan/tests/middleware/test_errordocument.py index 7062b9c..92c4c08 100644 --- a/pecan/tests/middleware/test_errordocument.py +++ b/pecan/tests/middleware/test_errordocument.py @@ -1,8 +1,8 @@ -from unittest import TestCase from webtest import TestApp from pecan.middleware.errordocument import ErrorDocumentMiddleware from pecan.middleware.recursive import RecursiveMiddleware +from pecan.tests import PecanTestCase def four_oh_four_app(environ, start_response): @@ -18,9 +18,10 @@ def four_oh_four_app(environ, start_response): return [] -class TestDebugMiddleware(TestCase): +class TestErrorDocumentMiddleware(PecanTestCase): def setUp(self): + super(TestErrorDocumentMiddleware, self).setUp() self.app = TestApp(RecursiveMiddleware(ErrorDocumentMiddleware( four_oh_four_app, {404: '/error/404'} ))) diff --git a/pecan/tests/middleware/test_recursive.py b/pecan/tests/middleware/test_recursive.py index c52612d..8cd213c 100644 --- a/pecan/tests/middleware/test_recursive.py +++ b/pecan/tests/middleware/test_recursive.py @@ -1,8 +1,8 @@ -from unittest import TestCase from webtest import TestApp from pecan.middleware.recursive import (RecursiveMiddleware, ForwardRequestException) +from pecan.tests import PecanTestCase def simple_app(environ, start_response): @@ -56,7 +56,7 @@ def forward(app): raise AssertionError('Failed to detect forwarding loop') -class TestRecursiveMiddleware(TestCase): +class TestRecursiveMiddleware(PecanTestCase): def test_ForwardRequest_url(self): class TestForwardRequestMiddleware(Middleware): diff --git a/pecan/tests/middleware/test_static.py b/pecan/tests/middleware/test_static.py index 976a747..9a0c08c 100644 --- a/pecan/tests/middleware/test_static.py +++ b/pecan/tests/middleware/test_static.py @@ -1,13 +1,15 @@ -from unittest import TestCase from pecan.middleware.static import (StaticFileMiddleware, FileWrapper, _dump_date) +from pecan.tests import PecanTestCase import os -class TestStaticFileMiddleware(TestCase): +class TestStaticFileMiddleware(PecanTestCase): def setUp(self): + super(TestStaticFileMiddleware, self).setUp() + def app(environ, start_response): response_headers = [('Content-type', 'text/plain')] start_response('200 OK', response_headers) diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py index bbabced..24ee248 100644 --- a/pecan/tests/test_base.py +++ b/pecan/tests/test_base.py @@ -1,12 +1,13 @@ import sys +import os import warnings -from webtest import TestApp - if sys.version_info < (2, 7): import unittest2 as unittest # pragma: nocover else: import unittest # pragma: nocover +from webtest import TestApp + from pecan import ( Pecan, expose, request, response, redirect, abort, make_app, override_template, render @@ -15,22 +16,21 @@ from pecan.templating import ( _builtin_renderers as builtin_renderers, error_formatters ) from pecan.decorators import accept_noncanonical - -import os +from pecan.tests import PecanTestCase class SampleRootController(object): pass -class TestAppRoot(unittest.TestCase): +class TestAppRoot(PecanTestCase): def test_controller_lookup_by_string_path(self): app = Pecan('pecan.tests.test_base.SampleRootController') assert app.root and isinstance(app.root, SampleRootController) -class TestIndexRouting(unittest.TestCase): +class TestIndexRouting(PecanTestCase): @property def app_(self): @@ -57,7 +57,7 @@ class TestIndexRouting(unittest.TestCase): assert r.body == 'Hello, World!' -class TestObjectDispatch(unittest.TestCase): +class TestObjectDispatch(PecanTestCase): @property def app_(self): @@ -124,7 +124,7 @@ class TestObjectDispatch(unittest.TestCase): assert r.body == '/sub/sub/deeper' -class TestLookups(unittest.TestCase): +class TestLookups(PecanTestCase): @property def app_(self): @@ -179,7 +179,7 @@ class TestLookups(unittest.TestCase): assert r.status_int == 404 -class TestControllerArguments(unittest.TestCase): +class TestControllerArguments(PecanTestCase): @property def app_(self): @@ -702,7 +702,7 @@ class TestControllerArguments(unittest.TestCase): assert r.body == 'eater: 10, dummy, day=12, month=1' -class TestAbort(unittest.TestCase): +class TestAbort(PecanTestCase): def test_abort(self): class RootController(object): @@ -725,9 +725,10 @@ class TestAbort(unittest.TestCase): assert r.status_int == 401 -class TestSriptName(unittest.TestCase): +class TestScriptName(PecanTestCase): def setUp(self): + super(TestScriptName, self).setUp() self.environ = {'SCRIPT_NAME': '/foo'} def test_handle_script_name(self): @@ -741,7 +742,7 @@ class TestSriptName(unittest.TestCase): assert r.status_int == 200 -class TestRedirect(unittest.TestCase): +class TestRedirect(PecanTestCase): @property def app_(self): @@ -817,7 +818,7 @@ class TestRedirect(unittest.TestCase): assert res.request.environ['HTTP_X_FORWARDED_PROTO'] == 'https' -class TestStreamedResponse(unittest.TestCase): +class TestStreamedResponse(PecanTestCase): def test_streaming_response(self): import StringIO @@ -847,7 +848,7 @@ class TestStreamedResponse(unittest.TestCase): assert r.body == 'plain text' -class TestStateCleanup(unittest.TestCase): +class TestStateCleanup(PecanTestCase): def test_request_state_cleanup(self): """ @@ -869,7 +870,7 @@ class TestStateCleanup(unittest.TestCase): assert state.__dict__.keys() == ['app'] -class TestFileTypeExtensions(unittest.TestCase): +class TestFileTypeExtensions(PecanTestCase): @property def app_(self): @@ -957,7 +958,7 @@ class TestFileTypeExtensions(unittest.TestCase): assert r.body == 'SOME VALUE' -class TestContentTypeByAcceptHeaders(unittest.TestCase): +class TestContentTypeByAcceptHeaders(PecanTestCase): @property def app_(self): @@ -1005,7 +1006,7 @@ class TestContentTypeByAcceptHeaders(unittest.TestCase): assert r.content_type == 'text/html' -class TestCanonicalRouting(unittest.TestCase): +class TestCanonicalRouting(PecanTestCase): @property def app_(self): @@ -1091,7 +1092,7 @@ class TestCanonicalRouting(unittest.TestCase): assert 'accept' == r.body -class TestNonCanonical(unittest.TestCase): +class TestNonCanonical(PecanTestCase): @property def app_(self): @@ -1170,7 +1171,7 @@ class TestNonCanonical(unittest.TestCase): assert len(wrapped_apps) == 1 -class TestLogging(unittest.TestCase): +class TestLogging(PecanTestCase): def test_logging_setup(self): class RootController(object): @@ -1232,7 +1233,7 @@ class TestLogging(unittest.TestCase): assert f.getvalue() == 'HELLO WORLD\n' -class TestEngines(unittest.TestCase): +class TestEngines(PecanTestCase): template_path = os.path.join(os.path.dirname(__file__), 'templates') diff --git a/pecan/tests/test_commands.py b/pecan/tests/test_commands.py index d7bebd8..e9d6ead 100644 --- a/pecan/tests/test_commands.py +++ b/pecan/tests/test_commands.py @@ -1,7 +1,7 @@ -import unittest +from pecan.tests import PecanTestCase -class TestCommandManager(unittest.TestCase): +class TestCommandManager(PecanTestCase): def test_commands(self): from pecan.commands import ServeCommand, ShellCommand, CreateCommand @@ -12,7 +12,7 @@ class TestCommandManager(unittest.TestCase): assert m.commands['create'] == CreateCommand -class TestCommandRunner(unittest.TestCase): +class TestCommandRunner(PecanTestCase): def test_commands(self): from pecan.commands import ( @@ -33,7 +33,7 @@ class TestCommandRunner(unittest.TestCase): ) -class TestCreateCommand(unittest.TestCase): +class TestCreateCommand(PecanTestCase): def test_run(self): from pecan.commands import CreateCommand diff --git a/pecan/tests/test_conf.py b/pecan/tests/test_conf.py index ae590c7..f063faf 100644 --- a/pecan/tests/test_conf.py +++ b/pecan/tests/test_conf.py @@ -1,11 +1,12 @@ import os import sys -from unittest import TestCase + +from pecan.tests import PecanTestCase __here__ = os.path.dirname(__file__) -class TestConf(TestCase): +class TestConf(PecanTestCase): def test_update_config_fail_identifier(self): """Fail when naming does not pass correctness""" @@ -234,7 +235,7 @@ class TestConf(TestCase): assert to_dict['prefix_app']['prefix_template_path'] == '' -class TestGlobalConfig(TestCase): +class TestGlobalConfig(PecanTestCase): def tearDown(self): from pecan import configuration @@ -283,7 +284,7 @@ class TestGlobalConfig(TestCase): self.assertRaises(RuntimeError, configuration.set_config, '/') -class TestConfFromEnv(TestCase): +class TestConfFromEnv(PecanTestCase): # # Note that there is a good chance of pollution if ``tearDown`` does not # reset the configuration like this class does. If implementing new classes @@ -291,16 +292,12 @@ class TestConfFromEnv(TestCase): # def setUp(self): + super(TestConfFromEnv, self).setUp() self.conf_from_env = self.get_conf_from_env() os.environ['PECAN_CONFIG'] = '' def tearDown(self): os.environ['PECAN_CONFIG'] = '' - from pecan import configuration - configuration.set_config( - dict(configuration.initconf()), - overwrite=True - ) def get_conf_from_env(self): from pecan import configuration diff --git a/pecan/tests/test_generic.py b/pecan/tests/test_generic.py index a77338e..82a6ca4 100644 --- a/pecan/tests/test_generic.py +++ b/pecan/tests/test_generic.py @@ -1,13 +1,14 @@ -from pecan import Pecan, expose -from unittest import TestCase from webtest import TestApp try: from simplejson import dumps except: from json import dumps # noqa +from pecan import Pecan, expose +from pecan.tests import PecanTestCase + -class TestGeneric(TestCase): +class TestGeneric(PecanTestCase): def test_simple_generic(self): class RootController(object): diff --git a/pecan/tests/test_hooks.py b/pecan/tests/test_hooks.py index 080b802..5ad4d46 100644 --- a/pecan/tests/test_hooks.py +++ b/pecan/tests/test_hooks.py @@ -1,4 +1,7 @@ from cStringIO import StringIO + +from webtest import TestApp + from pecan import make_app, expose, redirect, abort from pecan.core import state from pecan.hooks import ( @@ -6,11 +9,10 @@ from pecan.hooks import ( ) from pecan.configuration import Config from pecan.decorators import transactional, after_commit, after_rollback -from unittest import TestCase -from webtest import TestApp +from pecan.tests import PecanTestCase -class TestHooks(TestCase): +class TestHooks(PecanTestCase): def test_basic_single_hook(self): run_hook = [] @@ -329,7 +331,7 @@ class TestHooks(TestCase): assert run_hook[5] == 'after2' -class TestTransactionHook(TestCase): +class TestTransactionHook(PecanTestCase): def test_transaction_hook(self): run_hook = [] @@ -991,7 +993,7 @@ class TestTransactionHook(TestCase): assert run_hook[3] == 'clear' -class TestRequestViewerHook(TestCase): +class TestRequestViewerHook(PecanTestCase): def test_hook_from_config(self): from pecan.configuration import _runtime_conf as conf diff --git a/pecan/tests/test_jsonify.py b/pecan/tests/test_jsonify.py index 3c5d8b5..1c1c589 100644 --- a/pecan/tests/test_jsonify.py +++ b/pecan/tests/test_jsonify.py @@ -9,14 +9,14 @@ try: from sqlalchemy.engine import create_engine except ImportError: create_engine = None # noqa -from unittest import TestCase -from pecan.jsonify import jsonify, encode, ResultProxy, RowProxy -from pecan import Pecan, expose from webtest import TestApp - from webob.multidict import MultiDict +from pecan.jsonify import jsonify, encode, ResultProxy, RowProxy +from pecan import Pecan, expose +from pecan.tests import PecanTestCase + def make_person(): class Person(object): @@ -49,7 +49,7 @@ def test_simple_rule(): assert len(result) == 1 -class TestJsonify(TestCase): +class TestJsonify(PecanTestCase): def test_simple_jsonify(self): Person = make_person() @@ -75,7 +75,7 @@ class TestJsonify(TestCase): assert loads(r.body) == {'name': 'Jonathan LaCour'} -class TestJsonifyGenericEncoder(TestCase): +class TestJsonifyGenericEncoder(PecanTestCase): def test_json_callable(self): class JsonCallable(object): def __init__(self, arg): @@ -118,9 +118,10 @@ class TestJsonifyGenericEncoder(TestCase): self.assertRaises(TypeError, encode, Foo()) -class TestJsonifySQLAlchemyGenericEncoder(TestCase): +class TestJsonifySQLAlchemyGenericEncoder(PecanTestCase): def setUp(self): + super(TestJsonifySQLAlchemyGenericEncoder, self).setUp() if not create_engine: self.create_fake_proxies() else: diff --git a/pecan/tests/test_rest.py b/pecan/tests/test_rest.py index ce855ee..4c84d5d 100644 --- a/pecan/tests/test_rest.py +++ b/pecan/tests/test_rest.py @@ -1,6 +1,3 @@ -from pecan import abort, expose, make_app, response -from pecan.rest import RestController -from unittest import TestCase from webtest import TestApp import warnings try: @@ -8,8 +5,12 @@ try: except: from json import dumps, loads # noqa +from pecan import abort, expose, make_app, response +from pecan.rest import RestController +from pecan.tests import PecanTestCase + -class TestRestController(TestCase): +class TestRestController(PecanTestCase): def test_basic_rest(self): diff --git a/pecan/tests/test_scaffolds.py b/pecan/tests/test_scaffolds.py index 1711337..6eca46b 100644 --- a/pecan/tests/test_scaffolds.py +++ b/pecan/tests/test_scaffolds.py @@ -9,6 +9,7 @@ import time from cStringIO import StringIO import pecan +from pecan.tests import PecanTestCase if sys.version_info < (2, 7): import unittest2 as unittest @@ -25,7 +26,7 @@ def has_internet(): return False -class TestPecanScaffold(unittest.TestCase): +class TestPecanScaffold(PecanTestCase): def test_normalize_pkg_name(self): from pecan.scaffolds import PecanScaffold @@ -41,9 +42,10 @@ class TestPecanScaffold(unittest.TestCase): assert s.normalize_pkg_name('sam-sam') == 'samsam' -class TestScaffoldUtils(unittest.TestCase): +class TestScaffoldUtils(PecanTestCase): def setUp(self): + super(TestScaffoldUtils, self).setUp() self.scaffold_destination = tempfile.mkdtemp() self.out = sys.stdout @@ -172,7 +174,7 @@ class TestScaffoldUtils(unittest.TestCase): ), 'r').read().strip() == 'Pecan thingy' -class TestTemplateBuilds(unittest.TestCase): +class TestTemplateBuilds(PecanTestCase): """ Used to build and test the templated quickstart project(s). """ @@ -181,6 +183,7 @@ class TestTemplateBuilds(unittest.TestCase): cwd = os.getcwd() def setUp(self): + super(TestTemplateBuilds, self).setUp() # Make a temp install location and record the cwd self.install_scaffolded_package() diff --git a/pecan/tests/test_secure.py b/pecan/tests/test_secure.py index f71e740..1fb1fd9 100644 --- a/pecan/tests/test_secure.py +++ b/pecan/tests/test_secure.py @@ -9,6 +9,7 @@ from webtest import TestApp from pecan import expose, make_app from pecan.secure import secure, unlocked, SecureController +from pecan.tests import PecanTestCase try: set() @@ -16,7 +17,7 @@ except: from sets import Set as set -class TestSecure(unittest.TestCase): +class TestSecure(PecanTestCase): def test_simple_secure(self): authorized = False @@ -190,8 +191,10 @@ class TestSecure(unittest.TestCase): assert isinstance(e, TypeError) -class TestObjectPathSecurity(unittest.TestCase): +class TestObjectPathSecurity(PecanTestCase): + def setUp(self): + super(TestObjectPathSecurity, self).setUp() permissions_checked = set() class DeepSecretController(SecureController): @@ -400,10 +403,12 @@ class TestObjectPathSecurity(unittest.TestCase): assert response.body == 'Index unlocked' -class SecureControllerSharedPermissionsRegression(unittest.TestCase): +class SecureControllerSharedPermissionsRegression(PecanTestCase): """Regression tests for https://github.com/dreamhost/pecan/issues/131""" def setUp(self): + super(SecureControllerSharedPermissionsRegression, self).setUp() + class Parent(object): @expose() def index(self): diff --git a/pecan/tests/test_templating.py b/pecan/tests/test_templating.py index 567a6b8..90ef4fb 100644 --- a/pecan/tests/test_templating.py +++ b/pecan/tests/test_templating.py @@ -1,12 +1,12 @@ -from unittest import TestCase - from pecan.templating import RendererFactory, format_line_context +from pecan.tests import PecanTestCase import tempfile -class TestTemplate(TestCase): +class TestTemplate(PecanTestCase): def setUp(self): + super(TestTemplate, self).setUp() self.rf = RendererFactory() def test_available(self): @@ -31,9 +31,10 @@ class TestTemplate(TestCase): self.assertEqual(extra_vars.make_ns({'foo': 2}), {'foo': 2}) -class TestTemplateLineFormat(TestCase): +class TestTemplateLineFormat(PecanTestCase): def setUp(self): + super(TestTemplateLineFormat, self).setUp() self.f = tempfile.NamedTemporaryFile() def tearDown(self): |