summaryrefslogtreecommitdiff
path: root/repoze/bfg/testing.py
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2008-11-10 00:05:48 +0000
committerChris McDonough <chrism@agendaless.com>2008-11-10 00:05:48 +0000
commit6a79148f646fa474c962a5a7323554963c45177b (patch)
tree81bd237ce79b2f349a90b5ac123969ef39e35b6a /repoze/bfg/testing.py
parent747cd4e494285c1297953d76c33278feb622efca (diff)
downloadpyramid-6a79148f646fa474c962a5a7323554963c45177b.tar.gz
pyramid-6a79148f646fa474c962a5a7323554963c45177b.tar.bz2
pyramid-6a79148f646fa474c962a5a7323554963c45177b.zip
Features
- Added a ``clone`` method and a ``__contains__`` method to the DummyModel testing object. - Allow DummyModel objects to receive extra keyword arguments, which will be attached as attributes. - The DummyTemplateRenderer now returns ``self`` as its implementation.
Diffstat (limited to 'repoze/bfg/testing.py')
-rw-r--r--repoze/bfg/testing.py42
1 files changed, 33 insertions, 9 deletions
diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py
index d32934031..26690c48d 100644
--- a/repoze/bfg/testing.py
+++ b/repoze/bfg/testing.py
@@ -1,8 +1,12 @@
+import copy
+
from zope.interface import Interface
from zope.interface import implements
from repoze.bfg.interfaces import IRequest
+_marker = ()
+
def registerDummySecurityPolicy(userid=None, groupids=(), permissive=True):
""" Registers a dummy ``repoze.bfg`` security policy using the
userid ``userid`` and the group ids ``groupids``. If
@@ -168,7 +172,7 @@ def make_traverser_factory(root):
class DummyTemplateRenderer:
def implementation(self):
- return None
+ return self
def __call__(self, **kw):
self.__dict__.update(kw)
@@ -196,19 +200,20 @@ class DummyModel:
to the constructor will be used as the ``__name__`` attribute of
the model. the value of ``parent`` will be used as the
``__parent__`` attribute of the model. """
- def __init__(self, name=None, parent=None):
+ def __init__(self, __name__=None, __parent__=None, **kw):
""" 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
+ value of ``__name__``, and the model's ``__parent__``
+ attribute will be set to the value of ``__parent__``. Any
+ extra keywords will be set as direct attributes of the model."""
+ self.__name__ = __name__
+ self.__parent__ = __parent__
+ self.kw = kw
+ self.__dict__.update(**kw)
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__``
+ passed in as ``val`` 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
@@ -221,6 +226,25 @@ class DummyModel:
""" Return a named subobject (see ``__setitem__``)"""
ob = self.subs[name]
return ob
+
+ def clone(self, __name__=_marker, __parent__=_marker, **kw):
+ """ Create a clone of the model object. If ``__name__`` or
+ ``__parent__`` is passed in, use the value to override the
+ existing ``__name__`` or ``__parent__`` of the model. If any
+ extra keyword args are passed in, use these keywords to add to
+ or override existing model keywords (attributes)."""
+ oldkw = self.kw.copy()
+ oldkw.update(kw)
+ inst = self.__class__(self.__name__, self.__parent__, **oldkw)
+ inst.subs = copy.deepcopy(self.subs)
+ if __name__ is not _marker:
+ inst.__name__ = __name__
+ if __parent__ is not _marker:
+ inst.__parent__ = __parent__
+ return inst
+
+ def __contains__(self, name):
+ return name in self.subs
class DummyRequest:
""" A dummy request object (imitates a :term:`WebOb` ``Request``