From 1ccfc1c4576ec08571d922107e7b375cd152359c Mon Sep 17 00:00:00 2001 From: richard Date: Fri, 27 Jul 2001 06:23:09 +0000 Subject: [PATCH] Added some new hyperdb tests to make sure we raise the right exceptions. git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@92 57a73879-2fb5-44c3-a270-3262357dd7e2 --- tests/test_db.py | 117 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 12 deletions(-) diff --git a/tests/test_db.py b/tests/test_db.py index c70a5a6..1ce0cc8 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,19 +1,22 @@ -# $Id: test_db.py,v 1.4 2001-07-25 04:34:31 richard Exp $ +# $Id: test_db.py,v 1.5 2001-07-27 06:23:09 richard Exp $ import unittest, os, shutil from roundup.backends import anydbm -from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class +from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class, \ + DatabaseError -def setupSchema(db): +def setupSchema(db, create): status = Class(db, "status", name=String()) status.setkey("name") - status.create(name="unread") - status.create(name="in-progress") - status.create(name="testing") - status.create(name="resolved") + if create: + status.create(name="unread") + status.create(name="in-progress") + status.create(name="testing") + status.create(name="resolved") Class(db, "user", username=String(), password=String()) - Class(db, "issue", title=String(), status=Link("status")) + Class(db, "issue", title=String(), status=Link("status"), + nosy=Multilink("user")) class DBTestCase(unittest.TestCase): def setUp(self): @@ -24,7 +27,7 @@ class DBTestCase(unittest.TestCase): shutil.rmtree('_test_dir') os.mkdir('_test_dir') self.db = Database('_test_dir', 'test') - setupSchema(self.db) + setupSchema(self.db, 1) def tearDown(self): self.db.close() @@ -40,8 +43,8 @@ class DBTestCase(unittest.TestCase): props = self.db.issue.getprops() keys = props.keys() keys.sort() - self.assertEqual(keys, ['fixer', 'status', 'title'], 'wrong prop list') - self.db.issue.set('5', status=2) + self.assertEqual(keys, ['fixer', 'nosy', 'status', 'title']) + self.db.issue.set('5', status='2') self.db.issue.get('5', "status") self.db.status.get('2', "name") self.db.issue.get('5', "title") @@ -52,12 +55,102 @@ class DBTestCase(unittest.TestCase): def testExceptions(self): # this tests the exceptions that should be raised + ar = self.assertRaises + + # + # class create + # + # string property + ar(TypeError, self.db.status.create, name=1) + # invalid property name + ar(KeyError, self.db.status.create, foo='foo') + # key name clash + ar(ValueError, self.db.status.create, name='unread') + # invalid link index + ar(IndexError, self.db.issue.create, title='foo', status='bar') + # invalid link value + ar(ValueError, self.db.issue.create, title='foo', status=1) + # invalid multilink type + ar(TypeError, self.db.issue.create, title='foo', status='1', + nosy='hello') + # invalid multilink index type + ar(ValueError, self.db.issue.create, title='foo', status='1', + nosy=[1]) + # invalid multilink index + ar(IndexError, self.db.issue.create, title='foo', status='1', + nosy=['10']) + + # + # class get + # + # invalid node id + ar(IndexError, self.db.status.get, '10', 'name') + # invalid property name + ar(KeyError, self.db.status.get, '2', 'foo') + + # + # class set + # + # invalid node id + ar(IndexError, self.db.issue.set, '1', name='foo') + # invalid property name + ar(KeyError, self.db.status.set, '1', foo='foo') + # string property + ar(TypeError, self.db.status.set, '1', name=1) + # key name clash + ar(ValueError, self.db.status.set, '2', name='unread') + # set up a valid issue for me to work on + self.db.issue.create(title="spam", status='1') + # invalid link index + ar(IndexError, self.db.issue.set, '1', title='foo', status='bar') + # invalid link value + ar(ValueError, self.db.issue.set, '1', title='foo', status=1) + # invalid multilink type + ar(TypeError, self.db.issue.set, '1', title='foo', status='1', + nosy='hello') + # invalid multilink index type + ar(ValueError, self.db.issue.set, '1', title='foo', status='1', + nosy=[1]) + # invalid multilink index + ar(IndexError, self.db.issue.set, '1', title='foo', status='1', + nosy=['10']) + + def testRetire(self): + ''' test retiring a node + ''' pass + +class ReadOnlyDBTestCase(unittest.TestCase): + def setUp(self): + class Database(anydbm.Database): + pass + # remove previous test, ignore errors + if os.path.exists('_test_dir'): + shutil.rmtree('_test_dir') + os.mkdir('_test_dir') + db = Database('_test_dir', 'test') + setupSchema(db, 1) + db.close() + self.db = Database('_test_dir') + setupSchema(self.db, 0) + + def testExceptions(self): + # this tests the exceptions that should be raised + self.assertRaises(DatabaseError, self.db.status.create, name="foo") + self.assertRaises(DatabaseError, self.db.status.set, '1', name="foo") + self.assertRaises(DatabaseError, self.db.status.retire, '1') + + def suite(): - return unittest.makeSuite(DBTestCase, 'test') + db = unittest.makeSuite(DBTestCase, 'test') + readonlydb = unittest.makeSuite(ReadOnlyDBTestCase, 'test') + return unittest.TestSuite((db, readonlydb)) # # $Log: not supported by cvs2svn $ +# Revision 1.4 2001/07/25 04:34:31 richard +# Added id and log to tests files... +# # -- 2.30.2