[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/schemagen/tests - test_modulegen.py:1.3 test_typereg.py:1.2

Martijn Faassen m.faassen@vet.uu.nl
Thu, 12 Dec 2002 05:45:54 -0500


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

Modified Files:
	test_modulegen.py test_typereg.py 
Log Message:
* Represent field constructor arguments in the order defined by their
  schema.

* Test full round trip.
  fields -> source -> schema -> fields -> source
  works now.


=== Zope3/lib/python/Zope/App/schemagen/tests/test_modulegen.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/test_modulegen.py:1.2	Thu Dec 12 04:17:46 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/test_modulegen.py	Thu Dec 12 05:45:53 2002
@@ -20,7 +20,7 @@
 
 from unittest import TestCase, makeSuite, TestSuite
 from Interface import Interface
-from Zope.Schema import Text, Int, Float, getFields
+from Zope.Schema import Field, Text, Int, Float, getFieldsInOrder
 
 from Zope.App.schemagen.modulegen import generateModuleSource
     
@@ -30,6 +30,7 @@
 
     def setUp(self):
         source = generateModuleSource('IFoo', self.fields, "Foo")
+        self.source = source
         g = {}
         exec source in g
         del g['__builtins__'] # to ease inspection during debugging
@@ -37,17 +38,16 @@
         
     def test_schema(self):
         IFoo = self.g['IFoo']
+        self.assertEquals(self.fields, getFieldsInOrder(IFoo))
 
-        fieldsorter = lambda x, y: cmp(x[1].order, y[1].order)
-        new_fields = getFields(IFoo).items()
-        new_fields.sort(fieldsorter)
-        self.assertEquals(self.fields, new_fields)
-
-    # XXX we'd like to test the whole roundtrip eventually,
-    # by execing generated module source again and then generating
-    # module source for the schema in that. This requires the arguments
-    # to fields (their properties) to be in their own schema order.
-
+    def test_roundtrip(self):
+        IFoo = self.g['IFoo']
+        # not dealing with issues of schema inheritance,
+        # so simply get all fields
+        fields = getFieldsInOrder(IFoo) 
+        new_source = generateModuleSource('IFoo', fields, 'Foo')
+        self.assertEquals(self.source, new_source)
+    
     def test_class(self):
         from Zope.Schema.FieldProperty import FieldProperty
         IFoo = self.g['IFoo']
@@ -74,10 +74,9 @@
               ('hoi', Float(title=u"Float")),
               ('dag', Int(title=u"Dag", default=42)),]
 
- 
 def test_suite():
     return TestSuite(
         (makeSuite(GenerateModuleSourceTestsEmpty),
-         makeSuite(GenerateModuleSourceTests1)
+         makeSuite(GenerateModuleSourceTests1),
          ))
 


=== Zope3/lib/python/Zope/App/schemagen/tests/test_typereg.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/test_typereg.py:1.1	Wed Dec 11 11:11:14 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/test_typereg.py	Thu Dec 12 05:45:53 2002
@@ -25,6 +25,11 @@
      DefaultTypeRepresentation, DatetimeRepresentation,\
      DefaultFieldRepresentation
 
+from Zope import Schema
+
+from Zope.Schema.IField import IField
+from Zope.Schema import Field, Text, Int
+
 class DefaultTypeRepresentationTests(TestCase):
     def test_getTypes(self):
         c = DefaultTypeRepresentation
@@ -118,10 +123,24 @@
         self.assert_(isinstance(self.tr.represent('foo'),
                                 DefaultTypeRepresentation))
 
-
-from Zope import Schema
-
-
+class IFieldSchema(IField):
+    # the greek alphabet is not in alphabetical order, so we cannot
+    # depend on ascii sort order, which is good as we shouldn't.
+    alpha = Text(title=u"Alpha", default=u"")
+    beta = Int(title=u"Beta", default=0)
+    gamma = Text(title=u"Gamma", default=u"")
+    delta = Int(title=u"Delta", default=0)
+
+class MyField(Field):
+    __implements__ = IFieldSchema
+
+    def __init__(self, alpha=u'', beta=0, gamma=u'', delta=0, **kw):
+        super(MyField, self).__init__(**kw)
+        self.alpha = alpha
+        self.beta = beta
+        self.gamma = gamma
+        self.delta = delta
+        
 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
@@ -147,10 +166,22 @@
             import_list.sort()
             self.assertEquals(r_imports, import_list)
 
+    def test_order(self):
+        field = MyField(alpha=u'alpha', gamma=u'gamma', delta=23)
+        r = DefaultFieldRepresentation(field)
+        t = r.text
+        a = t.find('alpha')
+        g = t.find('gamma')
+        d = t.find('delta')
+        self.assertNotEquals(a, -1)
+        self.assertNotEquals(g, -1)
+        self.assertNotEquals(d, -1)
+        self.assert_(a < g < d)
     
 def test_suite():
     return TestSuite(
         (makeSuite(DefaultTypeRepresentationTests),
          makeSuite(DatetimeRepresentationTests),
          makeSuite(TypeRepresentationRegistryTests),
-         makeSuite(DefaultFieldRepresentationTests)))
+         makeSuite(DefaultFieldRepresentationTests),
+        ))