[Zope3-checkins] CVS: Zope3/lib/python/Zope/Schema/tests - testFieldProperty.py:1.1 testSchema.py:1.2 testStrField.py:1.2 testConverter.py:NONE

Jim Fulton jim@zope.com
Sat, 7 Sep 2002 12:18:52 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Schema/tests
In directory cvs.zope.org:/tmp/cvs-serv19433/lib/python/Zope/Schema/tests

Modified Files:
	testSchema.py testStrField.py 
Added Files:
	testFieldProperty.py 
Removed Files:
	testConverter.py 
Log Message:
More cleanup/refactoring of Schemas and forms. There's more to come,
but I'm checkpointing here.

I:

- Added schema field properties. These are like standard Python
  properies except that they are derived from Schema fields.

- Decomposed Str fields into Bytes fields and Text fields.
  Bytes fields contain 8-bit data and are stored as python strings.
  Text fields contain written human discourse, and are stored as
  unicode. It is invalid to store Python strings in Text fields or
  unicode in Bytes fields.

- Moved converters from schemas to forms, where they are used.

- Widgets are now responsible for:

  - Getting raw data from the request

  - Converting raw data to application data

  - Validating converted data against schema fields

- Began defining an error framework for errors in forms.

- Simplified FormViews to reflect new widget responsibilities.

- Added Bytes, Int and Float widgets and changed some application and
  test code to use them.




=== Added File Zope3/lib/python/Zope/Schema/tests/testFieldProperty.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: testFieldProperty.py,v 1.1 2002/09/07 16:18:51 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite

from Interface import Interface
from Zope.Schema import Float, Text, Bytes
from Zope.Schema.FieldProperty import FieldProperty
from Zope.Schema.Exceptions import ValidationError

class I(Interface):

    title = Text(description="Short summary", default=u'say something')
    weight = Float(min=0.0)
    code = Bytes(min_length=6, max_length=6, default='xxxxxx')

class C(object):

    title = FieldProperty(I['title'])
    weight = FieldProperty(I['weight'])
    code = FieldProperty(I['code'])

class Test(TestCase):

    def test(self):
        c = C()
        self.assertEqual(c.title, u'say something')
        self.assertEqual(c.weight, None)
        self.assertEqual(c.code, 'xxxxxx')
        self.assertRaises(ValidationError, setattr, c, 'title', 'foo')
        self.assertRaises(ValidationError, setattr, c, 'weight', 'foo')
        self.assertRaises(ValidationError, setattr, c, 'weight', -1)
        self.assertRaises(ValidationError, setattr, c, 'code', -1)
        self.assertRaises(ValidationError, setattr, c, 'code', 'xxxx')
        self.assertRaises(ValidationError, setattr, c, 'code', u'xxxxxx')

        c.title = u'c is good'
        c.weight = 10
        c.code = 'abcdef'

        self.assertEqual(c.title, u'c is good')
        self.assertEqual(c.weight, 10)
        self.assertEqual(c.code, 'abcdef')
        
        

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Zope3/lib/python/Zope/Schema/tests/testSchema.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/tests/testSchema.py:1.1	Thu Sep  5 14:55:04 2002
+++ Zope3/lib/python/Zope/Schema/tests/testSchema.py	Sat Sep  7 12:18:51 2002
@@ -18,23 +18,23 @@
 from Zope.Schema.Exceptions import StopValidation, ValidationError, \
      ValidationErrorsAll
 from Interface import Interface
-from Zope.Schema import Str, ErrorNames
+from Zope.Schema import Bytes, ErrorNames
 from Zope.Schema import validateMapping, validateMappingAll, getFields
 
 class ISchemaTest(Interface):
-    title = Str(
+    title = Bytes(
         title="Title",
         description="Title",
         default="",
         required=1)
     
-    description = Str(
+    description = Bytes(
         title="Description",
         description="Description",
         default="",
         required=1)
 
-    spam = Str(
+    spam = Bytes(
         title="Spam",
         description="Spam",
         default="",


=== Zope3/lib/python/Zope/Schema/tests/testStrField.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/Schema/tests/testStrField.py:1.1	Thu Sep  5 14:55:04 2002
+++ Zope3/lib/python/Zope/Schema/tests/testStrField.py	Sat Sep  7 12:18:51 2002
@@ -15,81 +15,105 @@
 $Id$
 """
 from unittest import TestSuite, main, makeSuite
-from Zope.Schema import Str, ErrorNames
+from Zope.Schema import Bytes, Text, ErrorNames
+from Zope.Schema.Exceptions import ValidationError 
 from testField import FieldTest
 
 class StrTest(FieldTest):
     """Test the Str Field."""
 
     def testValidate(self):
-        field = Str(id='field', title='Str field', description='',
+        field = self._Str(id='field', title='Str field', description='',
                        readonly=0, required=0)
         field.validate(None)
-        field.validate('foo')
-        field.validate('')
+        field.validate(self._convert('foo'))
+        field.validate(self._convert(''))
     
     def testValidateRequired(self):
-        field = Str(id='field', title='Str field', description='',
+        field = self._Str(id='field', title='Str field', description='',
                        readonly=0, required=1)
-        field.validate('foo')
+        field.validate(self._convert('foo'))
 
         self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
                                     field.validate, None)
         self.assertRaisesErrorNames(ErrorNames.RequiredEmptyStr,
-                                    field.validate, '')
+                                    field.validate, self._convert(''))
 
     def testAllowedValues(self):
-        field = Str(id="field", title='Str field', description='',
+        field = self._Str(id="field", title='Str field', description='',
                         readonly=0, required=0, allowed_values=('foo', 'bar'))
         field.validate(None)
-        field.validate('foo')
+        field.validate(self._convert('foo'))
 
         self.assertRaisesErrorNames(ErrorNames.InvalidValue,
-                                    field.validate, 'blah')
+                                    field.validate, self._convert('blah'))
 
     def testValidateMinLength(self):
-        field = Str(id='field', title='Str field', description='',
+        field = self._Str(id='field', title='Str field', description='',
                        readonly=0, required=0, min_length=3)
         field.validate(None)
-        field.validate('333')
-        field.validate('55555')
+        field.validate(self._convert('333'))
+        field.validate(self._convert('55555'))
 
-        self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '')
-        self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
-        self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '1')
+        self.assertRaisesErrorNames(ErrorNames.TooShort,
+                                    field.validate, self._convert(''))
+        self.assertRaisesErrorNames(ErrorNames.TooShort,
+                                    field.validate, self._convert('22'))
+        self.assertRaisesErrorNames(ErrorNames.TooShort,
+                                    field.validate, self._convert('1'))
 
     def testValidateMaxLength(self):
-        field = Str(id='field', title='Str field', description='',
+        field = self._Str(id='field', title='Str field', description='',
                        readonly=0, required=0, max_length=5)
         field.validate(None)
-        field.validate('')
-        field.validate('333')
-        field.validate('55555')
+        field.validate(self._convert(''))
+        field.validate(self._convert('333'))
+        field.validate(self._convert('55555'))
 
         self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
-                                    '666666')
+                                    self._convert('666666'))
         self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
-                                    '999999999')
+                                    self._convert('999999999'))
 
     def testValidateMinLengthAndMaxLength(self):
-        field = Str(id='field', title='Str field', description='',
+        field = self._Str(id='field', title='Str field', description='',
                        readonly=0, required=0, min_length=3, max_length=5)
 
         field.validate(None)
-        field.validate('333')
-        field.validate('4444')
-        field.validate('55555')
+        field.validate(self._convert('333'))
+        field.validate(self._convert('4444'))
+        field.validate(self._convert('55555'))
         
-        self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
-        self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
+        self.assertRaisesErrorNames(ErrorNames.TooShort,
+                                    field.validate, self._convert('22'))
+        self.assertRaisesErrorNames(ErrorNames.TooShort,
+                                    field.validate, self._convert('22'))
         self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
-                                    '666666')
+                                    self._convert('666666'))
         self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
-                                    '999999999')
+                                    self._convert('999999999'))
+
+class BytesTest(StrTest):
+    _Str = Bytes
+    _convert = str
+
+    def testBadStringType(self):
+        field = Bytes()
+        self.assertRaises(ValidationError, field.validate, u'hello')
+        
+
+class TextTest(StrTest):
+    _Str = Text
+    def _convert(self, v):
+        return unicode(v, 'ascii')
+
+    def testBadStringType(self):
+        field = Text()
+        self.assertRaises(ValidationError, field.validate, 'hello')
 
 
 def test_suite():
-    return makeSuite(StrTest)
+    return TestSuite((makeSuite(BytesTest), makeSuite(TextTest)))
 
 if __name__ == '__main__':
     main(defaultTest='test_suite')

=== Removed File Zope3/lib/python/Zope/Schema/tests/testConverter.py ===