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.19 2004-03-22 07:45:40 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')
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 # XXX add sorting tests for other types
896 # XXX test auditors and reactors
898 def testImportExport(self):
899 # use the filtering setup to create a bunch of items
900 ae, filt = self.filteringSetup()
901 self.db.user.retire('3')
902 self.db.issue.retire('2')
904 # grab snapshot of the current database
905 orig = {}
906 for cn,klass in self.db.classes.items():
907 cl = orig[cn] = {}
908 for id in klass.list():
909 it = cl[id] = {}
910 for name in klass.getprops().keys():
911 it[name] = klass.get(id, name)
913 # grab the export
914 export = {}
915 for cn,klass in self.db.classes.items():
916 names = klass.getprops().keys()
917 cl = export[cn] = [names+['is retired']]
918 for id in klass.getnodeids():
919 cl.append(klass.export_list(names, id))
921 # shut down this db and nuke it
922 self.db.close()
923 self.nuke_database()
925 # open a new, empty database
926 os.makedirs(config.DATABASE + '/files')
927 self.db = self.module.Database(config, 'admin')
928 setupSchema(self.db, 0, self.module)
930 # import
931 for cn, items in export.items():
932 klass = self.db.classes[cn]
933 names = items[0]
934 maxid = 1
935 for itemprops in items[1:]:
936 maxid = max(maxid, int(klass.import_list(names, itemprops)))
937 self.db.setid(cn, str(maxid+1))
939 # compare with snapshot of the database
940 for cn, items in orig.items():
941 klass = self.db.classes[cn]
942 # ensure retired items are retired :)
943 l = items.keys(); l.sort()
944 m = klass.list(); m.sort()
945 ae(l, m)
946 for id, props in items.items():
947 for name, value in props.items():
948 l = klass.get(id, name)
949 if isinstance(value, type([])):
950 value.sort()
951 l.sort()
952 ae(l, value)
954 # make sure the retired items are actually imported
955 ae(self.db.user.get('4', 'username'), 'blop')
956 ae(self.db.issue.get('2', 'title'), 'issue two')
958 # make sure id counters are set correctly
959 maxid = max([int(id) for id in self.db.user.list()])
960 newid = self.db.user.create(username='testing')
961 assert newid > maxid
963 def testSafeGet(self):
964 # existent nodeid, existent property
965 self.assertEqual(self.db.user.safeget('1', 'username'), 'admin')
966 # nonexistent nodeid, existent property
967 self.assertEqual(self.db.user.safeget('999', 'username'), None)
968 # different default
969 self.assertEqual(self.db.issue.safeget('999', 'nosy', []), [])
971 def testAddProperty(self):
972 self.db.issue.create(title="spam", status='1')
973 self.db.commit()
975 self.db.issue.addprop(fixer=Link("user"))
976 # force any post-init stuff to happen
977 self.db.post_init()
978 props = self.db.issue.getprops()
979 keys = props.keys()
980 keys.sort()
981 self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
982 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
983 'nosy', 'status', 'superseder', 'title'])
984 self.assertEqual(self.db.issue.get('1', "fixer"), None)
986 def testRemoveProperty(self):
987 self.db.issue.create(title="spam", status='1')
988 self.db.commit()
990 del self.db.issue.properties['title']
991 self.db.post_init()
992 props = self.db.issue.getprops()
993 keys = props.keys()
994 keys.sort()
995 self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
996 'creator', 'deadline', 'files', 'foo', 'id', 'messages',
997 'nosy', 'status', 'superseder'])
998 self.assertEqual(self.db.issue.list(), ['1'])
1000 def testAddRemoveProperty(self):
1001 self.db.issue.create(title="spam", status='1')
1002 self.db.commit()
1004 self.db.issue.addprop(fixer=Link("user"))
1005 del self.db.issue.properties['title']
1006 self.db.post_init()
1007 props = self.db.issue.getprops()
1008 keys = props.keys()
1009 keys.sort()
1010 self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
1011 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
1012 'nosy', 'status', 'superseder'])
1013 self.assertEqual(self.db.issue.list(), ['1'])
1015 class ROTest(MyTestCase):
1016 def setUp(self):
1017 # remove previous test, ignore errors
1018 if os.path.exists(config.DATABASE):
1019 shutil.rmtree(config.DATABASE)
1020 os.makedirs(config.DATABASE + '/files')
1021 self.db = self.module.Database(config, 'admin')
1022 setupSchema(self.db, 1, self.module)
1023 self.db.close()
1025 self.db = self.module.Database(config)
1026 setupSchema(self.db, 0, self.module)
1028 def testExceptions(self):
1029 # this tests the exceptions that should be raised
1030 ar = self.assertRaises
1032 # this tests the exceptions that should be raised
1033 ar(DatabaseError, self.db.status.create, name="foo")
1034 ar(DatabaseError, self.db.status.set, '1', name="foo")
1035 ar(DatabaseError, self.db.status.retire, '1')
1038 class SchemaTest(MyTestCase):
1039 def setUp(self):
1040 # remove previous test, ignore errors
1041 if os.path.exists(config.DATABASE):
1042 shutil.rmtree(config.DATABASE)
1043 os.makedirs(config.DATABASE + '/files')
1045 def test_reservedProperties(self):
1046 self.db = self.module.Database(config, 'admin')
1047 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1048 creation=String())
1049 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1050 activity=String())
1051 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1052 creator=String())
1053 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1054 actor=String())
1056 def init_a(self):
1057 self.db = self.module.Database(config, 'admin')
1058 a = self.module.Class(self.db, "a", name=String())
1059 a.setkey("name")
1060 self.db.post_init()
1062 def init_ab(self):
1063 self.db = self.module.Database(config, 'admin')
1064 a = self.module.Class(self.db, "a", name=String())
1065 a.setkey("name")
1066 b = self.module.Class(self.db, "b", name=String())
1067 b.setkey("name")
1068 self.db.post_init()
1070 def test_addNewClass(self):
1071 self.init_a()
1073 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1074 name=String())
1076 aid = self.db.a.create(name='apple')
1077 self.db.commit(); self.db.close()
1079 # add a new class to the schema and check creation of new items
1080 # (and existence of old ones)
1081 self.init_ab()
1082 bid = self.db.b.create(name='bear')
1083 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1084 self.db.commit()
1085 self.db.close()
1087 # now check we can recall the added class' items
1088 self.init_ab()
1089 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1090 self.assertEqual(self.db.a.lookup('apple'), aid)
1091 self.assertEqual(self.db.b.get(bid, 'name'), 'bear')
1092 self.assertEqual(self.db.b.lookup('bear'), bid)
1094 # confirm journal's ok
1095 self.db.getjournal('a', aid)
1096 self.db.getjournal('b', bid)
1098 def init_amod(self):
1099 self.db = self.module.Database(config, 'admin')
1100 a = self.module.Class(self.db, "a", name=String(), fooz=String())
1101 a.setkey("name")
1102 b = self.module.Class(self.db, "b", name=String())
1103 b.setkey("name")
1104 self.db.post_init()
1106 def test_modifyClass(self):
1107 self.init_ab()
1109 # add item to user and issue class
1110 aid = self.db.a.create(name='apple')
1111 bid = self.db.b.create(name='bear')
1112 self.db.commit(); self.db.close()
1114 # modify "a" schema
1115 self.init_amod()
1116 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1117 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1118 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1119 aid2 = self.db.a.create(name='aardvark', fooz='booz')
1120 self.db.commit(); self.db.close()
1122 # test
1123 self.init_amod()
1124 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1125 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1126 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1127 self.assertEqual(self.db.a.get(aid2, 'name'), 'aardvark')
1128 self.assertEqual(self.db.a.get(aid2, 'fooz'), 'booz')
1130 # confirm journal's ok
1131 self.db.getjournal('a', aid)
1132 self.db.getjournal('a', aid2)
1134 def init_amodkey(self):
1135 self.db = self.module.Database(config, 'admin')
1136 a = self.module.Class(self.db, "a", name=String(), fooz=String())
1137 a.setkey("fooz")
1138 b = self.module.Class(self.db, "b", name=String())
1139 b.setkey("name")
1140 self.db.post_init()
1142 def test_changeClassKey(self):
1143 self.init_amod()
1144 aid = self.db.a.create(name='apple')
1145 self.assertEqual(self.db.a.lookup('apple'), aid)
1146 self.db.commit(); self.db.close()
1148 # change the key to fooz on a
1149 self.init_amodkey()
1150 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1151 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1152 self.assertRaises(KeyError, self.db.a.lookup, 'apple')
1153 aid2 = self.db.a.create(name='aardvark', fooz='booz')
1154 self.db.commit(); self.db.close()
1156 # check
1157 self.init_amodkey()
1158 self.assertEqual(self.db.a.lookup('booz'), aid2)
1160 # confirm journal's ok
1161 self.db.getjournal('a', aid)
1163 def init_ml(self):
1164 self.db = self.module.Database(config, 'admin')
1165 a = self.module.Class(self.db, "a", name=String())
1166 a.setkey('name')
1167 b = self.module.Class(self.db, "b", name=String(),
1168 fooz=Multilink('a'))
1169 b.setkey("name")
1170 self.db.post_init()
1172 def test_makeNewMultilink(self):
1173 self.init_a()
1174 aid = self.db.a.create(name='apple')
1175 self.assertEqual(self.db.a.lookup('apple'), aid)
1176 self.db.commit(); self.db.close()
1178 # add a multilink prop
1179 self.init_ml()
1180 bid = self.db.b.create(name='bear', fooz=[aid])
1181 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1182 self.assertEqual(self.db.a.lookup('apple'), aid)
1183 self.db.commit(); self.db.close()
1185 # check
1186 self.init_ml()
1187 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1188 self.assertEqual(self.db.a.lookup('apple'), aid)
1189 self.assertEqual(self.db.b.lookup('bear'), bid)
1191 # confirm journal's ok
1192 self.db.getjournal('a', aid)
1193 self.db.getjournal('b', bid)
1195 def test_removeMultilink(self):
1196 # add a multilink prop
1197 self.init_ml()
1198 aid = self.db.a.create(name='apple')
1199 bid = self.db.b.create(name='bear', fooz=[aid])
1200 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1201 self.assertEqual(self.db.a.lookup('apple'), aid)
1202 self.assertEqual(self.db.b.lookup('bear'), bid)
1203 self.db.commit(); self.db.close()
1205 # remove the multilink
1206 self.init_ab()
1207 self.assertEqual(self.db.a.lookup('apple'), aid)
1208 self.assertEqual(self.db.b.lookup('bear'), bid)
1210 # confirm journal's ok
1211 self.db.getjournal('a', aid)
1212 self.db.getjournal('b', bid)
1214 def test_removeClass(self):
1215 self.init_ml()
1216 aid = self.db.a.create(name='apple')
1217 bid = self.db.b.create(name='bear', fooz=[aid])
1218 self.db.commit(); self.db.close()
1220 # drop the b class
1221 self.init_a()
1222 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1223 self.assertEqual(self.db.a.lookup('apple'), aid)
1224 self.db.commit(); self.db.close()
1226 # now check we can recall the added class' items
1227 self.init_a()
1228 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1229 self.assertEqual(self.db.a.lookup('apple'), aid)
1231 # confirm journal's ok
1232 self.db.getjournal('a', aid)
1234 class RDBMSTest:
1235 ''' tests specific to RDBMS backends '''
1236 def test_indexTest(self):
1237 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_id_idx'), 1)
1238 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_x_idx'), 0)
1241 class ClassicInitTest(unittest.TestCase):
1242 count = 0
1243 db = None
1244 extra_config = ''
1246 def setUp(self):
1247 ClassicInitTest.count = ClassicInitTest.count + 1
1248 self.dirname = '_test_init_%s'%self.count
1249 try:
1250 shutil.rmtree(self.dirname)
1251 except OSError, error:
1252 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
1254 def testCreation(self):
1255 ae = self.assertEqual
1257 # create the instance
1258 init.install(self.dirname, 'templates/classic')
1259 init.write_select_db(self.dirname, self.backend)
1261 if self.extra_config:
1262 f = open(os.path.join(self.dirname, 'config.py'), 'a')
1263 try:
1264 f.write(self.extra_config)
1265 finally:
1266 f.close()
1268 init.initialise(self.dirname, 'sekrit')
1270 # check we can load the package
1271 instance = imp.load_package(self.dirname, self.dirname)
1273 # and open the database
1274 db = self.db = instance.open()
1276 # check the basics of the schema and initial data set
1277 l = db.priority.list()
1278 ae(l, ['1', '2', '3', '4', '5'])
1279 l = db.status.list()
1280 ae(l, ['1', '2', '3', '4', '5', '6', '7', '8'])
1281 l = db.keyword.list()
1282 ae(l, [])
1283 l = db.user.list()
1284 ae(l, ['1', '2'])
1285 l = db.msg.list()
1286 ae(l, [])
1287 l = db.file.list()
1288 ae(l, [])
1289 l = db.issue.list()
1290 ae(l, [])
1292 def tearDown(self):
1293 if self.db is not None:
1294 self.db.close()
1295 try:
1296 shutil.rmtree(self.dirname)
1297 except OSError, error:
1298 if error.errno not in (errno.ENOENT, errno.ESRCH): raise