dc08709cc447092623c9c99610f39281eb5e5b74
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 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.19 2002-02-25 14:34:31 grubert Exp $
20 import unittest, os, shutil
22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \
23 Interval, Class, DatabaseError
24 from roundup.roundupdb import FileClass
25 from roundup import date
27 def setupSchema(db, create):
28 status = Class(db, "status", name=String())
29 status.setkey("name")
30 if create:
31 status.create(name="unread")
32 status.create(name="in-progress")
33 status.create(name="testing")
34 status.create(name="resolved")
35 Class(db, "user", username=String(), password=Password())
36 Class(db, "issue", title=String(), status=Link("status"),
37 nosy=Multilink("user"), deadline=Date(), foo=Interval())
38 FileClass(db, "file", name=String(), type=String())
39 db.commit()
41 class MyTestCase(unittest.TestCase):
42 def tearDown(self):
43 if os.path.exists('_test_dir'):
44 shutil.rmtree('_test_dir')
46 class config:
47 DATABASE='_test_dir'
48 MAILHOST = 'localhost'
49 MAIL_DOMAIN = 'fill.me.in.'
50 INSTANCE_NAME = 'Roundup issue tracker'
51 ISSUE_TRACKER_EMAIL = 'issue_tracker@%s'%MAIL_DOMAIN
52 ISSUE_TRACKER_WEB = 'http://some.useful.url/'
53 ADMIN_EMAIL = 'roundup-admin@%s'%MAIL_DOMAIN
54 FILTER_POSITION = 'bottom' # one of 'top', 'bottom', 'top and bottom'
55 ANONYMOUS_ACCESS = 'deny' # either 'deny' or 'allow'
56 ANONYMOUS_REGISTER = 'deny' # either 'deny' or 'allow'
57 MESSAGES_TO_AUTHOR = 'no' # either 'yes' or 'no'
58 EMAIL_SIGNATURE_POSITION = 'bottom'
60 class anydbmDBTestCase(MyTestCase):
61 def setUp(self):
62 from roundup.backends import anydbm
63 # remove previous test, ignore errors
64 if os.path.exists(config.DATABASE):
65 shutil.rmtree(config.DATABASE)
66 os.makedirs(config.DATABASE + '/files')
67 self.db = anydbm.Database(config, 'test')
68 setupSchema(self.db, 1)
70 def testChanges(self):
71 self.db.issue.create(title="spam", status='1')
72 self.db.issue.create(title="eggs", status='2')
73 self.db.issue.create(title="ham", status='4')
74 self.db.issue.create(title="arguments", status='2')
75 self.db.issue.create(title="abuse", status='1')
76 self.db.issue.addprop(fixer=Link("user"))
77 props = self.db.issue.getprops()
78 keys = props.keys()
79 keys.sort()
80 self.assertEqual(keys, ['deadline', 'fixer', 'foo', 'id', 'nosy',
81 'status', 'title'])
82 self.db.issue.set('5', status='2')
83 self.db.issue.get('5', "status")
85 a = self.db.issue.get('5', "deadline")
86 self.db.issue.set('5', deadline=date.Date())
87 self.assertNotEqual(a, self.db.issue.get('5', "deadline"))
89 a = self.db.issue.get('5', "foo")
90 self.db.issue.set('5', foo=date.Interval('-1d'))
91 self.assertNotEqual(a, self.db.issue.get('5', "foo"))
93 self.db.status.get('2', "name")
94 self.db.issue.get('5', "title")
95 self.db.issue.find(status = self.db.status.lookup("in-progress"))
96 self.db.commit()
97 self.db.issue.history('5')
98 self.db.status.history('1')
99 self.db.status.history('2')
101 def testTransactions(self):
102 # remember the number of items we started
103 num_issues = len(self.db.issue.list())
104 num_files = self.db.numfiles()
105 self.db.issue.create(title="don't commit me!", status='1')
106 self.assertNotEqual(num_issues, len(self.db.issue.list()))
107 self.db.rollback()
108 self.assertEqual(num_issues, len(self.db.issue.list()))
109 self.db.issue.create(title="please commit me!", status='1')
110 self.assertNotEqual(num_issues, len(self.db.issue.list()))
111 self.db.commit()
112 self.assertNotEqual(num_issues, len(self.db.issue.list()))
113 self.db.rollback()
114 self.assertNotEqual(num_issues, len(self.db.issue.list()))
115 self.db.file.create(name="test", type="text/plain", content="hi")
116 self.db.rollback()
117 self.assertEqual(num_files, self.db.numfiles())
118 for i in range(10):
119 self.db.file.create(name="test", type="text/plain",
120 content="hi %d"%(i))
121 self.db.commit()
122 num_files2 = self.db.numfiles()
123 self.assertNotEqual(num_files, num_files2)
124 self.db.file.create(name="test", type="text/plain", content="hi")
125 self.db.rollback()
126 self.assertNotEqual(num_files, self.db.numfiles())
127 self.assertEqual(num_files2, self.db.numfiles())
131 def testExceptions(self):
132 # this tests the exceptions that should be raised
133 ar = self.assertRaises
135 #
136 # class create
137 #
138 # string property
139 ar(TypeError, self.db.status.create, name=1)
140 # invalid property name
141 ar(KeyError, self.db.status.create, foo='foo')
142 # key name clash
143 ar(ValueError, self.db.status.create, name='unread')
144 # invalid link index
145 ar(IndexError, self.db.issue.create, title='foo', status='bar')
146 # invalid link value
147 ar(ValueError, self.db.issue.create, title='foo', status=1)
148 # invalid multilink type
149 ar(TypeError, self.db.issue.create, title='foo', status='1',
150 nosy='hello')
151 # invalid multilink index type
152 ar(ValueError, self.db.issue.create, title='foo', status='1',
153 nosy=[1])
154 # invalid multilink index
155 ar(IndexError, self.db.issue.create, title='foo', status='1',
156 nosy=['10'])
158 #
159 # class get
160 #
161 # invalid node id
162 ar(IndexError, self.db.status.get, '10', 'name')
163 # invalid property name
164 ar(KeyError, self.db.status.get, '2', 'foo')
166 #
167 # class set
168 #
169 # invalid node id
170 ar(IndexError, self.db.issue.set, '1', name='foo')
171 # invalid property name
172 ar(KeyError, self.db.status.set, '1', foo='foo')
173 # string property
174 ar(TypeError, self.db.status.set, '1', name=1)
175 # key name clash
176 ar(ValueError, self.db.status.set, '2', name='unread')
177 # set up a valid issue for me to work on
178 self.db.issue.create(title="spam", status='1')
179 # invalid link index
180 ar(IndexError, self.db.issue.set, '1', title='foo', status='bar')
181 # invalid link value
182 ar(ValueError, self.db.issue.set, '1', title='foo', status=1)
183 # invalid multilink type
184 ar(TypeError, self.db.issue.set, '1', title='foo', status='1',
185 nosy='hello')
186 # invalid multilink index type
187 ar(ValueError, self.db.issue.set, '1', title='foo', status='1',
188 nosy=[1])
189 # invalid multilink index
190 ar(IndexError, self.db.issue.set, '1', title='foo', status='1',
191 nosy=['10'])
193 def testJournals(self):
194 self.db.issue.addprop(fixer=Link("user", do_journal='yes'))
195 self.db.user.create(username="mary")
196 self.db.user.create(username="pete")
197 self.db.issue.create(title="spam", status='1')
198 self.db.commit()
200 # journal entry for issue create
201 journal = self.db.getjournal('issue', '1')
202 self.assertEqual(1, len(journal))
203 (nodeid, date_stamp, journaltag, action, params) = journal[0]
204 self.assertEqual(nodeid, '1')
205 self.assertEqual(journaltag, 'test')
206 self.assertEqual(action, 'create')
207 keys = params.keys()
208 keys.sort()
209 self.assertEqual(keys, ['deadline', 'fixer', 'foo', 'nosy',
210 'status', 'title'])
211 self.assertEqual(None,params['deadline'])
212 self.assertEqual(None,params['fixer'])
213 self.assertEqual(None,params['foo'])
214 self.assertEqual([],params['nosy'])
215 self.assertEqual('1',params['status'])
216 self.assertEqual('spam',params['title'])
218 # journal entry for link
219 journal = self.db.getjournal('user', '1')
220 self.assertEqual(1, len(journal))
221 self.db.issue.set('1', fixer='1')
222 self.db.commit()
223 journal = self.db.getjournal('user', '1')
224 self.assertEqual(2, len(journal))
225 (nodeid, date_stamp, journaltag, action, params) = journal[1]
226 self.assertEqual('1', nodeid)
227 self.assertEqual('test', journaltag)
228 self.assertEqual('link', action)
229 self.assertEqual(('issue', '1', 'fixer'), params)
231 # journal entry for unlink
232 self.db.issue.set('1', fixer='2')
233 self.db.commit()
234 journal = self.db.getjournal('user', '1')
235 self.assertEqual(3, len(journal))
236 (nodeid, date_stamp, journaltag, action, params) = journal[2]
237 self.assertEqual('1', nodeid)
238 self.assertEqual('test', journaltag)
239 self.assertEqual('unlink', action)
240 self.assertEqual(('issue', '1', 'fixer'), params)
242 def testPack(self):
243 self.db.issue.create(title="spam", status='1')
244 self.db.commit()
245 self.db.issue.set('1', status='2')
246 self.db.commit()
247 self.db.issue.set('1', status='3')
248 self.db.commit()
249 pack_before = date.Date(". + 1d")
250 self.db.pack(pack_before)
251 journal = self.db.getjournal('issue', '1')
252 self.assertEqual(2, len(journal))
254 def testRetire(self):
255 pass
258 class anydbmReadOnlyDBTestCase(MyTestCase):
259 def setUp(self):
260 from roundup.backends import anydbm
261 # remove previous test, ignore errors
262 if os.path.exists(config.DATABASE):
263 shutil.rmtree(config.DATABASE)
264 os.makedirs(config.DATABASE + '/files')
265 db = anydbm.Database(config, 'test')
266 setupSchema(db, 1)
267 self.db = anydbm.Database(config)
268 setupSchema(self.db, 0)
270 def testExceptions(self):
271 # this tests the exceptions that should be raised
272 ar = self.assertRaises
274 # this tests the exceptions that should be raised
275 ar(DatabaseError, self.db.status.create, name="foo")
276 ar(DatabaseError, self.db.status.set, '1', name="foo")
277 ar(DatabaseError, self.db.status.retire, '1')
280 class bsddbDBTestCase(anydbmDBTestCase):
281 def setUp(self):
282 from roundup.backends import bsddb
283 # remove previous test, ignore errors
284 if os.path.exists(config.DATABASE):
285 shutil.rmtree(config.DATABASE)
286 os.makedirs(config.DATABASE + '/files')
287 self.db = bsddb.Database(config, 'test')
288 setupSchema(self.db, 1)
290 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
291 def setUp(self):
292 from roundup.backends import bsddb
293 # remove previous test, ignore errors
294 if os.path.exists(config.DATABASE):
295 shutil.rmtree(config.DATABASE)
296 os.makedirs(config.DATABASE + '/files')
297 db = bsddb.Database(config, 'test')
298 setupSchema(db, 1)
299 self.db = bsddb.Database(config)
300 setupSchema(self.db, 0)
303 class bsddb3DBTestCase(anydbmDBTestCase):
304 def setUp(self):
305 from roundup.backends import bsddb3
306 # remove previous test, ignore errors
307 if os.path.exists(config.DATABASE):
308 shutil.rmtree(config.DATABASE)
309 os.makedirs(config.DATABASE + '/files')
310 self.db = bsddb3.Database(config, 'test')
311 setupSchema(self.db, 1)
313 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
314 def setUp(self):
315 from roundup.backends import bsddb3
316 # remove previous test, ignore errors
317 if os.path.exists(config.DATABASE):
318 shutil.rmtree(config.DATABASE)
319 os.makedirs(config.DATABASE + '/files')
320 db = bsddb3.Database(config, 'test')
321 setupSchema(db, 1)
322 self.db = bsddb3.Database(config)
323 setupSchema(self.db, 0)
326 def suite():
327 l = [unittest.makeSuite(anydbmDBTestCase, 'test'),
328 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
329 ]
331 try:
332 import bsddb
333 l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
334 l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))
335 except:
336 print 'bsddb module not found, skipping bsddb DBTestCase'
338 # try:
339 # import bsddb3
340 # l.append(unittest.makeSuite(bsddb3DBTestCase, 'test'))
341 # l.append(unittest.makeSuite(bsddb3ReadOnlyDBTestCase, 'test'))
342 # except:
343 # print 'bsddb3 module not found, skipping bsddb3 DBTestCase'
345 return unittest.TestSuite(l)
347 #
348 # $Log: not supported by cvs2svn $
349 # Revision 1.18 2002/01/22 07:21:13 richard
350 # . fixed back_bsddb so it passed the journal tests
351 #
352 # ... it didn't seem happy using the back_anydbm _open method, which is odd.
353 # Yet another occurrance of whichdb not being able to recognise older bsddb
354 # databases. Yadda yadda. Made the HYPERDBDEBUG stuff more sane in the
355 # process.
356 #
357 # Revision 1.17 2002/01/22 05:06:09 rochecompaan
358 # We need to keep the last 'set' entry in the journal to preserve
359 # information on 'activity' for nodes.
360 #
361 # Revision 1.16 2002/01/21 16:33:20 rochecompaan
362 # You can now use the roundup-admin tool to pack the database
363 #
364 # Revision 1.15 2002/01/19 13:16:04 rochecompaan
365 # Journal entries for link and multilink properties can now be switched on
366 # or off.
367 #
368 # Revision 1.14 2002/01/16 07:02:57 richard
369 # . lots of date/interval related changes:
370 # - more relaxed date format for input
371 #
372 # Revision 1.13 2002/01/14 02:20:15 richard
373 # . changed all config accesses so they access either the instance or the
374 # config attriubute on the db. This means that all config is obtained from
375 # instance_config instead of the mish-mash of classes. This will make
376 # switching to a ConfigParser setup easier too, I hope.
377 #
378 # At a minimum, this makes migration a _little_ easier (a lot easier in the
379 # 0.5.0 switch, I hope!)
380 #
381 # Revision 1.12 2001/12/17 03:52:48 richard
382 # Implemented file store rollback. As a bonus, the hyperdb is now capable of
383 # storing more than one file per node - if a property name is supplied,
384 # the file is called designator.property.
385 # I decided not to migrate the existing files stored over to the new naming
386 # scheme - the FileClass just doesn't specify the property name.
387 #
388 # Revision 1.11 2001/12/10 23:17:20 richard
389 # Added transaction tests to test_db
390 #
391 # Revision 1.10 2001/12/03 21:33:39 richard
392 # Fixes so the tests use commit and not close
393 #
394 # Revision 1.9 2001/12/02 05:06:16 richard
395 # . We now use weakrefs in the Classes to keep the database reference, so
396 # the close() method on the database is no longer needed.
397 # I bumped the minimum python requirement up to 2.1 accordingly.
398 # . #487480 ] roundup-server
399 # . #487476 ] INSTALL.txt
400 #
401 # I also cleaned up the change message / post-edit stuff in the cgi client.
402 # There's now a clearly marked "TODO: append the change note" where I believe
403 # the change note should be added there. The "changes" list will obviously
404 # have to be modified to be a dict of the changes, or somesuch.
405 #
406 # More testing needed.
407 #
408 # Revision 1.8 2001/10/09 07:25:59 richard
409 # Added the Password property type. See "pydoc roundup.password" for
410 # implementation details. Have updated some of the documentation too.
411 #
412 # Revision 1.7 2001/08/29 06:23:59 richard
413 # Disabled the bsddb3 module entirely in the unit testing. See CHANGES for
414 # details.
415 #
416 # Revision 1.6 2001/08/07 00:24:43 richard
417 # stupid typo
418 #
419 # Revision 1.5 2001/08/07 00:15:51 richard
420 # Added the copyright/license notice to (nearly) all files at request of
421 # Bizar Software.
422 #
423 # Revision 1.4 2001/07/30 03:45:56 richard
424 # Added more DB to test_db. Can skip tests where imports fail.
425 #
426 # Revision 1.3 2001/07/29 07:01:39 richard
427 # Added vim command to all source so that we don't get no steenkin' tabs :)
428 #
429 # Revision 1.2 2001/07/29 04:09:20 richard
430 # Added the fabricated property "id" to all hyperdb classes.
431 #
432 # Revision 1.1 2001/07/27 06:55:07 richard
433 # moving tests -> test
434 #
435 # Revision 1.7 2001/07/27 06:26:43 richard
436 # oops - wasn't deleting the test dir after the read-only tests
437 #
438 # Revision 1.6 2001/07/27 06:23:59 richard
439 # consistency
440 #
441 # Revision 1.5 2001/07/27 06:23:09 richard
442 # Added some new hyperdb tests to make sure we raise the right exceptions.
443 #
444 # Revision 1.4 2001/07/25 04:34:31 richard
445 # Added id and log to tests files...
446 #
447 #
448 # vim: set filetype=python ts=4 sw=4 et si