33bc69caee2492bd67798394328bb1cb8f4bccae
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.53 2002-09-20 19:26:28 gmcm Exp $
20 import unittest, os, shutil, time
22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \
23 Interval, DatabaseError, Boolean, Number
24 from roundup import date, password
25 from roundup.indexer import Indexer
27 def setupSchema(db, create, module):
28 status = module.Class(db, "status", name=String())
29 status.setkey("name")
30 user = module.Class(db, "user", username=String(), password=Password(),
31 assignable=Boolean(), age=Number(), roles=String())
32 user.setkey("username")
33 file = module.FileClass(db, "file", name=String(), type=String(),
34 comment=String(indexme="yes"), fooz=Password())
35 issue = module.IssueClass(db, "issue", title=String(indexme="yes"),
36 status=Link("status"), nosy=Multilink("user"), deadline=Date(),
37 foo=Interval(), files=Multilink("file"), assignedto=Link('user'))
38 session = module.Class(db, 'session', title=String())
39 session.disableJournalling()
40 db.post_init()
41 if create:
42 user.create(username="admin", roles='Admin')
43 status.create(name="unread")
44 status.create(name="in-progress")
45 status.create(name="testing")
46 status.create(name="resolved")
47 db.commit()
49 class MyTestCase(unittest.TestCase):
50 def tearDown(self):
51 self.db.close()
52 if hasattr(self, 'db2'):
53 self.db2.close()
54 if os.path.exists('_test_dir'):
55 shutil.rmtree('_test_dir')
57 class config:
58 DATABASE='_test_dir'
59 MAILHOST = 'localhost'
60 MAIL_DOMAIN = 'fill.me.in.'
61 TRACKER_NAME = 'Roundup issue tracker'
62 TRACKER_EMAIL = 'issue_tracker@%s'%MAIL_DOMAIN
63 TRACKER_WEB = 'http://some.useful.url/'
64 ADMIN_EMAIL = 'roundup-admin@%s'%MAIL_DOMAIN
65 FILTER_POSITION = 'bottom' # one of 'top', 'bottom', 'top and bottom'
66 ANONYMOUS_ACCESS = 'deny' # either 'deny' or 'allow'
67 ANONYMOUS_REGISTER = 'deny' # either 'deny' or 'allow'
68 MESSAGES_TO_AUTHOR = 'no' # either 'yes' or 'no'
69 EMAIL_SIGNATURE_POSITION = 'bottom'
71 class anydbmDBTestCase(MyTestCase):
72 def setUp(self):
73 from roundup.backends import anydbm
74 # remove previous test, ignore errors
75 if os.path.exists(config.DATABASE):
76 shutil.rmtree(config.DATABASE)
77 os.makedirs(config.DATABASE + '/files')
78 self.db = anydbm.Database(config, 'admin')
79 setupSchema(self.db, 1, anydbm)
80 self.db2 = anydbm.Database(config, 'admin')
81 setupSchema(self.db2, 0, anydbm)
83 def testStringChange(self):
84 # test set & retrieve
85 self.db.issue.create(title="spam", status='1')
86 self.assertEqual(self.db.issue.get('1', 'title'), 'spam')
88 # change and make sure we retrieve the correct value
89 self.db.issue.set('1', title='eggs')
90 self.assertEqual(self.db.issue.get('1', 'title'), 'eggs')
92 # do some commit stuff
93 self.db.commit()
94 self.assertEqual(self.db.issue.get('1', 'title'), 'eggs')
95 self.db.issue.create(title="spam", status='1')
96 self.db.commit()
97 self.assertEqual(self.db.issue.get('2', 'title'), 'spam')
98 self.db.issue.set('2', title='ham')
99 self.assertEqual(self.db.issue.get('2', 'title'), 'ham')
100 self.db.commit()
101 self.assertEqual(self.db.issue.get('2', 'title'), 'ham')
103 # make sure we can unset
104 self.db.issue.set('1', title=None)
105 self.assertEqual(self.db.issue.get('1', "title"), None)
107 def testLinkChange(self):
108 self.db.issue.create(title="spam", status='1')
109 self.assertEqual(self.db.issue.get('1', "status"), '1')
110 self.db.issue.set('1', status='2')
111 self.assertEqual(self.db.issue.get('1', "status"), '2')
112 self.db.issue.set('1', status=None)
113 self.assertEqual(self.db.issue.get('1', "status"), None)
115 def testMultilinkChange(self):
116 u1 = self.db.user.create(username='foo')
117 u2 = self.db.user.create(username='bar')
118 self.db.issue.create(title="spam", nosy=[u1])
119 self.assertEqual(self.db.issue.get('1', "nosy"), [u1])
120 self.db.issue.set('1', nosy=[])
121 self.assertEqual(self.db.issue.get('1', "nosy"), [])
122 self.db.issue.set('1', nosy=[u1,u2])
123 self.assertEqual(self.db.issue.get('1', "nosy"), [u1,u2])
125 def testDateChange(self):
126 self.db.issue.create(title="spam", status='1')
127 a = self.db.issue.get('1', "deadline")
128 self.db.issue.set('1', deadline=date.Date())
129 b = self.db.issue.get('1', "deadline")
130 self.db.commit()
131 self.assertNotEqual(a, b)
132 self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
133 self.db.issue.set('1', deadline=date.Date())
134 self.db.issue.set('1', deadline=None)
135 self.assertEqual(self.db.issue.get('1', "deadline"), None)
137 def testIntervalChange(self):
138 self.db.issue.create(title="spam", status='1')
139 a = self.db.issue.get('1', "foo")
140 self.db.issue.set('1', foo=date.Interval('-1d'))
141 self.assertNotEqual(self.db.issue.get('1', "foo"), a)
142 self.db.issue.set('1', foo=None)
143 self.assertEqual(self.db.issue.get('1', "foo"), None)
145 def testBooleanChange(self):
146 userid = self.db.user.create(username='foo', assignable=1)
147 self.assertEqual(1, self.db.user.get(userid, 'assignable'))
148 self.db.user.set(userid, assignable=0)
149 self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
150 self.db.user.set(userid, assignable=None)
151 self.assertEqual(self.db.user.get('1', "assignable"), None)
153 def testNumberChange(self):
154 nid = self.db.user.create(username='foo', age=1)
155 self.assertEqual(1, self.db.user.get(nid, 'age'))
156 self.db.user.set('1', age=3)
157 self.assertNotEqual(self.db.user.get('1', 'age'), 1)
158 self.db.user.set('1', age=1.0)
159 self.db.user.set('1', age=None)
160 self.assertEqual(self.db.user.get('1', "age"), None)
162 def testKeyValue(self):
163 newid = self.db.user.create(username="spam")
164 self.assertEqual(self.db.user.lookup('spam'), newid)
165 self.db.commit()
166 self.assertEqual(self.db.user.lookup('spam'), newid)
167 self.db.user.retire(newid)
168 self.assertRaises(KeyError, self.db.user.lookup, 'spam')
170 def testNewProperty(self):
171 self.db.issue.create(title="spam", status='1')
172 self.db.issue.addprop(fixer=Link("user"))
173 # force any post-init stuff to happen
174 self.db.post_init()
175 props = self.db.issue.getprops()
176 keys = props.keys()
177 keys.sort()
178 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
179 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
180 'nosy', 'status', 'superseder', 'title'])
181 self.assertEqual(self.db.issue.get('1', "fixer"), None)
183 def testRetire(self):
184 self.db.issue.create(title="spam", status='1')
185 b = self.db.status.get('1', 'name')
186 a = self.db.status.list()
187 self.db.status.retire('1')
188 # make sure the list is different
189 self.assertNotEqual(a, self.db.status.list())
190 # can still access the node if necessary
191 self.assertEqual(self.db.status.get('1', 'name'), b)
192 self.db.commit()
193 self.assertEqual(self.db.status.get('1', 'name'), b)
194 self.assertNotEqual(a, self.db.status.list())
196 def testSerialisation(self):
197 self.db.issue.create(title="spam", status='1',
198 deadline=date.Date(), foo=date.Interval('-1d'))
199 self.db.commit()
200 assert isinstance(self.db.issue.get('1', 'deadline'), date.Date)
201 assert isinstance(self.db.issue.get('1', 'foo'), date.Interval)
202 self.db.user.create(username="fozzy",
203 password=password.Password('t. bear'))
204 self.db.commit()
205 assert isinstance(self.db.user.get('1', 'password'), password.Password)
207 def testTransactions(self):
208 # remember the number of items we started
209 num_issues = len(self.db.issue.list())
210 num_files = self.db.numfiles()
211 self.db.issue.create(title="don't commit me!", status='1')
212 self.assertNotEqual(num_issues, len(self.db.issue.list()))
213 self.db.rollback()
214 self.assertEqual(num_issues, len(self.db.issue.list()))
215 self.db.issue.create(title="please commit me!", status='1')
216 self.assertNotEqual(num_issues, len(self.db.issue.list()))
217 self.db.commit()
218 self.assertNotEqual(num_issues, len(self.db.issue.list()))
219 self.db.rollback()
220 self.assertNotEqual(num_issues, len(self.db.issue.list()))
221 self.db.file.create(name="test", type="text/plain", content="hi")
222 self.db.rollback()
223 self.assertEqual(num_files, self.db.numfiles())
224 for i in range(10):
225 self.db.file.create(name="test", type="text/plain",
226 content="hi %d"%(i))
227 self.db.commit()
228 num_files2 = self.db.numfiles()
229 self.assertNotEqual(num_files, num_files2)
230 self.db.file.create(name="test", type="text/plain", content="hi")
231 self.db.rollback()
232 self.assertNotEqual(num_files, self.db.numfiles())
233 self.assertEqual(num_files2, self.db.numfiles())
235 def testDestroyNoJournalling(self):
236 self.innerTestDestroy(klass=self.db.session)
238 def testDestroyJournalling(self):
239 self.innerTestDestroy(klass=self.db.issue)
241 def innerTestDestroy(self, klass):
242 newid = klass.create(title='Mr Friendly')
243 n = len(klass.list())
244 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
245 klass.destroy(newid)
246 self.assertRaises(IndexError, klass.get, newid, 'title')
247 self.assertNotEqual(len(klass.list()), n)
248 if klass.do_journal:
249 self.assertRaises(IndexError, klass.history, newid)
251 # now with a commit
252 newid = klass.create(title='Mr Friendly')
253 n = len(klass.list())
254 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
255 self.db.commit()
256 klass.destroy(newid)
257 self.assertRaises(IndexError, klass.get, newid, 'title')
258 self.db.commit()
259 self.assertRaises(IndexError, klass.get, newid, 'title')
260 self.assertNotEqual(len(klass.list()), n)
261 if klass.do_journal:
262 self.assertRaises(IndexError, klass.history, newid)
264 # now with a rollback
265 newid = klass.create(title='Mr Friendly')
266 n = len(klass.list())
267 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
268 self.db.commit()
269 klass.destroy(newid)
270 self.assertNotEqual(len(klass.list()), n)
271 self.assertRaises(IndexError, klass.get, newid, 'title')
272 self.db.rollback()
273 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
274 self.assertEqual(len(klass.list()), n)
275 if klass.do_journal:
276 self.assertNotEqual(klass.history(newid), [])
278 def testExceptions(self):
279 # this tests the exceptions that should be raised
280 ar = self.assertRaises
282 #
283 # class create
284 #
285 # string property
286 ar(TypeError, self.db.status.create, name=1)
287 # invalid property name
288 ar(KeyError, self.db.status.create, foo='foo')
289 # key name clash
290 ar(ValueError, self.db.status.create, name='unread')
291 # invalid link index
292 ar(IndexError, self.db.issue.create, title='foo', status='bar')
293 # invalid link value
294 ar(ValueError, self.db.issue.create, title='foo', status=1)
295 # invalid multilink type
296 ar(TypeError, self.db.issue.create, title='foo', status='1',
297 nosy='hello')
298 # invalid multilink index type
299 ar(ValueError, self.db.issue.create, title='foo', status='1',
300 nosy=[1])
301 # invalid multilink index
302 ar(IndexError, self.db.issue.create, title='foo', status='1',
303 nosy=['10'])
305 #
306 # key property
307 #
308 # key must be a String
309 ar(TypeError, self.db.file.setkey, 'fooz')
310 # key must exist
311 ar(KeyError, self.db.file.setkey, 'fubar')
313 #
314 # class get
315 #
316 # invalid node id
317 ar(IndexError, self.db.issue.get, '99', 'title')
318 # invalid property name
319 ar(KeyError, self.db.status.get, '2', 'foo')
321 #
322 # class set
323 #
324 # invalid node id
325 ar(IndexError, self.db.issue.set, '99', title='foo')
326 # invalid property name
327 ar(KeyError, self.db.status.set, '1', foo='foo')
328 # string property
329 ar(TypeError, self.db.status.set, '1', name=1)
330 # key name clash
331 ar(ValueError, self.db.status.set, '2', name='unread')
332 # set up a valid issue for me to work on
333 id = self.db.issue.create(title="spam", status='1')
334 # invalid link index
335 ar(IndexError, self.db.issue.set, id, title='foo', status='bar')
336 # invalid link value
337 ar(ValueError, self.db.issue.set, id, title='foo', status=1)
338 # invalid multilink type
339 ar(TypeError, self.db.issue.set, id, title='foo', status='1',
340 nosy='hello')
341 # invalid multilink index type
342 ar(ValueError, self.db.issue.set, id, title='foo', status='1',
343 nosy=[1])
344 # invalid multilink index
345 ar(IndexError, self.db.issue.set, id, title='foo', status='1',
346 nosy=['10'])
347 # invalid number value
348 ar(TypeError, self.db.user.create, username='foo', age='a')
349 # invalid boolean value
350 ar(TypeError, self.db.user.create, username='foo', assignable='true')
351 nid = self.db.user.create(username='foo')
352 # invalid number value
353 ar(TypeError, self.db.user.set, nid, username='foo', age='a')
354 # invalid boolean value
355 ar(TypeError, self.db.user.set, nid, username='foo', assignable='true')
357 def testJournals(self):
358 self.db.user.create(username="mary")
359 self.db.user.create(username="pete")
360 self.db.issue.create(title="spam", status='1')
361 self.db.commit()
363 # journal entry for issue create
364 journal = self.db.getjournal('issue', '1')
365 self.assertEqual(1, len(journal))
366 (nodeid, date_stamp, journaltag, action, params) = journal[0]
367 self.assertEqual(nodeid, '1')
368 self.assertEqual(journaltag, self.db.user.lookup('admin'))
369 self.assertEqual(action, 'create')
370 keys = params.keys()
371 keys.sort()
372 self.assertEqual(keys, ['assignedto', 'deadline', 'files',
373 'foo', 'messages', 'nosy', 'status', 'superseder', 'title'])
374 self.assertEqual(None,params['deadline'])
375 self.assertEqual(None,params['foo'])
376 self.assertEqual([],params['nosy'])
377 self.assertEqual('1',params['status'])
378 self.assertEqual('spam',params['title'])
380 # journal entry for link
381 journal = self.db.getjournal('user', '1')
382 self.assertEqual(1, len(journal))
383 self.db.issue.set('1', assignedto='1')
384 self.db.commit()
385 journal = self.db.getjournal('user', '1')
386 self.assertEqual(2, len(journal))
387 (nodeid, date_stamp, journaltag, action, params) = journal[1]
388 self.assertEqual('1', nodeid)
389 self.assertEqual('1', journaltag)
390 self.assertEqual('link', action)
391 self.assertEqual(('issue', '1', 'assignedto'), params)
393 # journal entry for unlink
394 self.db.issue.set('1', assignedto='2')
395 self.db.commit()
396 journal = self.db.getjournal('user', '1')
397 self.assertEqual(3, len(journal))
398 (nodeid, date_stamp, journaltag, action, params) = journal[2]
399 self.assertEqual('1', nodeid)
400 self.assertEqual('1', journaltag)
401 self.assertEqual('unlink', action)
402 self.assertEqual(('issue', '1', 'assignedto'), params)
404 # test disabling journalling
405 # ... get the last entry
406 time.sleep(1)
407 entry = self.db.getjournal('issue', '1')[-1]
408 (x, date_stamp, x, x, x) = entry
409 self.db.issue.disableJournalling()
410 self.db.issue.set('1', title='hello world')
411 self.db.commit()
412 entry = self.db.getjournal('issue', '1')[-1]
413 (x, date_stamp2, x, x, x) = entry
414 # see if the change was journalled when it shouldn't have been
415 self.assertEqual(date_stamp, date_stamp2)
416 self.db.issue.enableJournalling()
417 self.db.issue.set('1', title='hello world 2')
418 self.db.commit()
419 entry = self.db.getjournal('issue', '1')[-1]
420 (x, date_stamp2, x, x, x) = entry
421 # see if the change was journalled
422 self.assertNotEqual(date_stamp, date_stamp2)
424 def testPack(self):
425 id = self.db.issue.create(title="spam", status='1')
426 self.db.commit()
427 self.db.issue.set(id, status='2')
428 self.db.commit()
430 # sleep for at least a second, then get a date to pack at
431 time.sleep(1)
432 pack_before = date.Date('.')
433 time.sleep(1)
435 # one more entry
436 self.db.issue.set(id, status='3')
437 self.db.commit()
439 # pack
440 self.db.pack(pack_before)
441 journal = self.db.getjournal('issue', id)
443 # we should have the create and last set entries now
444 self.assertEqual(2, len(journal))
446 def testIDGeneration(self):
447 id1 = self.db.issue.create(title="spam", status='1')
448 id2 = self.db2.issue.create(title="eggs", status='2')
449 self.assertNotEqual(id1, id2)
451 def testSearching(self):
452 self.db.file.create(content='hello', type="text/plain")
453 self.db.file.create(content='world', type="text/frozz",
454 comment='blah blah')
455 self.db.issue.create(files=['1', '2'], title="flebble plop")
456 self.db.issue.create(title="flebble frooz")
457 self.db.commit()
458 self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
459 {'1': {'files': ['1']}})
460 self.assertEquals(self.db.indexer.search(['world'], self.db.issue), {})
461 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
462 {'2': {}})
463 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
464 {'2': {}, '1': {}})
466 def testReindexing(self):
467 self.db.issue.create(title="frooz")
468 self.db.commit()
469 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
470 {'1': {}})
471 self.db.issue.set('1', title="dooble")
472 self.db.commit()
473 self.assertEquals(self.db.indexer.search(['dooble'], self.db.issue),
474 {'1': {}})
475 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue), {})
477 def testForcedReindexing(self):
478 self.db.issue.create(title="flebble frooz")
479 self.db.commit()
480 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
481 {'1': {}})
482 self.db.indexer.quiet = 1
483 self.db.indexer.force_reindex()
484 self.db.post_init()
485 self.db.indexer.quiet = 9
486 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
487 {'1': {}})
489 def filteringSetup(self):
490 for user in (
491 {'username': 'bleep'},
492 {'username': 'blop'},
493 {'username': 'blorp'}):
494 self.db.user.create(**user)
495 iss = self.db.issue
496 for issue in (
497 {'title': 'issue one', 'status': '2'},
498 {'title': 'issue two', 'status': '1'},
499 {'title': 'issue three', 'status': '1', 'nosy': ['1','2']}):
500 self.db.issue.create(**issue)
501 self.db.commit()
502 return self.assertEqual, self.db.issue.filter
504 def testFilteringString(self):
505 ae, filt = self.filteringSetup()
506 ae(filt(None, {'title': 'issue one'}, ('+','id'), (None,None)), ['1'])
507 ae(filt(None, {'title': 'issue'}, ('+','id'), (None,None)),
508 ['1','2','3'])
510 def testFilteringLink(self):
511 ae, filt = self.filteringSetup()
512 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['2','3'])
514 def testFilteringMultilink(self):
515 ae, filt = self.filteringSetup()
516 ae(filt(None, {'nosy': '2'}, ('+','id'), (None,None)), ['3'])
518 def testFilteringMany(self):
519 ae, filt = self.filteringSetup()
520 ae(filt(None, {'nosy': '2', 'status': '1'}, ('+','id'), (None,None)),
521 ['3'])
523 class anydbmReadOnlyDBTestCase(MyTestCase):
524 def setUp(self):
525 from roundup.backends import anydbm
526 # remove previous test, ignore errors
527 if os.path.exists(config.DATABASE):
528 shutil.rmtree(config.DATABASE)
529 os.makedirs(config.DATABASE + '/files')
530 db = anydbm.Database(config, 'admin')
531 setupSchema(db, 1, anydbm)
532 self.db = anydbm.Database(config)
533 setupSchema(self.db, 0, anydbm)
534 self.db2 = anydbm.Database(config, 'admin')
535 setupSchema(self.db2, 0, anydbm)
537 def testExceptions(self):
538 # this tests the exceptions that should be raised
539 ar = self.assertRaises
541 # this tests the exceptions that should be raised
542 ar(DatabaseError, self.db.status.create, name="foo")
543 ar(DatabaseError, self.db.status.set, '1', name="foo")
544 ar(DatabaseError, self.db.status.retire, '1')
547 class bsddbDBTestCase(anydbmDBTestCase):
548 def setUp(self):
549 from roundup.backends import bsddb
550 # remove previous test, ignore errors
551 if os.path.exists(config.DATABASE):
552 shutil.rmtree(config.DATABASE)
553 os.makedirs(config.DATABASE + '/files')
554 self.db = bsddb.Database(config, 'admin')
555 setupSchema(self.db, 1, bsddb)
556 self.db2 = bsddb.Database(config, 'admin')
557 setupSchema(self.db2, 0, bsddb)
559 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
560 def setUp(self):
561 from roundup.backends import bsddb
562 # remove previous test, ignore errors
563 if os.path.exists(config.DATABASE):
564 shutil.rmtree(config.DATABASE)
565 os.makedirs(config.DATABASE + '/files')
566 db = bsddb.Database(config, 'admin')
567 setupSchema(db, 1, bsddb)
568 self.db = bsddb.Database(config)
569 setupSchema(self.db, 0, bsddb)
570 self.db2 = bsddb.Database(config, 'admin')
571 setupSchema(self.db2, 0, bsddb)
574 class bsddb3DBTestCase(anydbmDBTestCase):
575 def setUp(self):
576 from roundup.backends import bsddb3
577 # remove previous test, ignore errors
578 if os.path.exists(config.DATABASE):
579 shutil.rmtree(config.DATABASE)
580 os.makedirs(config.DATABASE + '/files')
581 self.db = bsddb3.Database(config, 'admin')
582 setupSchema(self.db, 1, bsddb3)
583 self.db2 = bsddb3.Database(config, 'admin')
584 setupSchema(self.db2, 0, bsddb3)
586 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
587 def setUp(self):
588 from roundup.backends import bsddb3
589 # remove previous test, ignore errors
590 if os.path.exists(config.DATABASE):
591 shutil.rmtree(config.DATABASE)
592 os.makedirs(config.DATABASE + '/files')
593 db = bsddb3.Database(config, 'admin')
594 setupSchema(db, 1, bsddb3)
595 self.db = bsddb3.Database(config)
596 setupSchema(self.db, 0, bsddb3)
597 self.db2 = bsddb3.Database(config, 'admin')
598 setupSchema(self.db2, 0, bsddb3)
601 class gadflyDBTestCase(anydbmDBTestCase):
602 ''' Gadfly doesn't support multiple connections to the one local
603 database
604 '''
605 def setUp(self):
606 from roundup.backends import gadfly
607 # remove previous test, ignore errors
608 if os.path.exists(config.DATABASE):
609 shutil.rmtree(config.DATABASE)
610 config.GADFLY_DATABASE = ('test', config.DATABASE)
611 os.makedirs(config.DATABASE + '/files')
612 self.db = gadfly.Database(config, 'admin')
613 setupSchema(self.db, 1, gadfly)
615 def testIDGeneration(self):
616 id1 = self.db.issue.create(title="spam", status='1')
617 id2 = self.db.issue.create(title="eggs", status='2')
618 self.assertNotEqual(id1, id2)
620 class gadflyReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
621 def setUp(self):
622 from roundup.backends import gadfly
623 # remove previous test, ignore errors
624 if os.path.exists(config.DATABASE):
625 shutil.rmtree(config.DATABASE)
626 config.GADFLY_DATABASE = ('test', config.DATABASE)
627 os.makedirs(config.DATABASE + '/files')
628 db = gadfly.Database(config, 'admin')
629 setupSchema(db, 1, gadfly)
630 self.db = gadfly.Database(config)
631 setupSchema(self.db, 0, gadfly)
634 class sqliteDBTestCase(anydbmDBTestCase):
635 def setUp(self):
636 from roundup.backends import sqlite
637 # remove previous test, ignore errors
638 if os.path.exists(config.DATABASE):
639 shutil.rmtree(config.DATABASE)
640 os.makedirs(config.DATABASE + '/files')
641 self.db = sqlite.Database(config, 'admin')
642 setupSchema(self.db, 1, sqlite)
644 def testIDGeneration(self):
645 pass
647 class sqliteReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
648 def setUp(self):
649 from roundup.backends import sqlite
650 # remove previous test, ignore errors
651 if os.path.exists(config.DATABASE):
652 shutil.rmtree(config.DATABASE)
653 os.makedirs(config.DATABASE + '/files')
654 db = sqlite.Database(config, 'admin')
655 setupSchema(db, 1, sqlite)
656 self.db = sqlite.Database(config)
657 setupSchema(self.db, 0, sqlite)
660 class metakitDBTestCase(anydbmDBTestCase):
661 def setUp(self):
662 from roundup.backends import metakit
663 import weakref
664 metakit._instances = weakref.WeakValueDictionary()
665 # remove previous test, ignore errors
666 if os.path.exists(config.DATABASE):
667 shutil.rmtree(config.DATABASE)
668 os.makedirs(config.DATABASE + '/files')
669 self.db = metakit.Database(config, 'admin')
670 setupSchema(self.db, 1, metakit)
672 def testIDGeneration(self):
673 id1 = self.db.issue.create(title="spam", status='1')
674 id2 = self.db.issue.create(title="eggs", status='2')
675 self.assertNotEqual(id1, id2)
677 def testTransactions(self):
678 # remember the number of items we started
679 num_issues = len(self.db.issue.list())
680 self.db.issue.create(title="don't commit me!", status='1')
681 self.assertNotEqual(num_issues, len(self.db.issue.list()))
682 self.db.rollback()
683 self.assertEqual(num_issues, len(self.db.issue.list()))
684 self.db.issue.create(title="please commit me!", status='1')
685 self.assertNotEqual(num_issues, len(self.db.issue.list()))
686 self.db.commit()
687 self.assertNotEqual(num_issues, len(self.db.issue.list()))
688 self.db.rollback()
689 self.assertNotEqual(num_issues, len(self.db.issue.list()))
690 self.db.file.create(name="test", type="text/plain", content="hi")
691 self.db.rollback()
692 for i in range(10):
693 self.db.file.create(name="test", type="text/plain",
694 content="hi %d"%(i))
695 self.db.commit()
696 # TODO: would be good to be able to ensure the file is not on disk after
697 # a rollback...
698 self.assertNotEqual(num_files, num_files2)
699 self.db.file.create(name="test", type="text/plain", content="hi")
700 self.db.rollback()
702 class metakitReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
703 def setUp(self):
704 from roundup.backends import metakit
705 import weakref
706 metakit._instances = weakref.WeakValueDictionary()
707 # remove previous test, ignore errors
708 if os.path.exists(config.DATABASE):
709 shutil.rmtree(config.DATABASE)
710 os.makedirs(config.DATABASE + '/files')
711 db = metakit.Database(config, 'admin')
712 setupSchema(db, 1, metakit)
713 self.db = metakit.Database(config)
714 setupSchema(self.db, 0, metakit)
716 def suite():
717 l = [
718 unittest.makeSuite(anydbmDBTestCase, 'test'),
719 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
720 ]
721 # return unittest.TestSuite(l)
723 try:
724 import gadfly
725 l.append(unittest.makeSuite(gadflyDBTestCase, 'test'))
726 l.append(unittest.makeSuite(gadflyReadOnlyDBTestCase, 'test'))
727 except:
728 print 'gadfly module not found, skipping gadfly DBTestCase'
730 try:
731 import sqlite
732 l.append(unittest.makeSuite(sqliteDBTestCase, 'test'))
733 l.append(unittest.makeSuite(sqliteReadOnlyDBTestCase, 'test'))
734 except:
735 print 'sqlite module not found, skipping gadfly DBTestCase'
737 try:
738 import bsddb
739 l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
740 l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))
741 except:
742 print 'bsddb module not found, skipping bsddb DBTestCase'
744 try:
745 import bsddb3
746 l.append(unittest.makeSuite(bsddb3DBTestCase, 'test'))
747 l.append(unittest.makeSuite(bsddb3ReadOnlyDBTestCase, 'test'))
748 except:
749 print 'bsddb3 module not found, skipping bsddb3 DBTestCase'
751 try:
752 import metakit
753 l.append(unittest.makeSuite(metakitDBTestCase, 'test'))
754 l.append(unittest.makeSuite(metakitReadOnlyDBTestCase, 'test'))
755 except:
756 print 'metakit module not found, skipping metakit DBTestCase'
758 return unittest.TestSuite(l)
760 # vim: set filetype=python ts=4 sw=4 et si