Code

TODO changes
[roundup.git] / test / test_db.py
index 7863bc695f3e3f24daf6413196793e6187b56d08..a7aa3c0b28d1b31129ae458185c32cdd0fadc812 100644 (file)
@@ -15,7 +15,7 @@
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 # 
-# $Id: test_db.py,v 1.33 2002-07-18 11:50:58 richard Exp $ 
+# $Id: test_db.py,v 1.39 2002-07-31 23:57:37 richard Exp $ 
 
 import unittest, os, shutil, time
 
@@ -28,12 +28,15 @@ def setupSchema(db, create, module):
     status = module.Class(db, "status", name=String())
     status.setkey("name")
     user = module.Class(db, "user", username=String(), password=Password(),
-        assignable=Boolean(), age=Number())
+        assignable=Boolean(), age=Number(), roles=String())
+    user.setkey("username")
     file = module.FileClass(db, "file", name=String(), type=String(),
         comment=String(indexme="yes"))
     issue = module.IssueClass(db, "issue", title=String(indexme="yes"),
         status=Link("status"), nosy=Multilink("user"), deadline=Date(),
-        foo=Interval(), files=Multilink("file"))
+        foo=Interval(), files=Multilink("file"), assignedto=Link('user'))
+    session = module.Class(db, 'session', title=String())
+    session.disableJournalling()
     db.post_init()
     if create:
         status.create(name="unread")
@@ -73,7 +76,7 @@ class anydbmDBTestCase(MyTestCase):
         self.db2 = anydbm.Database(config, 'test')
         setupSchema(self.db2, 0, anydbm)
 
-    def xtestStringChange(self):
+    def testStringChange(self):
         self.db.issue.create(title="spam", status='1')
         self.assertEqual(self.db.issue.get('1', 'title'), 'spam')
         self.db.issue.set('1', title='eggs')
@@ -87,14 +90,18 @@ class anydbmDBTestCase(MyTestCase):
         self.assertEqual(self.db.issue.get('2', 'title'), 'ham')
         self.db.commit()
         self.assertEqual(self.db.issue.get('2', 'title'), 'ham')
+        self.db.issue.set('1', title=None)
+        self.assertEqual(self.db.issue.get('1', "title"), None)
 
-    def xtestLinkChange(self):
+    def testLinkChange(self):
         self.db.issue.create(title="spam", status='1')
         self.assertEqual(self.db.issue.get('1', "status"), '1')
         self.db.issue.set('1', status='2')
         self.assertEqual(self.db.issue.get('1', "status"), '2')
+        self.db.issue.set('1', status=None)
+        self.assertEqual(self.db.issue.get('1', "status"), None)
 
-    def xtestDateChange(self):
+    def testDateChange(self):
         self.db.issue.create(title="spam", status='1')
         a = self.db.issue.get('1', "deadline")
         self.db.issue.set('1', deadline=date.Date())
@@ -103,24 +110,27 @@ class anydbmDBTestCase(MyTestCase):
         self.assertNotEqual(a, b)
         self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
         self.db.issue.set('1', deadline=date.Date())
+        self.db.issue.set('1', deadline=None)
+        self.assertEqual(self.db.issue.get('1', "deadline"), None)
 
-    def xtestIntervalChange(self):
+    def testIntervalChange(self):
         self.db.issue.create(title="spam", status='1')
         a = self.db.issue.get('1', "foo")
         self.db.issue.set('1', foo=date.Interval('-1d'))
         self.assertNotEqual(self.db.issue.get('1', "foo"), a)
+        self.db.issue.set('1', foo=None)
+        self.assertEqual(self.db.issue.get('1', "foo"), None)
 
     def testBooleanChange(self):
-        self.db.user.create(username='foo', assignable=1)
-        a = self.db.user.get('1', 'assignable')
-        self.db.user.set('1', assignable='false')
-        self.assertNotEqual(self.db.user.get('1', 'assignable'), a)
-        self.db.user.set('1', assignable='FaLse')
-        self.db.user.set('1', assignable='nO')
-        self.db.user.set('1', assignable=0)
-        self.db.user.set('1', assignable='tRuE')
-        self.db.user.set('1', assignable='yEs')
-        self.db.user.set('1', assignable=1)
+        userid = self.db.user.create(username='foo', assignable=1)
+        self.db.user.create(username='foo2', assignable=0)
+        a = self.db.user.get(userid, 'assignable')
+        self.db.user.set(userid, assignable=0)
+        self.assertNotEqual(self.db.user.get(userid, 'assignable'), a)
+        self.db.user.set(userid, assignable=0)
+        self.db.user.set(userid, assignable=1)
+        self.db.user.set('1', assignable=None)
+        self.assertEqual(self.db.user.get('1', "assignable"), None)
 
     def testNumberChange(self):
         self.db.user.create(username='foo', age='1')
@@ -128,20 +138,21 @@ class anydbmDBTestCase(MyTestCase):
         self.db.user.set('1', age='3')
         self.assertNotEqual(self.db.user.get('1', 'age'), a)
         self.db.user.set('1', age='1.0')
+        self.db.user.set('1', age=None)
+        self.assertEqual(self.db.user.get('1', "age"), None)
 
-    def xtestNewProperty(self):
-        ' make sure a new property is added ok '
+    def testNewProperty(self):
         self.db.issue.create(title="spam", status='1')
         self.db.issue.addprop(fixer=Link("user"))
         props = self.db.issue.getprops()
         keys = props.keys()
         keys.sort()
-        self.assertEqual(keys, ['activity', 'creation', 'creator', 'deadline',
-            'files', 'fixer', 'foo', 'id', 'messages', 'nosy', 'status',
-            'superseder', 'title'])
+        self.assertEqual(keys, ['activity', 'assignedto', 'creation',
+            'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
+            'nosy', 'status', 'superseder', 'title'])
         self.assertEqual(self.db.issue.get('1', "fixer"), None)
 
-    def xtestRetire(self):
+    def testRetire(self):
         self.db.issue.create(title="spam", status='1')
         b = self.db.status.get('1', 'name')
         a = self.db.status.list()
@@ -154,7 +165,7 @@ class anydbmDBTestCase(MyTestCase):
         self.assertEqual(self.db.status.get('1', 'name'), b)
         self.assertNotEqual(a, self.db.status.list())
 
-    def xtestSerialisation(self):
+    def testSerialisation(self):
         self.db.issue.create(title="spam", status='1',
             deadline=date.Date(), foo=date.Interval('-1d'))
         self.db.commit()
@@ -165,7 +176,7 @@ class anydbmDBTestCase(MyTestCase):
         self.db.commit()
         assert isinstance(self.db.user.get('1', 'password'), password.Password)
 
-    def xtestTransactions(self):
+    def testTransactions(self):
         # remember the number of items we started
         num_issues = len(self.db.issue.list())
         num_files = self.db.numfiles()
@@ -193,6 +204,49 @@ class anydbmDBTestCase(MyTestCase):
         self.assertNotEqual(num_files, self.db.numfiles())
         self.assertEqual(num_files2, self.db.numfiles())
 
+    def testDestroyNoJournalling(self):
+        self.innerTestDestroy(klass=self.db.session)
+
+    def testDestroyJournalling(self):
+        self.innerTestDestroy(klass=self.db.issue)
+
+    def innerTestDestroy(self, klass):
+        newid = klass.create(title='Mr Friendly')
+        n = len(klass.list())
+        self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
+        klass.destroy(newid)
+        self.assertRaises(IndexError, klass.get, newid, 'title')
+        self.assertNotEqual(len(klass.list()), n)
+        if klass.do_journal:
+            self.assertRaises(IndexError, klass.history, newid)
+
+        # now with a commit
+        newid = klass.create(title='Mr Friendly')
+        n = len(klass.list())
+        self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
+        self.db.commit()
+        klass.destroy(newid)
+        self.assertRaises(IndexError, klass.get, newid, 'title')
+        self.db.commit()
+        self.assertRaises(IndexError, klass.get, newid, 'title')
+        self.assertNotEqual(len(klass.list()), n)
+        if klass.do_journal:
+            self.assertRaises(IndexError, klass.history, newid)
+
+        # now with a rollback
+        newid = klass.create(title='Mr Friendly')
+        n = len(klass.list())
+        self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
+        self.db.commit()
+        klass.destroy(newid)
+        self.assertNotEqual(len(klass.list()), n)
+        self.assertRaises(IndexError, klass.get, newid, 'title')
+        self.db.rollback()
+        self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
+        self.assertEqual(len(klass.list()), n)
+        if klass.do_journal:
+            self.assertNotEqual(klass.history(newid), [])
+
     def testExceptions(self):
         # this tests the exceptions that should be raised
         ar = self.assertRaises
@@ -265,14 +319,14 @@ class anydbmDBTestCase(MyTestCase):
         # invalid number value
         ar(TypeError, self.db.user.create, username='foo', age='a')
         # invalid boolean value
-        ar(TypeError, self.db.user.create, username='foo', assignable='fubar')
+        ar(TypeError, self.db.user.create, username='foo', assignable='true')
         self.db.user.create(username='foo')
         # invalid number value
         ar(TypeError, self.db.user.set, '3', username='foo', age='a')
         # invalid boolean value
-        ar(TypeError, self.db.user.set, '3', username='foo', assignable='fubar')
+        ar(TypeError, self.db.user.set, '3', username='foo', assignable='true')
 
-    def xtestJournals(self):
+    def testJournals(self):
         self.db.issue.addprop(fixer=Link("user", do_journal='yes'))
         self.db.user.create(username="mary")
         self.db.user.create(username="pete")
@@ -288,8 +342,8 @@ class anydbmDBTestCase(MyTestCase):
         self.assertEqual(action, 'create')
         keys = params.keys()
         keys.sort()
-        self.assertEqual(keys, ['deadline', 'files', 'fixer', 'foo',
-            'messages', 'nosy', 'status', 'superseder', 'title'])
+        self.assertEqual(keys, ['assignedto', 'deadline', 'files', 'fixer',
+            'foo', 'messages', 'nosy', 'status', 'superseder', 'title'])
         self.assertEqual(None,params['deadline'])
         self.assertEqual(None,params['fixer'])
         self.assertEqual(None,params['foo'])
@@ -341,7 +395,7 @@ class anydbmDBTestCase(MyTestCase):
         # see if the change was journalled
         self.assertNotEqual(date_stamp, date_stamp2)
 
-    def xtestPack(self):
+    def testPack(self):
         self.db.issue.create(title="spam", status='1')
         self.db.commit()
         self.db.issue.set('1', status='2')
@@ -353,12 +407,12 @@ class anydbmDBTestCase(MyTestCase):
         journal = self.db.getjournal('issue', '1')
         self.assertEqual(2, len(journal))
 
-    def xtestIDGeneration(self):
+    def testIDGeneration(self):
         id1 = self.db.issue.create(title="spam", status='1')
         id2 = self.db2.issue.create(title="eggs", status='2')
         self.assertNotEqual(id1, id2)
 
-    def xtestSearching(self):
+    def testSearching(self):
         self.db.file.create(content='hello', type="text/plain")
         self.db.file.create(content='world', type="text/frozz",
             comment='blah blah')
@@ -373,7 +427,7 @@ class anydbmDBTestCase(MyTestCase):
         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
             {'2': {}, '1': {}})
 
-    def xtestReindexing(self):
+    def testReindexing(self):
         self.db.issue.create(title="frooz")
         self.db.commit()
         self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
@@ -384,7 +438,7 @@ class anydbmDBTestCase(MyTestCase):
             {'1': {}})
         self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue), {})
 
-    def xtestForcedReindexing(self):
+    def testForcedReindexing(self):
         self.db.issue.create(title="flebble frooz")
         self.db.commit()
         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
@@ -410,8 +464,7 @@ class anydbmReadOnlyDBTestCase(MyTestCase):
         self.db2 = anydbm.Database(config, 'test')
         setupSchema(self.db2, 0, anydbm)
 
-    def xtestExceptions(self):
-        ' make sure exceptions are raised on writes to a read-only db '
+    def testExceptions(self):
         # this tests the exceptions that should be raised
         ar = self.assertRaises
 
@@ -489,7 +542,7 @@ class metakitDBTestCase(anydbmDBTestCase):
         self.db2 = metakit.Database(config, 'test')
         setupSchema(self.db2, 0, metakit)
 
-    def xtestTransactions(self):
+    def testTransactions(self):
         # remember the number of items we started
         num_issues = len(self.db.issue.list())
         self.db.issue.create(title="don't commit me!", status='1')
@@ -535,7 +588,7 @@ def suite():
          unittest.makeSuite(anydbmDBTestCase, 'test'),
          unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
     ]
-    return unittest.TestSuite(l)
+    #return unittest.TestSuite(l)
 
     try:
         import bsddb
@@ -562,6 +615,37 @@ def suite():
 
 #
 # $Log: not supported by cvs2svn $
+# Revision 1.38  2002/07/26 08:27:00  richard
+# Very close now. The cgi and mailgw now use the new security API. The two
+# templates have been migrated to that setup. Lots of unit tests. Still some
+# issue in the web form for editing Roles assigned to users.
+#
+# Revision 1.37  2002/07/25 07:14:06  richard
+# Bugger it. Here's the current shape of the new security implementation.
+# Still to do:
+#  . call the security funcs from cgi and mailgw
+#  . change shipped templates to include correct initialisation and remove
+#    the old config vars
+# ... that seems like a lot. The bulk of the work has been done though. Honest :)
+#
+# Revision 1.36  2002/07/19 03:36:34  richard
+# Implemented the destroy() method needed by the session database (and possibly
+# others). At the same time, I removed the leading underscores from the hyperdb
+# methods that Really Didn't Need Them.
+# The journal also raises IndexError now for all situations where there is a
+# request for the journal of a node that doesn't have one. It used to return
+# [] in _some_ situations, but not all. This _may_ break code, but the tests
+# pass...
+#
+# Revision 1.35  2002/07/18 23:07:08  richard
+# Unit tests and a few fixes.
+#
+# Revision 1.34  2002/07/18 11:52:00  richard
+# oops
+#
+# Revision 1.33  2002/07/18 11:50:58  richard
+# added tests for number type too
+#
 # Revision 1.32  2002/07/18 11:41:10  richard
 # added tests for boolean type, and fixes to anydbm backend
 #