Code

oops
[roundup.git] / test / db_test_base.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: db_test_base.py,v 1.23 2004-04-08 00:42:13 richard Exp $ 
20 import unittest, os, shutil, errno, imp, sys, time, pprint
22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \
23     Interval, DatabaseError, Boolean, Number, Node
24 from roundup import date, password
25 from roundup import init
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     stuff = module.Class(db, "stuff", stuff=String())
39     session = module.Class(db, 'session', title=String())
40     session.disableJournalling()
41     db.post_init()
42     if create:
43         user.create(username="admin", roles='Admin',
44             password=password.Password('sekrit'))
45         user.create(username="fred", roles='User',
46             password=password.Password('sekrit'))
47         status.create(name="unread")
48         status.create(name="in-progress")
49         status.create(name="testing")
50         status.create(name="resolved")
51     db.commit()
53 class MyTestCase(unittest.TestCase):
54     def tearDown(self):
55         if hasattr(self, 'db'):
56             self.db.close()
57         if os.path.exists(config.DATABASE):
58             shutil.rmtree(config.DATABASE)
60 class config:
61     DATABASE='_test_dir'
62     MAILHOST = 'localhost'
63     MAIL_DOMAIN = 'fill.me.in.'
64     TRACKER_NAME = 'Roundup issue tracker'
65     TRACKER_EMAIL = 'issue_tracker@%s'%MAIL_DOMAIN
66     TRACKER_WEB = 'http://some.useful.url/'
67     ADMIN_EMAIL = 'roundup-admin@%s'%MAIL_DOMAIN
68     FILTER_POSITION = 'bottom'      # one of 'top', 'bottom', 'top and bottom'
69     ANONYMOUS_ACCESS = 'deny'       # either 'deny' or 'allow'
70     ANONYMOUS_REGISTER = 'deny'     # either 'deny' or 'allow'
71     MESSAGES_TO_AUTHOR = 'no'       # either 'yes' or 'no'
72     EMAIL_SIGNATURE_POSITION = 'bottom'
75 class DBTest(MyTestCase):
76     def setUp(self):
77         # remove previous test, ignore errors
78         if os.path.exists(config.DATABASE):
79             shutil.rmtree(config.DATABASE)
80         os.makedirs(config.DATABASE + '/files')
81         self.db = self.module.Database(config, 'admin')
82         setupSchema(self.db, 1, self.module)
84     def testRefresh(self):
85         self.db.refresh_database()
87     #
88     # automatic properties (well, the two easy ones anyway)
89     #
90     def testCreatorProperty(self):
91         i = self.db.issue
92         id1 = i.create(title='spam')
93         self.db.commit()
94         self.db.close()
95         self.db = self.module.Database(config, 'fred')
96         setupSchema(self.db, 0, self.module)
97         i = self.db.issue
98         id2 = i.create(title='spam')
99         self.assertNotEqual(id1, id2)
100         self.assertNotEqual(i.get(id1, 'creator'), i.get(id2, 'creator'))
102     def testActorProperty(self):
103         i = self.db.issue
104         id1 = i.create(title='spam')
105         self.db.commit()
106         self.db.close()
107         self.db = self.module.Database(config, 'fred')
108         setupSchema(self.db, 0, self.module)
109         i = self.db.issue
110         i.set(id1, title='asfasd')
111         self.assertNotEqual(i.get(id1, 'creator'), i.get(id1, 'actor'))
113     # ID number controls
114     def testIDGeneration(self):
115         id1 = self.db.issue.create(title="spam", status='1')
116         id2 = self.db.issue.create(title="eggs", status='2')
117         self.assertNotEqual(id1, id2)
118     def testIDSetting(self):
119         # XXX numeric ids
120         self.db.setid('issue', 10)
121         id2 = self.db.issue.create(title="eggs", status='2')
122         self.assertEqual('11', id2)
124     #
125     # basic operations
126     #
127     def testEmptySet(self):
128         id1 = self.db.issue.create(title="spam", status='1')
129         self.db.issue.set(id1)
131     # String
132     def testStringChange(self):
133         for commit in (0,1):
134             # test set & retrieve
135             nid = self.db.issue.create(title="spam", status='1')
136             self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
138             # change and make sure we retrieve the correct value
139             self.db.issue.set(nid, title='eggs')
140             if commit: self.db.commit()
141             self.assertEqual(self.db.issue.get(nid, 'title'), 'eggs')
143     def testStringUnset(self):
144         for commit in (0,1):
145             nid = self.db.issue.create(title="spam", status='1')
146             if commit: self.db.commit()
147             self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
148             # make sure we can unset
149             self.db.issue.set(nid, title=None)
150             if commit: self.db.commit()
151             self.assertEqual(self.db.issue.get(nid, "title"), None)
153     # FileClass "content" property (no unset test)
154     def testFileClassContentChange(self):
155         for commit in (0,1):
156             # test set & retrieve
157             nid = self.db.file.create(content="spam")
158             self.assertEqual(self.db.file.get(nid, 'content'), 'spam')
160             # change and make sure we retrieve the correct value
161             self.db.file.set(nid, content='eggs')
162             if commit: self.db.commit()
163             self.assertEqual(self.db.file.get(nid, 'content'), 'eggs')
165     # Link
166     def testLinkChange(self):
167         self.assertRaises(IndexError, self.db.issue.create, title="spam",
168             status='100')
169         for commit in (0,1):
170             nid = self.db.issue.create(title="spam", status='1')
171             if commit: self.db.commit()
172             self.assertEqual(self.db.issue.get(nid, "status"), '1')
173             self.db.issue.set(nid, status='2')
174             if commit: self.db.commit()
175             self.assertEqual(self.db.issue.get(nid, "status"), '2')
177     def testLinkUnset(self):
178         for commit in (0,1):
179             nid = self.db.issue.create(title="spam", status='1')
180             if commit: self.db.commit()
181             self.db.issue.set(nid, status=None)
182             if commit: self.db.commit()
183             self.assertEqual(self.db.issue.get(nid, "status"), None)
185     # Multilink
186     def testMultilinkChange(self):
187         for commit in (0,1):
188             self.assertRaises(IndexError, self.db.issue.create, title="spam",
189                 nosy=['foo%s'%commit])
190             u1 = self.db.user.create(username='foo%s'%commit)
191             u2 = self.db.user.create(username='bar%s'%commit)
192             nid = self.db.issue.create(title="spam", nosy=[u1])
193             if commit: self.db.commit()
194             self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
195             self.db.issue.set(nid, nosy=[])
196             if commit: self.db.commit()
197             self.assertEqual(self.db.issue.get(nid, "nosy"), [])
198             self.db.issue.set(nid, nosy=[u1,u2])
199             if commit: self.db.commit()
200             l = [u1,u2]; l.sort()
201             m = self.db.issue.get(nid, "nosy"); m.sort()
202             self.assertEqual(l, m)
204     # Date
205     def testDateChange(self):
206         self.assertRaises(TypeError, self.db.issue.create, 
207             title='spam', deadline=1)
208         for commit in (0,1):
209             nid = self.db.issue.create(title="spam", status='1')
210             self.assertRaises(TypeError, self.db.issue.set, nid, deadline=1)
211             a = self.db.issue.get(nid, "deadline")
212             if commit: self.db.commit()
213             self.db.issue.set(nid, deadline=date.Date())
214             b = self.db.issue.get(nid, "deadline")
215             if commit: self.db.commit()
216             self.assertNotEqual(a, b)
217             self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
219     def testDateUnset(self):
220         for commit in (0,1):
221             nid = self.db.issue.create(title="spam", status='1')
222             self.db.issue.set(nid, deadline=date.Date())
223             if commit: self.db.commit()
224             self.assertNotEqual(self.db.issue.get(nid, "deadline"), None)
225             self.db.issue.set(nid, deadline=None)
226             if commit: self.db.commit()
227             self.assertEqual(self.db.issue.get(nid, "deadline"), None)
229     # Interval
230     def testIntervalChange(self):
231         self.assertRaises(TypeError, self.db.issue.create, 
232             title='spam', foo=1)
233         for commit in (0,1):
234             nid = self.db.issue.create(title="spam", status='1')
235             self.assertRaises(TypeError, self.db.issue.set, nid, foo=1)
236             if commit: self.db.commit()
237             a = self.db.issue.get(nid, "foo")
238             i = date.Interval('-1d')
239             self.db.issue.set(nid, foo=i)
240             if commit: self.db.commit()
241             self.assertNotEqual(self.db.issue.get(nid, "foo"), a)
242             self.assertEqual(i, self.db.issue.get(nid, "foo"))
243             j = date.Interval('1y')
244             self.db.issue.set(nid, foo=j)
245             if commit: self.db.commit()
246             self.assertNotEqual(self.db.issue.get(nid, "foo"), i)
247             self.assertEqual(j, self.db.issue.get(nid, "foo"))
249     def testIntervalUnset(self):
250         for commit in (0,1):
251             nid = self.db.issue.create(title="spam", status='1')
252             self.db.issue.set(nid, foo=date.Interval('-1d'))
253             if commit: self.db.commit()
254             self.assertNotEqual(self.db.issue.get(nid, "foo"), None)
255             self.db.issue.set(nid, foo=None)
256             if commit: self.db.commit()
257             self.assertEqual(self.db.issue.get(nid, "foo"), None)
259     # Boolean
260     def testBooleanChange(self):
261         userid = self.db.user.create(username='foo', assignable=1)
262         self.assertEqual(1, self.db.user.get(userid, 'assignable'))
263         self.db.user.set(userid, assignable=0)
264         self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
266     def testBooleanUnset(self):
267         nid = self.db.user.create(username='foo', assignable=1)
268         self.db.user.set(nid, assignable=None)
269         self.assertEqual(self.db.user.get(nid, "assignable"), None)
271     # Number
272     def testNumberChange(self):
273         nid = self.db.user.create(username='foo', age=1)
274         self.assertEqual(1, self.db.user.get(nid, 'age'))
275         self.db.user.set(nid, age=3)
276         self.assertNotEqual(self.db.user.get(nid, 'age'), 1)
277         self.db.user.set(nid, age=1.0)
278         self.assertEqual(self.db.user.get(nid, 'age'), 1)
279         self.db.user.set(nid, age=0)
280         self.assertEqual(self.db.user.get(nid, 'age'), 0)
282         nid = self.db.user.create(username='bar', age=0)
283         self.assertEqual(self.db.user.get(nid, 'age'), 0)
285     def testNumberUnset(self):
286         nid = self.db.user.create(username='foo', age=1)
287         self.db.user.set(nid, age=None)
288         self.assertEqual(self.db.user.get(nid, "age"), None)
290     # Password
291     def testPasswordChange(self):
292         x = password.Password('x')
293         userid = self.db.user.create(username='foo', password=x)
294         self.assertEqual(x, self.db.user.get(userid, 'password'))
295         self.assertEqual(self.db.user.get(userid, 'password'), 'x')
296         y = password.Password('y')
297         self.db.user.set(userid, password=y)
298         self.assertEqual(self.db.user.get(userid, 'password'), 'y')
299         self.assertRaises(TypeError, self.db.user.create, userid,
300             username='bar', password='x')
301         self.assertRaises(TypeError, self.db.user.set, userid, password='x')
303     def testPasswordUnset(self):
304         x = password.Password('x')
305         nid = self.db.user.create(username='foo', password=x)
306         self.db.user.set(nid, assignable=None)
307         self.assertEqual(self.db.user.get(nid, "assignable"), None)
309     # key value
310     def testKeyValue(self):
311         self.assertRaises(ValueError, self.db.user.create)
313         newid = self.db.user.create(username="spam")
314         self.assertEqual(self.db.user.lookup('spam'), newid)
315         self.db.commit()
316         self.assertEqual(self.db.user.lookup('spam'), newid)
317         self.db.user.retire(newid)
318         self.assertRaises(KeyError, self.db.user.lookup, 'spam')
320         # use the key again now that the old is retired
321         newid2 = self.db.user.create(username="spam")
322         self.assertNotEqual(newid, newid2)
323         # try to restore old node. this shouldn't succeed!
324         self.assertRaises(KeyError, self.db.user.restore, newid)
326         self.assertRaises(TypeError, self.db.issue.lookup, 'fubar')
328     # label property
329     def testLabelProp(self):
330         # key prop
331         self.assertEqual(self.db.status.labelprop(), 'name')
332         self.assertEqual(self.db.user.labelprop(), 'username')
333         # title
334         self.assertEqual(self.db.issue.labelprop(), 'title')
335         # name
336         self.assertEqual(self.db.file.labelprop(), 'name')
337         # id
338         self.assertEqual(self.db.stuff.labelprop(default_to_id=1), 'id')
340     # retirement
341     def testRetire(self):
342         self.db.issue.create(title="spam", status='1')
343         b = self.db.status.get('1', 'name')
344         a = self.db.status.list()
345         self.db.status.retire('1')
346         # make sure the list is different 
347         self.assertNotEqual(a, self.db.status.list())
348         # can still access the node if necessary
349         self.assertEqual(self.db.status.get('1', 'name'), b)
350         self.assertRaises(IndexError, self.db.status.set, '1', name='hello')
351         self.db.commit()
352         self.assertEqual(self.db.status.get('1', 'name'), b)
353         self.assertNotEqual(a, self.db.status.list())
354         # try to restore retired node
355         self.db.status.restore('1')
356  
357     def testCacheCreateSet(self):
358         self.db.issue.create(title="spam", status='1')
359         a = self.db.issue.get('1', 'title')
360         self.assertEqual(a, 'spam')
361         self.db.issue.set('1', title='ham')
362         b = self.db.issue.get('1', 'title')
363         self.assertEqual(b, 'ham')
365     def testSerialisation(self):
366         nid = self.db.issue.create(title="spam", status='1',
367             deadline=date.Date(), foo=date.Interval('-1d'))
368         self.db.commit()
369         assert isinstance(self.db.issue.get(nid, 'deadline'), date.Date)
370         assert isinstance(self.db.issue.get(nid, 'foo'), date.Interval)
371         uid = self.db.user.create(username="fozzy",
372             password=password.Password('t. bear'))
373         self.db.commit()
374         assert isinstance(self.db.user.get(uid, 'password'), password.Password)
376     def testTransactions(self):
377         # remember the number of items we started
378         num_issues = len(self.db.issue.list())
379         num_files = self.db.numfiles()
380         self.db.issue.create(title="don't commit me!", status='1')
381         self.assertNotEqual(num_issues, len(self.db.issue.list()))
382         self.db.rollback()
383         self.assertEqual(num_issues, len(self.db.issue.list()))
384         self.db.issue.create(title="please commit me!", status='1')
385         self.assertNotEqual(num_issues, len(self.db.issue.list()))
386         self.db.commit()
387         self.assertNotEqual(num_issues, len(self.db.issue.list()))
388         self.db.rollback()
389         self.assertNotEqual(num_issues, len(self.db.issue.list()))
390         self.db.file.create(name="test", type="text/plain", content="hi")
391         self.db.rollback()
392         self.assertEqual(num_files, self.db.numfiles())
393         for i in range(10):
394             self.db.file.create(name="test", type="text/plain", 
395                     content="hi %d"%(i))
396             self.db.commit()
397         num_files2 = self.db.numfiles()
398         self.assertNotEqual(num_files, num_files2)
399         self.db.file.create(name="test", type="text/plain", content="hi")
400         self.db.rollback()
401         self.assertNotEqual(num_files, self.db.numfiles())
402         self.assertEqual(num_files2, self.db.numfiles())
404         # rollback / cache interaction
405         name1 = self.db.user.get('1', 'username')
406         self.db.user.set('1', username = name1+name1)
407         # get the prop so the info's forced into the cache (if there is one)
408         self.db.user.get('1', 'username')
409         self.db.rollback()
410         name2 = self.db.user.get('1', 'username')
411         self.assertEqual(name1, name2)
413     def testDestroyNoJournalling(self):
414         self.innerTestDestroy(klass=self.db.session)
416     def testDestroyJournalling(self):
417         self.innerTestDestroy(klass=self.db.issue)
419     def innerTestDestroy(self, klass):
420         newid = klass.create(title='Mr Friendly')
421         n = len(klass.list())
422         self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
423         count = klass.count()
424         klass.destroy(newid)
425         self.assertNotEqual(count, klass.count())
426         self.assertRaises(IndexError, klass.get, newid, 'title')
427         self.assertNotEqual(len(klass.list()), n)
428         if klass.do_journal:
429             self.assertRaises(IndexError, klass.history, newid)
431         # now with a commit
432         newid = klass.create(title='Mr Friendly')
433         n = len(klass.list())
434         self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
435         self.db.commit()
436         count = klass.count()
437         klass.destroy(newid)
438         self.assertNotEqual(count, klass.count())
439         self.assertRaises(IndexError, klass.get, newid, 'title')
440         self.db.commit()
441         self.assertRaises(IndexError, klass.get, newid, 'title')
442         self.assertNotEqual(len(klass.list()), n)
443         if klass.do_journal:
444             self.assertRaises(IndexError, klass.history, newid)
446         # now with a rollback
447         newid = klass.create(title='Mr Friendly')
448         n = len(klass.list())
449         self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
450         self.db.commit()
451         count = klass.count()
452         klass.destroy(newid)
453         self.assertNotEqual(len(klass.list()), n)
454         self.assertRaises(IndexError, klass.get, newid, 'title')
455         self.db.rollback()
456         self.assertEqual(count, klass.count())
457         self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
458         self.assertEqual(len(klass.list()), n)
459         if klass.do_journal:
460             self.assertNotEqual(klass.history(newid), [])
462     def testExceptions(self):
463         # this tests the exceptions that should be raised
464         ar = self.assertRaises
466         ar(KeyError, self.db.getclass, 'fubar')
468         #
469         # class create
470         #
471         # string property
472         ar(TypeError, self.db.status.create, name=1)
473         # id, creation, creator and activity properties are reserved
474         ar(KeyError, self.db.status.create, id=1)
475         ar(KeyError, self.db.status.create, creation=1)
476         ar(KeyError, self.db.status.create, creator=1)
477         ar(KeyError, self.db.status.create, activity=1)
478         ar(KeyError, self.db.status.create, actor=1)
479         # invalid property name
480         ar(KeyError, self.db.status.create, foo='foo')
481         # key name clash
482         ar(ValueError, self.db.status.create, name='unread')
483         # invalid link index
484         ar(IndexError, self.db.issue.create, title='foo', status='bar')
485         # invalid link value
486         ar(ValueError, self.db.issue.create, title='foo', status=1)
487         # invalid multilink type
488         ar(TypeError, self.db.issue.create, title='foo', status='1',
489             nosy='hello')
490         # invalid multilink index type
491         ar(ValueError, self.db.issue.create, title='foo', status='1',
492             nosy=[1])
493         # invalid multilink index
494         ar(IndexError, self.db.issue.create, title='foo', status='1',
495             nosy=['10'])
497         #
498         # key property
499         # 
500         # key must be a String
501         ar(TypeError, self.db.file.setkey, 'fooz')
502         # key must exist
503         ar(KeyError, self.db.file.setkey, 'fubar')
505         #
506         # class get
507         #
508         # invalid node id
509         ar(IndexError, self.db.issue.get, '99', 'title')
510         # invalid property name
511         ar(KeyError, self.db.status.get, '2', 'foo')
513         #
514         # class set
515         #
516         # invalid node id
517         ar(IndexError, self.db.issue.set, '99', title='foo')
518         # invalid property name
519         ar(KeyError, self.db.status.set, '1', foo='foo')
520         # string property
521         ar(TypeError, self.db.status.set, '1', name=1)
522         # key name clash
523         ar(ValueError, self.db.status.set, '2', name='unread')
524         # set up a valid issue for me to work on
525         id = self.db.issue.create(title="spam", status='1')
526         # invalid link index
527         ar(IndexError, self.db.issue.set, id, title='foo', status='bar')
528         # invalid link value
529         ar(ValueError, self.db.issue.set, id, title='foo', status=1)
530         # invalid multilink type
531         ar(TypeError, self.db.issue.set, id, title='foo', status='1',
532             nosy='hello')
533         # invalid multilink index type
534         ar(ValueError, self.db.issue.set, id, title='foo', status='1',
535             nosy=[1])
536         # invalid multilink index
537         ar(IndexError, self.db.issue.set, id, title='foo', status='1',
538             nosy=['10'])
539         # NOTE: the following increment the username to avoid problems
540         # within metakit's backend (it creates the node, and then sets the
541         # info, so the create (and by a fluke the username set) go through
542         # before the age/assignable/etc. set, which raises the exception)
543         # invalid number value
544         ar(TypeError, self.db.user.create, username='foo', age='a')
545         # invalid boolean value
546         ar(TypeError, self.db.user.create, username='foo2', assignable='true')
547         nid = self.db.user.create(username='foo3')
548         # invalid number value
549         ar(TypeError, self.db.user.set, nid, age='a')
550         # invalid boolean value
551         ar(TypeError, self.db.user.set, nid, assignable='true')
553     def testJournals(self):
554         self.db.user.create(username="mary")
555         self.db.user.create(username="pete")
556         self.db.issue.create(title="spam", status='1')
557         self.db.commit()
559         # journal entry for issue create
560         journal = self.db.getjournal('issue', '1')
561         self.assertEqual(1, len(journal))
562         (nodeid, date_stamp, journaltag, action, params) = journal[0]
563         self.assertEqual(nodeid, '1')
564         self.assertEqual(journaltag, self.db.user.lookup('admin'))
565         self.assertEqual(action, 'create')
566         keys = params.keys()
567         keys.sort()
568         self.assertEqual(keys, [])
570         # journal entry for link
571         journal = self.db.getjournal('user', '1')
572         self.assertEqual(1, len(journal))
573         self.db.issue.set('1', assignedto='1')
574         self.db.commit()
575         journal = self.db.getjournal('user', '1')
576         self.assertEqual(2, len(journal))
577         (nodeid, date_stamp, journaltag, action, params) = journal[1]
578         self.assertEqual('1', nodeid)
579         self.assertEqual('1', journaltag)
580         self.assertEqual('link', action)
581         self.assertEqual(('issue', '1', 'assignedto'), params)
583         # journal entry for unlink
584         self.db.issue.set('1', assignedto='2')
585         self.db.commit()
586         journal = self.db.getjournal('user', '1')
587         self.assertEqual(3, len(journal))
588         (nodeid, date_stamp, journaltag, action, params) = journal[2]
589         self.assertEqual('1', nodeid)
590         self.assertEqual('1', journaltag)
591         self.assertEqual('unlink', action)
592         self.assertEqual(('issue', '1', 'assignedto'), params)
594         # test disabling journalling
595         # ... get the last entry
596         jlen = len(self.db.getjournal('user', '1'))
597         self.db.issue.disableJournalling()
598         self.db.issue.set('1', title='hello world')
599         self.db.commit()
600         # see if the change was journalled when it shouldn't have been
601         self.assertEqual(jlen,  len(self.db.getjournal('user', '1')))
602         jlen = len(self.db.getjournal('issue', '1'))
603         self.db.issue.enableJournalling()
604         self.db.issue.set('1', title='hello world 2')
605         self.db.commit()
606         # see if the change was journalled
607         self.assertNotEqual(jlen,  len(self.db.getjournal('issue', '1')))
609     def testJournalPreCommit(self):
610         id = self.db.user.create(username="mary")
611         self.assertEqual(len(self.db.getjournal('user', id)), 1)
612         self.db.commit()
614     def testPack(self):
615         id = self.db.issue.create(title="spam", status='1')
616         self.db.commit()
617         self.db.issue.set(id, status='2')
618         self.db.commit()
620         # sleep for at least a second, then get a date to pack at
621         time.sleep(1)
622         pack_before = date.Date('.')
624         # wait another second and add one more entry
625         time.sleep(1)
626         self.db.issue.set(id, status='3')
627         self.db.commit()
628         jlen = len(self.db.getjournal('issue', id))
630         # pack
631         self.db.pack(pack_before)
633         # we should have the create and last set entries now
634         self.assertEqual(jlen-1, len(self.db.getjournal('issue', id)))
636     def testIndexerSearching(self):
637         f1 = self.db.file.create(content='hello', type="text/plain")
638         # content='world' has the wrong content-type and won't be indexed
639         f2 = self.db.file.create(content='world', type="text/frozz",
640             comment='blah blah')
641         i1 = self.db.issue.create(files=[f1, f2], title="flebble plop")
642         i2 = self.db.issue.create(title="flebble frooz")
643         self.db.commit()
644         self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
645             {i1: {'files': [f1]}})
646         self.assertEquals(self.db.indexer.search(['world'], self.db.issue), {})
647         self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
648             {i2: {}})
649         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
650             {i1: {}, i2: {}})
652     def testReindexing(self):
653         search = self.db.indexer.search
654         issue = self.db.issue
655         i1 = issue.create(title="flebble plop")
656         i2 = issue.create(title="flebble frooz")
657         self.db.commit()
658         self.assertEquals(search(['plop'], issue), {i1: {}})
659         self.assertEquals(search(['flebble'], issue), {i1: {}, i2: {}})
661         # change i1's title
662         issue.set(i1, title="plop")
663         self.db.commit()
664         self.assertEquals(search(['plop'], issue), {i1: {}})
665         self.assertEquals(search(['flebble'], issue), {i2: {}})
667         # unset i1's title
668         issue.set(i1, title="")
669         self.db.commit()
670         self.assertEquals(search(['plop'], issue), {})
671         self.assertEquals(search(['flebble'], issue), {i2: {}})
673     def testFileClassReindexing(self):
674         f1 = self.db.file.create(content='hello')
675         f2 = self.db.file.create(content='hello, world')
676         i1 = self.db.issue.create(files=[f1, f2])
677         self.db.commit()
678         d = self.db.indexer.search(['hello'], self.db.issue)
679         d[i1]['files'].sort()
680         self.assertEquals(d, {i1: {'files': [f1, f2]}})
681         self.assertEquals(self.db.indexer.search(['world'], self.db.issue),
682             {i1: {'files': [f2]}})
683         self.db.file.set(f1, content="world")
684         self.db.commit()
685         d = self.db.indexer.search(['world'], self.db.issue)
686         d[i1]['files'].sort()
687         self.assertEquals(d, {i1: {'files': [f1, f2]}})
688         self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
689             {i1: {'files': [f2]}})
692     def testForcedReindexing(self):
693         self.db.issue.create(title="flebble frooz")
694         self.db.commit()
695         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
696             {'1': {}})
697         self.db.indexer.quiet = 1
698         self.db.indexer.force_reindex()
699         self.db.post_init()
700         self.db.indexer.quiet = 9
701         self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
702             {'1': {}})
704     #
705     # searching tests follow
706     #
707     def testFindIncorrectProperty(self):
708         self.assertRaises(TypeError, self.db.issue.find, title='fubar')
710     def _find_test_setup(self):
711         self.db.file.create(content='')
712         self.db.file.create(content='')
713         self.db.user.create(username='')
714         one = self.db.issue.create(status="1", nosy=['1'])
715         two = self.db.issue.create(status="2", nosy=['2'], files=['1'],
716             assignedto='2')
717         three = self.db.issue.create(status="1", nosy=['1','2'])
718         four = self.db.issue.create(status="3", assignedto='1',
719             files=['1','2'])
720         return one, two, three, four
722     def testFindLink(self):
723         one, two, three, four = self._find_test_setup()
724         got = self.db.issue.find(status='1')
725         got.sort()
726         self.assertEqual(got, [one, three])
727         got = self.db.issue.find(status={'1':1})
728         got.sort()
729         self.assertEqual(got, [one, three])
731     def testFindLinkFail(self):
732         self._find_test_setup()
733         self.assertEqual(self.db.issue.find(status='4'), [])
734         self.assertEqual(self.db.issue.find(status={'4':1}), [])
736     def testFindLinkUnset(self):
737         one, two, three, four = self._find_test_setup()
738         got = self.db.issue.find(assignedto=None)
739         got.sort()
740         self.assertEqual(got, [one, three])
741         got = self.db.issue.find(assignedto={None:1})
742         got.sort()
743         self.assertEqual(got, [one, three])
745     def testFindMultilink(self):
746         one, two, three, four = self._find_test_setup()
747         got = self.db.issue.find(nosy='2')
748         got.sort()
749         self.assertEqual(got, [two, three])
750         got = self.db.issue.find(nosy={'2':1})
751         got.sort()
752         self.assertEqual(got, [two, three])
753         got = self.db.issue.find(nosy={'2':1}, files={})
754         got.sort()
755         self.assertEqual(got, [two, three])
757     def testFindMultiMultilink(self):
758         one, two, three, four = self._find_test_setup()
759         got = self.db.issue.find(nosy='2', files='1')
760         got.sort()
761         self.assertEqual(got, [two, three, four])
762         got = self.db.issue.find(nosy={'2':1}, files={'1':1})
763         got.sort()
764         self.assertEqual(got, [two, three, four])
766     def testFindMultilinkFail(self):
767         self._find_test_setup()
768         self.assertEqual(self.db.issue.find(nosy='3'), [])
769         self.assertEqual(self.db.issue.find(nosy={'3':1}), [])
771     def testFindMultilinkUnset(self):
772         self._find_test_setup()
773         self.assertEqual(self.db.issue.find(nosy={}), [])
775     def testFindLinkAndMultilink(self):
776         one, two, three, four = self._find_test_setup()
777         got = self.db.issue.find(status='1', nosy='2')
778         got.sort()
779         self.assertEqual(got, [one, two, three])
780         got = self.db.issue.find(status={'1':1}, nosy={'2':1})
781         got.sort()
782         self.assertEqual(got, [one, two, three])
784     def testFindRetired(self):
785         one, two, three, four = self._find_test_setup()
786         self.assertEqual(len(self.db.issue.find(status='1')), 2)
787         self.db.issue.retire(one)
788         self.assertEqual(len(self.db.issue.find(status='1')), 1)
790     def testStringFind(self):
791         self.assertRaises(TypeError, self.db.issue.stringFind, status='1')
793         ids = []
794         ids.append(self.db.issue.create(title="spam"))
795         self.db.issue.create(title="not spam")
796         ids.append(self.db.issue.create(title="spam"))
797         ids.sort()
798         got = self.db.issue.stringFind(title='spam')
799         got.sort()
800         self.assertEqual(got, ids)
801         self.assertEqual(self.db.issue.stringFind(title='fubar'), [])
803         # test retiring a node
804         self.db.issue.retire(ids[0])
805         self.assertEqual(len(self.db.issue.stringFind(title='spam')), 1)
807     def filteringSetup(self):
808         for user in (
809                 {'username': 'bleep'},
810                 {'username': 'blop'},
811                 {'username': 'blorp'}):
812             self.db.user.create(**user)
813         iss = self.db.issue
814         for issue in (
815                 {'title': 'issue one', 'status': '2', 'assignedto': '1',
816                     'foo': date.Interval('1:10'), 
817                     'deadline': date.Date('2003-01-01.00:00')},
818                     {'title': 'issue two', 'status': '1', 'assignedto': '2',
819                     'foo': date.Interval('1d'), 
820                     'deadline': date.Date('2003-02-16.22:50')},
821                 {'title': 'issue three', 'status': '1',
822                     'nosy': ['1','2'], 'deadline': date.Date('2003-02-18')},
823                 {'title': 'non four', 'status': '3',
824                     'foo': date.Interval('0:10'), 
825                     'nosy': ['1'], 'deadline': date.Date('2004-03-08')}):
826             self.db.issue.create(**issue)
827         self.db.commit()
828         return self.assertEqual, self.db.issue.filter
830     def testFilteringID(self):
831         ae, filt = self.filteringSetup()
832         ae(filt(None, {'id': '1'}, ('+','id'), (None,None)), ['1'])
833         ae(filt(None, {'id': '2'}, ('+','id'), (None,None)), ['2'])
834         ae(filt(None, {'id': '10'}, ('+','id'), (None,None)), [])
836     def testFilteringString(self):
837         ae, filt = self.filteringSetup()
838         ae(filt(None, {'title': ['one']}, ('+','id'), (None,None)), ['1'])
839         ae(filt(None, {'title': ['issue']}, ('+','id'), (None,None)),
840             ['1','2','3'])
841         ae(filt(None, {'title': ['one', 'two']}, ('+','id'), (None,None)),
842             ['1', '2'])
844     def testFilteringLink(self):
845         ae, filt = self.filteringSetup()
846         ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['2','3'])
847         ae(filt(None, {'assignedto': '-1'}, ('+','id'), (None,None)), ['3','4'])
849     def testFilteringRetired(self):
850         ae, filt = self.filteringSetup()
851         self.db.issue.retire('2')
852         ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['3'])
854     def testFilteringMultilink(self):
855         ae, filt = self.filteringSetup()
856         ae(filt(None, {'nosy': '2'}, ('+','id'), (None,None)), ['3'])
857         ae(filt(None, {'nosy': '-1'}, ('+','id'), (None,None)), ['1', '2'])
859     def testFilteringMany(self):
860         ae, filt = self.filteringSetup()
861         ae(filt(None, {'nosy': '2', 'status': '1'}, ('+','id'), (None,None)),
862             ['3'])
864     def testFilteringRange(self):
865         ae, filt = self.filteringSetup()
866         # Date ranges
867         ae(filt(None, {'deadline': 'from 2003-02-10 to 2003-02-23'}), ['2','3'])
868         ae(filt(None, {'deadline': '2003-02-10; 2003-02-23'}), ['2','3'])
869         ae(filt(None, {'deadline': '; 2003-02-16'}), ['1'])
870         # Lets assume people won't invent a time machine, otherwise this test
871         # may fail :)
872         ae(filt(None, {'deadline': 'from 2003-02-16'}), ['2', '3', '4'])
873         ae(filt(None, {'deadline': '2003-02-16;'}), ['2', '3', '4'])
874         # year and month granularity
875         ae(filt(None, {'deadline': '2002'}), [])
876         ae(filt(None, {'deadline': '2003'}), ['1', '2', '3'])
877         ae(filt(None, {'deadline': '2004'}), ['4'])
878         ae(filt(None, {'deadline': '2003-02'}), ['2', '3'])
879         ae(filt(None, {'deadline': '2003-03'}), [])
880         ae(filt(None, {'deadline': '2003-02-16'}), ['2'])
881         ae(filt(None, {'deadline': '2003-02-17'}), [])
882         # Interval ranges
883         ae(filt(None, {'foo': 'from 0:50 to 2:00'}), ['1'])
884         ae(filt(None, {'foo': 'from 0:50 to 1d 2:00'}), ['1', '2'])
885         ae(filt(None, {'foo': 'from 5:50'}), ['2'])
886         ae(filt(None, {'foo': 'to 0:05'}), [])
888     def testFilteringIntervalSort(self):
889         ae, filt = self.filteringSetup()
890         # ascending should sort None, 1:10, 1d
891         ae(filt(None, {}, ('+','foo'), (None,None)), ['3', '4', '1', '2'])
892         # descending should sort 1d, 1:10, None
893         ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3'])
895     def testFilteringMultilinkSort(self):
896         ae, filt = self.filteringSetup()
897         ae(filt(None, {}, ('+','nosy'), (None,None)), ['1', '2', '4', '3'])
898         ae(filt(None, {}, ('-','nosy'), (None,None)), ['3', '4', '1', '2'])
900 # XXX add sorting tests for other types
901 # XXX test auditors and reactors
903     def testImportExport(self):
904         # use the filtering setup to create a bunch of items
905         ae, filt = self.filteringSetup()
906         self.db.user.retire('3')
907         self.db.issue.retire('2')
909         # grab snapshot of the current database
910         orig = {}
911         for cn,klass in self.db.classes.items():
912             cl = orig[cn] = {}
913             for id in klass.list():
914                 it = cl[id] = {}
915                 for name in klass.getprops().keys():
916                     it[name] = klass.get(id, name)
918         # grab the export
919         export = {}
920         for cn,klass in self.db.classes.items():
921             names = klass.getprops().keys()
922             cl = export[cn] = [names+['is retired']]
923             for id in klass.getnodeids():
924                 cl.append(klass.export_list(names, id))
926         # shut down this db and nuke it
927         self.db.close()
928         self.nuke_database()
930         # open a new, empty database
931         os.makedirs(config.DATABASE + '/files')
932         self.db = self.module.Database(config, 'admin')
933         setupSchema(self.db, 0, self.module)
935         # import
936         for cn, items in export.items():
937             klass = self.db.classes[cn]
938             names = items[0]
939             maxid = 1
940             for itemprops in items[1:]:
941                 maxid = max(maxid, int(klass.import_list(names, itemprops)))
942             self.db.setid(cn, str(maxid+1))
944         # compare with snapshot of the database
945         for cn, items in orig.items():
946             klass = self.db.classes[cn]
947             propdefs = klass.getprops(1)
948             # ensure retired items are retired :)
949             l = items.keys(); l.sort()
950             m = klass.list(); m.sort()
951             ae(l, m)
952             for id, props in items.items():
953                 for name, value in props.items():
954                     l = klass.get(id, name)
955                     if isinstance(value, type([])):
956                         value.sort()
957                         l.sort()
958                     try:
959                         ae(l, value)
960                     except AssertionError:
961                         if not isinstance(propdefs[name], Date):
962                             raise
963                         # don't get hung up on rounding errors
964                         assert not l.__cmp__(value, int_seconds=1)
966         # make sure the retired items are actually imported
967         ae(self.db.user.get('4', 'username'), 'blop')
968         ae(self.db.issue.get('2', 'title'), 'issue two')
970         # make sure id counters are set correctly
971         maxid = max([int(id) for id in self.db.user.list()])
972         newid = self.db.user.create(username='testing')
973         assert newid > maxid
975     def testSafeGet(self):
976         # existent nodeid, existent property
977         self.assertEqual(self.db.user.safeget('1', 'username'), 'admin')
978         # nonexistent nodeid, existent property
979         self.assertEqual(self.db.user.safeget('999', 'username'), None)
980         # different default
981         self.assertEqual(self.db.issue.safeget('999', 'nosy', []), [])
983     def testAddProperty(self):
984         self.db.issue.create(title="spam", status='1')
985         self.db.commit()
987         self.db.issue.addprop(fixer=Link("user"))
988         # force any post-init stuff to happen
989         self.db.post_init()
990         props = self.db.issue.getprops()
991         keys = props.keys()
992         keys.sort()
993         self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
994             'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
995             'nosy', 'status', 'superseder', 'title'])
996         self.assertEqual(self.db.issue.get('1', "fixer"), None)
998     def testRemoveProperty(self):
999         self.db.issue.create(title="spam", status='1')
1000         self.db.commit()
1002         del self.db.issue.properties['title']
1003         self.db.post_init()
1004         props = self.db.issue.getprops()
1005         keys = props.keys()
1006         keys.sort()
1007         self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
1008             'creator', 'deadline', 'files', 'foo', 'id', 'messages',
1009             'nosy', 'status', 'superseder'])
1010         self.assertEqual(self.db.issue.list(), ['1'])
1012     def testAddRemoveProperty(self):
1013         self.db.issue.create(title="spam", status='1')
1014         self.db.commit()
1016         self.db.issue.addprop(fixer=Link("user"))
1017         del self.db.issue.properties['title']
1018         self.db.post_init()
1019         props = self.db.issue.getprops()
1020         keys = props.keys()
1021         keys.sort()
1022         self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
1023             'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
1024             'nosy', 'status', 'superseder'])
1025         self.assertEqual(self.db.issue.list(), ['1'])
1027 class ROTest(MyTestCase):
1028     def setUp(self):
1029         # remove previous test, ignore errors
1030         if os.path.exists(config.DATABASE):
1031             shutil.rmtree(config.DATABASE)
1032         os.makedirs(config.DATABASE + '/files')
1033         self.db = self.module.Database(config, 'admin')
1034         setupSchema(self.db, 1, self.module)
1035         self.db.close()
1037         self.db = self.module.Database(config)
1038         setupSchema(self.db, 0, self.module)
1040     def testExceptions(self):
1041         # this tests the exceptions that should be raised
1042         ar = self.assertRaises
1044         # this tests the exceptions that should be raised
1045         ar(DatabaseError, self.db.status.create, name="foo")
1046         ar(DatabaseError, self.db.status.set, '1', name="foo")
1047         ar(DatabaseError, self.db.status.retire, '1')
1050 class SchemaTest(MyTestCase):
1051     def setUp(self):
1052         # remove previous test, ignore errors
1053         if os.path.exists(config.DATABASE):
1054             shutil.rmtree(config.DATABASE)
1055         os.makedirs(config.DATABASE + '/files')
1057     def test_reservedProperties(self):
1058         self.db = self.module.Database(config, 'admin')
1059         self.assertRaises(ValueError, self.module.Class, self.db, "a",
1060             creation=String())
1061         self.assertRaises(ValueError, self.module.Class, self.db, "a",
1062             activity=String())
1063         self.assertRaises(ValueError, self.module.Class, self.db, "a",
1064             creator=String())
1065         self.assertRaises(ValueError, self.module.Class, self.db, "a",
1066             actor=String())
1068     def init_a(self):
1069         self.db = self.module.Database(config, 'admin')
1070         a = self.module.Class(self.db, "a", name=String())
1071         a.setkey("name")
1072         self.db.post_init()
1074     def init_ab(self):
1075         self.db = self.module.Database(config, 'admin')
1076         a = self.module.Class(self.db, "a", name=String())
1077         a.setkey("name")
1078         b = self.module.Class(self.db, "b", name=String(),
1079             fooz=Multilink('a'))
1080         b.setkey("name")
1081         self.db.post_init()
1083     def test_addNewClass(self):
1084         self.init_a()
1086         self.assertRaises(ValueError, self.module.Class, self.db, "a",
1087             name=String())
1089         aid = self.db.a.create(name='apple')
1090         self.db.commit(); self.db.close()
1092         # add a new class to the schema and check creation of new items
1093         # (and existence of old ones)
1094         self.init_ab()
1095         bid = self.db.b.create(name='bear', fooz=[aid])
1096         self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1097         self.db.commit()
1098         self.db.close()
1100         # now check we can recall the added class' items
1101         self.init_ab()
1102         self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1103         self.assertEqual(self.db.a.lookup('apple'), aid)
1104         self.assertEqual(self.db.b.get(bid, 'name'), 'bear')
1105         self.assertEqual(self.db.b.get(bid, 'fooz'), [aid])
1106         self.assertEqual(self.db.b.lookup('bear'), bid)
1108         # confirm journal's ok
1109         self.db.getjournal('a', aid)
1110         self.db.getjournal('b', bid)
1112     def init_amod(self):
1113         self.db = self.module.Database(config, 'admin')
1114         a = self.module.Class(self.db, "a", name=String(), fooz=String())
1115         a.setkey("name")
1116         b = self.module.Class(self.db, "b", name=String())
1117         b.setkey("name")
1118         self.db.post_init()
1120     def test_modifyClass(self):
1121         self.init_ab()
1123         # add item to user and issue class
1124         aid = self.db.a.create(name='apple')
1125         bid = self.db.b.create(name='bear')
1126         self.db.commit(); self.db.close()
1128         # modify "a" schema
1129         self.init_amod()
1130         self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1131         self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1132         self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1133         aid2 = self.db.a.create(name='aardvark', fooz='booz')
1134         self.db.commit(); self.db.close()
1136         # test
1137         self.init_amod()
1138         self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1139         self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1140         self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1141         self.assertEqual(self.db.a.get(aid2, 'name'), 'aardvark')
1142         self.assertEqual(self.db.a.get(aid2, 'fooz'), 'booz')
1144         # confirm journal's ok
1145         self.db.getjournal('a', aid)
1146         self.db.getjournal('a', aid2)
1148     def init_amodkey(self):
1149         self.db = self.module.Database(config, 'admin')
1150         a = self.module.Class(self.db, "a", name=String(), fooz=String())
1151         a.setkey("fooz")
1152         b = self.module.Class(self.db, "b", name=String())
1153         b.setkey("name")
1154         self.db.post_init()
1156     def test_changeClassKey(self):
1157         self.init_amod()
1158         aid = self.db.a.create(name='apple')
1159         self.assertEqual(self.db.a.lookup('apple'), aid)
1160         self.db.commit(); self.db.close()
1162         # change the key to fooz on a
1163         self.init_amodkey()
1164         self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1165         self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1166         self.assertRaises(KeyError, self.db.a.lookup, 'apple')
1167         aid2 = self.db.a.create(name='aardvark', fooz='booz')
1168         self.db.commit(); self.db.close()
1170         # check
1171         self.init_amodkey()
1172         self.assertEqual(self.db.a.lookup('booz'), aid2)
1174         # confirm journal's ok
1175         self.db.getjournal('a', aid)
1177     def init_ml(self):
1178         self.db = self.module.Database(config, 'admin')
1179         a = self.module.Class(self.db, "a", name=String(),
1180             fooz=Multilink('a'))
1181         a.setkey('name')
1182         self.db.post_init()
1184     def test_makeNewMultilink(self):
1185         self.init_a()
1186         aid = self.db.a.create(name='apple')
1187         self.assertEqual(self.db.a.lookup('apple'), aid)
1188         self.db.commit(); self.db.close()
1190         # add a multilink prop
1191         self.init_ml()
1192         bid = self.db.a.create(name='bear', fooz=[aid])
1193         self.assertEqual(self.db.a.find(fooz=aid), [bid])
1194         self.assertEqual(self.db.a.lookup('apple'), aid)
1195         self.db.commit(); self.db.close()
1197         # check
1198         self.init_ml()
1199         self.assertEqual(self.db.a.find(fooz=aid), [bid])
1200         self.assertEqual(self.db.a.lookup('apple'), aid)
1201         self.assertEqual(self.db.a.lookup('bear'), bid)
1203         # confirm journal's ok
1204         self.db.getjournal('a', aid)
1205         self.db.getjournal('a', bid)
1207     def test_removeMultilink(self):
1208         # add a multilink prop
1209         self.init_ml()
1210         aid = self.db.a.create(name='apple')
1211         bid = self.db.a.create(name='bear', fooz=[aid])
1212         self.assertEqual(self.db.a.find(fooz=aid), [bid])
1213         self.assertEqual(self.db.a.lookup('apple'), aid)
1214         self.assertEqual(self.db.a.lookup('bear'), bid)
1215         self.db.commit(); self.db.close()
1217         # remove the multilink
1218         self.init_a()
1219         self.assertEqual(self.db.a.lookup('apple'), aid)
1220         self.assertEqual(self.db.a.lookup('bear'), bid)
1222         # confirm journal's ok
1223         self.db.getjournal('a', aid)
1224         self.db.getjournal('a', bid)
1226     def test_removeClass(self):
1227         self.init_ml()
1228         aid = self.db.a.create(name='apple')
1229         bid = self.db.b.create(name='bear', fooz=[aid])
1230         self.db.commit(); self.db.close()
1232         # drop the b class
1233         self.init_a()
1234         self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1235         self.assertEqual(self.db.a.lookup('apple'), aid)
1236         self.db.commit(); self.db.close()
1238         # now check we can recall the added class' items
1239         self.init_a()
1240         self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1241         self.assertEqual(self.db.a.lookup('apple'), aid)
1243         # confirm journal's ok
1244         self.db.getjournal('a', aid)
1246 class RDBMSTest:
1247     ''' tests specific to RDBMS backends '''
1248     def test_indexTest(self):
1249         self.assertEqual(self.db.sql_index_exists('_issue', '_issue_id_idx'), 1)
1250         self.assertEqual(self.db.sql_index_exists('_issue', '_issue_x_idx'), 0)
1253 class ClassicInitTest(unittest.TestCase):
1254     count = 0
1255     db = None
1256     extra_config = ''
1258     def setUp(self):
1259         ClassicInitTest.count = ClassicInitTest.count + 1
1260         self.dirname = '_test_init_%s'%self.count
1261         try:
1262             shutil.rmtree(self.dirname)
1263         except OSError, error:
1264             if error.errno not in (errno.ENOENT, errno.ESRCH): raise
1266     def testCreation(self):
1267         ae = self.assertEqual
1269         # create the instance
1270         init.install(self.dirname, 'templates/classic')
1271         init.write_select_db(self.dirname, self.backend)
1273         if self.extra_config:
1274             f = open(os.path.join(self.dirname, 'config.py'), 'a')
1275             try:
1276                 f.write(self.extra_config)
1277             finally:
1278                 f.close()
1279         
1280         init.initialise(self.dirname, 'sekrit')
1282         # check we can load the package
1283         instance = imp.load_package(self.dirname, self.dirname)
1285         # and open the database
1286         db = self.db = instance.open()
1288         # check the basics of the schema and initial data set
1289         l = db.priority.list()
1290         ae(l, ['1', '2', '3', '4', '5'])
1291         l = db.status.list()
1292         ae(l, ['1', '2', '3', '4', '5', '6', '7', '8'])
1293         l = db.keyword.list()
1294         ae(l, [])
1295         l = db.user.list()
1296         ae(l, ['1', '2'])
1297         l = db.msg.list()
1298         ae(l, [])
1299         l = db.file.list()
1300         ae(l, [])
1301         l = db.issue.list()
1302         ae(l, [])
1304     def tearDown(self):
1305         if self.db is not None:
1306             self.db.close()
1307         try:
1308             shutil.rmtree(self.dirname)
1309         except OSError, error:
1310             if error.errno not in (errno.ENOENT, errno.ESRCH): raise