From a9a8a2dcfa3381ee9da3550f84dcadd55825e1d4 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 8 Nov 2008 22:40:37 +0000 Subject: Get rid of BFGTestCase base class: use only functions. --- CHANGES.txt | 4 +- docs/narr/unittesting.rst | 84 ++++++---- repoze/bfg/testing.py | 341 ++++++++++++++++----------------------- repoze/bfg/tests/test_testing.py | 128 +++++++-------- 4 files changed, 252 insertions(+), 305 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9c0a204e4..a555cf60b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,13 +4,13 @@ Next Release - Added a ``repoze.bfg.testing`` module to attempt to make it slightly easier to write unittest-based automated tests of BFG - applications. Information about this class is in the + applications. Information about this module is in the documentation. - The default template renderer now supports testing better by looking for ``ITestingTemplateRenderer`` using a relative pathname. This is exposed indirectly through the API named - ``registerTemplate`` in ``repoze.bfg.testing``. + ``registerTemplateRenderer`` in ``repoze.bfg.testing``. Deprecations diff --git a/docs/narr/unittesting.rst b/docs/narr/unittesting.rst index 8724e4a87..00db66e96 100644 --- a/docs/narr/unittesting.rst +++ b/docs/narr/unittesting.rst @@ -10,15 +10,14 @@ test cases easier to write. The facilities become particularly useful when your code calls into :mod:`repoze.bfg` -related framework functions. -The ``BFGTestCase`` Base Class ------------------------------- +Writing A Unit Test +------------------- -The ``repoze.bfg.testing`` module provides a class named -``BFGTestCase`` which you can use as a base class for unittest test -classes. +The ``repoze.bfg.testing`` module provides a number of functions which +can be used during unit testing. An example of a unit test class that +uses these functions is below. -.. code-block:: python - :linenos: +.. code-block:: python :linenos: def view_fn(context, request): from repoze.bfg.chameleon_zpt import render_template_to_response @@ -27,48 +26,63 @@ classes. say=request.params['say']) return render_template_to_response('templates/show.pt', say='Hello') - from repoze.bfg.testing import BFGTestCase + import unittest + from zope.testing.cleanup import cleanUp + from.repoze.bfg import testing - class MyTest(BFGTestCase): + class MyTest(unittest.TestCase): + def setUp(self): + cleanUp() + + def tearDown(self): + cleanUp() + def test_view_fn_not_submitted(self): - template = self.registerDummyTemplate('templates/show.pt') - request = self.makeRequest() - context = self.makeModel() + renderer = testing.registerTemplateRenderer('templates/show.pt') + context = testing.DummyModel() + request = testing.DummyRequest() response = view_fn(context, request) - self.assertEqual(template.say, 'Hello') + self.assertEqual(renderer.say, 'Hello') def test_view_fn_submitted(self): - template = self.registerDummyTemplate('templates/submitted.pt') - request = self.makeRequest() + renderer = testing.registerTemplateRenderer('templates/submitted.pt') + context = testing.DummyModel() + request = testing.DummyRequest() request.params['say'] = 'Yo' - context = self.makeModel() response = view_fn(context, request) - self.assertEqual(template.say, 'Yo') + self.assertEqual(renderer.say, 'Yo') In the above example, we create a ``MyTest`` test case that inherits -from ``BFGTestCase``. It has two test methods. The first test -method, ``test_view_fn_not_submitted`` tests the ``view_fn`` function -in the case that no "form" values (represented by request.params) have -been submitted. Its first line registers a "dummy template" named -``templates/show.pt`` via the ``registerDummyTemplate`` method (a -``BFGTestCase`` API); this function returns a DummyTemplate instance -which we hang on to for later. We then call ``makeRequest`` to get a -DummyRequest object, and ``makeModel`` to get a DummyModel object. We -call the function being tested with the manufactured context and -request. When the function is called, ``render_template_to_response`` -will call the "dummy" template object instead of the real template +from ``unittest.TestCase``. It has two test methods. + +The first test method, ``test_view_fn_not_submitted`` tests the +``view_fn`` function in the case that no "form" values (represented by +request.params) have been submitted. Its first line registers a +"dummy template renderer" named ``templates/show.pt`` via the +``registerTemplateRenderer`` function (a ``repoze.bfg.testing`` API); +this function returns a DummyTemplateRenderer instance which we hang +on to for later. We then call ``DummyRequest`` to get a dummy request +object, and ``DummyModel`` to get a dummy context object. We call the +function being tested with the manufactured context and request. When +the function is called, ``render_template_to_response`` will call the +"dummy" template renderer object instead of the real template renderer object. When it's called, it will set attributes on itself corresponding to the non-path keyword arguments provided to the ``render_template_to_response`` function. We check that the ``say`` parameter sent into the template rendering function was ``Hello`` in -this specific example. The second test, named -``test_view_fn_submitted`` tests the alternate case, where the ``say`` -form value has already been set in the request and performs a similar -template registration and assertion. +this specific example. + +The second test method, named ``test_view_fn_submitted`` tests the +alternate case, where the ``say`` form value has already been set in +the request and performs a similar template registration and +assertion. + +Note that the test calls the ``zope.testing.cleanup.cleanUp`` function +in its ``setUp`` and ``tearDown`` functions. This is required to +perform cleanup between the test runs. If you use any of the testing +API, be sure to call this function at setup and teardown of individual +tests. See the :ref:`testing_module` chapter for the entire :mod:`repoze.bfg` -specific testing API. -The ``BFGTestCase`` class inherits from ``unittest.TestCase``, so it -will be found by test finders. - diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py index d0b426160..8bfcac58e 100644 --- a/repoze/bfg/testing.py +++ b/repoze/bfg/testing.py @@ -1,160 +1,107 @@ -import unittest -from zope.component.testing import PlacelessSetup from zope.interface import Interface +from zope.interface import implements -class BFGTestCase(unittest.TestCase, PlacelessSetup): - """ A class which inherits from both ``unittest.TestCase`` and - ``zope.component.testing.PlacelessSetup`` that provides a - convenience API for writing BFG-specific tests. This class can be - subclassed within test modules and those subclasses will be found - by test loaders. Since this test case inherits from - ``PlacelessSetup`` the Zope component architecture registry is set - up and torn down between each test, which provides isolation - between tests.""" - - def setUp(self): - PlacelessSetup.setUp(self) - - def tearDown(self): - PlacelessSetup.tearDown(self) - - def registerSecurityPolicy(self, userid=None, groupids=(), permissive=True): - """ Registers a ``repoze.bfg`` security policy using the - userid ``userid`` and the group ids ``groupids``. If - ``permissive`` is true, a 'permissive' security policy is - registered; this policy allows all access. If ``permissive`` - is false, a nonpermissive security policy is registered; this - policy denies all access. To register your own (possibly more - granular) security policy, see the ``registerSecurityPolicy`` - *function* in the testing package. This function is most - useful when dealing with code that uses the - ``repoze.bfg.security``APIs named ``has_permission``, - ``authenticated_userid``, effective_principals, and - ``principals_allowed_by_permission``.""" - if permissive: - policy = DummyAllowingSecurityPolicy(userid, groupids) - else: - policy = DummyDenyingSecurityPolicy(userid, groupids) - return registerSecurityPolicy(policy) - - def registerModels(self, models): - """ Registers a dictionary of models. This is most useful for - dealing with code that wants to call the - ``repoze.bfg.traversal.find_model`` API. This API is called - with a path as one of its arguments. If the dictionary you - register when calling this method contains that path as a key - (e.g. '/foo/bar' or 'foo'), the corresponding value will be - returned to ``find_model`` (and thus to your code).""" - traverser = make_traverser_factory(models) - registerTraverserFactory(traverser) - return models - - def registerDummyTemplate(self, name): - """ Registers a 'dummy' template renderer implementation and - returns it. This is most useful when dealing with code that - wants to call ``repoze.bfg.chameleon_zpt.render_template*``or - ``repoze.bfg.chameleon_genshi.render_template*``. If you call - this method with the exact template path string that a call to - one of the ``render_template`` functions uses, the dummy - template will stand in for the real implementation. The dummy - template object will set attributes on itself corresponding to - the non-path keyword arguments provided to the ``render`` - function. You can then compare these values against what you - expect. """ - return registerTemplateRenderer(name) - - def registerEventListener(self, event_iface=Interface): - """ Registers an event listener (aka 'subscriber') listening - for events of the type ``event_iface`` and returns a list - which is appended to by the subscriber. When an event is - dispatched that matches ``event_iface``, that event will be - appended to the list. You can then compare the values in the - list to expected event notifications. This method is useful - when dealing with code that wants to call - ``zope.component.event.dispatch``.""" - L = [] - def subscriber(event): - L.append(event) - registerSubscriber(subscriber, event_iface) - return L - - def registerView(self, name, result='', view=None): - """ Registers a ``repoze.bfg`` view function under the name - ``name``. The view will return a webob Response object with - the ``result`` value as its body attribute. To gain more - control, if you pass in a non-None ``view``, this view - function will be used instead of an automatically generated - view function (and ``result`` is not used). This method is - useful when dealing with code that wants to call, - e.g. ``repoze.bfg.view.render_view_to_response``.""" - if view is None: - view = make_view(result) - registerView(view, name) - return view - - def registerViewPermission(self, name, result=True, viewpermission=None): - """ Registers a ``repoze.bfg`` 'view permission' object under - the name ``name``. The view permission return a result - denoted by the ``result`` argument. If ``result`` is True, a - ``repoze.bfg.security.Allowed`` object is returned; else a - ``repoze.bfg.security.Denied`` object is returned. To gain - more control, if you pass in a non-None ``viewpermission``, - this view permission object will be used instead of an - automatically generated view permission (and ``result`` is not - used). This method is useful when dealing with code that - wants to call, e.g. ``repoze.bfg.view.view_execution_permitted``. - Note that view permissions are not checked unless a security - policy is in effect (see ``registerSecurityPolicy``).""" - from repoze.bfg.security import Allowed - from repoze.bfg.security import Denied - if result is True: - result = Allowed('message') - else: - result = Denied('message') - if viewpermission is None: - viewpermission = make_view_permission(result) - return registerViewPermission(viewpermission, name) - - def registerAdapter(self, impl, for_, provides, name=''): - """ A shortcut for calling the Zope Component Architecture's - global site manager's ``registerAdapter`` function. The - argument ordering matches that function's exactly. Registers a - ZCA adapter.""" - return registerAdapter(impl, for_, provides, name) +from repoze.bfg.interfaces import IRequest - def registerUtility(self, impl, iface, name=''): - """ A shortcut for calling the Zope Component Architecture's - global site manager's ``registerUtility`` function. The - argument ordering matches that function's exactly. Registers - a ZCA utility.""" - return registerUtility(impl, iface, name) +def registerDummySecurityPolicy(userid=None, groupids=(), permissive=True): + """ Registers a dummy ``repoze.bfg`` security policy using the + userid ``userid`` and the group ids ``groupids``. If + ``permissive`` is true, a 'permissive' security policy is + registered; this policy allows all access. If ``permissive`` + is false, a nonpermissive security policy is registered; this + policy denies all access. To register your own (possibly more + granular) security policy, see the ``registerSecurityPolicy`` + *function* in the testing package. This function is most + useful when dealing with code that uses the + ``repoze.bfg.security``APIs named ``has_permission``, + ``authenticated_userid``, effective_principals, and + ``principals_allowed_by_permission``.""" + if permissive: + policy = DummyAllowingSecurityPolicy(userid, groupids) + else: + policy = DummyDenyingSecurityPolicy(userid, groupids) + return registerSecurityPolicy(policy) + +def registerModels(models): + """ Registers a dictionary of models. This is most useful for + dealing with code that wants to call the + ``repoze.bfg.traversal.find_model`` API. This API is called + with a path as one of its arguments. If the dictionary you + register when calling this method contains that path as a key + (e.g. '/foo/bar' or 'foo'), the corresponding value will be + returned to ``find_model`` (and thus to your code).""" + traverser = make_traverser_factory(models) + registerTraverserFactory(traverser) + return models + +def registerEventListener(event_iface=Interface): + """ Registers an event listener (aka 'subscriber') listening + for events of the type ``event_iface`` and returns a list + which is appended to by the subscriber. When an event is + dispatched that matches ``event_iface``, that event will be + appended to the list. You can then compare the values in the + list to expected event notifications. This method is useful + when dealing with code that wants to call + ``zope.component.event.dispatch``.""" + L = [] + def subscriber(event): + L.append(event) + registerSubscriber(subscriber, event_iface) + return L + +def registerTemplateRenderer(path, renderer=None, for_=None): + """ Create and register a dummy template renderer at ``path`` + (usually a relative filename ala ``templates/foo.pt``) and return + the renderer object. If ``renderer`` is non-None, it will be + registered as the renderer and returned (no dummy renderer object + will be created).""" + if for_ is None: + from repoze.bfg.interfaces import ITestingTemplateRenderer + for_ = ITestingTemplateRenderer + if renderer is None: + renderer = DummyTemplateRenderer() + return registerUtility(renderer, for_, path) + +def registerView(name, result='', view=None, for_=(Interface, Interface)): + """ Registers ``repoze.bfg`` view function under the name + ``name``. The view will return a webob Response object with the + ``result`` value as its body attribute. To gain more control, if + you pass in a non-None ``view``, this view function will be used + instead of an automatically generated view function (and + ``result`` is not used). This method is useful when dealing with + code that wants to call, + e.g. ``repoze.bfg.view.render_view_to_response``.""" + if view is None: + view = make_view(result) + from repoze.bfg.interfaces import IView + return registerAdapter(view, for_, IView, name) - def makeModel(self, name=None, parent=None): - """ Returns a 'dummy' model object, with the model's - ``__name__`` attribute set to the value of ``name``, and the - model's ``__parent__`` attribute set to the value of - ``parent``. A dummy model has a ``__setitem__`` method and a - ``__getitem__`` method. The ``__setitem__`` method can be - called with a key/value pair; the value will be decorated with - a ``__parent__`` attribute pointing at the dummy object and a - ``__name__`` attribute that is the value of the key. - A dummy model has no other attributes or methods.""" - return DummyModel(name, parent) +def registerViewPermission(name, result=True, viewpermission=None, + for_=(Interface, Interface)): + """ Registers a ``repoze.bfg`` 'view permission' object under + the name ``name``. The view permission return a result + denoted by the ``result`` argument. If ``result`` is True, a + ``repoze.bfg.security.Allowed`` object is returned; else a + ``repoze.bfg.security.Denied`` object is returned. To gain + more control, if you pass in a non-None ``viewpermission``, + this view permission object will be used instead of an + automatically generated view permission (and ``result`` is not + used). This method is useful when dealing with code that + wants to call, e.g. ``repoze.bfg.view.view_execution_permitted``. + Note that view permissions are not checked unless a security + policy is in effect (see ``registerSecurityPolicy``).""" + from repoze.bfg.security import Allowed + from repoze.bfg.security import Denied + if result is True: + result = Allowed('message') + else: + result = Denied('message') + if viewpermission is None: + viewpermission = make_view_permission(result) + from repoze.bfg.interfaces import IViewPermission + return registerAdapter(viewpermission, for_, IViewPermission, name) - def makeRequest(self, path='/', params=None, environ=None, headers=None, - **kw): - """ Returns a ``DummyRequest`` object (mimics a WebOb Request - object) using ``path`` as the path. If ``environ`` is - non-None, it should contain keys that will form the request's - environment. If ``base_url`` is passed in, then the - ``wsgi.url_scheme``, ``HTTP_HOST``, and ``SCRIPT_NAME`` will - be filled in from that value. If ``headers`` is not None, - these will be used as ``request.headers``. The returned - request object will implement the ``repoze.bfg.IRequest`` - interface.""" - return makeRequest(path, params, environ, headers, **kw) - -def registerUtility(impl, iface, name=''): +def registerUtility(impl, iface=Interface, name=''): import zope.component gsm = zope.component.getGlobalSiteManager() gsm.registerUtility(impl, iface, name=name) @@ -176,14 +123,6 @@ def registerSubscriber(subscriber, iface=Interface): gsm.registerHandler(subscriber, iface) return subscriber -def registerTemplateRenderer(path, renderer=None, iface=None): - if iface is None: - from repoze.bfg.interfaces import ITestingTemplateRenderer - iface = ITestingTemplateRenderer - if renderer is None: - renderer = DummyTemplateRenderer() - return registerUtility(renderer, iface, path) - def registerSecurityPolicy(policy): from repoze.bfg.interfaces import ISecurityPolicy return registerUtility(policy, ISecurityPolicy) @@ -192,47 +131,6 @@ def registerTraverserFactory(traverser, for_=Interface): from repoze.bfg.interfaces import ITraverserFactory return registerAdapter(traverser, for_, ITraverserFactory) -def registerView(view, name, for_=(Interface, Interface)): - from repoze.bfg.interfaces import IView - return registerAdapter(view, for_, IView, name) - -def registerViewPermission(viewpermission, name, for_=(Interface, Interface)): - from repoze.bfg.interfaces import IViewPermission - return registerAdapter(viewpermission, for_, IViewPermission, name) - -def makeRequest(path, environ=None, base_url=None, headers=None, **kw): - return DummyRequest(path, environ, base_url, headers, **kw) - -from zope.interface import implements -from repoze.bfg.interfaces import IRequest - -class DummyRequest: - implements(IRequest) - def __init__(self, path, params=None, environ=None, headers=None, **kw): - if environ is None: - environ = {} - if params is None: - params = {} - if headers is None: - headers = {} - self.environ = environ - self.headers = headers - self.params = params - self.GET = params - self.POST = params - self.application_url = 'http://example.com' - self.host_url = self.application_url - self.path_url = self.application_url - self.path = path - self.path_info = path - self.script_name = '' - self.path_qs = '' - self.url = self.application_url - self.host = 'example.com:80' - self.body = '' - self.cookies = {} - self.__dict__.update(kw) - class _DummySecurityPolicy: def __init__(self, userid=None, groupids=()): self.userid = userid @@ -302,17 +200,58 @@ def make_view_permission(result): return DummyViewPermission class DummyModel: + """ A dummy :mod:`repoze.bfg` model object """ def __init__(self, name=None, parent=None): + """ The he model's ``__name__`` attribute will be set to the + value of ``name``, and the model's ``__parent__`` attribute + will be set to the value of ``parent``. A dummy model has a + ``__setitem__`` method and a ``__getitem__`` method. A dummy + model has no other attributes or methods.""" self.__name__ = name self.__parent__ = parent self.subs = {} def __setitem__(self, name, val): + """ When the ``__setitem__`` method is called, the object + passed in as ``value`` will be decorated with a ``__parent__`` + attribute pointing at the dummy model and a ``__name__`` + attribute that is the value of ``name``. The value will then + be returned when dummy model's ``__getitem__`` is called with + the name ``name```.""" val.__name__ = name val.__parent__ = self self.subs[name] = val def __getitem__(self, name): + """ Return a named subobject (see ``__setitem__``)""" ob = self.subs[name] return ob +class DummyRequest: + """ A dummy request object (imitates a WebOb request object) """ + implements(IRequest) + def __init__(self, path='/', params=None, environ=None, headers=None, **kw): + if environ is None: + environ = {} + if params is None: + params = {} + if headers is None: + headers = {} + self.environ = environ + self.headers = headers + self.params = params + self.GET = params + self.POST = params + self.application_url = 'http://example.com' + self.host_url = self.application_url + self.path_url = self.application_url + self.path = path + self.path_info = path + self.script_name = '' + self.path_qs = '' + self.url = self.application_url + self.host = 'example.com:80' + self.body = '' + self.cookies = {} + self.__dict__.update(kw) + diff --git a/repoze/bfg/tests/test_testing.py b/repoze/bfg/tests/test_testing.py index 23d194af6..6d133ee93 100644 --- a/repoze/bfg/tests/test_testing.py +++ b/repoze/bfg/tests/test_testing.py @@ -1,24 +1,17 @@ from zope.component.testing import PlacelessSetup import unittest -class TestBFGTestCase(unittest.TestCase, PlacelessSetup): +class TestTestingFunctions(unittest.TestCase, PlacelessSetup): def setUp(self): PlacelessSetup.setUp(self) def tearDown(self): PlacelessSetup.tearDown(self) - def _getTargetClass(self): - from repoze.bfg.testing import BFGTestCase - return BFGTestCase - - def _makeOne(self): - return self._getTargetClass()(methodName='__doc__') - - def test_registerSecurityPolicy_permissive(self): - case = self._makeOne() - case.registerSecurityPolicy('user', ('group1', 'group2'), - permissive=True) + def test_registerDummySecurityPolicy_permissive(self): + from repoze.bfg import testing + testing.registerDummySecurityPolicy('user', ('group1', 'group2'), + permissive=True) from repoze.bfg.interfaces import ISecurityPolicy from zope.component import getUtility ut = getUtility(ISecurityPolicy) @@ -27,10 +20,10 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): self.assertEqual(ut.userid, 'user') self.assertEqual(ut.groupids, ('group1', 'group2')) - def test_registerSecurityPolicy_nonpermissive(self): - case = self._makeOne() - case.registerSecurityPolicy('user', ('group1', 'group2'), - permissive=False) + def test_registerDummySecurityPolicy_nonpermissive(self): + from repoze.bfg import testing + testing.registerDummySecurityPolicy('user', ('group1', 'group2'), + permissive=False) from repoze.bfg.interfaces import ISecurityPolicy from zope.component import getUtility ut = getUtility(ISecurityPolicy) @@ -43,8 +36,8 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): ob1 = object() ob2 = object() models = {'/ob1':ob1, '/ob2':ob2} - case = self._makeOne() - case.registerModels(models) + from repoze.bfg import testing + testing.registerModels(models) from zope.component import getAdapter from repoze.bfg.interfaces import ITraverserFactory adapter = getAdapter(None, ITraverserFactory) @@ -54,9 +47,9 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): from repoze.bfg.traversal import find_model self.assertEqual(find_model(None, '/ob1'), ob1) - def test_registerDummyTemplate(self): - case = self._makeOne() - template = case.registerDummyTemplate('templates/foo') + def test_registerTemplateRenderer(self): + from repoze.bfg import testing + template = testing.registerTemplateRenderer('templates/foo') from repoze.bfg.testing import DummyTemplateRenderer self.failUnless(isinstance(template, DummyTemplateRenderer)) from repoze.bfg.chameleon_zpt import render_template_to_response @@ -66,14 +59,14 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): self.assertEqual(response.body, '') def test_registerEventListener_single(self): - case = self._makeOne() + from repoze.bfg import testing from zope.interface import implements from zope.interface import Interface class IEvent(Interface): pass class Event: implements(IEvent) - L = case.registerEventListener(IEvent) + L = testing.registerEventListener(IEvent) from zope.component.event import dispatch event = Event() dispatch(event) @@ -83,8 +76,8 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): self.assertEqual(len(L), 1) def test_registerEventListener_defaults(self): - case = self._makeOne() - L = case.registerEventListener() + from repoze.bfg import testing + L = testing.registerEventListener() from zope.component.event import dispatch event = object() dispatch(event) @@ -94,8 +87,8 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): self.assertEqual(len(L), 3) def test_registerView_defaults(self): - case = self._makeOne() - view = case.registerView('moo.html') + from repoze.bfg import testing + view = testing.registerView('moo.html') import types self.failUnless(isinstance(view, types.FunctionType)) from repoze.bfg.view import render_view_to_response @@ -103,8 +96,8 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): self.assertEqual(view(None, None).body, response.body) def test_registerView_withresult(self): - case = self._makeOne() - view = case.registerView('moo.html', 'yo') + from repoze.bfg import testing + view = testing.registerView('moo.html', 'yo') import types self.failUnless(isinstance(view, types.FunctionType)) from repoze.bfg.view import render_view_to_response @@ -112,11 +105,11 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): self.assertEqual(response.body, 'yo') def test_registerView_custom(self): - case = self._makeOne() + from repoze.bfg import testing def view(context, request): from webob import Response return Response('123') - view = case.registerView('moo.html', view=view) + view = testing.registerView('moo.html', view=view) import types self.failUnless(isinstance(view, types.FunctionType)) from repoze.bfg.view import render_view_to_response @@ -124,19 +117,19 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): self.assertEqual(response.body, '123') def test_registerViewPermission_defaults(self): - case = self._makeOne() - view = case.registerViewPermission('moo.html') + from repoze.bfg import testing + view = testing.registerViewPermission('moo.html') from repoze.bfg.view import view_execution_permitted - case.registerSecurityPolicy() + testing.registerDummySecurityPolicy() result = view_execution_permitted(None, None, 'moo.html') self.failUnless(result) self.assertEqual(result.msg, 'message') def test_registerViewPermission_denying(self): - case = self._makeOne() - view = case.registerViewPermission('moo.html', result=False) + from repoze.bfg import testing + view = testing.registerViewPermission('moo.html', result=False) from repoze.bfg.view import view_execution_permitted - case.registerSecurityPolicy() + testing.registerDummySecurityPolicy() result = view_execution_permitted(None, None, 'moo.html') self.failIf(result) self.assertEqual(result.msg, 'message') @@ -150,11 +143,11 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): def __call__(self, secpol): return True - case = self._makeOne() - view = case.registerViewPermission('moo.html', - viewpermission=ViewPermission) + from repoze.bfg import testing + view = testing.registerViewPermission('moo.html', + viewpermission=ViewPermission) from repoze.bfg.view import view_execution_permitted - case.registerSecurityPolicy() + testing.registerDummySecurityPolicy() result = view_execution_permitted(None, None, 'moo.html') self.failUnless(result is True) @@ -175,8 +168,8 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): implements(for_) for1 = For_() for2 = For_() - case = self._makeOne() - case.registerAdapter(Provider, (for_, for_), provides, name='foo') + from repoze.bfg import testing + testing.registerAdapter(Provider, (for_, for_), provides, name='foo') adapter = getMultiAdapter((for1, for2), provides, name='foo') self.failUnless(isinstance(adapter, Provider)) self.assertEqual(adapter.context, for1) @@ -192,33 +185,11 @@ class TestBFGTestCase(unittest.TestCase, PlacelessSetup): implements(iface) def __call__(self): return 'foo' - case = self._makeOne() utility = impl() - case.registerUtility(utility, iface, name='mudge') + from repoze.bfg import testing + testing.registerUtility(utility, iface, name='mudge') self.assertEqual(getUtility(iface, name='mudge')(), 'foo') - def test_makeModel(self): - case = self._makeOne() - parent = object() - model = case.makeModel('name', parent) - self.assertEqual(model.__name__, 'name') - self.assertEqual(model.__parent__, parent) - - def test_makeRequest(self): - case = self._makeOne() - request = case.makeRequest('/abc', - params = {'say':'Hello'}, - environ = {'PATH_INFO':'/foo'}, - headers = {'X-Foo':'YUP'}, - water = 1) - self.assertEqual(request.path, '/abc') - self.assertEqual(request.params['say'], 'Hello') - self.assertEqual(request.GET['say'], 'Hello') - self.assertEqual(request.POST['say'], 'Hello') - self.assertEqual(request.headers['X-Foo'], 'YUP') - self.assertEqual(request.environ['PATH_INFO'], '/foo') - self.assertEqual(request.water, 1) - class TestDummyAllowingSecurityPolicy(unittest.TestCase): def _getTargetClass(self): from repoze.bfg.testing import DummyAllowingSecurityPolicy @@ -310,3 +281,26 @@ class TestDummyModel(unittest.TestCase): self.assertEqual(model['abc'], dummy) self.assertRaises(KeyError, model.__getitem__, 'none') +class TestDummyRequest(unittest.TestCase): + def _getTargetClass(self): + from repoze.bfg.testing import DummyRequest + return DummyRequest + + def _makeOne(self, *arg, **kw): + return self._getTargetClass()(*arg, **kw) + + def test_it(self): + request = self._makeOne('/abc', + params = {'say':'Hello'}, + environ = {'PATH_INFO':'/foo'}, + headers = {'X-Foo':'YUP'}, + water = 1) + self.assertEqual(request.path, '/abc') + self.assertEqual(request.params['say'], 'Hello') + self.assertEqual(request.GET['say'], 'Hello') + self.assertEqual(request.POST['say'], 'Hello') + self.assertEqual(request.headers['X-Foo'], 'YUP') + self.assertEqual(request.environ['PATH_INFO'], '/foo') + self.assertEqual(request.water, 1) + + -- cgit v1.2.3