Back to index

plone3  3.1.7
test_validation.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 
00026 from Products.Archetypes.tests.attestcase import ATTestCase
00027 
00028 from Products.validation import validation as validationService
00029 
00030 class TestValidation(ATTestCase):
00031     def test_inNumericRange(self):
00032         v = validationService.validatorFor('inNumericRange')
00033         self.failUnlessEqual(v(10, 1, 20), 1)
00034         self.failUnlessEqual(v('10', 1, 20), 1)
00035         self.failIfEqual(v(0, 4, 5), 1)
00036 
00037     def test_isPrintable(self):
00038         v = validationService.validatorFor('isPrintable')
00039         self.failUnlessEqual(v('text'), 1)
00040         self.failIfEqual(v('\u203'), 1)
00041         self.failIfEqual(v(10), 1)
00042 
00043     def test_isSSN(self):
00044         v = validationService.validatorFor('isSSN')
00045         self.failUnlessEqual(v('111223333'), 1)
00046         self.failUnlessEqual(v('111-22-3333', ignore=r'-'), 1)
00047 
00048     def test_isUSPhoneNumber(self):
00049         v = validationService.validatorFor('isUSPhoneNumber')
00050         self.failUnlessEqual(v('(212) 555-1212',
00051                                ignore=r'[\s\(\)\-]'), 1)
00052         self.failUnlessEqual(v('2125551212',
00053                                ignore=r'[\s\(\)\-]'), 1)
00054 
00055         self.failUnlessEqual(v('(212) 555-1212'), 1)
00056 
00057 
00058     def test_isURL(self):
00059         v = validationService.validatorFor('isURL')
00060         self.failUnlessEqual(v('http://foo.bar:8080/manage'), 1)
00061         self.failIfEqual(v('http://\n'), 1)
00062         self.failIfEqual(v('../foo/bar'), 1)
00063 
00064 
00065     def test_isEmail(self):
00066         v = validationService.validatorFor('isEmail')
00067         self.failUnlessEqual(v('test@test.com'), 1)
00068         self.failIfEqual(v('@foo.bar'), 1)
00069         self.failIfEqual(v('me'), 1)
00070 
00071     def test_isUnixLikeName(self):
00072         v = validationService.validatorFor('isUnixLikeName')
00073         self.failUnlessEqual(v('abcd'), 1)
00074         self.failUnlessEqual(v('a_123456'), 1)
00075         self.failIfEqual(v('123'), 1)
00076         self.failIfEqual(v('ab.c'), 1)
00077         self.failIfEqual(v('ab,c'),1 )
00078         self.failIfEqual(v('aaaaaaaab'), 1) # too long
00079 
00080 
00081 def test_suite():
00082     from unittest import TestSuite, makeSuite
00083     suite = TestSuite()
00084     suite.addTest(makeSuite(TestValidation))
00085     return suite