--- a/iro/validate.py Tue Feb 28 01:18:11 2012 +0100
+++ b/iro/validate.py Tue Feb 28 01:18:52 2012 +0100
@@ -133,22 +133,27 @@
def validate(kwd,func, need=True,*args,**kargs):
'''validate decorator
- use it like this:
- @validate(kwd=userhash, func=vuserhash)
- f(userhash)
- that will validate usrhash with the function vuserhash.
- Every validate function should raise an Exception, if the the value is not valid'''
+use it like this:
+ @validate(kwd=userhash, func=vuserhash)
+ f(userhash)
+that will validate usrhash with the function vuserhash.
+Every validate function should raise an Exception, if the the value is not valid.
+All args and kargs are used to call the validate function.
+if need is True, the kwd can't be None.'''
@decorator
def v(f,*a,**k):
kp=getcallargs(f,*a,**k)
def dfunc(*x,**y):
return None
try:
- if need or kp[kwd] is not None:
+ if kp[kwd] is not None:
dfunc=func
+ elif need:
+ raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
except KeyError:
if need:
raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
+ kp[kwd] = None
def _gotResult(value):
kp[kwd] = value
--- a/tests/validate.py Tue Feb 28 01:18:11 2012 +0100
+++ b/tests/validate.py Tue Feb 28 01:18:52 2012 +0100
@@ -1,6 +1,7 @@
from twisted.trial import unittest
+from mock import Mock
-from iro.validate import vBool, vInteger, vHash
+from iro.validate import vBool, vInteger, vHash, validate
from iro.error import ValidateException
class testValidators(unittest.TestCase):
@@ -60,3 +61,75 @@
self.assertEqual(vHash("Fa",None,maxlength=2),"fa")
self.assertRaises(ValidateException, vHash, "123", None, maxlength=1)
+
+
+ def testValidate(self):
+ f = Mock()
+ f.return_value = "valid"
+ @validate("t",f,True,1,2,3,4,k=5)
+ def g(u=False, t="bla"):
+ return t
+ d = g(t="uhuhu")
+ def r(t):
+ f.called_once_with("uhuhu","t",1,2,3,4,k=5)
+ self.assertEqual(t,"valid")
+ d.addCallback(r)
+ return d
+
+ def testValidateMissingNeed(self):
+ f = Mock()
+ @validate("t",f,True,1,2,3,4,k=5)
+ def g(u, t="buuh"):
+ return t
+ e = self.assertRaises(ValidateException, g, u="uhuhu", t = None)
+ self.assertEqual(str(e),'700:t is nessasary')
+
+ def testValidateMissingNeedNonExplicit(self):
+ f = Mock()
+ @validate("t",f,True,1,2,3,4,k=5)
+ def g(u, **k):
+ return k["t"]
+ e = self.assertRaises(ValidateException, g, u="uhuhu")
+ self.assertEqual(str(e),'700:t is nessasary')
+
+
+ def testValidateMissingNeed2(self):
+ f = Mock()
+ f.return_value = "valid"
+ @validate("t",f,True,1,2,3,4,k=5)
+ def g(u, t="buuh"):
+ return t
+
+ d = g(True)
+
+ def r(t):
+ f.called_once_with("buuh","t",1,2,3,4,k=5)
+ self.assertEqual(t,"valid")
+ d.addCallback(r)
+ return d
+
+ def testvalidateNoNeed(self):
+ f = Mock()
+ f.return_value = "valid"
+ @validate("t",f,False,1,2,3,4,k=5)
+ def g(u, t="buuh"):
+ return t
+ d = g("uhu")
+ def r(t):
+ self.assertEqual(f.called,True)
+ self.assertEqual(t,"valid")
+ d.addCallback(r)
+ return d
+
+ def testvalidateNoNeed2(self):
+ f = Mock()
+ f.return_value = "valid"
+ @validate("t",f,False,1,2,3,4,k=5)
+ def g(u, **k):
+ return k["t"]
+ d = g("uhu")
+ def r(t):
+ self.assertEqual(f.called,False)
+ self.assertEqual(t,None)
+ d.addCallback(r)
+ return d