[Zope-CVS] CVS: Products/AdaptableStorage/tests - testZope2FS.py:1.6

Shane Hathaway shane@zope.com
Mon, 9 Dec 2002 15:27:09 -0500


Update of /cvs-repository/Products/AdaptableStorage/tests
In directory cvs.zope.org:/tmp/cvs-serv10661/tests

Modified Files:
	testZope2FS.py 
Log Message:
Added two mappers to Zope2FS for serializing any kind of object.  If no mapper
is provided for a class, the database falls back to a default mapper--either
the "fileish" or the "folderish" mapper.

... which is *really cool*. :-)  I've been trying to achieve this in a clean
way for months.  (Deeeeep, loooonnnnnggggg sigh of relief!)



=== Products/AdaptableStorage/tests/testZope2FS.py 1.5 => 1.6 ===
--- Products/AdaptableStorage/tests/testZope2FS.py:1.5	Sat Dec  7 00:59:14 2002
+++ Products/AdaptableStorage/tests/testZope2FS.py	Mon Dec  9 15:27:09 2002
@@ -21,8 +21,10 @@
 import unittest
 from tempfile import mktemp
 
-import ZODB
+from ZODB import Persistent
 from Persistence import PersistentMapping
+from OFS.Folder import Folder
+from OFS.SimpleItem import SimpleItem
 
 from Products.AdaptableStorage.zodb.ASDB import ASDB
 from Products.AdaptableStorage.zodb.ASStorage import ASStorage
@@ -30,6 +32,22 @@
 from Products.AdaptableStorage.Zope2FS import createMapper
 
 
+class TestFolder(Folder):
+
+    meta_type = 'Zope2FS Test Folder'
+
+    def __init__(self, title):
+        self.title = title
+
+
+class TestFile(SimpleItem):
+
+    meta_type = 'Zope2FS Test File'
+
+    def __init__(self, content):
+        self.content = content
+
+
 class Zope2FSTests (unittest.TestCase):
 
     def setUp(self):
@@ -60,7 +78,6 @@
     def testStore(self):
         conn = self.db.open()
         try:
-            from OFS.Folder import Folder
             app = conn.root()['Application']
             f = Folder()
             f.id = 'Holidays'
@@ -94,7 +111,6 @@
         # it's the container that writes it, not the object.
         conn = self.db.open()
         try:
-            from OFS.Folder import Folder
             app = conn.root()['Application']
             f = Folder()
             f.id = 'Holidays'
@@ -115,7 +131,70 @@
                 keychain = conn.db()._oid_encoder.decode(folder._p_oid)
                 key = keychain[-1]
                 text = self.fs_conn.readSection(key, 'classification')
-                self.assertEqual(text.strip(), 'meta_type=Folder')
+                self.assert_(text.find('meta_type=Folder') >= 0)
+        finally:
+            conn.close()
+
+
+    def testAnyFolderishStorage(self):
+        # Try to store a folderish object of an otherwise unknown class
+        conn = self.db.open()
+        try:
+            app = conn.root()['Application']
+            f = Folder()
+            f.id = 'Holidays'
+            app._setObject(f.id, f, set_owner=0)
+            get_transaction().commit()
+
+            f2 = TestFolder("New Year's Eve")
+            f2.id = 'NewYear'
+            f._setObject(f2.id, f2, set_owner=0)
+            get_transaction().commit()
+
+            # Verify the object is in its own database record
+            self.assertNotEqual(f2._p_oid, None)
+            f2._p_changed = None
+            self.assert_(f2._p_changed is None)
+
+            # Verify the ability to load it
+            conn2 = self.db.open()
+            try:
+                app2 = conn2.root()['Application']
+                ff = app2.Holidays.NewYear
+                self.assertEqual(ff.title, "New Year's Eve")
+                self.assertEqual(ff.__class__, TestFolder)
+            finally:
+                conn2.close()
+        finally:
+            conn.close()
+
+
+    def testAnyFileishStorage(self):
+        # Try to store a fileish object of an otherwise unknown class
+        conn = self.db.open()
+        try:
+            content = 'insert wise expression here'
+
+            app = conn.root()['Application']
+            f = TestFile(content)
+            f.id = 'testitem'
+            app._setObject(f.id, f, set_owner=0)
+            get_transaction().commit()
+
+            # Verify the object is in its own database record
+            self.assertNotEqual(f._p_oid, None)
+            f._p_changed = None
+            self.assert_(f._p_changed is None)
+
+            # Verify the ability to load it
+            conn2 = self.db.open()
+            try:
+                app2 = conn2.root()['Application']
+                ff = app2.testitem
+                self.assertEqual(ff.content, content)
+                self.assertEqual(ff.__class__, TestFile)
+            finally:
+                conn2.close()
         finally:
             conn.close()