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.14 2004-01-20 05:55:51 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
26 from roundup.indexer import Indexer
28 def setupSchema(db, create, module):
29 status = module.Class(db, "status", name=String())
30 status.setkey("name")
31 user = module.Class(db, "user", username=String(), password=Password(),
32 assignable=Boolean(), age=Number(), roles=String())
33 user.setkey("username")
34 file = module.FileClass(db, "file", name=String(), type=String(),
35 comment=String(indexme="yes"), fooz=Password())
36 issue = module.IssueClass(db, "issue", title=String(indexme="yes"),
37 status=Link("status"), nosy=Multilink("user"), deadline=Date(),
38 foo=Interval(), files=Multilink("file"), assignedto=Link('user'))
39 stuff = module.Class(db, "stuff", stuff=String())
40 session = module.Class(db, 'session', title=String())
41 session.disableJournalling()
42 db.post_init()
43 if create:
44 user.create(username="admin", roles='Admin',
45 password=password.Password('sekrit'))
46 status.create(name="unread")
47 status.create(name="in-progress")
48 status.create(name="testing")
49 status.create(name="resolved")
50 db.commit()
52 class MyTestCase(unittest.TestCase):
53 def tearDown(self):
54 if hasattr(self, 'db'):
55 self.db.close()
56 if os.path.exists('_test_dir'):
57 shutil.rmtree('_test_dir')
59 class config:
60 DATABASE='_test_dir'
61 MAILHOST = 'localhost'
62 MAIL_DOMAIN = 'fill.me.in.'
63 TRACKER_NAME = 'Roundup issue tracker'
64 TRACKER_EMAIL = 'issue_tracker@%s'%MAIL_DOMAIN
65 TRACKER_WEB = 'http://some.useful.url/'
66 ADMIN_EMAIL = 'roundup-admin@%s'%MAIL_DOMAIN
67 FILTER_POSITION = 'bottom' # one of 'top', 'bottom', 'top and bottom'
68 ANONYMOUS_ACCESS = 'deny' # either 'deny' or 'allow'
69 ANONYMOUS_REGISTER = 'deny' # either 'deny' or 'allow'
70 MESSAGES_TO_AUTHOR = 'no' # either 'yes' or 'no'
71 EMAIL_SIGNATURE_POSITION = 'bottom'
74 class DBTest(MyTestCase):
75 def setUp(self):
76 # remove previous test, ignore errors
77 if os.path.exists(config.DATABASE):
78 shutil.rmtree(config.DATABASE)
79 os.makedirs(config.DATABASE + '/files')
80 self.db = self.module.Database(config, 'admin')
81 setupSchema(self.db, 1, self.module)
83 def testRefresh(self):
84 self.db.refresh_database()
86 #
87 # basic operations
88 #
89 def testIDGeneration(self):
90 id1 = self.db.issue.create(title="spam", status='1')
91 id2 = self.db.issue.create(title="eggs", status='2')
92 self.assertNotEqual(id1, id2)
94 def testEmptySet(self):
95 id1 = self.db.issue.create(title="spam", status='1')
96 self.db.issue.set(id1)
98 def testStringChange(self):
99 for commit in (0,1):
100 # test set & retrieve
101 nid = self.db.issue.create(title="spam", status='1')
102 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
104 # change and make sure we retrieve the correct value
105 self.db.issue.set(nid, title='eggs')
106 if commit: self.db.commit()
107 self.assertEqual(self.db.issue.get(nid, 'title'), 'eggs')
109 def testStringUnset(self):
110 for commit in (0,1):
111 nid = self.db.issue.create(title="spam", status='1')
112 if commit: self.db.commit()
113 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
114 # make sure we can unset
115 self.db.issue.set(nid, title=None)
116 if commit: self.db.commit()
117 self.assertEqual(self.db.issue.get(nid, "title"), None)
119 def testLinkChange(self):
120 self.assertRaises(IndexError, self.db.issue.create, title="spam",
121 status='100')
122 for commit in (0,1):
123 nid = self.db.issue.create(title="spam", status='1')
124 if commit: self.db.commit()
125 self.assertEqual(self.db.issue.get(nid, "status"), '1')
126 self.db.issue.set(nid, status='2')
127 if commit: self.db.commit()
128 self.assertEqual(self.db.issue.get(nid, "status"), '2')
130 def testLinkUnset(self):
131 for commit in (0,1):
132 nid = self.db.issue.create(title="spam", status='1')
133 if commit: self.db.commit()
134 self.db.issue.set(nid, status=None)
135 if commit: self.db.commit()
136 self.assertEqual(self.db.issue.get(nid, "status"), None)
138 def testMultilinkChange(self):
139 for commit in (0,1):
140 self.assertRaises(IndexError, self.db.issue.create, title="spam",
141 nosy=['foo%s'%commit])
142 u1 = self.db.user.create(username='foo%s'%commit)
143 u2 = self.db.user.create(username='bar%s'%commit)
144 nid = self.db.issue.create(title="spam", nosy=[u1])
145 if commit: self.db.commit()
146 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
147 self.db.issue.set(nid, nosy=[])
148 if commit: self.db.commit()
149 self.assertEqual(self.db.issue.get(nid, "nosy"), [])
150 self.db.issue.set(nid, nosy=[u1,u2])
151 if commit: self.db.commit()
152 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1,u2])
154 def testDateChange(self):
155 self.assertRaises(TypeError, self.db.issue.create,
156 title='spam', deadline=1)
157 for commit in (0,1):
158 nid = self.db.issue.create(title="spam", status='1')
159 self.assertRaises(TypeError, self.db.issue.set, nid, deadline=1)
160 a = self.db.issue.get(nid, "deadline")
161 if commit: self.db.commit()
162 self.db.issue.set(nid, deadline=date.Date())
163 b = self.db.issue.get(nid, "deadline")
164 if commit: self.db.commit()
165 self.assertNotEqual(a, b)
166 self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
168 def testDateUnset(self):
169 for commit in (0,1):
170 nid = self.db.issue.create(title="spam", status='1')
171 self.db.issue.set(nid, deadline=date.Date())
172 if commit: self.db.commit()
173 self.assertNotEqual(self.db.issue.get(nid, "deadline"), None)
174 self.db.issue.set(nid, deadline=None)
175 if commit: self.db.commit()
176 self.assertEqual(self.db.issue.get(nid, "deadline"), None)
178 def testIntervalChange(self):
179 self.assertRaises(TypeError, self.db.issue.create,
180 title='spam', foo=1)
181 for commit in (0,1):
182 nid = self.db.issue.create(title="spam", status='1')
183 self.assertRaises(TypeError, self.db.issue.set, nid, foo=1)
184 if commit: self.db.commit()
185 a = self.db.issue.get(nid, "foo")
186 i = date.Interval('-1d')
187 self.db.issue.set(nid, foo=i)
188 if commit: self.db.commit()
189 self.assertNotEqual(self.db.issue.get(nid, "foo"), a)
190 self.assertEqual(i, self.db.issue.get(nid, "foo"))
191 j = date.Interval('1y')
192 self.db.issue.set(nid, foo=j)
193 if commit: self.db.commit()
194 self.assertNotEqual(self.db.issue.get(nid, "foo"), i)
195 self.assertEqual(j, self.db.issue.get(nid, "foo"))
197 def testIntervalUnset(self):
198 for commit in (0,1):
199 nid = self.db.issue.create(title="spam", status='1')
200 self.db.issue.set(nid, foo=date.Interval('-1d'))
201 if commit: self.db.commit()
202 self.assertNotEqual(self.db.issue.get(nid, "foo"), None)
203 self.db.issue.set(nid, foo=None)
204 if commit: self.db.commit()
205 self.assertEqual(self.db.issue.get(nid, "foo"), None)
207 def testBooleanChange(self):
208 userid = self.db.user.create(username='foo', assignable=1)
209 self.assertEqual(1, self.db.user.get(userid, 'assignable'))
210 self.db.user.set(userid, assignable=0)
211 self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
213 def testBooleanUnset(self):
214 nid = self.db.user.create(username='foo', assignable=1)
215 self.db.user.set(nid, assignable=None)
216 self.assertEqual(self.db.user.get(nid, "assignable"), None)
218 def testNumberChange(self):
219 nid = self.db.user.create(username='foo', age=1)
220 self.assertEqual(1, self.db.user.get(nid, 'age'))
221 self.db.user.set(nid, age=3)
222 self.assertNotEqual(self.db.user.get(nid, 'age'), 1)
223 self.db.user.set(nid, age=1.0)
224 self.assertEqual(self.db.user.get(nid, 'age'), 1)
225 self.db.user.set(nid, age=0)
226 self.assertEqual(self.db.user.get(nid, 'age'), 0)
228 nid = self.db.user.create(username='bar', age=0)
229 self.assertEqual(self.db.user.get(nid, 'age'), 0)
231 def testNumberUnset(self):
232 nid = self.db.user.create(username='foo', age=1)
233 self.db.user.set(nid, age=None)
234 self.assertEqual(self.db.user.get(nid, "age"), None)
236 def testPasswordChange(self):
237 x = password.Password('x')
238 userid = self.db.user.create(username='foo', password=x)
239 self.assertEqual(x, self.db.user.get(userid, 'password'))
240 self.assertEqual(self.db.user.get(userid, 'password'), 'x')
241 y = password.Password('y')
242 self.db.user.set(userid, password=y)
243 self.assertEqual(self.db.user.get(userid, 'password'), 'y')
244 self.assertRaises(TypeError, self.db.user.create, userid,
245 username='bar', password='x')
246 self.assertRaises(TypeError, self.db.user.set, userid, password='x')
248 def testPasswordUnset(self):
249 x = password.Password('x')
250 nid = self.db.user.create(username='foo', password=x)
251 self.db.user.set(nid, assignable=None)
252 self.assertEqual(self.db.user.get(nid, "assignable"), None)
254 def testKeyValue(self):
255 self.assertRaises(ValueError, self.db.user.create)
257 newid = self.db.user.create(username="spam")
258 self.assertEqual(self.db.user.lookup('spam'), newid)
259 self.db.commit()
260 self.assertEqual(self.db.user.lookup('spam'), newid)
261 self.db.user.retire(newid)
262 self.assertRaises(KeyError, self.db.user.lookup, 'spam')
264 # use the key again now that the old is retired
265 newid2 = self.db.user.create(username="spam")
266 self.assertNotEqual(newid, newid2)
267 # try to restore old node. this shouldn't succeed!
268 self.assertRaises(KeyError, self.db.user.restore, newid)
270 self.assertRaises(TypeError, self.db.issue.lookup, 'fubar')
272 def testLabelProp(self):
273 # key prop
274 self.assertEqual(self.db.status.labelprop(), 'name')
275 self.assertEqual(self.db.user.labelprop(), 'username')
276 # title
277 self.assertEqual(self.db.issue.labelprop(), 'title')
278 # name
279 self.assertEqual(self.db.file.labelprop(), 'name')
280 # id
281 self.assertEqual(self.db.stuff.labelprop(default_to_id=1), 'id')
283 def testRetire(self):
284 self.db.issue.create(title="spam", status='1')
285 b = self.db.status.get('1', 'name')
286 a = self.db.status.list()
287 self.db.status.retire('1')
288 # make sure the list is different
289 self.assertNotEqual(a, self.db.status.list())
290 # can still access the node if necessary
291 self.assertEqual(self.db.status.get('1', 'name'), b)
292 self.assertRaises(IndexError, self.db.status.set, '1', name='hello')
293 self.db.commit()
294 self.assertEqual(self.db.status.get('1', 'name'), b)
295 self.assertNotEqual(a, self.db.status.list())
296 # try to restore retired node
297 self.db.status.restore('1')
299 def testCacheCreateSet(self):
300 self.db.issue.create(title="spam", status='1')
301 a = self.db.issue.get('1', 'title')
302 self.assertEqual(a, 'spam')
303 self.db.issue.set('1', title='ham')
304 b = self.db.issue.get('1', 'title')
305 self.assertEqual(b, 'ham')
307 def testSerialisation(self):
308 nid = self.db.issue.create(title="spam", status='1',
309 deadline=date.Date(), foo=date.Interval('-1d'))
310 self.db.commit()
311 assert isinstance(self.db.issue.get(nid, 'deadline'), date.Date)
312 assert isinstance(self.db.issue.get(nid, 'foo'), date.Interval)
313 uid = self.db.user.create(username="fozzy",
314 password=password.Password('t. bear'))
315 self.db.commit()
316 assert isinstance(self.db.user.get(uid, 'password'), password.Password)
318 def testTransactions(self):
319 # remember the number of items we started
320 num_issues = len(self.db.issue.list())
321 num_files = self.db.numfiles()
322 self.db.issue.create(title="don't commit me!", status='1')
323 self.assertNotEqual(num_issues, len(self.db.issue.list()))
324 self.db.rollback()
325 self.assertEqual(num_issues, len(self.db.issue.list()))
326 self.db.issue.create(title="please commit me!", status='1')
327 self.assertNotEqual(num_issues, len(self.db.issue.list()))
328 self.db.commit()
329 self.assertNotEqual(num_issues, len(self.db.issue.list()))
330 self.db.rollback()
331 self.assertNotEqual(num_issues, len(self.db.issue.list()))
332 self.db.file.create(name="test", type="text/plain", content="hi")
333 self.db.rollback()
334 self.assertEqual(num_files, self.db.numfiles())
335 for i in range(10):
336 self.db.file.create(name="test", type="text/plain",
337 content="hi %d"%(i))
338 self.db.commit()
339 num_files2 = self.db.numfiles()
340 self.assertNotEqual(num_files, num_files2)
341 self.db.file.create(name="test", type="text/plain", content="hi")
342 self.db.rollback()
343 self.assertNotEqual(num_files, self.db.numfiles())
344 self.assertEqual(num_files2, self.db.numfiles())
346 # rollback / cache interaction
347 name1 = self.db.user.get('1', 'username')
348 self.db.user.set('1', username = name1+name1)
349 # get the prop so the info's forced into the cache (if there is one)
350 self.db.user.get('1', 'username')
351 self.db.rollback()
352 name2 = self.db.user.get('1', 'username')
353 self.assertEqual(name1, name2)
355 def testDestroyNoJournalling(self):
356 self.innerTestDestroy(klass=self.db.session)
358 def testDestroyJournalling(self):
359 self.innerTestDestroy(klass=self.db.issue)
361 def innerTestDestroy(self, klass):
362 newid = klass.create(title='Mr Friendly')
363 n = len(klass.list())
364 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
365 count = klass.count()
366 klass.destroy(newid)
367 self.assertNotEqual(count, klass.count())
368 self.assertRaises(IndexError, klass.get, newid, 'title')
369 self.assertNotEqual(len(klass.list()), n)
370 if klass.do_journal:
371 self.assertRaises(IndexError, klass.history, newid)
373 # now with a commit
374 newid = klass.create(title='Mr Friendly')
375 n = len(klass.list())
376 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
377 self.db.commit()
378 count = klass.count()
379 klass.destroy(newid)
380 self.assertNotEqual(count, klass.count())
381 self.assertRaises(IndexError, klass.get, newid, 'title')
382 self.db.commit()
383 self.assertRaises(IndexError, klass.get, newid, 'title')
384 self.assertNotEqual(len(klass.list()), n)
385 if klass.do_journal:
386 self.assertRaises(IndexError, klass.history, newid)
388 # now with a rollback
389 newid = klass.create(title='Mr Friendly')
390 n = len(klass.list())
391 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
392 self.db.commit()
393 count = klass.count()
394 klass.destroy(newid)
395 self.assertNotEqual(len(klass.list()), n)
396 self.assertRaises(IndexError, klass.get, newid, 'title')
397 self.db.rollback()
398 self.assertEqual(count, klass.count())
399 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
400 self.assertEqual(len(klass.list()), n)
401 if klass.do_journal:
402 self.assertNotEqual(klass.history(newid), [])
404 def testExceptions(self):
405 # this tests the exceptions that should be raised
406 ar = self.assertRaises
408 ar(KeyError, self.db.getclass, 'fubar')
410 #
411 # class create
412 #
413 # string property
414 ar(TypeError, self.db.status.create, name=1)
415 # id, creation, creator and activity properties are reserved
416 ar(KeyError, self.db.status.create, id=1)
417 ar(KeyError, self.db.status.create, creation=1)
418 ar(KeyError, self.db.status.create, creator=1)
419 ar(KeyError, self.db.status.create, activity=1)
420 # invalid property name
421 ar(KeyError, self.db.status.create, foo='foo')
422 # key name clash
423 ar(ValueError, self.db.status.create, name='unread')
424 # invalid link index
425 ar(IndexError, self.db.issue.create, title='foo', status='bar')
426 # invalid link value
427 ar(ValueError, self.db.issue.create, title='foo', status=1)
428 # invalid multilink type
429 ar(TypeError, self.db.issue.create, title='foo', status='1',
430 nosy='hello')
431 # invalid multilink index type
432 ar(ValueError, self.db.issue.create, title='foo', status='1',
433 nosy=[1])
434 # invalid multilink index
435 ar(IndexError, self.db.issue.create, title='foo', status='1',
436 nosy=['10'])
438 #
439 # key property
440 #
441 # key must be a String
442 ar(TypeError, self.db.file.setkey, 'fooz')
443 # key must exist
444 ar(KeyError, self.db.file.setkey, 'fubar')
446 #
447 # class get
448 #
449 # invalid node id
450 ar(IndexError, self.db.issue.get, '99', 'title')
451 # invalid property name
452 ar(KeyError, self.db.status.get, '2', 'foo')
454 #
455 # class set
456 #
457 # invalid node id
458 ar(IndexError, self.db.issue.set, '99', title='foo')
459 # invalid property name
460 ar(KeyError, self.db.status.set, '1', foo='foo')
461 # string property
462 ar(TypeError, self.db.status.set, '1', name=1)
463 # key name clash
464 ar(ValueError, self.db.status.set, '2', name='unread')
465 # set up a valid issue for me to work on
466 id = self.db.issue.create(title="spam", status='1')
467 # invalid link index
468 ar(IndexError, self.db.issue.set, id, title='foo', status='bar')
469 # invalid link value
470 ar(ValueError, self.db.issue.set, id, title='foo', status=1)
471 # invalid multilink type
472 ar(TypeError, self.db.issue.set, id, title='foo', status='1',
473 nosy='hello')
474 # invalid multilink index type
475 ar(ValueError, self.db.issue.set, id, title='foo', status='1',
476 nosy=[1])
477 # invalid multilink index
478 ar(IndexError, self.db.issue.set, id, title='foo', status='1',
479 nosy=['10'])
480 # NOTE: the following increment the username to avoid problems
481 # within metakit's backend (it creates the node, and then sets the
482 # info, so the create (and by a fluke the username set) go through
483 # before the age/assignable/etc. set, which raises the exception)
484 # invalid number value
485 ar(TypeError, self.db.user.create, username='foo', age='a')
486 # invalid boolean value
487 ar(TypeError, self.db.user.create, username='foo2', assignable='true')
488 nid = self.db.user.create(username='foo3')
489 # invalid number value
490 ar(TypeError, self.db.user.set, nid, age='a')
491 # invalid boolean value
492 ar(TypeError, self.db.user.set, nid, assignable='true')
494 def testJournals(self):
495 self.db.user.create(username="mary")
496 self.db.user.create(username="pete")
497 self.db.issue.create(title="spam", status='1')
498 self.db.commit()
500 # journal entry for issue create
501 journal = self.db.getjournal('issue', '1')
502 self.assertEqual(1, len(journal))
503 (nodeid, date_stamp, journaltag, action, params) = journal[0]
504 self.assertEqual(nodeid, '1')
505 self.assertEqual(journaltag, self.db.user.lookup('admin'))
506 self.assertEqual(action, 'create')
507 keys = params.keys()
508 keys.sort()
509 self.assertEqual(keys, [])
511 # journal entry for link
512 journal = self.db.getjournal('user', '1')
513 self.assertEqual(1, len(journal))
514 self.db.issue.set('1', assignedto='1')
515 self.db.commit()
516 journal = self.db.getjournal('user', '1')
517 self.assertEqual(2, len(journal))
518 (nodeid, date_stamp, journaltag, action, params) = journal[1]
519 self.assertEqual('1', nodeid)
520 self.assertEqual('1', journaltag)
521 self.assertEqual('link', action)
522 self.assertEqual(('issue', '1', 'assignedto'), params)
524 # journal entry for unlink
525 self.db.issue.set('1', assignedto='2')
526 self.db.commit()
527 journal = self.db.getjournal('user', '1')
528 self.assertEqual(3, len(journal))
529 (nodeid, date_stamp, journaltag, action, params) = journal[2]
530 self.assertEqual('1', nodeid)
531 self.assertEqual('1', journaltag)
532 self.assertEqual('unlink', action)
533 self.assertEqual(('issue', '1', 'assignedto'), params)
535 # test disabling journalling
536 # ... get the last entry
537 time.sleep(1)
538 entry = self.db.getjournal('issue', '1')[-1]
539 (x, date_stamp, x, x, x) = entry
540 self.db.issue.disableJournalling()
541 self.db.issue.set('1', title='hello world')
542 self.db.commit()
543 entry = self.db.getjournal('issue', '1')[-1]
544 (x, date_stamp2, x, x, x) = entry
545 # see if the change was journalled when it shouldn't have been
546 self.assertEqual(date_stamp, date_stamp2)
547 time.sleep(1)
548 self.db.issue.enableJournalling()
549 self.db.issue.set('1', title='hello world 2')
550 self.db.commit()
551 entry = self.db.getjournal('issue', '1')[-1]
552 (x, date_stamp2, x, x, x) = entry
553 # see if the change was journalled
554 self.assertNotEqual(date_stamp, date_stamp2)
556 def testJournalPreCommit(self):
557 id = self.db.user.create(username="mary")
558 self.assertEqual(len(self.db.getjournal('user', id)), 1)
559 self.db.commit()
561 def testPack(self):
562 id = self.db.issue.create(title="spam", status='1')
563 self.db.commit()
564 self.db.issue.set(id, status='2')
565 self.db.commit()
567 # sleep for at least a second, then get a date to pack at
568 time.sleep(1)
569 pack_before = date.Date('.')
571 # wait another second and add one more entry
572 time.sleep(1)
573 self.db.issue.set(id, status='3')
574 self.db.commit()
575 jlen = len(self.db.getjournal('issue', id))
577 # pack
578 self.db.pack(pack_before)
580 # we should have the create and last set entries now
581 self.assertEqual(jlen-1, len(self.db.getjournal('issue', id)))
583 def testIndexerSearching(self):
584 f1 = self.db.file.create(content='hello', type="text/plain")
585 f2 = self.db.file.create(content='world', type="text/frozz",
586 comment='blah blah')
587 i1 = self.db.issue.create(files=[f1, f2], title="flebble plop")
588 i2 = self.db.issue.create(title="flebble frooz")
589 self.db.commit()
590 self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
591 {i1: {'files': [f1]}})
592 self.assertEquals(self.db.indexer.search(['world'], self.db.issue), {})
593 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
594 {i2: {}})
595 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
596 {i1: {}, i2: {}})
598 def testReindexing(self):
599 self.db.issue.create(title="frooz")
600 self.db.commit()
601 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
602 {'1': {}})
603 self.db.issue.set('1', title="dooble")
604 self.db.commit()
605 self.assertEquals(self.db.indexer.search(['dooble'], self.db.issue),
606 {'1': {}})
607 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue), {})
609 def testForcedReindexing(self):
610 self.db.issue.create(title="flebble frooz")
611 self.db.commit()
612 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
613 {'1': {}})
614 self.db.indexer.quiet = 1
615 self.db.indexer.force_reindex()
616 self.db.post_init()
617 self.db.indexer.quiet = 9
618 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
619 {'1': {}})
621 #
622 # searching tests follow
623 #
624 def testFindIncorrectProperty(self):
625 self.assertRaises(TypeError, self.db.issue.find, title='fubar')
627 def _find_test_setup(self):
628 self.db.file.create(content='')
629 self.db.file.create(content='')
630 self.db.user.create(username='')
631 one = self.db.issue.create(status="1", nosy=['1'])
632 two = self.db.issue.create(status="2", nosy=['2'], files=['1'],
633 assignedto='2')
634 three = self.db.issue.create(status="1", nosy=['1','2'])
635 four = self.db.issue.create(status="3", assignedto='1',
636 files=['1','2'])
637 return one, two, three, four
639 def testFindLink(self):
640 one, two, three, four = self._find_test_setup()
641 got = self.db.issue.find(status='1')
642 got.sort()
643 self.assertEqual(got, [one, three])
644 got = self.db.issue.find(status={'1':1})
645 got.sort()
646 self.assertEqual(got, [one, three])
648 def testFindLinkFail(self):
649 self._find_test_setup()
650 self.assertEqual(self.db.issue.find(status='4'), [])
651 self.assertEqual(self.db.issue.find(status={'4':1}), [])
653 def testFindLinkUnset(self):
654 one, two, three, four = self._find_test_setup()
655 got = self.db.issue.find(assignedto=None)
656 got.sort()
657 self.assertEqual(got, [one, three])
658 got = self.db.issue.find(assignedto={None:1})
659 got.sort()
660 self.assertEqual(got, [one, three])
662 def testFindMultilink(self):
663 one, two, three, four = self._find_test_setup()
664 got = self.db.issue.find(nosy='2')
665 got.sort()
666 self.assertEqual(got, [two, three])
667 got = self.db.issue.find(nosy={'2':1})
668 got.sort()
669 self.assertEqual(got, [two, three])
670 got = self.db.issue.find(nosy={'2':1}, files={})
671 got.sort()
672 self.assertEqual(got, [two, three])
674 def testFindMultiMultilink(self):
675 one, two, three, four = self._find_test_setup()
676 got = self.db.issue.find(nosy='2', files='1')
677 got.sort()
678 self.assertEqual(got, [two, three, four])
679 got = self.db.issue.find(nosy={'2':1}, files={'1':1})
680 got.sort()
681 self.assertEqual(got, [two, three, four])
683 def testFindMultilinkFail(self):
684 self._find_test_setup()
685 self.assertEqual(self.db.issue.find(nosy='3'), [])
686 self.assertEqual(self.db.issue.find(nosy={'3':1}), [])
688 def testFindMultilinkUnset(self):
689 self._find_test_setup()
690 self.assertEqual(self.db.issue.find(nosy={}), [])
692 def testFindLinkAndMultilink(self):
693 one, two, three, four = self._find_test_setup()
694 got = self.db.issue.find(status='1', nosy='2')
695 got.sort()
696 self.assertEqual(got, [one, two, three])
697 got = self.db.issue.find(status={'1':1}, nosy={'2':1})
698 got.sort()
699 self.assertEqual(got, [one, two, three])
701 def testFindRetired(self):
702 one, two, three, four = self._find_test_setup()
703 self.assertEqual(len(self.db.issue.find(status='1')), 2)
704 self.db.issue.retire(one)
705 self.assertEqual(len(self.db.issue.find(status='1')), 1)
707 def testStringFind(self):
708 self.assertRaises(TypeError, self.db.issue.stringFind, status='1')
710 ids = []
711 ids.append(self.db.issue.create(title="spam"))
712 self.db.issue.create(title="not spam")
713 ids.append(self.db.issue.create(title="spam"))
714 ids.sort()
715 got = self.db.issue.stringFind(title='spam')
716 got.sort()
717 self.assertEqual(got, ids)
718 self.assertEqual(self.db.issue.stringFind(title='fubar'), [])
720 # test retiring a node
721 self.db.issue.retire(ids[0])
722 self.assertEqual(len(self.db.issue.stringFind(title='spam')), 1)
724 def filteringSetup(self):
725 for user in (
726 {'username': 'bleep'},
727 {'username': 'blop'},
728 {'username': 'blorp'}):
729 self.db.user.create(**user)
730 iss = self.db.issue
731 for issue in (
732 {'title': 'issue one', 'status': '2', 'assignedto': '1',
733 'foo': date.Interval('1:10'),
734 'deadline': date.Date('2003-01-01.00:00')},
735 {'title': 'issue two', 'status': '1', 'assignedto': '2',
736 'foo': date.Interval('1d'),
737 'deadline': date.Date('2003-02-16.22:50')},
738 {'title': 'issue three', 'status': '1',
739 'nosy': ['1','2'], 'deadline': date.Date('2003-02-18')},
740 {'title': 'non four', 'status': '3',
741 'foo': date.Interval('0:10'),
742 'nosy': ['1'], 'deadline': date.Date('2004-03-08')}):
743 self.db.issue.create(**issue)
744 self.db.commit()
745 return self.assertEqual, self.db.issue.filter
747 def testFilteringID(self):
748 ae, filt = self.filteringSetup()
749 ae(filt(None, {'id': '1'}, ('+','id'), (None,None)), ['1'])
751 def testFilteringString(self):
752 ae, filt = self.filteringSetup()
753 ae(filt(None, {'title': ['one']}, ('+','id'), (None,None)), ['1'])
754 ae(filt(None, {'title': ['issue']}, ('+','id'), (None,None)),
755 ['1','2','3'])
756 ae(filt(None, {'title': ['one', 'two']}, ('+','id'), (None,None)),
757 ['1', '2'])
759 def testFilteringLink(self):
760 ae, filt = self.filteringSetup()
761 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['2','3'])
762 ae(filt(None, {'assignedto': '-1'}, ('+','id'), (None,None)), ['3','4'])
764 def testFilteringRetired(self):
765 ae, filt = self.filteringSetup()
766 self.db.issue.retire('2')
767 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['3'])
769 def testFilteringMultilink(self):
770 ae, filt = self.filteringSetup()
771 ae(filt(None, {'nosy': '2'}, ('+','id'), (None,None)), ['3'])
772 ae(filt(None, {'nosy': '-1'}, ('+','id'), (None,None)), ['1', '2'])
774 def testFilteringMany(self):
775 ae, filt = self.filteringSetup()
776 ae(filt(None, {'nosy': '2', 'status': '1'}, ('+','id'), (None,None)),
777 ['3'])
779 def testFilteringRange(self):
780 ae, filt = self.filteringSetup()
781 # Date ranges
782 ae(filt(None, {'deadline': 'from 2003-02-10 to 2003-02-23'}), ['2','3'])
783 ae(filt(None, {'deadline': '2003-02-10; 2003-02-23'}), ['2','3'])
784 ae(filt(None, {'deadline': '; 2003-02-16'}), ['1'])
785 # Lets assume people won't invent a time machine, otherwise this test
786 # may fail :)
787 ae(filt(None, {'deadline': 'from 2003-02-16'}), ['2', '3', '4'])
788 ae(filt(None, {'deadline': '2003-02-16;'}), ['2', '3', '4'])
789 # year and month granularity
790 ae(filt(None, {'deadline': '2002'}), [])
791 ae(filt(None, {'deadline': '2003'}), ['1', '2', '3'])
792 ae(filt(None, {'deadline': '2004'}), ['4'])
793 ae(filt(None, {'deadline': '2003-02'}), ['2', '3'])
794 ae(filt(None, {'deadline': '2003-03'}), [])
795 ae(filt(None, {'deadline': '2003-02-16'}), ['2'])
796 ae(filt(None, {'deadline': '2003-02-17'}), [])
797 # Interval ranges
798 ae(filt(None, {'foo': 'from 0:50 to 2:00'}), ['1'])
799 ae(filt(None, {'foo': 'from 0:50 to 1d 2:00'}), ['1', '2'])
800 ae(filt(None, {'foo': 'from 5:50'}), ['2'])
801 ae(filt(None, {'foo': 'to 0:05'}), [])
803 def testFilteringIntervalSort(self):
804 ae, filt = self.filteringSetup()
805 # ascending should sort None, 1:10, 1d
806 ae(filt(None, {}, ('+','foo'), (None,None)), ['3', '4', '1', '2'])
807 # descending should sort 1d, 1:10, None
808 ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3'])
810 # XXX add sorting tests for other types
811 # XXX test auditors and reactors
813 def testImportExport(self):
814 # use the filtering setup to create a bunch of items
815 ae, filt = self.filteringSetup()
816 self.db.user.retire('3')
817 self.db.issue.retire('2')
819 # grab snapshot of the current database
820 orig = {}
821 for cn,klass in self.db.classes.items():
822 cl = orig[cn] = {}
823 for id in klass.list():
824 it = cl[id] = {}
825 for name in klass.getprops().keys():
826 it[name] = klass.get(id, name)
828 # grab the export
829 export = {}
830 for cn,klass in self.db.classes.items():
831 names = klass.getprops().keys()
832 cl = export[cn] = [names+['is retired']]
833 for id in klass.getnodeids():
834 cl.append(klass.export_list(names, id))
836 # shut down this db and nuke it
837 self.db.close()
838 self.nuke_database()
840 # open a new, empty database
841 os.makedirs(config.DATABASE + '/files')
842 self.db = self.module.Database(config, 'admin')
843 setupSchema(self.db, 0, self.module)
845 # import
846 for cn, items in export.items():
847 klass = self.db.classes[cn]
848 names = items[0]
849 maxid = 1
850 for itemprops in items[1:]:
851 maxid = max(maxid, int(klass.import_list(names, itemprops)))
852 self.db.setid(cn, str(maxid+1))
854 # compare with snapshot of the database
855 for cn, items in orig.items():
856 klass = self.db.classes[cn]
857 # ensure retired items are retired :)
858 l = items.keys(); l.sort()
859 m = klass.list(); m.sort()
860 ae(l, m)
861 for id, props in items.items():
862 for name, value in props.items():
863 ae(klass.get(id, name), value)
865 # make sure the retired items are actually imported
866 ae(self.db.user.get('3', 'username'), 'blop')
867 ae(self.db.issue.get('2', 'title'), 'issue two')
869 # make sure id counters are set correctly
870 maxid = max([int(id) for id in self.db.user.list()])
871 newid = self.db.user.create(username='testing')
872 assert newid > maxid
874 def testSafeGet(self):
875 # existent nodeid, existent property
876 self.assertEqual(self.db.user.safeget('1', 'username'), 'admin')
877 # nonexistent nodeid, existent property
878 self.assertEqual(self.db.user.safeget('999', 'username'), None)
879 # different default
880 self.assertEqual(self.db.issue.safeget('999', 'nosy', []), [])
882 def testAddProperty(self):
883 self.db.issue.create(title="spam", status='1')
884 self.db.commit()
886 self.db.issue.addprop(fixer=Link("user"))
887 # force any post-init stuff to happen
888 self.db.post_init()
889 props = self.db.issue.getprops()
890 keys = props.keys()
891 keys.sort()
892 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
893 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
894 'nosy', 'status', 'superseder', 'title'])
895 self.assertEqual(self.db.issue.get('1', "fixer"), None)
897 def testRemoveProperty(self):
898 self.db.issue.create(title="spam", status='1')
899 self.db.commit()
901 del self.db.issue.properties['title']
902 self.db.post_init()
903 props = self.db.issue.getprops()
904 keys = props.keys()
905 keys.sort()
906 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
907 'creator', 'deadline', 'files', 'foo', 'id', 'messages',
908 'nosy', 'status', 'superseder'])
909 self.assertEqual(self.db.issue.list(), ['1'])
911 def testAddRemoveProperty(self):
912 self.db.issue.create(title="spam", status='1')
913 self.db.commit()
915 self.db.issue.addprop(fixer=Link("user"))
916 del self.db.issue.properties['title']
917 self.db.post_init()
918 props = self.db.issue.getprops()
919 keys = props.keys()
920 keys.sort()
921 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
922 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
923 'nosy', 'status', 'superseder'])
924 self.assertEqual(self.db.issue.list(), ['1'])
926 class ROTest(MyTestCase):
927 def setUp(self):
928 # remove previous test, ignore errors
929 if os.path.exists(config.DATABASE):
930 shutil.rmtree(config.DATABASE)
931 os.makedirs(config.DATABASE + '/files')
932 self.db = self.module.Database(config, 'admin')
933 setupSchema(self.db, 1, self.module)
934 self.db.close()
936 self.db = self.module.Database(config)
937 setupSchema(self.db, 0, self.module)
939 def testExceptions(self):
940 # this tests the exceptions that should be raised
941 ar = self.assertRaises
943 # this tests the exceptions that should be raised
944 ar(DatabaseError, self.db.status.create, name="foo")
945 ar(DatabaseError, self.db.status.set, '1', name="foo")
946 ar(DatabaseError, self.db.status.retire, '1')
949 class SchemaTest(MyTestCase):
950 def setUp(self):
951 # remove previous test, ignore errors
952 if os.path.exists(config.DATABASE):
953 shutil.rmtree(config.DATABASE)
954 os.makedirs(config.DATABASE + '/files')
956 def test_reservedProperties(self):
957 self.db = self.module.Database(config, 'admin')
958 self.assertRaises(ValueError, self.module.Class, self.db, "a",
959 creation=String())
960 self.assertRaises(ValueError, self.module.Class, self.db, "a",
961 activity=String())
962 self.assertRaises(ValueError, self.module.Class, self.db, "a",
963 creator=String())
965 def init_a(self):
966 self.db = self.module.Database(config, 'admin')
967 a = self.module.Class(self.db, "a", name=String())
968 a.setkey("name")
969 self.db.post_init()
971 def init_ab(self):
972 self.db = self.module.Database(config, 'admin')
973 a = self.module.Class(self.db, "a", name=String())
974 a.setkey("name")
975 b = self.module.Class(self.db, "b", name=String())
976 b.setkey("name")
977 self.db.post_init()
979 def test_addNewClass(self):
980 self.init_a()
982 self.assertRaises(ValueError, self.module.Class, self.db, "a",
983 name=String())
985 aid = self.db.a.create(name='apple')
986 self.db.commit(); self.db.close()
988 # add a new class to the schema and check creation of new items
989 # (and existence of old ones)
990 self.init_ab()
991 bid = self.db.b.create(name='bear')
992 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
993 self.db.commit()
994 self.db.close()
996 # now check we can recall the added class' items
997 self.init_ab()
998 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
999 self.assertEqual(self.db.a.lookup('apple'), aid)
1000 self.assertEqual(self.db.b.get(bid, 'name'), 'bear')
1001 self.assertEqual(self.db.b.lookup('bear'), bid)
1003 # confirm journal's ok
1004 self.db.getjournal('a', aid)
1005 self.db.getjournal('b', bid)
1007 def init_amod(self):
1008 self.db = self.module.Database(config, 'admin')
1009 a = self.module.Class(self.db, "a", name=String(), fooz=String())
1010 a.setkey("name")
1011 b = self.module.Class(self.db, "b", name=String())
1012 b.setkey("name")
1013 self.db.post_init()
1015 def test_modifyClass(self):
1016 self.init_ab()
1018 # add item to user and issue class
1019 aid = self.db.a.create(name='apple')
1020 bid = self.db.b.create(name='bear')
1021 self.db.commit(); self.db.close()
1023 # modify "a" schema
1024 self.init_amod()
1025 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1026 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1027 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1028 aid2 = self.db.a.create(name='aardvark', fooz='booz')
1029 self.db.commit(); self.db.close()
1031 # test
1032 self.init_amod()
1033 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1034 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1035 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1036 self.assertEqual(self.db.a.get(aid2, 'name'), 'aardvark')
1037 self.assertEqual(self.db.a.get(aid2, 'fooz'), 'booz')
1039 # confirm journal's ok
1040 self.db.getjournal('a', aid)
1041 self.db.getjournal('a', aid2)
1043 def init_amodkey(self):
1044 self.db = self.module.Database(config, 'admin')
1045 a = self.module.Class(self.db, "a", name=String(), fooz=String())
1046 a.setkey("fooz")
1047 b = self.module.Class(self.db, "b", name=String())
1048 b.setkey("name")
1049 self.db.post_init()
1051 def test_changeClassKey(self):
1052 self.init_amod()
1053 aid = self.db.a.create(name='apple')
1054 self.assertEqual(self.db.a.lookup('apple'), aid)
1055 self.db.commit(); self.db.close()
1057 # change the key to fooz on a
1058 self.init_amodkey()
1059 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1060 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1061 self.assertRaises(KeyError, self.db.a.lookup, 'apple')
1062 aid2 = self.db.a.create(name='aardvark', fooz='booz')
1063 self.db.commit(); self.db.close()
1065 # check
1066 self.init_amodkey()
1067 self.assertEqual(self.db.a.lookup('booz'), aid2)
1069 # confirm journal's ok
1070 self.db.getjournal('a', aid)
1072 def init_ml(self):
1073 self.db = self.module.Database(config, 'admin')
1074 a = self.module.Class(self.db, "a", name=String())
1075 a.setkey('name')
1076 b = self.module.Class(self.db, "b", name=String(),
1077 fooz=Multilink('a'))
1078 b.setkey("name")
1079 self.db.post_init()
1081 def test_makeNewMultilink(self):
1082 self.init_a()
1083 aid = self.db.a.create(name='apple')
1084 self.assertEqual(self.db.a.lookup('apple'), aid)
1085 self.db.commit(); self.db.close()
1087 # add a multilink prop
1088 self.init_ml()
1089 bid = self.db.b.create(name='bear', fooz=[aid])
1090 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1091 self.assertEqual(self.db.a.lookup('apple'), aid)
1092 self.db.commit(); self.db.close()
1094 # check
1095 self.init_ml()
1096 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1097 self.assertEqual(self.db.a.lookup('apple'), aid)
1098 self.assertEqual(self.db.b.lookup('bear'), bid)
1100 # confirm journal's ok
1101 self.db.getjournal('a', aid)
1102 self.db.getjournal('b', bid)
1104 def test_removeMultilink(self):
1105 # add a multilink prop
1106 self.init_ml()
1107 aid = self.db.a.create(name='apple')
1108 bid = self.db.b.create(name='bear', fooz=[aid])
1109 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1110 self.assertEqual(self.db.a.lookup('apple'), aid)
1111 self.assertEqual(self.db.b.lookup('bear'), bid)
1112 self.db.commit(); self.db.close()
1114 # remove the multilink
1115 self.init_ab()
1116 self.assertEqual(self.db.a.lookup('apple'), aid)
1117 self.assertEqual(self.db.b.lookup('bear'), bid)
1119 # confirm journal's ok
1120 self.db.getjournal('a', aid)
1121 self.db.getjournal('b', bid)
1123 def test_removeClass(self):
1124 self.init_ml()
1125 aid = self.db.a.create(name='apple')
1126 bid = self.db.b.create(name='bear', fooz=[aid])
1127 self.db.commit(); self.db.close()
1129 # drop the b class
1130 self.init_a()
1131 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1132 self.assertEqual(self.db.a.lookup('apple'), aid)
1133 self.db.commit(); self.db.close()
1135 # now check we can recall the added class' items
1136 self.init_a()
1137 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1138 self.assertEqual(self.db.a.lookup('apple'), aid)
1140 # confirm journal's ok
1141 self.db.getjournal('a', aid)
1143 class RDBMSTest:
1144 ''' tests specific to RDBMS backends '''
1145 def test_indexTest(self):
1146 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_id_idx'), 1)
1147 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_x_idx'), 0)
1150 class ClassicInitTest(unittest.TestCase):
1151 count = 0
1152 db = None
1153 extra_config = ''
1155 def setUp(self):
1156 ClassicInitTest.count = ClassicInitTest.count + 1
1157 self.dirname = '_test_init_%s'%self.count
1158 try:
1159 shutil.rmtree(self.dirname)
1160 except OSError, error:
1161 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
1163 def testCreation(self):
1164 ae = self.assertEqual
1166 # create the instance
1167 init.install(self.dirname, 'templates/classic')
1168 init.write_select_db(self.dirname, self.backend)
1170 if self.extra_config:
1171 f = open(os.path.join(self.dirname, 'config.py'), 'a')
1172 try:
1173 f.write(self.extra_config)
1174 finally:
1175 f.close()
1177 init.initialise(self.dirname, 'sekrit')
1179 # check we can load the package
1180 instance = imp.load_package(self.dirname, self.dirname)
1182 # and open the database
1183 db = self.db = instance.open()
1185 # check the basics of the schema and initial data set
1186 l = db.priority.list()
1187 ae(l, ['1', '2', '3', '4', '5'])
1188 l = db.status.list()
1189 ae(l, ['1', '2', '3', '4', '5', '6', '7', '8'])
1190 l = db.keyword.list()
1191 ae(l, [])
1192 l = db.user.list()
1193 ae(l, ['1', '2'])
1194 l = db.msg.list()
1195 ae(l, [])
1196 l = db.file.list()
1197 ae(l, [])
1198 l = db.issue.list()
1199 ae(l, [])
1201 def tearDown(self):
1202 if self.db is not None:
1203 self.db.close()
1204 try:
1205 shutil.rmtree(self.dirname)
1206 except OSError, error:
1207 if error.errno not in (errno.ENOENT, errno.ESRCH): raise