summaryrefslogtreecommitdiff
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
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.
-rw-r--r--CHANGES.txt12
-rw-r--r--repoze/bfg/testing.py42
-rw-r--r--repoze/bfg/tests/test_testing.py16
3 files changed, 59 insertions, 11 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 9f799aff6..72b1b2699 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,15 @@
+0.4.5 (11/9/2008)
+
+ 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.
+
0.4.4 (11/8/2008)
Features
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``
diff --git a/repoze/bfg/tests/test_testing.py b/repoze/bfg/tests/test_testing.py
index 5ef66e759..abd955c96 100644
--- a/repoze/bfg/tests/test_testing.py
+++ b/repoze/bfg/tests/test_testing.py
@@ -266,9 +266,9 @@ class TestDummyModel(unittest.TestCase):
from repoze.bfg.testing import DummyModel
return DummyModel
- def _makeOne(self, name=None, parent=None):
+ def _makeOne(self, name=None, parent=None, **kw):
klass = self._getTargetClass()
- return klass(name, parent)
+ return klass(name, parent, **kw)
def test__setitem__and__getitem__(self):
class Dummy:
@@ -281,6 +281,18 @@ class TestDummyModel(unittest.TestCase):
self.assertEqual(model['abc'], dummy)
self.assertRaises(KeyError, model.__getitem__, 'none')
+ def test_extra_params(self):
+ model = self._makeOne(foo=1)
+ self.assertEqual(model.foo, 1)
+
+ def test_clone(self):
+ model = self._makeOne('name', 'parent', foo=1, bar=2)
+ clone = model.clone('name2', 'parent2', bar=1)
+ self.assertEqual(clone.bar, 1)
+ self.assertEqual(clone.__name__, 'name2')
+ self.assertEqual(clone.__parent__, 'parent2')
+ self.assertEqual(clone.foo, 1)
+
class TestDummyRequest(unittest.TestCase):
def _getTargetClass(self):
from repoze.bfg.testing import DummyRequest