Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

test standalone default predicates; make protocol "phash()" and "text…

…()" rather than "__phash__()" and "__text__()"
  • Loading branch information...
commit 4d2602cecb90ff75e2ba33e4435b9dcee830085f 1 parent 4f0b02e
@mcdonc mcdonc authored
View
84 pyramid/config/predicates.py
@@ -17,11 +17,10 @@ class XHRPredicate(object):
def __init__(self, val):
self.val = bool(val)
- def __text__(self):
+ def text(self):
return 'xhr = %s' % self.val
- def __phash__(self):
- return 'xhr:%s' % self.val
+ phash = text
def __call__(self, context, request):
return bool(request.is_xhr) is self.val
@@ -30,14 +29,10 @@ class RequestMethodPredicate(object):
def __init__(self, val):
self.val = as_sorted_tuple(val)
- def __text__(self):
- return 'request method = %s' % (','.join(self.val))
+ def text(self):
+ return 'request_method = %s' % (','.join(self.val))
- def __phash__(self):
- L = []
- for v in self.val:
- L.append('request_method:%s' % v)
- return L
+ phash = text
def __call__(self, context, request):
return request.method in self.val
@@ -51,11 +46,10 @@ def __init__(self, val):
raise ConfigurationError(why.args[0])
self.val = val
- def __text__(self):
+ def text(self):
return 'path_info = %s' % (self.orig,)
- def __phash__(self):
- return 'path_info:%s' % (self.orig,)
+ phash = text
def __call__(self, context, request):
return self.val.match(request.upath_info) is not None
@@ -68,17 +62,16 @@ def __init__(self, val):
name, v = name.split('=', 1)
name, v = name.strip(), v.strip()
if v is None:
- self.text = 'request_param %s' % (name,)
+ self._text = 'request_param %s' % (name,)
else:
- self.text = 'request_param %s = %s' % (name, v)
+ self._text = 'request_param %s = %s' % (name, v)
self.name = name
self.val = v
- def __text__(self):
- return self.text
+ def text(self):
+ return self._text
- def __phash__(self):
- return 'request_param:%s=%r' % (self.name, self.val)
+ phash = text
def __call__(self, context, request):
if self.val is None:
@@ -97,17 +90,16 @@ def __init__(self, val):
except re.error as why:
raise ConfigurationError(why.args[0])
if v is None:
- self.text = 'header %s' % (name,)
+ self._text = 'header %s' % (name,)
else:
- self.text = 'header %s = %s' % (name, v)
+ self._text = 'header %s = %s' % (name, v)
self.name = name
self.val = v
- def __text__(self):
- return self.text
+ def text(self):
+ return self._text
- def __phash__(self):
- return 'header:%r=%r' % (self.name, self.val)
+ phash = text
def __call__(self, context, request):
if self.val is None:
@@ -121,11 +113,10 @@ class AcceptPredicate(object):
def __init__(self, val):
self.val = val
- def __text__(self):
+ def text(self):
return 'accept = %s' % (self.val,)
- def __phash__(self):
- return 'accept:%r' % (self.val,)
+ phash = text
def __call__(self, context, request):
return self.val in request.accept
@@ -134,11 +125,10 @@ class ContainmentPredicate(object):
def __init__(self, val):
self.val = val
- def __text__(self):
+ def text(self):
return 'containment = %s' % (self.val,)
- def __phash__(self):
- return 'containment:%r' % hash(self.val)
+ phash = text
def __call__(self, context, request):
ctx = getattr(request, 'context', context)
@@ -148,11 +138,10 @@ class RequestTypePredicate(object):
def __init__(self, val):
self.val = val
- def __text__(self):
+ def text(self):
return 'request_type = %s' % (self.val,)
- def __phash__(self):
- return 'request_type:%r' % hash(self.val)
+ phash = text
def __call__(self, context, request):
return self.val.providedBy(request)
@@ -163,18 +152,15 @@ def __init__(self, val):
val = (val,)
val = sorted(val)
self.val = val
- self.reqs = [
- (x.strip(), y.strip()) for x, y in [ p.split('=', 1) for p in val ]
- ]
+ reqs = [ p.split('=', 1) for p in val ]
+ self.reqs = [ (x.strip(), y.strip()) for x, y in reqs ]
- def __text__(self):
- return 'match_param %s' % (self.val,)
+ def text(self):
+ return 'match_param %s' % ','.join(
+ ['%s=%s' % (x,y) for x, y in self.reqs]
+ )
- def __phash__(self):
- L = []
- for k, v in self.reqs:
- L.append('match_param:%r=%r' % (k, v))
- return L
+ phash = text
def __call__(self, context, request):
for k, v in self.reqs:
@@ -186,10 +172,10 @@ class CustomPredicate(object):
def __init__(self, func):
self.func = func
- def __text__(self):
+ def text(self):
return getattr(self.func, '__text__', repr(self.func))
- def __phash__(self):
+ def phash(self):
return 'custom:%r' % hash(self.func)
def __call__(self, context, request):
@@ -201,10 +187,10 @@ def __init__(self, val):
_, self.tgenerate = _compile_route(val)
self.val = val
- def __text__(self):
+ def text(self):
return 'traverse matchdict pseudo-predicate'
- def __phash__(self):
+ def phash(self):
return ''
def __call__(self, context, request):
@@ -214,5 +200,3 @@ def __call__(self, context, request):
tvalue = self.tgenerate(m)
m['traverse'] = traversal_path(tvalue)
return True
-
-
View
2  pyramid/config/util.py
@@ -468,7 +468,7 @@ def make(self, **kw):
vals = (vals,)
for val in vals:
predicate = predicate_factory(val)
- hashes = predicate.__phash__()
+ hashes = predicate.phash()
if not is_nonstr_iter(hashes):
hashes = [hashes]
for h in hashes:
View
2  pyramid/config/views.py
@@ -285,7 +285,7 @@ def predicate_wrapper(context, request):
view_name = getattr(view, '__name__', view)
raise PredicateMismatch(
'predicate mismatch for view %s (%s)' % (
- view_name, predicate.__text__))
+ view_name, predicate.text()))
return view(context, request)
def checker(context, request):
return all((predicate(context, request) for predicate in
View
8 pyramid/scripts/pviews.py
@@ -17,11 +17,11 @@ class PViewsCommand(object):
each route+predicate set, print each view that might match and its
predicates.
- This command accepts two positional arguments: "config_uri" specifies the
+ This command accepts two positional arguments: 'config_uri' specifies the
PasteDeploy config file to use for the interactive shell. The format is
- "inifile#name". If the name is left off, "main" will be assumed. "url"
+ 'inifile#name'. If the name is left off, 'main' will be assumed. 'url'
specifies the path info portion of a URL that will be used to find
- matching views. Example: "proutes myapp.ini#main /url"
+ matching views. Example: 'proutes myapp.ini#main /url'
"""
stdout = sys.stdout
@@ -223,7 +223,7 @@ def output_view_info(self, view_wrapper, level=1):
self.out("%srequired permission = %s" % (indent, permission))
predicates = getattr(view_wrapper, '__predicates__', None)
if predicates is not None:
- predicate_text = ', '.join([p.__text__ for p in predicates])
+ predicate_text = ', '.join([p.text() for p in predicates])
self.out("%sview predicates (%s)" % (indent, predicate_text))
def run(self):
View
163 pyramid/tests/test_config/test_predicates.py
@@ -21,13 +21,13 @@ def test___call___false(self):
result = inst(None, request)
self.assertFalse(result)
- def test___text__(self):
+ def test_text(self):
inst = self._makeOne(True)
- self.assertEqual(inst.__text__(), 'xhr = True')
+ self.assertEqual(inst.text(), 'xhr = True')
- def test___phash__(self):
+ def test_phash(self):
inst = self._makeOne(True)
- self.assertEqual(inst.__phash__(), 'xhr:True')
+ self.assertEqual(inst.phash(), 'xhr = True')
class TestRequestMethodPredicate(unittest.TestCase):
def _makeOne(self, val):
@@ -55,14 +55,13 @@ def test___call___false(self):
result = inst(None, request)
self.assertFalse(result)
- def test___text__(self):
+ def test_text(self):
inst = self._makeOne(('HEAD','GET'))
- self.assertEqual(inst.__text__(), 'request method = GET,HEAD')
+ self.assertEqual(inst.text(), 'request_method = GET,HEAD')
- def test___phash__(self):
+ def test_phash(self):
inst = self._makeOne(('HEAD','GET'))
- self.assertEqual(inst.__phash__(), ['request_method:GET',
- 'request_method:HEAD'])
+ self.assertEqual(inst.phash(), 'request_method = GET,HEAD')
class TestPathInfoPredicate(unittest.TestCase):
def _makeOne(self, val):
@@ -87,13 +86,13 @@ def test___call___false(self):
result = inst(None, request)
self.assertFalse(result)
- def test___text__(self):
+ def test_text(self):
inst = self._makeOne('/')
- self.assertEqual(inst.__text__(), 'path_info = /')
+ self.assertEqual(inst.text(), 'path_info = /')
- def test___phash__(self):
+ def test_phash(self):
inst = self._makeOne('/')
- self.assertEqual(inst.__phash__(), 'path_info:/')
+ self.assertEqual(inst.phash(), 'path_info = /')
class TestRequestParamPredicate(unittest.TestCase):
def _makeOne(self, val):
@@ -121,22 +120,144 @@ def test___call___false(self):
result = inst(None, request)
self.assertFalse(result)
- def test___text__exists(self):
+ def test_text_exists(self):
inst = self._makeOne('abc')
- self.assertEqual(inst.__text__(), 'request_param abc')
+ self.assertEqual(inst.text(), 'request_param abc')
- def test___text__withval(self):
+ def test_text_withval(self):
inst = self._makeOne('abc= 1')
- self.assertEqual(inst.__text__(), 'request_param abc = 1')
+ self.assertEqual(inst.text(), 'request_param abc = 1')
- def test___phash__exists(self):
+ def test_phash_exists(self):
inst = self._makeOne('abc')
- self.assertEqual(inst.__phash__(), 'request_param:abc=None')
+ self.assertEqual(inst.phash(), 'request_param abc')
- def test___phash__withval(self):
+ def test_phash_withval(self):
inst = self._makeOne('abc= 1')
- self.assertEqual(inst.__phash__(), "request_param:abc='1'")
+ self.assertEqual(inst.phash(), "request_param abc = 1")
+class TestMatchParamPredicate(unittest.TestCase):
+ def _makeOne(self, val):
+ from pyramid.config.predicates import MatchParamPredicate
+ return MatchParamPredicate(val)
+
+ def test___call___true_single(self):
+ inst = self._makeOne('abc=1')
+ request = Dummy()
+ request.matchdict = {'abc':'1'}
+ result = inst(None, request)
+ self.assertTrue(result)
+
+
+ def test___call___true_multi(self):
+ inst = self._makeOne(('abc=1', 'def=2'))
+ request = Dummy()
+ request.matchdict = {'abc':'1', 'def':'2'}
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___false(self):
+ inst = self._makeOne('abc=1')
+ request = Dummy()
+ request.matchdict = {}
+ result = inst(None, request)
+ self.assertFalse(result)
+
+ def test_text(self):
+ inst = self._makeOne(('def= 1', 'abc =2'))
+ self.assertEqual(inst.text(), 'match_param abc=2,def=1')
+
+ def test_phash(self):
+ inst = self._makeOne(('def= 1', 'abc =2'))
+ self.assertEqual(inst.phash(), 'match_param abc=2,def=1')
+
+class TestCustomPredicate(unittest.TestCase):
+ def _makeOne(self, val):
+ from pyramid.config.predicates import CustomPredicate
+ return CustomPredicate(val)
+
+ def test___call___true(self):
+ def func(context, request):
+ self.assertEqual(context, None)
+ self.assertEqual(request, None)
+ return True
+ inst = self._makeOne(func)
+ result = inst(None, None)
+ self.assertTrue(result)
+
+ def test___call___false(self):
+ def func(context, request):
+ self.assertEqual(context, None)
+ self.assertEqual(request, None)
+ return False
+ inst = self._makeOne(func)
+ result = inst(None, None)
+ self.assertFalse(result)
+
+ def test_text_func_has___text__(self):
+ pred = predicate()
+ pred.__text__ = 'text'
+ inst = self._makeOne(pred)
+ self.assertEqual(inst.text(), 'text')
+
+ def test_text_func_repr(self):
+ pred = predicate()
+ inst = self._makeOne(pred)
+ self.assertTrue(inst.text(), 'predicate')
+
+ def test_phash(self):
+ pred = predicate()
+ inst = self._makeOne(pred)
+ self.assertTrue(inst.phash(), 'custom:1')
+
+class TestTraversePredicate(unittest.TestCase):
+ def _makeOne(self, val):
+ from pyramid.config.predicates import TraversePredicate
+ return TraversePredicate(val)
+
+ def test___call__traverse_has_remainder_already(self):
+ inst = self._makeOne('/1/:a/:b')
+ info = {'traverse':'abc'}
+ request = Dummy()
+ result = inst(info, request)
+ self.assertEqual(result, True)
+ self.assertEqual(info, {'traverse':'abc'})
+
+ def test___call__traverse_matches(self):
+ inst = self._makeOne('/1/:a/:b')
+ info = {'match':{'a':'a', 'b':'b'}}
+ request = Dummy()
+ result = inst(info, request)
+ self.assertEqual(result, True)
+ self.assertEqual(info, {'match':
+ {'a':'a', 'b':'b', 'traverse':('1', 'a', 'b')}})
+
+ def test___call__traverse_matches_with_highorder_chars(self):
+ inst = self._makeOne(text_(b'/La Pe\xc3\xb1a/{x}', 'utf-8'))
+ info = {'match':{'x':text_(b'Qu\xc3\xa9bec', 'utf-8')}}
+ request = Dummy()
+ result = inst(info, request)
+ self.assertEqual(result, True)
+ self.assertEqual(
+ info['match']['traverse'],
+ (text_(b'La Pe\xc3\xb1a', 'utf-8'),
+ text_(b'Qu\xc3\xa9bec', 'utf-8'))
+ )
+
+ def test_text(self):
+ inst = self._makeOne('/abc')
+ self.assertEqual(inst.text(), 'traverse matchdict pseudo-predicate')
+
+ def test_phash(self):
+ inst = self._makeOne('/abc')
+ self.assertEqual(inst.phash(), '')
+
+class predicate(object):
+ def __repr__(self):
+ return 'predicate'
+ def __hash__(self):
+ return 1
+
class Dummy(object):
def __init__(self, **kw):
self.__dict__.update(**kw)
View
16 pyramid/tests/test_config/test_views.py
@@ -400,7 +400,7 @@ def test_add_view_same_phash_overrides_existing_single_view(self):
from pyramid.interfaces import IViewClassifier
from pyramid.interfaces import IMultiView
phash = md5()
- phash.update(b'xhr:True')
+ phash.update(b'xhr = True')
view = lambda *arg: 'NOT OK'
view.__phash__ = phash.hexdigest()
config = self._makeOne(autocommit=True)
@@ -424,7 +424,7 @@ def test_add_view_exc_same_phash_overrides_existing_single_view(self):
from pyramid.interfaces import IExceptionViewClassifier
from pyramid.interfaces import IMultiView
phash = md5()
- phash.update(b'xhr:True')
+ phash.update(b'xhr = True')
view = lambda *arg: 'NOT OK'
view.__phash__ = phash.hexdigest()
config = self._makeOne(autocommit=True)
@@ -2905,7 +2905,7 @@ def test_predicate_mismatch_view_has_no_name(self):
view = lambda *arg: response
def predicate1(context, request):
return False
- predicate1.__text__ = 'text'
+ predicate1.text = lambda *arg: 'text'
deriver = self._makeOne(predicates=[predicate1])
result = deriver(view)
request = self._makeRequest()
@@ -2923,7 +2923,7 @@ def test_predicate_mismatch_view_has_name(self):
def myview(request): pass
def predicate1(context, request):
return False
- predicate1.__text__ = 'text'
+ predicate1.text = lambda *arg: 'text'
deriver = self._makeOne(predicates=[predicate1])
result = deriver(myview)
request = self._makeRequest()
@@ -2941,10 +2941,10 @@ def test_predicate_mismatch_exception_has_text_in_detail(self):
def myview(request): pass
def predicate1(context, request):
return True
- predicate1.__text__ = 'pred1'
+ predicate1.text = lambda *arg: 'pred1'
def predicate2(context, request):
return False
- predicate2.__text__ = 'pred2'
+ predicate2.text = lambda *arg: 'pred2'
deriver = self._makeOne(predicates=[predicate1, predicate2])
result = deriver(myview)
request = self._makeRequest()
@@ -2999,11 +2999,11 @@ def test_with_predicates_notall(self):
def predicate1(context, request):
predicates.append(True)
return True
- predicate1.__text__ = 'text'
+ predicate1.text = lambda *arg: 'text'
def predicate2(context, request):
predicates.append(True)
return False
- predicate2.__text__ = 'text'
+ predicate2.text = lambda *arg: 'text'
deriver = self._makeOne(predicates=[predicate1, predicate2])
result = deriver(view)
request = self._makeRequest()
View
4 pyramid/tests/test_scripts/test_pviews.py
@@ -309,7 +309,7 @@ def test_views_command_single_view_traversal_with_predicates(self):
L = []
command.out = L.append
def predicate(): pass
- predicate.__text__ = "predicate = x"
+ predicate.text = lambda *arg: "predicate = x"
view = dummy.DummyView(context='context', view_name='a')
view.__predicates__ = [predicate]
command._find_view = lambda arg1, arg2: view
@@ -448,7 +448,7 @@ def test_views_command_multiview_with_predicates(self):
L = []
command.out = L.append
def predicate(): pass
- predicate.__text__ = "predicate = x"
+ predicate.text = lambda *arg: "predicate = x"
view = dummy.DummyView(context='context')
view.__name__ = 'view'
view.__view_attr__ = 'call'
Please sign in to comment.
Something went wrong with that request. Please try again.