[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/schemagen/tests - setstate.py.gen:1.1 setstatemodule.py.gen:1.1 setstatemodule_no_history.py.gen:1.1 test_schemaspec.py:1.2 test_typereg.py:1.3

Martijn Faassen m.faassen@vet.uu.nl
Thu, 12 Dec 2002 13:28:04 -0500


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

Modified Files:
	test_schemaspec.py test_typereg.py 
Added Files:
	setstate.py.gen setstatemodule.py.gen 
	setstatemodule_no_history.py.gen 
Log Message:
Added some tests for various forms of source code generations. Also
made generateModuleSource on schemaspec.SchemaSpec not generate
__setstate__ method to upgrade history when this isn't necessary.


=== Added File Zope3/lib/python/Zope/App/schemagen/tests/setstate.py.gen ===
    def __setstate__(self, state):
        transformations = schemaspec.prepareSetstate(self, state, 6)
        if transformations is None:
            return
        if 0 in transformations:
            schemaspec.AddField.update(dict, state, 'alpha')
        if 1 in transformations:
            schemaspec.RemoveField.update(dict, state, 'alpha')
        if 2 in transformations:
            schemaspec.AddField.update(dict, state, 'beta')
        if 3 in transformations:
            schemaspec.InsertField.update(dict, state, 'gamma', 0)
        if 4 in transformations:
            schemaspec.MoveField.update(dict, state, 'gamma', 2)
        if 5 in transformations:
            schemaspec.RenameField.update(dict, state, 'gamma', 'gamma2')


=== Added File Zope3/lib/python/Zope/App/schemagen/tests/setstatemodule.py.gen ===
from Interface import Interface
from Persistence import Persistent
from Zope.Schema.FieldProperty import FieldProperty

# field imports
from Zope.Schema import Text
from Zope.App.schemagen import schemaspec

class IFoo(Interface):
    """Autogenerated schema."""
    alpha = Text(title=u'alpha')
    
class FooClass(Persistent):
    """Autogenerated class for IFoo."""
    __implements__ = IFoo

    def __init__(self):
        self.__schema_version__ = 1
        
    alpha = FieldProperty(IFoo['alpha'])

    def __setstate__(self, state):
        transformations = schemaspec.prepareSetstate(self, state, 1)
        if transformations is None:
            return
        if 0 in transformations:
            schemaspec.AddField.update(dict, state, 'alpha')



=== Added File Zope3/lib/python/Zope/App/schemagen/tests/setstatemodule_no_history.py.gen ===
from Interface import Interface
from Persistence import Persistent
from Zope.Schema.FieldProperty import FieldProperty

# field imports


class IFoo(Interface):
    """Autogenerated schema."""

    
class FooClass(Persistent):
    """Autogenerated class for IFoo."""
    __implements__ = IFoo

    def __init__(self):
        self.__schema_version__ = 0
        




=== Zope3/lib/python/Zope/App/schemagen/tests/test_schemaspec.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/test_schemaspec.py:1.1	Thu Dec 12 12:43:16 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/test_schemaspec.py	Thu Dec 12 13:28:03 2002
@@ -18,6 +18,10 @@
 from unittest import TestCase, makeSuite, TestSuite
 from Zope.App.schemagen.schemaspec import SchemaSpec
 from Zope.Schema import Text
+import os
+
+def openInTests(name, mode):
+    return open(os.path.join(os.path.dirname(__file__), name), mode)
 
 class SchemaSpecTests(TestCase):
 
@@ -148,6 +152,9 @@
         self.assertRaises(IndexError, s.moveField,
                           'beta', -1)
 
+    # XXX the following tests compare python source text
+    # this is very dependent on whitespace issues, which we really
+    # don't care about. Is there a better way? (compare some form of AST?)
     def test_history(self):
         s = self.s
         alpha = Text(title=u'Alpha')
@@ -178,25 +185,28 @@
         # now compare history
         self.assertEquals(history, s.getHistory())
 
-        source = """\
-    def __setstate__(self, state):
-        transformations = schemaspec.prepareSetstate(self, state, 6)
-        if transformations is None:
-            return
-        if 0 in transformations:
-            schemaspec.AddField.update(dict, state, 'alpha')
-        if 1 in transformations:
-            schemaspec.RemoveField.update(dict, state, 'alpha')
-        if 2 in transformations:
-            schemaspec.AddField.update(dict, state, 'beta')
-        if 3 in transformations:
-            schemaspec.InsertField.update(dict, state, 'gamma', 0)
-        if 4 in transformations:
-            schemaspec.MoveField.update(dict, state, 'gamma', 2)
-        if 5 in transformations:
-            schemaspec.RenameField.update(dict, state, 'gamma', 'gamma2')
-"""
+        # check whether generated source is as we expect
+        f = openInTests('setstate.py.gen', 'r')
+        source = f.read()
+        f.close()
         self.assertEquals(source, s.generateSetstateSource())
+
+    def test_generateModuleSource(self):
+        s = self.s
+        s.addField('alpha', self.alpha)
+        
+        f = openInTests('setstatemodule.py.gen', 'r')
+        source = f.read()
+        f.close()
+        self.assertEquals(source, s.generateModuleSource())
+
+    def test_generateModuleSource(self):
+        s = self.s
+        # no history, so expect no setstate
+        f = openInTests('setstatemodule_no_history.py.gen', 'r')
+        source = f.read()
+        f.close()
+        self.assertEquals(source.strip(), s.generateModuleSource().strip())
 
 def test_suite():
     return TestSuite(


=== Zope3/lib/python/Zope/App/schemagen/tests/test_typereg.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/test_typereg.py:1.2	Thu Dec 12 05:45:53 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/test_typereg.py	Thu Dec 12 13:28:03 2002
@@ -11,10 +11,7 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""XXX short summary goes here.
-
-XXX longer description goes here.
-
+"""
 $Id$
 """