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

Shane Hathaway shane@zope.com
Tue, 4 Feb 2003 23:59:22 -0500


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

Modified Files:
	testZope2FS.py 
Log Message:
Added an automatic filename extensions feature to FSConnection and
corresponding unit tests.  Also cleaned up minor details in FSConnection.  


=== Products/AdaptableStorage/tests/testZope2FS.py 1.15 => 1.16 ===
--- Products/AdaptableStorage/tests/testZope2FS.py:1.15	Fri Jan 10 14:02:16 2003
+++ Products/AdaptableStorage/tests/testZope2FS.py	Tue Feb  4 23:59:19 2003
@@ -21,6 +21,8 @@
 import unittest
 from tempfile import mktemp
 
+from ZODB.POSException import ConflictError
+from Products.PythonScripts.PythonScript import PythonScript
 from Products.AdaptableStorage.zodb.ASDB import ASDB
 from Products.AdaptableStorage.zodb.ASStorage import ASStorage
 from Products.AdaptableStorage.zodb.StaticResource import StaticResource
@@ -121,6 +123,247 @@
             f.id = 'Holidays'
             app._setObject(f.id, f, set_owner=0)
             get_transaction().commit()
+        finally:
+            conn.close()
+
+
+    def testPreserveNamesWithoutExtensions(self):
+        # Verifies that FSConnection retains original object names,
+        # even though the files might be stored with extensions.
+        conn = self.db.open()
+        try:
+            app = conn.root()['Application']
+            f = Folder()
+            f.id = 'folder'
+            app._setObject(f.id, f, set_owner=0)
+            for n in range(3):
+                script = PythonScript('script%d' % n)
+                script.write('##title=test script\nreturn "OK"')
+                f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            conn2 = self.db.open()
+            try:
+                app = conn2.root()['Application']
+                f = app.folder
+                for n in range(3):
+                    self.assert_(hasattr(f, 'script%d' % n))
+                    self.assert_(not hasattr(f, 'script%d.py' % n))
+                # white box test: verify the scripts were actually stored
+                # with .py extensions.
+                dir = self.conn.expandPath('/folder')
+                names = os.listdir(dir)
+                for n in range(3):
+                    self.assert_(('script%d.py' % n) in names, names)
+            finally:
+                conn2.close()
+        finally:
+            conn.close()
+
+
+    def testPreserveNamesWithExtensions(self):
+        # Verifies that FSConnection retains original object names
+        # even though the object names already have extensions.
+        conn = self.db.open()
+        try:
+            app = conn.root()['Application']
+            f = Folder()
+            f.id = 'folder'
+            app._setObject(f.id, f, set_owner=0)
+            for n in range(3):
+                script = PythonScript('script%d.py' % n)
+                script.write('##title=test script\nreturn "OK"')
+                f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            conn2 = self.db.open()
+            try:
+                app = conn2.root()['Application']
+                f = app.folder
+                for n in range(3):
+                    self.assert_(hasattr(f, 'script%d.py' % n))
+                    self.assert_(not hasattr(f, 'script%d' % n))
+                # white box test: verify the scripts were actually stored
+                # with .py extensions.
+                dir = self.conn.expandPath('/folder')
+                names = os.listdir(dir)
+                for n in range(3):
+                    self.assert_(('script%d.py' % n) in names, names)
+            finally:
+                conn2.close()
+        finally:
+            conn.close()
+
+
+    def testNameExtensionConflictDetection(self):
+        # Verifies that conflicting names resulting from automatic extensions
+        # don't go unnoticed.
+        conn = self.db.open()
+        try:
+            app = conn.root()['Application']
+            f = Folder()
+            f.id = 'folder'
+            app._setObject(f.id, f, set_owner=0)
+
+            # Can't write to 'script0' then 'script0.py'.
+            script = PythonScript('script0')
+            script.write('##title=test script\nreturn "OK"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            dir = self.conn.expandPath('/folder')
+            names = os.listdir(dir)
+            self.assert_(('script0.py') in names, names)
+            self.assert_(('script0') not in names, names)
+
+            # script0.py already exists, so the transaction will fail.
+            script = PythonScript('script0.py')
+            script.write('##title=test script\nreturn "Hello, world!"')
+            f._setObject(script.id, script, set_owner=0)
+            self.assertRaises(ConflictError, get_transaction().commit)
+        finally:
+            conn.close()
+
+
+    def testNonConflictingNameExtensions1(self):
+        # Verifies that FSConnection can write to 'script0.py' then 'script0'
+        conn = self.db.open()
+        try:
+            app = conn.root()['Application']
+            f = Folder()
+            f.id = 'folder'
+            app._setObject(f.id, f, set_owner=0)
+
+            # It's OK to write to 'script0.py' then 'script0'.
+            script = PythonScript('script0.py')
+            script.write('##title=test script\nreturn "OK"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            script = PythonScript('script0')
+            script.write('##title=test script\nreturn "Hello, world!"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            dir = self.conn.expandPath('/folder')
+            names = os.listdir(dir)
+            self.assert_(('script0.py') in names, names)
+            self.assert_(('script0') in names, names)
+
+            conn2 = self.db.open()
+            try:
+                app = conn2.root()['Application']
+                f = app.folder
+                self.assertEqual(f['script0.py'](), 'OK')
+                self.assertEqual(f['script0'](), 'Hello, world!')
+            finally:
+                conn2.close()
+        finally:
+            conn.close()
+
+
+    def testNonConflictingNameExtensions2(self):
+        # Verifies that FSConnection can write to 'script0.py' and 'script0'
+        # at the same time
+        conn = self.db.open()
+        try:
+            app = conn.root()['Application']
+            f = Folder()
+            f.id = 'folder'
+            app._setObject(f.id, f, set_owner=0)
+
+            # It's OK to write to 'script0.py' then 'script0'.
+            script = PythonScript('script0.py')
+            script.write('##title=test script\nreturn "OK"')
+            f._setObject(script.id, script, set_owner=0)
+            script = PythonScript('script0')
+            script.write('##title=test script\nreturn "Hello, world!"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            conn2 = self.db.open()
+            try:
+                app = conn2.root()['Application']
+                f = app.folder
+                self.assertEqual(f['script0.py'](), 'OK')
+                self.assertEqual(f['script0'](), 'Hello, world!')
+            finally:
+                conn2.close()
+        finally:
+            conn.close()
+
+
+    def testNonConflictingNameExtensions3(self):
+        # Verifies that FSConnection can write to 'script0.py'
+        # then 'script0.dtml', then 'script0'.
+        # Then verifies that removal of items works correctly.
+        conn = self.db.open()
+        try:
+            app = conn.root()['Application']
+            f = Folder()
+            f.id = 'folder'
+            app._setObject(f.id, f, set_owner=0)
+
+            script = PythonScript('script0.py')
+            script.write('##title=test script\nreturn "OK"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            script = PythonScript('script0.dtml')
+            script.write('##title=test script\nreturn "No DTML here"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            script = PythonScript('script0')
+            script.write('##title=test script\nreturn "Hello, world!"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+
+            dir = self.conn.expandPath('/folder')
+            names = os.listdir(dir)
+            self.assert_(('script0.py') in names, names)
+            self.assert_(('script0.dtml') in names, names)
+            self.assert_(('script0') in names, names)
+
+            conn2 = self.db.open()
+            try:
+                app2 = conn2.root()['Application']
+                f2 = app2.folder
+                self.assertEqual(f2['script0.py'](), 'OK')
+                self.assertEqual(f2['script0.dtml'](), 'No DTML here')
+                self.assertEqual(f2['script0'](), 'Hello, world!')
+            finally:
+                conn2.close()
+
+            f._delObject('script0.py')
+            get_transaction().commit()
+            names = os.listdir(dir)
+            self.assert_(('script0.py') not in names, names)
+            self.assert_(('script0.dtml') in names, names)
+            self.assert_(('script0') in names, names)
+
+            f._delObject('script0')
+            get_transaction().commit()
+            names = os.listdir(dir)
+            self.assert_(('script0.py') not in names, names)
+            self.assert_(('script0.dtml') in names, names)
+            self.assert_(('script0') not in names, names)
+
+            script = PythonScript('script0')
+            script.write('##title=test script\nreturn "Hello, world!"')
+            f._setObject(script.id, script, set_owner=0)
+            get_transaction().commit()
+            names = os.listdir(dir)
+            self.assert_(('script0.py') not in names, names)
+            self.assert_(('script0.dtml') in names, names)
+            self.assert_(('script0') in names, names)
+
+            f._delObject('script0.dtml')
+            get_transaction().commit()
+            names = os.listdir(dir)
+            self.assert_(('script0.py') not in names, names)
+            self.assert_(('script0.dtml') not in names, names)
+            self.assert_(('script0') in names, names)
         finally:
             conn.close()