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

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


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

Modified Files:
	setstate.py.gen setstatemodule.py.gen test_schemaspec.py 
Log Message:
Added test that exercises generated __setstate__ in simple way; still
need more extensive tests. Fixed various bugs along the way. Also
implemented a test for prepareSetstate().


=== Zope3/lib/python/Zope/App/schemagen/tests/setstate.py.gen 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/setstate.py.gen:1.1	Thu Dec 12 13:28:03 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/setstate.py.gen	Thu Dec 12 13:54:20 2002
@@ -2,6 +2,7 @@
         transformations = schemaspec.prepareSetstate(self, state, 6)
         if transformations is None:
             return
+        dict = self.__dict__
         if 0 in transformations:
             schemaspec.AddField.update(dict, state, 'alpha')
         if 1 in transformations:


=== Zope3/lib/python/Zope/App/schemagen/tests/setstatemodule.py.gen 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/setstatemodule.py.gen:1.1	Thu Dec 12 13:28:03 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/setstatemodule.py.gen	Thu Dec 12 13:54:20 2002
@@ -23,6 +23,7 @@
         transformations = schemaspec.prepareSetstate(self, state, 1)
         if transformations is None:
             return
+        dict = self.__dict__
         if 0 in transformations:
             schemaspec.AddField.update(dict, state, 'alpha')
 


=== Zope3/lib/python/Zope/App/schemagen/tests/test_schemaspec.py 1.2 => 1.3 ===
--- Zope3/lib/python/Zope/App/schemagen/tests/test_schemaspec.py:1.2	Thu Dec 12 13:28:03 2002
+++ Zope3/lib/python/Zope/App/schemagen/tests/test_schemaspec.py	Thu Dec 12 13:54:20 2002
@@ -208,7 +208,46 @@
         f.close()
         self.assertEquals(source.strip(), s.generateModuleSource().strip())
 
+    def test_prepareSetstate(self):
+        from Zope.App.schemagen.schemaspec import prepareSetstate
+        class DummySchemaClass(object):
+            pass
+        obj = DummySchemaClass()
+        state = { '__schema_version__': 1, 'foo': 'baz' }
+        version = 2
+        d = prepareSetstate(obj, state, version)
+        self.assertEquals({ 1: 1}, d)
+        self.assertEquals(obj.foo, 'baz')
+        self.assertEquals(obj.__schema_version__, 2)
+        
+class ModuleUsageTests(TestCase):
+
+    def executeModule(self, s):
+        source = s.generateModuleSource()
+        g = {}
+        exec source in g
+        del g['__builtins__'] # to ease inspection during debugging
+        return g
+        
+    def test_setstate(self):
+        s = SchemaSpec('IFoo', 'Foo')
+        s.addField('alpha', Text(title=u'Alpha'))
+        s.addField('beta', Text(title=u'Beta', default=u"default"))
+        g = self.executeModule(s)
+        foo = g['Foo']()
+        self.assertEquals(foo.__schema_version__, 2)
+        self.assertEquals(foo.beta, u"default")
+        
+        foo.beta = u"Frog"
+        # this is evil, but we do this for testing
+        foo.__schema_version__ = 1
+        # do the automatical upgrade according to schema
+        foo.__setstate__(foo.__getstate__())
+        self.assertEquals(foo.beta, u"default")
+        self.assertEquals(foo.__schema_version__, 2)
+        
 def test_suite():
     return TestSuite(
         (makeSuite(SchemaSpecTests),
+         makeSuite(ModuleUsageTests),
          ))