Permalink
Browse files

add some tests

  • Loading branch information...
mcdonc committed Aug 3, 2012
1 parent a00621e commit 4f0b02e36591d0cfbfa0e328e1e428b5a286c09c
Showing with 151 additions and 8 deletions.
  1. +8 −8 pyramid/config/predicates.py
  2. +143 −0 pyramid/tests/test_config/test_predicates.py
@@ -18,26 +18,25 @@ def __init__(self, val):
self.val = bool(val)
def __text__(self):
- return 'xhr = True'
+ return 'xhr = %s' % self.val
def __phash__(self):
- return 'xhr:%r' % (self.val,)
+ return 'xhr:%s' % self.val
def __call__(self, context, request):
- return request.is_xhr
-
+ return bool(request.is_xhr) is self.val
class RequestMethodPredicate(object):
def __init__(self, val):
self.val = as_sorted_tuple(val)
def __text__(self):
- return 'request method = %r' % (self.val,)
+ return 'request method = %s' % (','.join(self.val))
def __phash__(self):
L = []
for v in self.val:
- L.append('request_method:%r' % v)
+ L.append('request_method:%s' % v)
return L
def __call__(self, context, request):
@@ -56,7 +55,7 @@ def __text__(self):
return 'path_info = %s' % (self.orig,)
def __phash__(self):
- return 'path_info:%r' % (self.orig,)
+ return 'path_info:%s' % (self.orig,)
def __call__(self, context, request):
return self.val.match(request.upath_info) is not None
@@ -67,6 +66,7 @@ def __init__(self, val):
v = None
if '=' in name:
name, v = name.split('=', 1)
+ name, v = name.strip(), v.strip()
if v is None:
self.text = 'request_param %s' % (name,)
else:
@@ -78,7 +78,7 @@ def __text__(self):
return self.text
def __phash__(self):
- return 'request_param:%r=%r' % (self.name, self.val)
+ return 'request_param:%s=%r' % (self.name, self.val)
def __call__(self, context, request):
if self.val is None:
@@ -0,0 +1,143 @@
+import unittest
+
+from pyramid.compat import text_
+
+class TestXHRPredicate(unittest.TestCase):
+ def _makeOne(self, val):
+ from pyramid.config.predicates import XHRPredicate
+ return XHRPredicate(val)
+
+ def test___call___true(self):
+ inst = self._makeOne(True)
+ request = Dummy()
+ request.is_xhr = True
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___false(self):
+ inst = self._makeOne(True)
+ request = Dummy()
+ request.is_xhr = False
+ result = inst(None, request)
+ self.assertFalse(result)
+
+ def test___text__(self):
+ inst = self._makeOne(True)
+ self.assertEqual(inst.__text__(), 'xhr = True')
+
+ def test___phash__(self):
+ inst = self._makeOne(True)
+ self.assertEqual(inst.__phash__(), 'xhr:True')
+
+class TestRequestMethodPredicate(unittest.TestCase):
+ def _makeOne(self, val):
+ from pyramid.config.predicates import RequestMethodPredicate
+ return RequestMethodPredicate(val)
+
+ def test___call___true_single(self):
+ inst = self._makeOne('GET')
+ request = Dummy()
+ request.method = 'GET'
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___true_multi(self):
+ inst = self._makeOne(('GET','HEAD'))
+ request = Dummy()
+ request.method = 'GET'
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___false(self):
+ inst = self._makeOne(('GET','HEAD'))
+ request = Dummy()
+ request.method = 'POST'
+ result = inst(None, request)
+ self.assertFalse(result)
+
+ def test___text__(self):
+ inst = self._makeOne(('HEAD','GET'))
+ self.assertEqual(inst.__text__(), 'request method = GET,HEAD')
+
+ def test___phash__(self):
+ inst = self._makeOne(('HEAD','GET'))
+ self.assertEqual(inst.__phash__(), ['request_method:GET',
+ 'request_method:HEAD'])
+
+class TestPathInfoPredicate(unittest.TestCase):
+ def _makeOne(self, val):
+ from pyramid.config.predicates import PathInfoPredicate
+ return PathInfoPredicate(val)
+
+ def test_ctor_compilefail(self):
+ from pyramid.exceptions import ConfigurationError
+ self.assertRaises(ConfigurationError, self._makeOne, '\\')
+
+ def test___call___true(self):
+ inst = self._makeOne(r'/\d{2}')
+ request = Dummy()
+ request.upath_info = text_('/12')
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___false(self):
+ inst = self._makeOne(r'/\d{2}')
+ request = Dummy()
+ request.upath_info = text_('/n12')
+ result = inst(None, request)
+ self.assertFalse(result)
+
+ def test___text__(self):
+ inst = self._makeOne('/')
+ self.assertEqual(inst.__text__(), 'path_info = /')
+
+ def test___phash__(self):
+ inst = self._makeOne('/')
+ self.assertEqual(inst.__phash__(), 'path_info:/')
+
+class TestRequestParamPredicate(unittest.TestCase):
+ def _makeOne(self, val):
+ from pyramid.config.predicates import RequestParamPredicate
+ return RequestParamPredicate(val)
+
+ def test___call___true_exists(self):
+ inst = self._makeOne('abc')
+ request = Dummy()
+ request.params = {'abc':1}
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___true_withval(self):
+ inst = self._makeOne('abc=1')
+ request = Dummy()
+ request.params = {'abc':'1'}
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___false(self):
+ inst = self._makeOne('abc')
+ request = Dummy()
+ request.params = {}
+ result = inst(None, request)
+ self.assertFalse(result)
+
+ def test___text__exists(self):
+ inst = self._makeOne('abc')
+ self.assertEqual(inst.__text__(), 'request_param abc')
+
+ def test___text__withval(self):
+ inst = self._makeOne('abc= 1')
+ self.assertEqual(inst.__text__(), 'request_param abc = 1')
+
+ def test___phash__exists(self):
+ inst = self._makeOne('abc')
+ self.assertEqual(inst.__phash__(), 'request_param:abc=None')
+
+ def test___phash__withval(self):
+ inst = self._makeOne('abc= 1')
+ self.assertEqual(inst.__phash__(), "request_param:abc='1'")
+
+class Dummy(object):
+ def __init__(self, **kw):
+ self.__dict__.update(**kw)
+

0 comments on commit 4f0b02e

Please sign in to comment.