[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/tests - __init__.py:1.1 testConverter.py:1.1

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


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

Added Files:
	__init__.py 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/App/Forms/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 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.
# 
##############################################################################


=== Added File Zope3/lib/python/Zope/App/Forms/tests/testConverter.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.
#
##############################################################################
"""Test conversion functionality of schema.

$Id: testConverter.py,v 1.1 2002/09/07 16:18:48 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.App.Forms.Exceptions import ConversionError
from Zope.App.Forms.Converter import NullConverter, StrToIntConverter,\
     IntToStrConverter, CombinedConverter, StrToFloatConverter,\
     FloatToStrConverter

class ConverterTest(TestCase):
    def test_NullConverter(self):
        null_converter = NullConverter()
        self.assertEquals(1, null_converter.convert(1))
        self.assertEquals('foo', null_converter.convert('foo'))

    def test_StrToIntConverter(self):
        str_to_int_converter = StrToIntConverter()
        self.assertEquals(1, str_to_int_converter.convert('1'))
        self.assertEquals(100, str_to_int_converter.convert('100'))
        self.assertRaises(ConversionError, str_to_int_converter.convert, 'foo')

    def test_IntToStrConverter(self):
        int_to_str_converter = IntToStrConverter()
        self.assertEquals('1', int_to_str_converter.convert(1))

    def test_StrToFloatConverter(self):
        str_to_float_converter = StrToFloatConverter()
        self.assertEquals(1., str_to_float_converter.convert('1.0'))
        self.assertEquals(1., str_to_float_converter.convert('1'))
        self.assertRaises(ConversionError, str_to_float_converter.convert,
                          'foo')

    def test_FloatToStrConverter(self):
        float_to_str_converter = FloatToStrConverter()
        self.assertEquals('1.0', float_to_str_converter.convert(1.0))
        
    def test_CombinedConverter(self):
        combined_converter = CombinedConverter([StrToIntConverter(),
                                                IntToStrConverter()])
        self.assertEquals('1', combined_converter.convert('1'))
        self.assertRaises(ConversionError, combined_converter.convert, 'foo')
        
def test_suite():
    return makeSuite(ConverterTest)

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