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