diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-04-13 20:09:37 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-04-13 20:09:37 +0000 |
| commit | b6cef5185dcb4e9ff6c26c378f65590daaa8d828 (patch) | |
| tree | 44e32fcfa700ae62f7255d102d3a9071b3363b2f | |
| parent | 223fe5414a519183192f0a95d017e968c344729c (diff) | |
| download | pyramid-b6cef5185dcb4e9ff6c26c378f65590daaa8d828.tar.gz pyramid-b6cef5185dcb4e9ff6c26c378f65590daaa8d828.tar.bz2 pyramid-b6cef5185dcb4e9ff6c26c378f65590daaa8d828.zip | |
- ``repoze.bfg.testing.DummyRequest`` will now contain ``params``,
``GET``, and ``POST`` attributes that are instances of the class
``FauxMultiDict``. A FauxMultiDict differs from a "plain"
dictionary inasmuch as it has a ``getall`` method. ``getall`` is an
interface exposed by the MultiDict implementation used by WebOb for
``params``, ``GET``, and ``POST``.
| -rw-r--r-- | CHANGES.txt | 13 | ||||
| -rw-r--r-- | repoze/bfg/testing.py | 20 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_testing.py | 44 |
3 files changed, 74 insertions, 3 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 463daf95c..61640109b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,16 @@ +Next release +============ + +Features +-------- + +- ``repoze.bfg.testing.DummyRequest`` will now contain ``params``, + ``GET``, and ``POST`` attributes that are instances of the class + ``FauxMultiDict``. A FauxMultiDict differs from a "plain" + dictionary inasmuch as it has a ``getall`` method. ``getall`` is an + interface exposed by the MultiDict implementation used by WebOb for + ``params``, ``GET``, and ``POST``. + 0.7.0 (2009-04-11) ================== diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py index 1ad8ddf17..e8164bfb9 100644 --- a/repoze/bfg/testing.py +++ b/repoze/bfg/testing.py @@ -346,14 +346,14 @@ class DummyRequest: cookies = {} self.environ = environ self.headers = headers - self.params = params + self.params = FauxMultiDict(params) self.cookies = cookies - self.GET = params + self.GET = FauxMultiDict(params) if post is not None: self.method = 'POST' self.POST = post else: - self.POST = params + self.POST = FauxMultiDict(params) self.host_url = self.application_url self.path_url = self.application_url self.url = self.application_url @@ -368,3 +368,17 @@ class DummyRequest: self.marshalled = params # repoze.monty self.__dict__.update(kw) +class FauxMultiDict(dict): + """ GET, POST, and params attrs of WebOb requests are not actually + dictionaries; they are 'multi dicts', which means in the actual + implementation they can have more than one value per key. We fake + out a multidict here, although our implementation does not allow + for more than one value per key. """ + def getall(self, key): + """ Return a single-valued sequence containing the value for + key, e.g. ['value'].""" + val = self.get(key, _marker) + if val is _marker: + return [] + return [val] + diff --git a/repoze/bfg/tests/test_testing.py b/repoze/bfg/tests/test_testing.py index 1154fc27b..bd0dba5fd 100644 --- a/repoze/bfg/tests/test_testing.py +++ b/repoze/bfg/tests/test_testing.py @@ -423,6 +423,50 @@ class TestDummyRequest(unittest.TestCase): request = self._makeOne(water = 1) self.assertEqual(request.water, 1) + def test_paramcompare(self): + request = self._makeOne(params={'foo': 'bar'}) + self.assertEqual(request.params, {'foo':'bar'}) + self.assertEqual(request.GET, {'foo':'bar'}) + +class TestFauxMultiDict(unittest.TestCase): + def _getTargetClass(self): + from repoze.bfg.testing import FauxMultiDict + return FauxMultiDict + + def _makeOne(self, dict=None): + klass = self._getTargetClass() + if dict is not None: + return klass(dict) + return klass() + + def test_getall_nodefault(self): + inst = self._makeOne() + inst['a'] = 'b' + self.assertEqual(inst.getall('a'), ['b']) + + def test_getall_withdefault(self): + inst = self._makeOne({'a':'b'}) + self.assertEqual(inst.getall('a'), ['b']) + + def test_getall_notexist(self): + inst = self._makeOne({'a':'b'}) + self.assertEqual(inst.getall('nope'), []) + + def test_repr(self): + inst = self._makeOne({'a':'b'}) + r = repr(inst) + self.assertEqual(r, "{'a': 'b'}") + + def test_eq(self): + inst = self._makeOne({'a':'b'}) + self.assertEqual(inst, {'a':'b'}) + + def test_convert_to_dict(self): + inst = self._makeOne({'a':'b'}) + d = dict(inst) + self.assertEqual(d, {'a':'b'}) + + class TestDummyTemplateRenderer(unittest.TestCase): def _getTargetClass(self, ): from repoze.bfg.testing import DummyTemplateRenderer |
