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.12 2003-12-10 01:40: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 testSearching(self):
584 self.db.file.create(content='hello', type="text/plain")
585 self.db.file.create(content='world', type="text/frozz",
586 comment='blah blah')
587 self.db.issue.create(files=['1', '2'], title="flebble plop")
588 self.db.issue.create(title="flebble frooz")
589 self.db.commit()
590 self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
591 {'1': {'files': ['1']}})
592 self.assertEquals(self.db.indexer.search(['world'], self.db.issue), {})
593 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
594 {'2': {}})
595 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
596 {'2': {}, '1': {}})
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 testFind(self):
625 self.assertRaises(TypeError, self.db.issue.find, title='fubar')
627 self.db.user.create(username='test')
628 ids = []
629 ids.append(self.db.issue.create(status="1", nosy=['1']))
630 oddid = self.db.issue.create(status="2", nosy=['2'], assignedto='2')
631 ids.append(self.db.issue.create(status="1", nosy=['1','2']))
632 self.db.issue.create(status="3", nosy=['1'], assignedto='1')
633 ids.sort()
635 # should match first and third
636 got = self.db.issue.find(status='1')
637 got.sort()
638 self.assertEqual(got, ids)
639 got = self.db.issue.find(status={'1':1})
640 got.sort()
641 self.assertEqual(got, ids)
643 # none
644 self.assertEqual(self.db.issue.find(status='4'), [])
645 self.assertEqual(self.db.issue.find(status={'4':1}), [])
647 # should match first and third
648 got = self.db.issue.find(assignedto=None)
649 got.sort()
650 self.assertEqual(got, ids)
651 got = self.db.issue.find(assignedto={None:1})
652 got.sort()
653 self.assertEqual(got, ids)
655 # should match first three
656 got = self.db.issue.find(status='1', nosy='2')
657 got.sort()
658 ids.append(oddid)
659 ids.sort()
660 self.assertEqual(got, ids)
661 got = self.db.issue.find(status={'1':1}, nosy={'2':1})
662 got.sort()
663 self.assertEqual(got, ids)
665 # none
666 self.assertEqual(self.db.issue.find(status='4', nosy='3'), [])
667 self.assertEqual(self.db.issue.find(status={'4':1}, nosy={'3':1}), [])
669 # test retiring a node
670 self.db.issue.retire(ids[0])
671 self.assertEqual(len(self.db.issue.find(status='1', nosy='2')), 2)
673 def testStringFind(self):
674 self.assertRaises(TypeError, self.db.issue.stringFind, status='1')
676 ids = []
677 ids.append(self.db.issue.create(title="spam"))
678 self.db.issue.create(title="not spam")
679 ids.append(self.db.issue.create(title="spam"))
680 ids.sort()
681 got = self.db.issue.stringFind(title='spam')
682 got.sort()
683 self.assertEqual(got, ids)
684 self.assertEqual(self.db.issue.stringFind(title='fubar'), [])
686 # test retiring a node
687 self.db.issue.retire(ids[0])
688 self.assertEqual(len(self.db.issue.stringFind(title='spam')), 1)
690 def filteringSetup(self):
691 for user in (
692 {'username': 'bleep'},
693 {'username': 'blop'},
694 {'username': 'blorp'}):
695 self.db.user.create(**user)
696 iss = self.db.issue
697 for issue in (
698 {'title': 'issue one', 'status': '2', 'assignedto': '1',
699 'foo': date.Interval('1:10'),
700 'deadline': date.Date('2003-01-01.00:00')},
701 {'title': 'issue two', 'status': '1', 'assignedto': '2',
702 'foo': date.Interval('1d'),
703 'deadline': date.Date('2003-02-16.22:50')},
704 {'title': 'issue three', 'status': '1',
705 'nosy': ['1','2'], 'deadline': date.Date('2003-02-18')},
706 {'title': 'non four', 'status': '3',
707 'foo': date.Interval('0:10'),
708 'nosy': ['1'], 'deadline': date.Date('2004-03-08')}):
709 self.db.issue.create(**issue)
710 self.db.commit()
711 return self.assertEqual, self.db.issue.filter
713 def testFilteringID(self):
714 ae, filt = self.filteringSetup()
715 ae(filt(None, {'id': '1'}, ('+','id'), (None,None)), ['1'])
717 def testFilteringString(self):
718 ae, filt = self.filteringSetup()
719 ae(filt(None, {'title': ['one']}, ('+','id'), (None,None)), ['1'])
720 ae(filt(None, {'title': ['issue']}, ('+','id'), (None,None)),
721 ['1','2','3'])
722 ae(filt(None, {'title': ['one', 'two']}, ('+','id'), (None,None)),
723 ['1', '2'])
725 def testFilteringLink(self):
726 ae, filt = self.filteringSetup()
727 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['2','3'])
728 ae(filt(None, {'assignedto': '-1'}, ('+','id'), (None,None)), ['3','4'])
730 def testFilteringRetired(self):
731 ae, filt = self.filteringSetup()
732 self.db.issue.retire('2')
733 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['3'])
735 def testFilteringMultilink(self):
736 ae, filt = self.filteringSetup()
737 ae(filt(None, {'nosy': '2'}, ('+','id'), (None,None)), ['3'])
738 ae(filt(None, {'nosy': '-1'}, ('+','id'), (None,None)), ['1', '2'])
740 def testFilteringMany(self):
741 ae, filt = self.filteringSetup()
742 ae(filt(None, {'nosy': '2', 'status': '1'}, ('+','id'), (None,None)),
743 ['3'])
745 def testFilteringRange(self):
746 ae, filt = self.filteringSetup()
747 # Date ranges
748 ae(filt(None, {'deadline': 'from 2003-02-10 to 2003-02-23'}), ['2','3'])
749 ae(filt(None, {'deadline': '2003-02-10; 2003-02-23'}), ['2','3'])
750 ae(filt(None, {'deadline': '; 2003-02-16'}), ['1'])
751 # Lets assume people won't invent a time machine, otherwise this test
752 # may fail :)
753 ae(filt(None, {'deadline': 'from 2003-02-16'}), ['2', '3', '4'])
754 ae(filt(None, {'deadline': '2003-02-16;'}), ['2', '3', '4'])
755 # year and month granularity
756 ae(filt(None, {'deadline': '2002'}), [])
757 ae(filt(None, {'deadline': '2003'}), ['1', '2', '3'])
758 ae(filt(None, {'deadline': '2004'}), ['4'])
759 ae(filt(None, {'deadline': '2003-02'}), ['2', '3'])
760 ae(filt(None, {'deadline': '2003-03'}), [])
761 ae(filt(None, {'deadline': '2003-02-16'}), ['2'])
762 ae(filt(None, {'deadline': '2003-02-17'}), [])
763 # Interval ranges
764 ae(filt(None, {'foo': 'from 0:50 to 2:00'}), ['1'])
765 ae(filt(None, {'foo': 'from 0:50 to 1d 2:00'}), ['1', '2'])
766 ae(filt(None, {'foo': 'from 5:50'}), ['2'])
767 ae(filt(None, {'foo': 'to 0:05'}), [])
769 def testFilteringIntervalSort(self):
770 ae, filt = self.filteringSetup()
771 # ascending should sort None, 1:10, 1d
772 ae(filt(None, {}, ('+','foo'), (None,None)), ['3', '4', '1', '2'])
773 # descending should sort 1d, 1:10, None
774 ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3'])
776 # XXX add sorting tests for other types
777 # XXX test auditors and reactors
779 def testImportExport(self):
780 # use the filtering setup to create a bunch of items
781 ae, filt = self.filteringSetup()
782 self.db.user.retire('3')
783 self.db.issue.retire('2')
785 # grab snapshot of the current database
786 orig = {}
787 for cn,klass in self.db.classes.items():
788 cl = orig[cn] = {}
789 for id in klass.list():
790 it = cl[id] = {}
791 for name in klass.getprops().keys():
792 it[name] = klass.get(id, name)
794 # grab the export
795 export = {}
796 for cn,klass in self.db.classes.items():
797 names = klass.getprops().keys()
798 cl = export[cn] = [names+['is retired']]
799 for id in klass.getnodeids():
800 cl.append(klass.export_list(names, id))
802 # shut down this db and nuke it
803 self.db.close()
804 self.nuke_database()
806 # open a new, empty database
807 os.makedirs(config.DATABASE + '/files')
808 self.db = self.module.Database(config, 'admin')
809 setupSchema(self.db, 0, self.module)
811 # import
812 for cn, items in export.items():
813 klass = self.db.classes[cn]
814 names = items[0]
815 maxid = 1
816 for itemprops in items[1:]:
817 maxid = max(maxid, int(klass.import_list(names, itemprops)))
818 self.db.setid(cn, str(maxid+1))
820 # compare with snapshot of the database
821 for cn, items in orig.items():
822 klass = self.db.classes[cn]
823 # ensure retired items are retired :)
824 l = items.keys(); l.sort()
825 m = klass.list(); m.sort()
826 ae(l, m)
827 for id, props in items.items():
828 for name, value in props.items():
829 ae(klass.get(id, name), value)
831 # make sure the retired items are actually imported
832 ae(self.db.user.get('3', 'username'), 'blop')
833 ae(self.db.issue.get('2', 'title'), 'issue two')
835 # make sure id counters are set correctly
836 maxid = max([int(id) for id in self.db.user.list()])
837 newid = self.db.user.create(username='testing')
838 assert newid > maxid
840 def testSafeGet(self):
841 # existent nodeid, existent property
842 self.assertEqual(self.db.user.safeget('1', 'username'), 'admin')
843 # nonexistent nodeid, existent property
844 self.assertEqual(self.db.user.safeget('999', 'username'), None)
845 # different default
846 self.assertEqual(self.db.issue.safeget('999', 'nosy', []), [])
848 def testAddProperty(self):
849 self.db.issue.create(title="spam", status='1')
850 self.db.commit()
852 self.db.issue.addprop(fixer=Link("user"))
853 # force any post-init stuff to happen
854 self.db.post_init()
855 props = self.db.issue.getprops()
856 keys = props.keys()
857 keys.sort()
858 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
859 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
860 'nosy', 'status', 'superseder', 'title'])
861 self.assertEqual(self.db.issue.get('1', "fixer"), None)
863 def testRemoveProperty(self):
864 self.db.issue.create(title="spam", status='1')
865 self.db.commit()
867 del self.db.issue.properties['title']
868 self.db.post_init()
869 props = self.db.issue.getprops()
870 keys = props.keys()
871 keys.sort()
872 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
873 'creator', 'deadline', 'files', 'foo', 'id', 'messages',
874 'nosy', 'status', 'superseder'])
875 self.assertEqual(self.db.issue.list(), ['1'])
877 def testAddRemoveProperty(self):
878 self.db.issue.create(title="spam", status='1')
879 self.db.commit()
881 self.db.issue.addprop(fixer=Link("user"))
882 del self.db.issue.properties['title']
883 self.db.post_init()
884 props = self.db.issue.getprops()
885 keys = props.keys()
886 keys.sort()
887 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
888 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
889 'nosy', 'status', 'superseder'])
890 self.assertEqual(self.db.issue.list(), ['1'])
892 class ROTest(MyTestCase):
893 def setUp(self):
894 # remove previous test, ignore errors
895 if os.path.exists(config.DATABASE):
896 shutil.rmtree(config.DATABASE)
897 os.makedirs(config.DATABASE + '/files')
898 self.db = self.module.Database(config, 'admin')
899 setupSchema(self.db, 1, self.module)
900 self.db.close()
902 self.db = self.module.Database(config)
903 setupSchema(self.db, 0, self.module)
905 def testExceptions(self):
906 # this tests the exceptions that should be raised
907 ar = self.assertRaises
909 # this tests the exceptions that should be raised
910 ar(DatabaseError, self.db.status.create, name="foo")
911 ar(DatabaseError, self.db.status.set, '1', name="foo")
912 ar(DatabaseError, self.db.status.retire, '1')
915 class SchemaTest(MyTestCase):
916 def setUp(self):
917 # remove previous test, ignore errors
918 if os.path.exists(config.DATABASE):
919 shutil.rmtree(config.DATABASE)
920 os.makedirs(config.DATABASE + '/files')
922 def test_reservedProperties(self):
923 self.db = self.module.Database(config, 'admin')
924 self.assertRaises(ValueError, self.module.Class, self.db, "a",
925 creation=String())
926 self.assertRaises(ValueError, self.module.Class, self.db, "a",
927 activity=String())
928 self.assertRaises(ValueError, self.module.Class, self.db, "a",
929 creator=String())
931 def init_a(self):
932 self.db = self.module.Database(config, 'admin')
933 a = self.module.Class(self.db, "a", name=String())
934 a.setkey("name")
935 self.db.post_init()
937 def init_ab(self):
938 self.db = self.module.Database(config, 'admin')
939 a = self.module.Class(self.db, "a", name=String())
940 a.setkey("name")
941 b = self.module.Class(self.db, "b", name=String())
942 b.setkey("name")
943 self.db.post_init()
945 def test_addNewClass(self):
946 self.init_a()
948 self.assertRaises(ValueError, self.module.Class, self.db, "a",
949 name=String())
951 aid = self.db.a.create(name='apple')
952 self.db.commit(); self.db.close()
954 # add a new class to the schema and check creation of new items
955 # (and existence of old ones)
956 self.init_ab()
957 bid = self.db.b.create(name='bear')
958 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
959 self.db.commit()
960 self.db.close()
962 # now check we can recall the added class' items
963 self.init_ab()
964 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
965 self.assertEqual(self.db.a.lookup('apple'), aid)
966 self.assertEqual(self.db.b.get(bid, 'name'), 'bear')
967 self.assertEqual(self.db.b.lookup('bear'), bid)
969 # confirm journal's ok
970 self.db.getjournal('a', aid)
971 self.db.getjournal('b', bid)
973 def init_amod(self):
974 self.db = self.module.Database(config, 'admin')
975 a = self.module.Class(self.db, "a", name=String(), fooz=String())
976 a.setkey("name")
977 b = self.module.Class(self.db, "b", name=String())
978 b.setkey("name")
979 self.db.post_init()
981 def test_modifyClass(self):
982 self.init_ab()
984 # add item to user and issue class
985 aid = self.db.a.create(name='apple')
986 bid = self.db.b.create(name='bear')
987 self.db.commit(); self.db.close()
989 # modify "a" schema
990 self.init_amod()
991 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
992 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
993 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
994 aid2 = self.db.a.create(name='aardvark', fooz='booz')
995 self.db.commit(); self.db.close()
997 # test
998 self.init_amod()
999 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1000 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1001 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1002 self.assertEqual(self.db.a.get(aid2, 'name'), 'aardvark')
1003 self.assertEqual(self.db.a.get(aid2, 'fooz'), 'booz')
1005 # confirm journal's ok
1006 self.db.getjournal('a', aid)
1007 self.db.getjournal('a', aid2)
1009 def init_amodkey(self):
1010 self.db = self.module.Database(config, 'admin')
1011 a = self.module.Class(self.db, "a", name=String(), fooz=String())
1012 a.setkey("fooz")
1013 b = self.module.Class(self.db, "b", name=String())
1014 b.setkey("name")
1015 self.db.post_init()
1017 def test_changeClassKey(self):
1018 self.init_amod()
1019 aid = self.db.a.create(name='apple')
1020 self.assertEqual(self.db.a.lookup('apple'), aid)
1021 self.db.commit(); self.db.close()
1023 # change the key to fooz on a
1024 self.init_amodkey()
1025 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1026 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
1027 self.assertRaises(KeyError, self.db.a.lookup, 'apple')
1028 aid2 = self.db.a.create(name='aardvark', fooz='booz')
1029 self.db.commit(); self.db.close()
1031 # check
1032 self.init_amodkey()
1033 self.assertEqual(self.db.a.lookup('booz'), aid2)
1035 # confirm journal's ok
1036 self.db.getjournal('a', aid)
1038 def init_ml(self):
1039 self.db = self.module.Database(config, 'admin')
1040 a = self.module.Class(self.db, "a", name=String())
1041 a.setkey('name')
1042 b = self.module.Class(self.db, "b", name=String(),
1043 fooz=Multilink('a'))
1044 b.setkey("name")
1045 self.db.post_init()
1047 def test_makeNewMultilink(self):
1048 self.init_a()
1049 aid = self.db.a.create(name='apple')
1050 self.assertEqual(self.db.a.lookup('apple'), aid)
1051 self.db.commit(); self.db.close()
1053 # add a multilink prop
1054 self.init_ml()
1055 bid = self.db.b.create(name='bear', fooz=[aid])
1056 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1057 self.assertEqual(self.db.a.lookup('apple'), aid)
1058 self.db.commit(); self.db.close()
1060 # check
1061 self.init_ml()
1062 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1063 self.assertEqual(self.db.a.lookup('apple'), aid)
1064 self.assertEqual(self.db.b.lookup('bear'), bid)
1066 # confirm journal's ok
1067 self.db.getjournal('a', aid)
1068 self.db.getjournal('b', bid)
1070 def test_removeMultilink(self):
1071 # add a multilink prop
1072 self.init_ml()
1073 aid = self.db.a.create(name='apple')
1074 bid = self.db.b.create(name='bear', fooz=[aid])
1075 self.assertEqual(self.db.b.find(fooz=aid), [bid])
1076 self.assertEqual(self.db.a.lookup('apple'), aid)
1077 self.assertEqual(self.db.b.lookup('bear'), bid)
1078 self.db.commit(); self.db.close()
1080 # remove the multilink
1081 self.init_ab()
1082 self.assertEqual(self.db.a.lookup('apple'), aid)
1083 self.assertEqual(self.db.b.lookup('bear'), bid)
1085 # confirm journal's ok
1086 self.db.getjournal('a', aid)
1087 self.db.getjournal('b', bid)
1089 def test_removeClass(self):
1090 self.init_ml()
1091 aid = self.db.a.create(name='apple')
1092 bid = self.db.b.create(name='bear', fooz=[aid])
1093 self.db.commit(); self.db.close()
1095 # drop the b class
1096 self.init_a()
1097 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1098 self.assertEqual(self.db.a.lookup('apple'), aid)
1099 self.db.commit(); self.db.close()
1101 # now check we can recall the added class' items
1102 self.init_a()
1103 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1104 self.assertEqual(self.db.a.lookup('apple'), aid)
1106 # confirm journal's ok
1107 self.db.getjournal('a', aid)
1109 class RDBMSTest:
1110 ''' tests specific to RDBMS backends '''
1111 def test_indexTest(self):
1112 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_id_idx'), 1)
1113 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_x_idx'), 0)
1116 class ClassicInitTest(unittest.TestCase):
1117 count = 0
1118 db = None
1119 extra_config = ''
1121 def setUp(self):
1122 ClassicInitTest.count = ClassicInitTest.count + 1
1123 self.dirname = '_test_init_%s'%self.count
1124 try:
1125 shutil.rmtree(self.dirname)
1126 except OSError, error:
1127 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
1129 def testCreation(self):
1130 ae = self.assertEqual
1132 # create the instance
1133 init.install(self.dirname, 'templates/classic')
1134 init.write_select_db(self.dirname, self.backend)
1136 if self.extra_config:
1137 f = open(os.path.join(self.dirname, 'config.py'), 'a')
1138 try:
1139 f.write(self.extra_config)
1140 finally:
1141 f.close()
1143 init.initialise(self.dirname, 'sekrit')
1145 # check we can load the package
1146 instance = imp.load_package(self.dirname, self.dirname)
1148 # and open the database
1149 db = self.db = instance.open()
1151 # check the basics of the schema and initial data set
1152 l = db.priority.list()
1153 ae(l, ['1', '2', '3', '4', '5'])
1154 l = db.status.list()
1155 ae(l, ['1', '2', '3', '4', '5', '6', '7', '8'])
1156 l = db.keyword.list()
1157 ae(l, [])
1158 l = db.user.list()
1159 ae(l, ['1', '2'])
1160 l = db.msg.list()
1161 ae(l, [])
1162 l = db.file.list()
1163 ae(l, [])
1164 l = db.issue.list()
1165 ae(l, [])
1167 def tearDown(self):
1168 if self.db is not None:
1169 self.db.close()
1170 try:
1171 shutil.rmtree(self.dirname)
1172 except OSError, error:
1173 if error.errno not in (errno.ENOENT, errno.ESRCH): raise