summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2010-07-15 17:41:05 +0000
committerChris McDonough <chrism@agendaless.com>2010-07-15 17:41:05 +0000
commit208ee5a8d6409bcdce361009dee6a2e335de1679 (patch)
tree3e47bda8becf94ae96d4b705c410fd8a2a925ffb /repoze/bfg/tests
parent6ef5b21bffe62c3ad6d276b36ba4229f681128ba (diff)
downloadpyramid-208ee5a8d6409bcdce361009dee6a2e335de1679.tar.gz
pyramid-208ee5a8d6409bcdce361009dee6a2e335de1679.tar.bz2
pyramid-208ee5a8d6409bcdce361009dee6a2e335de1679.zip
Features
-------- - New view predicate: match_val. The ``match_val`` value represents the presence of a value in the structure added to the request named ``matchdict`` during URL dispatch representing the match values from the route pattern (e.g. if the route pattern has ``:foo`` in it, and the route matches, a key will exist in the matchdict named ``foo``). Like all other view predicates, this feature is exposed via the ``bfg_view`` API, the Configurator ``add_view`` API, and the ZCML ``view`` directive. Documentation ------------- - API documentation for the ``add_view`` method of the configurator changed to include ``match_val``. - ZCML documentation for ``view`` ZCML directive changed to include ``match_val``. - The ``Views`` narrative chapter now contains a description of the ``match_val`` predicate. Bug Fixes --------- - The ``header`` predicate (when used as either a view predicate or a route predicate) had a problem when specified with a name/regex pair. When the header did not exist in the headers dictionary, the regex match could be fed ``None``, causing it to throw a ``TypeError: expected string or buffer`` exception. Now, the predicate returns False as intended. Internal -------- - Remove ``repoze.bfg.configuration.isclass`` function in favor of using ``inspect.isclass``.
Diffstat (limited to 'repoze/bfg/tests')
-rw-r--r--repoze/bfg/tests/test_configuration.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/repoze/bfg/tests/test_configuration.py b/repoze/bfg/tests/test_configuration.py
index 4762ed416..4df7af1c2 100644
--- a/repoze/bfg/tests/test_configuration.py
+++ b/repoze/bfg/tests/test_configuration.py
@@ -1334,6 +1334,16 @@ class ConfiguratorTests(unittest.TestCase):
request.headers = {'Host':'abc'}
self._assertNotFound(wrapper, None, request)
+ def test_add_view_with_header_val_missing(self):
+ from repoze.bfg.exceptions import NotFound
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ config.add_view(view=view, header=r'Host:\d')
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ request.headers = {'NoHost':'1'}
+ self.assertRaises(NotFound, wrapper, None, request)
+
def test_add_view_with_accept_match(self):
view = lambda *arg: 'OK'
config = self._makeOne()
@@ -1395,6 +1405,68 @@ class ConfiguratorTests(unittest.TestCase):
request.path_info = '/'
self._assertNotFound(wrapper, None, request)
+ def test_add_view_with_match_val_badregex(self):
+ from repoze.bfg.exceptions import ConfigurationError
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ self.assertRaises(ConfigurationError,
+ config.add_view, view=view, match_val='action:a\\')
+
+ def test_add_view_with_match_val_no_matchdict(self):
+ from repoze.bfg.exceptions import NotFound
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ config.add_view(view=view, match_val='action')
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ self.assertRaises(NotFound, wrapper, None, request)
+
+ def test_add_view_with_match_val_noval_match(self):
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ config.add_view(view=view, match_val='action')
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ request.matchdict = {'action':'whatever'}
+ self.assertEqual(wrapper(None, request), 'OK')
+
+ def test_add_view_with_match_val_noval_nomatch(self):
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ config.add_view(view=view, match_val='action')
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ request.matchdict = {'notaction':'whatever'}
+ self._assertNotFound(wrapper, None, request)
+
+ def test_add_view_with_match_val_val_match(self):
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ config.add_view(view=view, match_val='action:\d')
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ request.matchdict = {'action':'1'}
+ self.assertEqual(wrapper(None, request), 'OK')
+
+ def test_add_view_with_match_val_val_nomatch(self):
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ config.add_view(view=view, match_val=r'action:\d')
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ request.matchdict = {'action':'abc'}
+ self._assertNotFound(wrapper, None, request)
+
+ def test_add_view_with_match_val_val_missing(self):
+ from repoze.bfg.exceptions import NotFound
+ view = lambda *arg: 'OK'
+ config = self._makeOne()
+ config.add_view(view=view, match_val=r'action:\d')
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ request.matchdict = {'notaction':'1'}
+ self.assertRaises(NotFound, wrapper, None, request)
+
def test_add_view_with_custom_predicates_match(self):
view = lambda *arg: 'OK'
config = self._makeOne()
@@ -2914,6 +2986,7 @@ class Test__make_predicates(unittest.TestCase):
accept='accept',
containment='containment',
request_type='request_type',
+ view_match_val='view_match_val',
custom=('a',)
)
order2, _, _ = self._callFUT(
@@ -2925,6 +2998,7 @@ class Test__make_predicates(unittest.TestCase):
accept='accept',
containment='containment',
request_type='request_type',
+ view_match_val='view_match_val',
custom=('a',)
)
order3, _, _ = self._callFUT(
@@ -2936,6 +3010,7 @@ class Test__make_predicates(unittest.TestCase):
accept='accept',
containment='containment',
request_type='request_type',
+ view_match_val='view_match_val',
)
order4, _, _ = self._callFUT(
xhr='xhr',
@@ -2945,6 +3020,7 @@ class Test__make_predicates(unittest.TestCase):
header='header',
accept='accept',
containment='containment',
+ request_type='request_type',
)
order5, _, _ = self._callFUT(
xhr='xhr',
@@ -2953,6 +3029,7 @@ class Test__make_predicates(unittest.TestCase):
request_param='param',
header='header',
accept='accept',
+ containment='containment',
)
order6, _, _ = self._callFUT(
xhr='xhr',
@@ -2960,26 +3037,34 @@ class Test__make_predicates(unittest.TestCase):
path_info='path_info',
request_param='param',
header='header',
+ accept='accept',
)
order7, _, _ = self._callFUT(
xhr='xhr',
request_method='request_method',
path_info='path_info',
request_param='param',
+ header='header',
)
order8, _, _ = self._callFUT(
xhr='xhr',
request_method='request_method',
path_info='path_info',
+ request_param='param',
)
order9, _, _ = self._callFUT(
xhr='xhr',
request_method='request_method',
+ path_info='path_info',
)
order10, _, _ = self._callFUT(
xhr='xhr',
+ request_method='request_method',
)
order11, _, _ = self._callFUT(
+ xhr='xhr',
+ )
+ order12, _, _ = self._callFUT(
)
self.assertEqual(order1, order2)
self.failUnless(order3 > order2)
@@ -2991,6 +3076,7 @@ class Test__make_predicates(unittest.TestCase):
self.failUnless(order9 > order8)
self.failUnless(order10 > order9)
self.failUnless(order11 > order10)
+ self.failUnless(order12 > order11)
def test_ordering_importance_of_predicates(self):
order1, _, _ = self._callFUT(
@@ -3018,6 +3104,9 @@ class Test__make_predicates(unittest.TestCase):
request_type='request_type',
)
order9, _, _ = self._callFUT(
+ view_match_val='view_match_val',
+ )
+ order10, _, _ = self._callFUT(
custom=('a',),
)
self.failUnless(order1 > order2)
@@ -3028,6 +3117,7 @@ class Test__make_predicates(unittest.TestCase):
self.failUnless(order6 > order7)
self.failUnless(order7 > order8)
self.failUnless(order8 > order9)
+ self.failUnless(order9 > order10)
def test_ordering_importance_and_number(self):
order1, _, _ = self._callFUT(