c1bc92d589b1b7d36db6cd10bfce7429501075bc
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.23 2002-06-20 23:51:48 richard 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, password
27 def setupSchema(db, create):
28 status = Class(db, "status", name=String())
29 status.setkey("name")
30 user = Class(db, "user", username=String(), password=Password())
31 file = FileClass(db, "file", name=String(), type=String())
32 issue = Class(db, "issue", title=String(), status=Link("status"),
33 nosy=Multilink("user"), deadline=Date(), foo=Interval())
34 if create:
35 status.create(name="unread")
36 status.create(name="in-progress")
37 status.create(name="testing")
38 status.create(name="resolved")
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)
69 self.db2 = anydbm.Database(config, 'test')
70 setupSchema(self.db2, 0)
72 def testStringChange(self):
73 self.db.issue.create(title="spam", status='1')
74 self.assertEqual(self.db.issue.get('1', 'title'), 'spam')
75 self.db.issue.set('1', title='eggs')
76 self.assertEqual(self.db.issue.get('1', 'title'), 'eggs')
77 self.db.commit()
78 self.assertEqual(self.db.issue.get('1', 'title'), 'eggs')
79 self.db.issue.create(title="spam", status='1')
80 self.db.commit()
81 self.assertEqual(self.db.issue.get('2', 'title'), 'spam')
82 self.db.issue.set('2', title='ham')
83 self.assertEqual(self.db.issue.get('2', 'title'), 'ham')
84 self.db.commit()
85 self.assertEqual(self.db.issue.get('2', 'title'), 'ham')
87 def testLinkChange(self):
88 self.db.issue.create(title="spam", status='1')
89 self.assertEqual(self.db.issue.get('1', "status"), '1')
90 self.db.issue.set('1', status='2')
91 self.assertEqual(self.db.issue.get('1', "status"), '2')
93 def testDateChange(self):
94 self.db.issue.create(title="spam", status='1')
95 a = self.db.issue.get('1', "deadline")
96 self.db.issue.set('1', deadline=date.Date())
97 b = self.db.issue.get('1', "deadline")
98 self.db.commit()
99 self.assertNotEqual(a, b)
100 self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
101 self.db.issue.set('1', deadline=date.Date())
103 def testIntervalChange(self):
104 self.db.issue.create(title="spam", status='1')
105 a = self.db.issue.get('1', "foo")
106 self.db.issue.set('1', foo=date.Interval('-1d'))
107 self.assertNotEqual(self.db.issue.get('1', "foo"), a)
109 def testNewProperty(self):
110 self.db.issue.create(title="spam", status='1')
111 self.db.issue.addprop(fixer=Link("user"))
112 props = self.db.issue.getprops()
113 keys = props.keys()
114 keys.sort()
115 self.assertEqual(keys, ['deadline', 'fixer', 'foo', 'id', 'nosy',
116 'status', 'title'])
117 self.assertEqual(self.db.issue.get('1', "fixer"), None)
119 def testRetire(self):
120 self.db.issue.create(title="spam", status='1')
121 b = self.db.status.get('1', 'name')
122 a = self.db.status.list()
123 self.db.status.retire('1')
124 # make sure the list is different
125 self.assertNotEqual(a, self.db.status.list())
126 # can still access the node if necessary
127 self.assertEqual(self.db.status.get('1', 'name'), b)
128 self.db.commit()
129 self.assertEqual(self.db.status.get('1', 'name'), b)
130 self.assertNotEqual(a, self.db.status.list())
132 def testSerialisation(self):
133 self.db.issue.create(title="spam", status='1',
134 deadline=date.Date(), foo=date.Interval('-1d'))
135 self.db.commit()
136 assert isinstance(self.db.issue.get('1', 'deadline'), date.Date)
137 assert isinstance(self.db.issue.get('1', 'foo'), date.Interval)
138 self.db.user.create(username="fozzy",
139 password=password.Password('t. bear'))
140 self.db.commit()
141 assert isinstance(self.db.user.get('1', 'password'), password.Password)
143 def testTransactions(self):
144 # remember the number of items we started
145 num_issues = len(self.db.issue.list())
146 num_files = self.db.numfiles()
147 self.db.issue.create(title="don't commit me!", status='1')
148 self.assertNotEqual(num_issues, len(self.db.issue.list()))
149 self.db.rollback()
150 self.assertEqual(num_issues, len(self.db.issue.list()))
151 self.db.issue.create(title="please commit me!", status='1')
152 self.assertNotEqual(num_issues, len(self.db.issue.list()))
153 self.db.commit()
154 self.assertNotEqual(num_issues, len(self.db.issue.list()))
155 self.db.rollback()
156 self.assertNotEqual(num_issues, len(self.db.issue.list()))
157 self.db.file.create(name="test", type="text/plain", content="hi")
158 self.db.rollback()
159 self.assertEqual(num_files, self.db.numfiles())
160 for i in range(10):
161 self.db.file.create(name="test", type="text/plain",
162 content="hi %d"%(i))
163 self.db.commit()
164 num_files2 = self.db.numfiles()
165 self.assertNotEqual(num_files, num_files2)
166 self.db.file.create(name="test", type="text/plain", content="hi")
167 self.db.rollback()
168 self.assertNotEqual(num_files, self.db.numfiles())
169 self.assertEqual(num_files2, self.db.numfiles())
171 def testExceptions(self):
172 # this tests the exceptions that should be raised
173 ar = self.assertRaises
175 #
176 # class create
177 #
178 # string property
179 ar(TypeError, self.db.status.create, name=1)
180 # invalid property name
181 ar(KeyError, self.db.status.create, foo='foo')
182 # key name clash
183 ar(ValueError, self.db.status.create, name='unread')
184 # invalid link index
185 ar(IndexError, self.db.issue.create, title='foo', status='bar')
186 # invalid link value
187 ar(ValueError, self.db.issue.create, title='foo', status=1)
188 # invalid multilink type
189 ar(TypeError, self.db.issue.create, title='foo', status='1',
190 nosy='hello')
191 # invalid multilink index type
192 ar(ValueError, self.db.issue.create, title='foo', status='1',
193 nosy=[1])
194 # invalid multilink index
195 ar(IndexError, self.db.issue.create, title='foo', status='1',
196 nosy=['10'])
198 #
199 # class get
200 #
201 # invalid node id
202 ar(IndexError, self.db.status.get, '10', 'name')
203 # invalid property name
204 ar(KeyError, self.db.status.get, '2', 'foo')
206 #
207 # class set
208 #
209 # invalid node id
210 ar(IndexError, self.db.issue.set, '1', name='foo')
211 # invalid property name
212 ar(KeyError, self.db.status.set, '1', foo='foo')
213 # string property
214 ar(TypeError, self.db.status.set, '1', name=1)
215 # key name clash
216 ar(ValueError, self.db.status.set, '2', name='unread')
217 # set up a valid issue for me to work on
218 self.db.issue.create(title="spam", status='1')
219 # invalid link index
220 ar(IndexError, self.db.issue.set, '6', title='foo', status='bar')
221 # invalid link value
222 ar(ValueError, self.db.issue.set, '6', title='foo', status=1)
223 # invalid multilink type
224 ar(TypeError, self.db.issue.set, '6', title='foo', status='1',
225 nosy='hello')
226 # invalid multilink index type
227 ar(ValueError, self.db.issue.set, '6', title='foo', status='1',
228 nosy=[1])
229 # invalid multilink index
230 ar(IndexError, self.db.issue.set, '6', title='foo', status='1',
231 nosy=['10'])
233 def testJournals(self):
234 self.db.issue.addprop(fixer=Link("user", do_journal='yes'))
235 self.db.user.create(username="mary")
236 self.db.user.create(username="pete")
237 self.db.issue.create(title="spam", status='1')
238 self.db.commit()
240 # journal entry for issue create
241 journal = self.db.getjournal('issue', '1')
242 self.assertEqual(1, len(journal))
243 (nodeid, date_stamp, journaltag, action, params) = journal[0]
244 self.assertEqual(nodeid, '1')
245 self.assertEqual(journaltag, 'test')
246 self.assertEqual(action, 'create')
247 keys = params.keys()
248 keys.sort()
249 self.assertEqual(keys, ['deadline', 'fixer', 'foo', 'nosy',
250 'status', 'title'])
251 self.assertEqual(None,params['deadline'])
252 self.assertEqual(None,params['fixer'])
253 self.assertEqual(None,params['foo'])
254 self.assertEqual([],params['nosy'])
255 self.assertEqual('1',params['status'])
256 self.assertEqual('spam',params['title'])
258 # journal entry for link
259 journal = self.db.getjournal('user', '1')
260 self.assertEqual(1, len(journal))
261 self.db.issue.set('1', fixer='1')
262 self.db.commit()
263 journal = self.db.getjournal('user', '1')
264 self.assertEqual(2, len(journal))
265 (nodeid, date_stamp, journaltag, action, params) = journal[1]
266 self.assertEqual('1', nodeid)
267 self.assertEqual('test', journaltag)
268 self.assertEqual('link', action)
269 self.assertEqual(('issue', '1', 'fixer'), params)
271 # journal entry for unlink
272 self.db.issue.set('1', fixer='2')
273 self.db.commit()
274 journal = self.db.getjournal('user', '1')
275 self.assertEqual(3, len(journal))
276 (nodeid, date_stamp, journaltag, action, params) = journal[2]
277 self.assertEqual('1', nodeid)
278 self.assertEqual('test', journaltag)
279 self.assertEqual('unlink', action)
280 self.assertEqual(('issue', '1', 'fixer'), params)
282 def testPack(self):
283 self.db.issue.create(title="spam", status='1')
284 self.db.commit()
285 self.db.issue.set('1', status='2')
286 self.db.commit()
287 self.db.issue.set('1', status='3')
288 self.db.commit()
289 pack_before = date.Date(". + 1d")
290 self.db.pack(pack_before)
291 journal = self.db.getjournal('issue', '1')
292 self.assertEqual(2, len(journal))
294 def testIDGeneration(self):
295 id1 = self.db.issue.create(title="spam", status='1')
296 id2 = self.db2.issue.create(title="eggs", status='2')
297 self.assertNotEqual(id1, id2)
300 class anydbmReadOnlyDBTestCase(MyTestCase):
301 def setUp(self):
302 from roundup.backends import anydbm
303 # remove previous test, ignore errors
304 if os.path.exists(config.DATABASE):
305 shutil.rmtree(config.DATABASE)
306 os.makedirs(config.DATABASE + '/files')
307 db = anydbm.Database(config, 'test')
308 setupSchema(db, 1)
309 self.db = anydbm.Database(config)
310 setupSchema(self.db, 0)
311 self.db2 = anydbm.Database(config, 'test')
312 setupSchema(self.db2, 0)
314 def testExceptions(self):
315 # this tests the exceptions that should be raised
316 ar = self.assertRaises
318 # this tests the exceptions that should be raised
319 ar(DatabaseError, self.db.status.create, name="foo")
320 ar(DatabaseError, self.db.status.set, '1', name="foo")
321 ar(DatabaseError, self.db.status.retire, '1')
324 class bsddbDBTestCase(anydbmDBTestCase):
325 def setUp(self):
326 from roundup.backends import bsddb
327 # remove previous test, ignore errors
328 if os.path.exists(config.DATABASE):
329 shutil.rmtree(config.DATABASE)
330 os.makedirs(config.DATABASE + '/files')
331 self.db = bsddb.Database(config, 'test')
332 setupSchema(self.db, 1)
333 self.db2 = bsddb.Database(config, 'test')
334 setupSchema(self.db2, 0)
336 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
337 def setUp(self):
338 from roundup.backends import bsddb
339 # remove previous test, ignore errors
340 if os.path.exists(config.DATABASE):
341 shutil.rmtree(config.DATABASE)
342 os.makedirs(config.DATABASE + '/files')
343 db = bsddb.Database(config, 'test')
344 setupSchema(db, 1)
345 self.db = bsddb.Database(config)
346 setupSchema(self.db, 0)
347 self.db2 = bsddb.Database(config, 'test')
348 setupSchema(self.db2, 0)
351 class bsddb3DBTestCase(anydbmDBTestCase):
352 def setUp(self):
353 from roundup.backends import bsddb3
354 # remove previous test, ignore errors
355 if os.path.exists(config.DATABASE):
356 shutil.rmtree(config.DATABASE)
357 os.makedirs(config.DATABASE + '/files')
358 self.db = bsddb3.Database(config, 'test')
359 setupSchema(self.db, 1)
360 self.db2 = bsddb3.Database(config, 'test')
361 setupSchema(self.db2, 0)
363 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
364 def setUp(self):
365 from roundup.backends import bsddb3
366 # remove previous test, ignore errors
367 if os.path.exists(config.DATABASE):
368 shutil.rmtree(config.DATABASE)
369 os.makedirs(config.DATABASE + '/files')
370 db = bsddb3.Database(config, 'test')
371 setupSchema(db, 1)
372 self.db = bsddb3.Database(config)
373 setupSchema(self.db, 0)
374 self.db2 = bsddb3.Database(config, 'test')
375 setupSchema(self.db2, 0)
378 def suite():
379 l = [
380 unittest.makeSuite(anydbmDBTestCase, 'test'),
381 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
382 ]
384 try:
385 import bsddb
386 l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
387 l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))
388 except:
389 print 'bsddb module not found, skipping bsddb DBTestCase'
391 try:
392 import bsddb3
393 l.append(unittest.makeSuite(bsddb3DBTestCase, 'test'))
394 l.append(unittest.makeSuite(bsddb3ReadOnlyDBTestCase, 'test'))
395 except:
396 print 'bsddb3 module not found, skipping bsddb3 DBTestCase'
398 return unittest.TestSuite(l)
400 #
401 # $Log: not supported by cvs2svn $
402 # Revision 1.22 2002/05/21 05:52:11 richard
403 # Well whadya know, bsddb3 works again.
404 # The backend is implemented _exactly_ the same as bsddb - so there's no
405 # using its transaction or locking support. It'd be nice to use those some
406 # day I suppose.
407 #
408 # Revision 1.21 2002/04/15 23:25:15 richard
409 # . node ids are now generated from a lockable store - no more race conditions
410 #
411 # We're using the portalocker code by Jonathan Feinberg that was contributed
412 # to the ASPN Python cookbook. This gives us locking across Unix and Windows.
413 #
414 # Revision 1.20 2002/04/03 05:54:31 richard
415 # Fixed serialisation problem by moving the serialisation step out of the
416 # hyperdb.Class (get, set) into the hyperdb.Database.
417 #
418 # Also fixed htmltemplate after the showid changes I made yesterday.
419 #
420 # Unit tests for all of the above written.
421 #
422 # Revision 1.19 2002/02/25 14:34:31 grubert
423 # . use blobfiles in back_anydbm which is used in back_bsddb.
424 # change test_db as dirlist does not work for subdirectories.
425 # ATTENTION: blobfiles now creates subdirectories for files.
426 #
427 # Revision 1.18 2002/01/22 07:21:13 richard
428 # . fixed back_bsddb so it passed the journal tests
429 #
430 # ... it didn't seem happy using the back_anydbm _open method, which is odd.
431 # Yet another occurrance of whichdb not being able to recognise older bsddb
432 # databases. Yadda yadda. Made the HYPERDBDEBUG stuff more sane in the
433 # process.
434 #
435 # Revision 1.17 2002/01/22 05:06:09 rochecompaan
436 # We need to keep the last 'set' entry in the journal to preserve
437 # information on 'activity' for nodes.
438 #
439 # Revision 1.16 2002/01/21 16:33:20 rochecompaan
440 # You can now use the roundup-admin tool to pack the database
441 #
442 # Revision 1.15 2002/01/19 13:16:04 rochecompaan
443 # Journal entries for link and multilink properties can now be switched on
444 # or off.
445 #
446 # Revision 1.14 2002/01/16 07:02:57 richard
447 # . lots of date/interval related changes:
448 # - more relaxed date format for input
449 #
450 # Revision 1.13 2002/01/14 02:20:15 richard
451 # . changed all config accesses so they access either the instance or the
452 # config attriubute on the db. This means that all config is obtained from
453 # instance_config instead of the mish-mash of classes. This will make
454 # switching to a ConfigParser setup easier too, I hope.
455 #
456 # At a minimum, this makes migration a _little_ easier (a lot easier in the
457 # 0.5.0 switch, I hope!)
458 #
459 # Revision 1.12 2001/12/17 03:52:48 richard
460 # Implemented file store rollback. As a bonus, the hyperdb is now capable of
461 # storing more than one file per node - if a property name is supplied,
462 # the file is called designator.property.
463 # I decided not to migrate the existing files stored over to the new naming
464 # scheme - the FileClass just doesn't specify the property name.
465 #
466 # Revision 1.11 2001/12/10 23:17:20 richard
467 # Added transaction tests to test_db
468 #
469 # Revision 1.10 2001/12/03 21:33:39 richard
470 # Fixes so the tests use commit and not close
471 #
472 # Revision 1.9 2001/12/02 05:06:16 richard
473 # . We now use weakrefs in the Classes to keep the database reference, so
474 # the close() method on the database is no longer needed.
475 # I bumped the minimum python requirement up to 2.1 accordingly.
476 # . #487480 ] roundup-server
477 # . #487476 ] INSTALL.txt
478 #
479 # I also cleaned up the change message / post-edit stuff in the cgi client.
480 # There's now a clearly marked "TODO: append the change note" where I believe
481 # the change note should be added there. The "changes" list will obviously
482 # have to be modified to be a dict of the changes, or somesuch.
483 #
484 # More testing needed.
485 #
486 # Revision 1.8 2001/10/09 07:25:59 richard
487 # Added the Password property type. See "pydoc roundup.password" for
488 # implementation details. Have updated some of the documentation too.
489 #
490 # Revision 1.7 2001/08/29 06:23:59 richard
491 # Disabled the bsddb3 module entirely in the unit testing. See CHANGES for
492 # details.
493 #
494 # Revision 1.6 2001/08/07 00:24:43 richard
495 # stupid typo
496 #
497 # Revision 1.5 2001/08/07 00:15:51 richard
498 # Added the copyright/license notice to (nearly) all files at request of
499 # Bizar Software.
500 #
501 # Revision 1.4 2001/07/30 03:45:56 richard
502 # Added more DB to test_db. Can skip tests where imports fail.
503 #
504 # Revision 1.3 2001/07/29 07:01:39 richard
505 # Added vim command to all source so that we don't get no steenkin' tabs :)
506 #
507 # Revision 1.2 2001/07/29 04:09:20 richard
508 # Added the fabricated property "id" to all hyperdb classes.
509 #
510 # Revision 1.1 2001/07/27 06:55:07 richard
511 # moving tests -> test
512 #
513 # Revision 1.7 2001/07/27 06:26:43 richard
514 # oops - wasn't deleting the test dir after the read-only tests
515 #
516 # Revision 1.6 2001/07/27 06:23:59 richard
517 # consistency
518 #
519 # Revision 1.5 2001/07/27 06:23:09 richard
520 # Added some new hyperdb tests to make sure we raise the right exceptions.
521 #
522 # Revision 1.4 2001/07/25 04:34:31 richard
523 # Added id and log to tests files...
524 #
525 #
526 # vim: set filetype=python ts=4 sw=4 et si