Code

On second thought, that last checkin was dumb.
[roundup.git] / test / test_db.py
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.52 2002-09-20 05:08:00 richard 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, '1', '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, '1', 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         self.db.issue.create(title="spam", status='1')
334         # invalid link index
335         ar(IndexError, self.db.issue.set, '6', title='foo', status='bar')
336         # invalid link value
337         ar(ValueError, self.db.issue.set, '6', title='foo', status=1)
338         # invalid multilink type
339         ar(TypeError, self.db.issue.set, '6', title='foo', status='1',
340             nosy='hello')
341         # invalid multilink index type
342         ar(ValueError, self.db.issue.set, '6', title='foo', status='1',
343             nosy=[1])
344         # invalid multilink index
345         ar(IndexError, self.db.issue.set, '6', 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         self.db.issue.create(title="spam", status='1')
426         self.db.commit()
427         self.db.issue.set('1', 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('.')
434         # one more entry
435         self.db.issue.set('1', status='3')
436         self.db.commit()
438         # pack
439         self.db.pack(pack_before)
440         journal = self.db.getjournal('issue', '1')
442         # we should have the create and last set entries now
443         self.assertEqual(2, len(journal))
445     def testIDGeneration(self):
446         id1 = self.db.issue.create(title="spam", status='1')
447         id2 = self.db2.issue.create(title="eggs", status='2')
448         self.assertNotEqual(id1, id2)
450     def testSearching(self):
451         self.db.file.create(content='hello', type="text/plain")
452         self.db.file.create(content='world', type="text/frozz",
453             comment='blah blah')
454         self.db.issue.create(files=['1', '2'], title="flebble plop")
455         self.db.issue.create(title="flebble frooz")
456         self.db.commit()
457         self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
458             {'1': {'files': ['1']}})
459         self.assertEquals(self.db.indexer.search(['world'], self.db.issue), {})
460         self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
461             {'2': {}})
462         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
463             {'2': {}, '1': {}})
465     def testReindexing(self):
466         self.db.issue.create(title="frooz")
467         self.db.commit()
468         self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
469             {'1': {}})
470         self.db.issue.set('1', title="dooble")
471         self.db.commit()
472         self.assertEquals(self.db.indexer.search(['dooble'], self.db.issue),
473             {'1': {}})
474         self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue), {})
476     def testForcedReindexing(self):
477         self.db.issue.create(title="flebble frooz")
478         self.db.commit()
479         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
480             {'1': {}})
481         self.db.indexer.quiet = 1
482         self.db.indexer.force_reindex()
483         self.db.post_init()
484         self.db.indexer.quiet = 9
485         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
486             {'1': {}})
488     def filteringSetup(self):
489         for user in (
490                 {'username': 'bleep'},
491                 {'username': 'blop'},
492                 {'username': 'blorp'}):
493             self.db.user.create(**user)
494         iss = self.db.issue
495         for issue in (
496                 {'title': 'issue one', 'status': '2'},
497                 {'title': 'issue two', 'status': '1'},
498                 {'title': 'issue three', 'status': '1', 'nosy': ['1','2']}):
499             self.db.issue.create(**issue)
500         self.db.commit()
501         return self.assertEqual, self.db.issue.filter
503     def testFilteringString(self):
504         ae, filt = self.filteringSetup()
505         ae(filt(None, {'title': 'issue one'}, ('+','id'), (None,None)), ['1'])
506         ae(filt(None, {'title': 'issue'}, ('+','id'), (None,None)),
507             ['1','2','3'])
509     def testFilteringLink(self):
510         ae, filt = self.filteringSetup()
511         ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['2','3'])
513     def testFilteringMultilink(self):
514         ae, filt = self.filteringSetup()
515         ae(filt(None, {'nosy': '2'}, ('+','id'), (None,None)), ['3'])
517     def testFilteringMany(self):
518         ae, filt = self.filteringSetup()
519         ae(filt(None, {'nosy': '2', 'status': '1'}, ('+','id'), (None,None)),
520             ['3'])
522 class anydbmReadOnlyDBTestCase(MyTestCase):
523     def setUp(self):
524         from roundup.backends import anydbm
525         # remove previous test, ignore errors
526         if os.path.exists(config.DATABASE):
527             shutil.rmtree(config.DATABASE)
528         os.makedirs(config.DATABASE + '/files')
529         db = anydbm.Database(config, 'admin')
530         setupSchema(db, 1, anydbm)
531         self.db = anydbm.Database(config)
532         setupSchema(self.db, 0, anydbm)
533         self.db2 = anydbm.Database(config, 'admin')
534         setupSchema(self.db2, 0, anydbm)
536     def testExceptions(self):
537         # this tests the exceptions that should be raised
538         ar = self.assertRaises
540         # this tests the exceptions that should be raised
541         ar(DatabaseError, self.db.status.create, name="foo")
542         ar(DatabaseError, self.db.status.set, '1', name="foo")
543         ar(DatabaseError, self.db.status.retire, '1')
546 class bsddbDBTestCase(anydbmDBTestCase):
547     def setUp(self):
548         from roundup.backends import bsddb
549         # remove previous test, ignore errors
550         if os.path.exists(config.DATABASE):
551             shutil.rmtree(config.DATABASE)
552         os.makedirs(config.DATABASE + '/files')
553         self.db = bsddb.Database(config, 'admin')
554         setupSchema(self.db, 1, bsddb)
555         self.db2 = bsddb.Database(config, 'admin')
556         setupSchema(self.db2, 0, bsddb)
558 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
559     def setUp(self):
560         from roundup.backends import bsddb
561         # remove previous test, ignore errors
562         if os.path.exists(config.DATABASE):
563             shutil.rmtree(config.DATABASE)
564         os.makedirs(config.DATABASE + '/files')
565         db = bsddb.Database(config, 'admin')
566         setupSchema(db, 1, bsddb)
567         self.db = bsddb.Database(config)
568         setupSchema(self.db, 0, bsddb)
569         self.db2 = bsddb.Database(config, 'admin')
570         setupSchema(self.db2, 0, bsddb)
573 class bsddb3DBTestCase(anydbmDBTestCase):
574     def setUp(self):
575         from roundup.backends import bsddb3
576         # remove previous test, ignore errors
577         if os.path.exists(config.DATABASE):
578             shutil.rmtree(config.DATABASE)
579         os.makedirs(config.DATABASE + '/files')
580         self.db = bsddb3.Database(config, 'admin')
581         setupSchema(self.db, 1, bsddb3)
582         self.db2 = bsddb3.Database(config, 'admin')
583         setupSchema(self.db2, 0, bsddb3)
585 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
586     def setUp(self):
587         from roundup.backends import bsddb3
588         # remove previous test, ignore errors
589         if os.path.exists(config.DATABASE):
590             shutil.rmtree(config.DATABASE)
591         os.makedirs(config.DATABASE + '/files')
592         db = bsddb3.Database(config, 'admin')
593         setupSchema(db, 1, bsddb3)
594         self.db = bsddb3.Database(config)
595         setupSchema(self.db, 0, bsddb3)
596         self.db2 = bsddb3.Database(config, 'admin')
597         setupSchema(self.db2, 0, bsddb3)
600 class gadflyDBTestCase(anydbmDBTestCase):
601     ''' Gadfly doesn't support multiple connections to the one local
602         database
603     '''
604     def setUp(self):
605         from roundup.backends import gadfly
606         # remove previous test, ignore errors
607         if os.path.exists(config.DATABASE):
608             shutil.rmtree(config.DATABASE)
609         config.GADFLY_DATABASE = ('test', config.DATABASE)
610         os.makedirs(config.DATABASE + '/files')
611         self.db = gadfly.Database(config, 'admin')
612         setupSchema(self.db, 1, gadfly)
614     def testIDGeneration(self):
615         id1 = self.db.issue.create(title="spam", status='1')
616         id2 = self.db.issue.create(title="eggs", status='2')
617         self.assertNotEqual(id1, id2)
619 class gadflyReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
620     def setUp(self):
621         from roundup.backends import gadfly
622         # remove previous test, ignore errors
623         if os.path.exists(config.DATABASE):
624             shutil.rmtree(config.DATABASE)
625         config.GADFLY_DATABASE = ('test', config.DATABASE)
626         os.makedirs(config.DATABASE + '/files')
627         db = gadfly.Database(config, 'admin')
628         setupSchema(db, 1, gadfly)
629         self.db = gadfly.Database(config)
630         setupSchema(self.db, 0, gadfly)
633 class sqliteDBTestCase(anydbmDBTestCase):
634     def setUp(self):
635         from roundup.backends import sqlite
636         # remove previous test, ignore errors
637         if os.path.exists(config.DATABASE):
638             shutil.rmtree(config.DATABASE)
639         os.makedirs(config.DATABASE + '/files')
640         self.db = sqlite.Database(config, 'admin')
641         setupSchema(self.db, 1, sqlite)
643     def testIDGeneration(self):
644         pass
646 class sqliteReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
647     def setUp(self):
648         from roundup.backends import sqlite
649         # remove previous test, ignore errors
650         if os.path.exists(config.DATABASE):
651             shutil.rmtree(config.DATABASE)
652         os.makedirs(config.DATABASE + '/files')
653         db = sqlite.Database(config, 'admin')
654         setupSchema(db, 1, sqlite)
655         self.db = sqlite.Database(config)
656         setupSchema(self.db, 0, sqlite)
659 class metakitDBTestCase(anydbmDBTestCase):
660     def setUp(self):
661         from roundup.backends import metakit
662         import weakref
663         metakit._instances = weakref.WeakValueDictionary()
664         # remove previous test, ignore errors
665         if os.path.exists(config.DATABASE):
666             shutil.rmtree(config.DATABASE)
667         os.makedirs(config.DATABASE + '/files')
668         self.db = metakit.Database(config, 'admin')
669         setupSchema(self.db, 1, metakit)
671     def testIDGeneration(self):
672         id1 = self.db.issue.create(title="spam", status='1')
673         id2 = self.db.issue.create(title="eggs", status='2')
674         self.assertNotEqual(id1, id2)
676     def testTransactions(self):
677         # remember the number of items we started
678         num_issues = len(self.db.issue.list())
679         self.db.issue.create(title="don't commit me!", status='1')
680         self.assertNotEqual(num_issues, len(self.db.issue.list()))
681         self.db.rollback()
682         self.assertEqual(num_issues, len(self.db.issue.list()))
683         self.db.issue.create(title="please commit me!", status='1')
684         self.assertNotEqual(num_issues, len(self.db.issue.list()))
685         self.db.commit()
686         self.assertNotEqual(num_issues, len(self.db.issue.list()))
687         self.db.rollback()
688         self.assertNotEqual(num_issues, len(self.db.issue.list()))
689         self.db.file.create(name="test", type="text/plain", content="hi")
690         self.db.rollback()
691         for i in range(10):
692             self.db.file.create(name="test", type="text/plain", 
693                     content="hi %d"%(i))
694             self.db.commit()
695         # TODO: would be good to be able to ensure the file is not on disk after
696         # a rollback...
697         self.assertNotEqual(num_files, num_files2)
698         self.db.file.create(name="test", type="text/plain", content="hi")
699         self.db.rollback()
701 class metakitReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
702     def setUp(self):
703         from roundup.backends import metakit
704         import weakref
705         metakit._instances = weakref.WeakValueDictionary()
706         # remove previous test, ignore errors
707         if os.path.exists(config.DATABASE):
708             shutil.rmtree(config.DATABASE)
709         os.makedirs(config.DATABASE + '/files')
710         db = metakit.Database(config, 'admin')
711         setupSchema(db, 1, metakit)
712         self.db = metakit.Database(config)
713         setupSchema(self.db, 0, metakit)
715 def suite():
716     l = [
717          unittest.makeSuite(anydbmDBTestCase, 'test'),
718          unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
719     ]
720 #    return unittest.TestSuite(l)
722     try:
723         import gadfly
724         l.append(unittest.makeSuite(gadflyDBTestCase, 'test'))
725         l.append(unittest.makeSuite(gadflyReadOnlyDBTestCase, 'test'))
726     except:
727         print 'gadfly module not found, skipping gadfly DBTestCase'
729     try:
730         import sqlite
731         l.append(unittest.makeSuite(sqliteDBTestCase, 'test'))
732         l.append(unittest.makeSuite(sqliteReadOnlyDBTestCase, 'test'))
733     except:
734         print 'sqlite module not found, skipping gadfly DBTestCase'
736     try:
737         import bsddb
738         l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
739         l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))
740     except:
741         print 'bsddb module not found, skipping bsddb DBTestCase'
743     try:
744         import bsddb3
745         l.append(unittest.makeSuite(bsddb3DBTestCase, 'test'))
746         l.append(unittest.makeSuite(bsddb3ReadOnlyDBTestCase, 'test'))
747     except:
748         print 'bsddb3 module not found, skipping bsddb3 DBTestCase'
750     try:
751         import metakit
752         l.append(unittest.makeSuite(metakitDBTestCase, 'test'))
753         l.append(unittest.makeSuite(metakitReadOnlyDBTestCase, 'test'))
754     except:
755         print 'metakit module not found, skipping metakit DBTestCase'
757     return unittest.TestSuite(l)
759 # vim: set filetype=python ts=4 sw=4 et si