Skip to content

Commit b231368

Browse files
author
Richard Jones
committed
Added some new hyperdb tests to make sure we raise the right exceptions.
1 parent 7030f5a commit b231368

File tree

1 file changed

+105
-12
lines changed

1 file changed

+105
-12
lines changed

tests/test_db.py

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
# $Id: test_db.py,v 1.4 2001-07-25 04:34:31 richard Exp $
1+
# $Id: test_db.py,v 1.5 2001-07-27 06:23:09 richard Exp $
22

33
import unittest, os, shutil
44

55
from roundup.backends import anydbm
6-
from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class
6+
from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class, \
7+
DatabaseError
78

8-
def setupSchema(db):
9+
def setupSchema(db, create):
910
status = Class(db, "status", name=String())
1011
status.setkey("name")
11-
status.create(name="unread")
12-
status.create(name="in-progress")
13-
status.create(name="testing")
14-
status.create(name="resolved")
12+
if create:
13+
status.create(name="unread")
14+
status.create(name="in-progress")
15+
status.create(name="testing")
16+
status.create(name="resolved")
1517
Class(db, "user", username=String(), password=String())
16-
Class(db, "issue", title=String(), status=Link("status"))
18+
Class(db, "issue", title=String(), status=Link("status"),
19+
nosy=Multilink("user"))
1720

1821
class DBTestCase(unittest.TestCase):
1922
def setUp(self):
@@ -24,7 +27,7 @@ class Database(anydbm.Database):
2427
shutil.rmtree('_test_dir')
2528
os.mkdir('_test_dir')
2629
self.db = Database('_test_dir', 'test')
27-
setupSchema(self.db)
30+
setupSchema(self.db, 1)
2831

2932
def tearDown(self):
3033
self.db.close()
@@ -40,8 +43,8 @@ def testChanges(self):
4043
props = self.db.issue.getprops()
4144
keys = props.keys()
4245
keys.sort()
43-
self.assertEqual(keys, ['fixer', 'status', 'title'], 'wrong prop list')
44-
self.db.issue.set('5', status=2)
46+
self.assertEqual(keys, ['fixer', 'nosy', 'status', 'title'])
47+
self.db.issue.set('5', status='2')
4548
self.db.issue.get('5', "status")
4649
self.db.status.get('2', "name")
4750
self.db.issue.get('5', "title")
@@ -52,12 +55,102 @@ def testChanges(self):
5255

5356
def testExceptions(self):
5457
# this tests the exceptions that should be raised
58+
ar = self.assertRaises
59+
60+
#
61+
# class create
62+
#
63+
# string property
64+
ar(TypeError, self.db.status.create, name=1)
65+
# invalid property name
66+
ar(KeyError, self.db.status.create, foo='foo')
67+
# key name clash
68+
ar(ValueError, self.db.status.create, name='unread')
69+
# invalid link index
70+
ar(IndexError, self.db.issue.create, title='foo', status='bar')
71+
# invalid link value
72+
ar(ValueError, self.db.issue.create, title='foo', status=1)
73+
# invalid multilink type
74+
ar(TypeError, self.db.issue.create, title='foo', status='1',
75+
nosy='hello')
76+
# invalid multilink index type
77+
ar(ValueError, self.db.issue.create, title='foo', status='1',
78+
nosy=[1])
79+
# invalid multilink index
80+
ar(IndexError, self.db.issue.create, title='foo', status='1',
81+
nosy=['10'])
82+
83+
#
84+
# class get
85+
#
86+
# invalid node id
87+
ar(IndexError, self.db.status.get, '10', 'name')
88+
# invalid property name
89+
ar(KeyError, self.db.status.get, '2', 'foo')
90+
91+
#
92+
# class set
93+
#
94+
# invalid node id
95+
ar(IndexError, self.db.issue.set, '1', name='foo')
96+
# invalid property name
97+
ar(KeyError, self.db.status.set, '1', foo='foo')
98+
# string property
99+
ar(TypeError, self.db.status.set, '1', name=1)
100+
# key name clash
101+
ar(ValueError, self.db.status.set, '2', name='unread')
102+
# set up a valid issue for me to work on
103+
self.db.issue.create(title="spam", status='1')
104+
# invalid link index
105+
ar(IndexError, self.db.issue.set, '1', title='foo', status='bar')
106+
# invalid link value
107+
ar(ValueError, self.db.issue.set, '1', title='foo', status=1)
108+
# invalid multilink type
109+
ar(TypeError, self.db.issue.set, '1', title='foo', status='1',
110+
nosy='hello')
111+
# invalid multilink index type
112+
ar(ValueError, self.db.issue.set, '1', title='foo', status='1',
113+
nosy=[1])
114+
# invalid multilink index
115+
ar(IndexError, self.db.issue.set, '1', title='foo', status='1',
116+
nosy=['10'])
117+
118+
def testRetire(self):
119+
''' test retiring a node
120+
'''
55121
pass
56122

123+
124+
class ReadOnlyDBTestCase(unittest.TestCase):
125+
def setUp(self):
126+
class Database(anydbm.Database):
127+
pass
128+
# remove previous test, ignore errors
129+
if os.path.exists('_test_dir'):
130+
shutil.rmtree('_test_dir')
131+
os.mkdir('_test_dir')
132+
db = Database('_test_dir', 'test')
133+
setupSchema(db, 1)
134+
db.close()
135+
self.db = Database('_test_dir')
136+
setupSchema(self.db, 0)
137+
138+
def testExceptions(self):
139+
# this tests the exceptions that should be raised
140+
self.assertRaises(DatabaseError, self.db.status.create, name="foo")
141+
self.assertRaises(DatabaseError, self.db.status.set, '1', name="foo")
142+
self.assertRaises(DatabaseError, self.db.status.retire, '1')
143+
144+
57145
def suite():
58-
return unittest.makeSuite(DBTestCase, 'test')
146+
db = unittest.makeSuite(DBTestCase, 'test')
147+
readonlydb = unittest.makeSuite(ReadOnlyDBTestCase, 'test')
148+
return unittest.TestSuite((db, readonlydb))
59149

60150

61151
#
62152
# $Log: not supported by cvs2svn $
153+
# Revision 1.4 2001/07/25 04:34:31 richard
154+
# Added id and log to tests files...
155+
#
63156
#

0 commit comments

Comments
 (0)