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

Martijn Faassen m.faassen@vet.uu.nl
Wed, 11 Dec 2002 11:11:15 -0500


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

Added Files:
	__init__.py test_typereg.py 
Log Message:
Added code to generate schema python code from schema. Not done yet,
but this basic framework passes the tests.


=== Added File Zope3/lib/python/Zope/App/schemagen/tests/__init__.py ===
# this is empty



=== Added File Zope3/lib/python/Zope/App/schemagen/tests/test_typereg.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.
#
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: test_typereg.py,v 1.1 2002/12/11 16:11:14 faassen Exp $
"""

from unittest import TestCase, makeSuite, TestSuite
import datetime

from Zope.App.schemagen.typereg import TypeRepresentationRegistry,\
     DefaultTypeRepresentation, DatetimeRepresentation,\
     DefaultFieldRepresentation

class DefaultTypeRepresentationTests(TestCase):
    def test_getTypes(self):
        c = DefaultTypeRepresentation
        self.assertEquals((), tuple(c.getTypes()))

    def h(self, obj):
        r = DefaultTypeRepresentation(obj)
        self.assertEquals(eval(r.text), obj)
    
    def h_builtin(self, obj):
        r = DefaultTypeRepresentation(obj)
        self.assertEquals(eval(r.text), obj)
        self.assertEquals([], r.importList)

    builtin_instances = [
        'foo',
        1,
        1.1,
        (),
        (1, "foo"),
        [],
        [5, 2],
        {},
        {'foo':'bar', 'baz':'hoi'},
        {'foo': (1, "foo")}
        ]
    
    def test_builtins(self):
        for builtin_instance in self.builtin_instances:
            self.h_builtin(builtin_instance)

    def test_recursive(self):
        # we cannot cope with recursive structures
        recursive = [1, 2]
        recursive.append(recursive)
        r = DefaultTypeRepresentation(recursive)
        self.assertRaises(SyntaxError, eval, r.text)

class DatetimeRepresentationTests(TestCase):

    datetime_instances = [
        (datetime.date(2002, 10, 30),
         [('datetime', 'date')]),
        (datetime.datetime(2002, 10, 30, 11, 44, 10),
         [('datetime', 'datetime')]),
        (datetime.time(10, 0, 1),
         [('datetime', 'time')]),
        ]
    
    def test_date(self):
        for datetime_instance, import_list in self.datetime_instances:
            r = DatetimeRepresentation(datetime_instance)
            self.assertEquals(datetime_instance, evalRepresentation(r))
            r_imports = r.importList
            r_imports.sort()
            import_list.sort()
            self.assertEquals(r_imports, import_list)

def evalRepresentation(r):
    import_dict = {}
    for import_name, import_from in r.importList:
        module = __import__(import_name, {}, {}, [import_from])
        import_dict[import_from] = getattr(module, import_from)
    return eval(r.text, import_dict)

class TypeRepresentationRegistryTests(TestCase):
    def setUp(self):
        self.tr = TypeRepresentationRegistry(DefaultTypeRepresentation)

    def test_default(self):
        self.assert_(isinstance(
            self.tr.represent(1), DefaultTypeRepresentation))
        
    def test_register(self):
        from Zope.App.schemagen.interfaces import ITypeRepresentation
        class IntRepresentation:
            __implements__ = ITypeRepresentation
            def __init__(self, obj):
                pass
            def getTypes():
                return (int,)

            getTypes = staticmethod(getTypes)

            importList = []
            text = ''
            
        self.tr.register(IntRepresentation)
        self.assert_(isinstance(self.tr.represent(1), IntRepresentation))

        self.assert_(isinstance(self.tr.represent('foo'),
                                DefaultTypeRepresentation))


from Zope import Schema


class DefaultFieldRepresentationTests(TestCase):
    # XXX there is an issue with field classes which have the same name
    # multiple 'from x import y' statements will cause one name to be
    # shadowed by another. We can't test for this yet.

    schema_fields = [
        (Schema.Text(title=u"text"),
         [('Zope.Schema', 'Text')]),
        (Schema.Int(title=u"int"),
         [('Zope.Schema', 'Int')]),
        (Schema.TextLine(title=u"text"),
         [('Zope.Schema', 'TextLine')]),
        (Schema.Float(title=u"float"),
         [('Zope.Schema', 'Float')])
        ]
    
    def test_field(self):
        for field, import_list in self.schema_fields:
            r = DefaultFieldRepresentation(field)
            self.assertEquals(field, evalRepresentation(r))
            r_imports = r.importList
            r_imports.sort()
            import_list.sort()
            self.assertEquals(r_imports, import_list)

    
def test_suite():
    return TestSuite(
        (makeSuite(DefaultTypeRepresentationTests),
         makeSuite(DatetimeRepresentationTests),
         makeSuite(TypeRepresentationRegistryTests),
         makeSuite(DefaultFieldRepresentationTests)))