1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL THE BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: test_db.py,v 1.5 2001-08-07 00:15:51 richard Exp $
20 import unittest, os, shutil
22 from roundup.hyperdb import String, Link, Multilink, Date, Interval, Class, \
23 DatabaseError
25 def setupSchema(db, create):
26 status = Class(db, "status", name=String())
27 status.setkey("name")
28 if create:
29 status.create(name="unread")
30 status.create(name="in-progress")
31 status.create(name="testing")
32 status.create(name="resolved")
33 Class(db, "user", username=String(), password=String())
34 Class(db, "issue", title=String(), status=Link("status"),
35 nosy=Multilink("user"))
37 #class MyTestResult(unittest._TestResult):
38 # def addError(self, test, err):
39 # print `err`
40 # TestResult.addError(self, test, err)
41 # if self.showAll:
42 # self.stream.writeln("ERROR")
43 # elif self.dots:
44 # self.stream.write('E')
45 # if err[0] is KeyboardInterrupt:
46 # self.shouldStop = 1
48 class MyTestCase(unittest.TestCase):
49 # def defaultTestResult(self):
50 # return MyTestResult()
51 def tearDown(self):
52 if self.db is not None:
53 self.db.close()
54 shutil.rmtree('_test_dir')
56 class DBTestCase(MyTestCase):
57 def setUp(self):
58 from roundup.backends import anydbm
59 # remove previous test, ignore errors
60 if os.path.exists('_test_dir'):
61 shutil.rmtree('_test_dir')
62 os.mkdir('_test_dir')
63 self.db = anydbm.Database('_test_dir', 'test')
64 setupSchema(self.db, 1)
66 def testChanges(self):
67 self.db.issue.create(title="spam", status='1')
68 self.db.issue.create(title="eggs", status='2')
69 self.db.issue.create(title="ham", status='4')
70 self.db.issue.create(title="arguments", status='2')
71 self.db.issue.create(title="abuse", status='1')
72 self.db.issue.addprop(fixer=Link("user"))
73 props = self.db.issue.getprops()
74 keys = props.keys()
75 keys.sort()
76 self.assertEqual(keys, ['fixer', 'id', 'nosy', 'status', 'title'])
77 self.db.issue.set('5', status='2')
78 self.db.issue.get('5', "status")
79 self.db.status.get('2', "name")
80 self.db.issue.get('5', "title")
81 self.db.issue.find(status = self.db.status.lookup("in-progress"))
82 self.db.issue.history('5')
83 self.db.status.history('1')
84 self.db.status.history('2')
86 def testExceptions(self):
87 # this tests the exceptions that should be raised
88 ar = self.assertRaises
90 #
91 # class create
92 #
93 # string property
94 ar(TypeError, self.db.status.create, name=1)
95 # invalid property name
96 ar(KeyError, self.db.status.create, foo='foo')
97 # key name clash
98 ar(ValueError, self.db.status.create, name='unread')
99 # invalid link index
100 ar(IndexError, self.db.issue.create, title='foo', status='bar')
101 # invalid link value
102 ar(ValueError, self.db.issue.create, title='foo', status=1)
103 # invalid multilink type
104 ar(TypeError, self.db.issue.create, title='foo', status='1',
105 nosy='hello')
106 # invalid multilink index type
107 ar(ValueError, self.db.issue.create, title='foo', status='1',
108 nosy=[1])
109 # invalid multilink index
110 ar(IndexError, self.db.issue.create, title='foo', status='1',
111 nosy=['10'])
113 #
114 # class get
115 #
116 # invalid node id
117 ar(IndexError, self.db.status.get, '10', 'name')
118 # invalid property name
119 ar(KeyError, self.db.status.get, '2', 'foo')
121 #
122 # class set
123 #
124 # invalid node id
125 ar(IndexError, self.db.issue.set, '1', name='foo')
126 # invalid property name
127 ar(KeyError, self.db.status.set, '1', foo='foo')
128 # string property
129 ar(TypeError, self.db.status.set, '1', name=1)
130 # key name clash
131 ar(ValueError, self.db.status.set, '2', name='unread')
132 # set up a valid issue for me to work on
133 self.db.issue.create(title="spam", status='1')
134 # invalid link index
135 ar(IndexError, self.db.issue.set, '1', title='foo', status='bar')
136 # invalid link value
137 ar(ValueError, self.db.issue.set, '1', title='foo', status=1)
138 # invalid multilink type
139 ar(TypeError, self.db.issue.set, '1', title='foo', status='1',
140 nosy='hello')
141 # invalid multilink index type
142 ar(ValueError, self.db.issue.set, '1', title='foo', status='1',
143 nosy=[1])
144 # invalid multilink index
145 ar(IndexError, self.db.issue.set, '1', title='foo', status='1',
146 nosy=['10'])
148 def testRetire(self):
149 pass
152 class ReadOnlyDBTestCase(MyTestCase):
153 def setUp(self):
154 from roundup.backends import anydbm
155 # remove previous test, ignore errors
156 if os.path.exists('_test_dir'):
157 shutil.rmtree('_test_dir')
158 os.mkdir('_test_dir')
159 db = anydbm.Database('_test_dir', 'test')
160 setupSchema(db, 1)
161 db.close()
162 self.db = anydbm.Database('_test_dir')
163 setupSchema(self.db, 0)
165 def testExceptions(self):
166 # this tests the exceptions that should be raised
167 ar = self.assertRaises
169 # this tests the exceptions that should be raised
170 ar(DatabaseError, self.db.status.create, name="foo")
171 ar(DatabaseError, self.db.status.set, '1', name="foo")
172 ar(DatabaseError, self.db.status.retire, '1')
175 class bsddbDBTestCase(DBTestCase):
176 def setUp(self):
177 from roundup.backends import bsddb
178 # remove previous test, ignore errors
179 if os.path.exists('_test_dir'):
180 shutil.rmtree('_test_dir')
181 os.mkdir('_test_dir')
182 self.db = bsddb.Database('_test_dir', 'test')
183 setupSchema(self.db, 1)
185 class bsddbReadOnlyDBTestCase(ReadOnlyDBTestCase):
186 def setUp(self):
187 from roundup.backends import bsddb
188 # remove previous test, ignore errors
189 if os.path.exists('_test_dir'):
190 shutil.rmtree('_test_dir')
191 os.mkdir('_test_dir')
192 db = bsddb.Database('_test_dir', 'test')
193 setupSchema(db, 1)
194 db.close()
195 self.db = bsddb.Database('_test_dir')
196 setupSchema(self.db, 0)
199 class bsddb3DBTestCase(DBTestCase):
200 def setUp(self):
201 from roundup.backends import bsddb3
202 # remove previous test, ignore errors
203 if os.path.exists('_test_dir'):
204 shutil.rmtree('_test_dir')
205 os.mkdir('_test_dir')
206 self.db = bsddb3.Database('_test_dir', 'test')
207 setupSchema(self.db, 1)
209 class bsddb3ReadOnlyDBTestCase(ReadOnlyDBTestCase):
210 def setUp(self):
211 from roundup.backends import bsddb3
212 # remove previous test, ignore errors
213 if os.path.exists('_test_dir'):
214 shutil.rmtree('_test_dir')
215 os.mkdir('_test_dir')
216 db = bsddb3.Database('_test_dir', 'test')
217 setupSchema(db, 1)
218 db.close()
219 self.db = bsddb3.Database('_test_dir')
220 setupSchema(self.db, 0)
223 def suite():
224 l = [unittest.makeSuite(DBTestCase, 'test'),
225 unittest.makeSuite(ReadOnlyDBTestCase, 'test')]
227 try:
228 import bsddb
229 l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
230 l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))
231 except:
232 print 'bsddb module not found, skipping bsddb DBTestCase'
234 try:
235 import bsddb3
236 l.append(unittest.makeSuite(bsddb3DBTestCase, 'test'))
237 l.append(unittest.makeSuite(bsddb3ReadOnlyDBTestCase, 'test'))
238 except:
239 print 'bsddb3 module not found, skipping bsddb3 DBTestCase'
241 return unittest.TestSuite(l)
243 #
244 # $Log: not supported by cvs2svn $
245 # Revision 1.4 2001/07/30 03:45:56 richard
246 # Added more DB to test_db. Can skip tests where imports fail.
247 #
248 # Revision 1.3 2001/07/29 07:01:39 richard
249 # Added vim command to all source so that we don't get no steenkin' tabs :)
250 #
251 # Revision 1.2 2001/07/29 04:09:20 richard
252 # Added the fabricated property "id" to all hyperdb classes.
253 #
254 # Revision 1.1 2001/07/27 06:55:07 richard
255 # moving tests -> test
256 #
257 # Revision 1.7 2001/07/27 06:26:43 richard
258 # oops - wasn't deleting the test dir after the read-only tests
259 #
260 # Revision 1.6 2001/07/27 06:23:59 richard
261 # consistency
262 #
263 # Revision 1.5 2001/07/27 06:23:09 richard
264 # Added some new hyperdb tests to make sure we raise the right exceptions.
265 #
266 # Revision 1.4 2001/07/25 04:34:31 richard
267 # Added id and log to tests files...
268 #
269 #
270 # vim: set filetype=python ts=4 sw=4 et si