Back to index

plone3  3.1.7
test_validation.py
Go to the documentation of this file.
00001 
00002 from Testing import ZopeTestCase
00003 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00004 from Testing.ZopeTestCase import doctest
00005 
00006 from Products.validation import validation
00007 
00008 class TestValidation(ATSiteTestCase):
00009 
00010     def test_inNumericRange(self):
00011         v = validation.validatorFor('inNumericRange')
00012         self.failUnlessEqual(v(10, 1, 20), 1)
00013         self.failUnlessEqual(v('10', 1, 20), 1)
00014         self.failIfEqual(v(0, 4, 5), 1)
00015 
00016     def test_isPrintable(self):
00017         v = validation.validatorFor('isPrintable')
00018         self.failUnlessEqual(v('text'), 1)
00019         self.failIfEqual(v('\u203'), 1)
00020         self.failIfEqual(v(10), 1)
00021 
00022     def test_isSSN(self):
00023         v = validation.validatorFor('isSSN')
00024         self.failUnlessEqual(v('111223333'), 1)
00025         self.failUnlessEqual(v('111-22-3333', ignore=r'-'), 1)
00026 
00027     def test_isUSPhoneNumber(self):
00028         v = validation.validatorFor('isUSPhoneNumber')
00029         self.failUnlessEqual(v('(212) 555-1212',
00030                                ignore=r'[\s\(\)\-]'), 1)
00031         self.failUnlessEqual(v('2125551212',
00032                                ignore=r'[\s\(\)\-]'), 1)
00033 
00034         self.failUnlessEqual(v('(212) 555-1212'), 1)
00035 
00036     def test_isURL(self):
00037         v = validation.validatorFor('isURL')
00038         self.failUnlessEqual(v('http://foo.bar:8080/manage'), 1)
00039         self.failUnlessEqual(v('https://foo.bar:8080/manage'), 1)
00040         self.failUnlessEqual(v('irc://tiran@irc.freenode.net:6667/#plone'), 1)
00041         self.failUnlessEqual(v('fish://tiran:password@myserver/~/'), 1)
00042         self.failIfEqual(v('http://\n'), 1)
00043         self.failIfEqual(v('../foo/bar'), 1)
00044 
00045     def test_isEmail(self):
00046         v = validation.validatorFor('isEmail')
00047         self.failUnlessEqual(v('test@test.com'), 1)
00048         self.failIfEqual(v('@foo.bar'), 1)
00049         self.failIfEqual(v('me'), 1)
00050 
00051     def test_isMailto(self):
00052         v = validation.validatorFor('isMailto')
00053         self.failUnlessEqual(v('mailto:test@test.com'), 1)
00054         self.failIfEqual(v('test@test.com'), 1)
00055         self.failIfEqual(v('mailto:@foo.bar'), 1)
00056         self.failIfEqual(v('@foo.bar'), 1)
00057         self.failIfEqual(v('mailto:'), 1)
00058         self.failIfEqual(v('me'), 1)
00059 
00060     def test_isUnixLikeName(self):
00061         v = validation.validatorFor('isUnixLikeName')
00062         self.failUnlessEqual(v('abcd'), 1)
00063         self.failUnless(v('a_123456'), 1)
00064         self.failIfEqual(v('123'), 1)
00065         self.failIfEqual(v('ab.c'), 1)
00066         self.failIfEqual(v('ab,c'), 1)
00067         self.failIfEqual(v('aaaaaaaab'), 1) # too long
00068         
00069     def test_isValidId(self):
00070         v = validation.validatorFor("isValidId")
00071         self.failIfEqual(v("a b", object()), 1)
00072         # TODO: more tests require a site
00073 
00074         
00075 def test_suite():
00076     from unittest import TestSuite, makeSuite
00077     suite = TestSuite()
00078     suite.addTest(makeSuite(TestValidation))
00079     
00080     doctests = (
00081         'Products.validation.validators.ExpressionValidator',
00082         )
00083     for module in doctests:
00084         suite.addTest(doctest.DocTestSuite(module))
00085     
00086     return suite