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.4 2003-11-05 21:54:57 jlgijsbers Exp $
20 import unittest, os, shutil, errno, imp, sys, time
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 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 status.create(name="unread")
45 status.create(name="in-progress")
46 status.create(name="testing")
47 status.create(name="resolved")
48 db.commit()
50 class MyTestCase(unittest.TestCase):
51 def tearDown(self):
52 if hasattr(self, 'db'):
53 self.db.close()
54 if os.path.exists('_test_dir'):
55 shutil.rmtree('_test_dir')
57 class config:
58 DATABASE='_test_dir'
59 MAILHOST = 'localhost'
60 MAIL_DOMAIN = 'fill.me.in.'
61 TRACKER_NAME = 'Roundup issue tracker'
62 TRACKER_EMAIL = 'issue_tracker@%s'%MAIL_DOMAIN
63 TRACKER_WEB = 'http://some.useful.url/'
64 ADMIN_EMAIL = 'roundup-admin@%s'%MAIL_DOMAIN
65 FILTER_POSITION = 'bottom' # one of 'top', 'bottom', 'top and bottom'
66 ANONYMOUS_ACCESS = 'deny' # either 'deny' or 'allow'
67 ANONYMOUS_REGISTER = 'deny' # either 'deny' or 'allow'
68 MESSAGES_TO_AUTHOR = 'no' # either 'yes' or 'no'
69 EMAIL_SIGNATURE_POSITION = 'bottom'
71 # Mysql connection data
72 MYSQL_DBHOST = 'localhost'
73 MYSQL_DBUSER = 'rounduptest'
74 MYSQL_DBPASSWORD = 'rounduptest'
75 MYSQL_DBNAME = 'rounduptest'
76 MYSQL_DATABASE = (MYSQL_DBHOST, MYSQL_DBUSER, MYSQL_DBPASSWORD, MYSQL_DBNAME)
78 # Postgresql connection data
79 POSTGRESQL_DBHOST = 'localhost'
80 POSTGRESQL_DBUSER = 'rounduptest'
81 POSTGRESQL_DBPASSWORD = 'rounduptest'
82 POSTGRESQL_DBNAME = 'rounduptest'
83 POSTGRESQL_PORT = 5432
84 POSTGRESQL_DATABASE = {'host': POSTGRESQL_DBHOST, 'port': POSTGRESQL_PORT,
85 'user': POSTGRESQL_DBUSER, 'password': POSTGRESQL_DBPASSWORD,
86 'database': POSTGRESQL_DBNAME}
88 class nodbconfig(config):
89 MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD)
91 class DBTest(MyTestCase):
92 def setUp(self):
93 # remove previous test, ignore errors
94 if os.path.exists(config.DATABASE):
95 shutil.rmtree(config.DATABASE)
96 os.makedirs(config.DATABASE + '/files')
97 self.db = self.module.Database(config, 'admin')
98 setupSchema(self.db, 1, self.module)
100 #
101 # schema mutation
102 #
103 def testAddProperty(self):
104 self.db.issue.create(title="spam", status='1')
105 self.db.commit()
107 self.db.issue.addprop(fixer=Link("user"))
108 # force any post-init stuff to happen
109 self.db.post_init()
110 props = self.db.issue.getprops()
111 keys = props.keys()
112 keys.sort()
113 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
114 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
115 'nosy', 'status', 'superseder', 'title'])
116 self.assertEqual(self.db.issue.get('1', "fixer"), None)
118 def testRemoveProperty(self):
119 self.db.issue.create(title="spam", status='1')
120 self.db.commit()
122 del self.db.issue.properties['title']
123 self.db.post_init()
124 props = self.db.issue.getprops()
125 keys = props.keys()
126 keys.sort()
127 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
128 'creator', 'deadline', 'files', 'foo', 'id', 'messages',
129 'nosy', 'status', 'superseder'])
130 self.assertEqual(self.db.issue.list(), ['1'])
132 def testAddRemoveProperty(self):
133 self.db.issue.create(title="spam", status='1')
134 self.db.commit()
136 self.db.issue.addprop(fixer=Link("user"))
137 del self.db.issue.properties['title']
138 self.db.post_init()
139 props = self.db.issue.getprops()
140 keys = props.keys()
141 keys.sort()
142 self.assertEqual(keys, ['activity', 'assignedto', 'creation',
143 'creator', 'deadline', 'files', 'fixer', 'foo', 'id', 'messages',
144 'nosy', 'status', 'superseder'])
145 self.assertEqual(self.db.issue.list(), ['1'])
147 #
148 # basic operations
149 #
150 def testIDGeneration(self):
151 id1 = self.db.issue.create(title="spam", status='1')
152 id2 = self.db.issue.create(title="eggs", status='2')
153 self.assertNotEqual(id1, id2)
155 def testStringChange(self):
156 for commit in (0,1):
157 # test set & retrieve
158 nid = self.db.issue.create(title="spam", status='1')
159 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
161 # change and make sure we retrieve the correct value
162 self.db.issue.set(nid, title='eggs')
163 if commit: self.db.commit()
164 self.assertEqual(self.db.issue.get(nid, 'title'), 'eggs')
166 def testStringUnset(self):
167 for commit in (0,1):
168 nid = self.db.issue.create(title="spam", status='1')
169 if commit: self.db.commit()
170 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
171 # make sure we can unset
172 self.db.issue.set(nid, title=None)
173 if commit: self.db.commit()
174 self.assertEqual(self.db.issue.get(nid, "title"), None)
176 def testLinkChange(self):
177 for commit in (0,1):
178 nid = self.db.issue.create(title="spam", status='1')
179 if commit: self.db.commit()
180 self.assertEqual(self.db.issue.get(nid, "status"), '1')
181 self.db.issue.set(nid, status='2')
182 if commit: self.db.commit()
183 self.assertEqual(self.db.issue.get(nid, "status"), '2')
185 def testLinkUnset(self):
186 for commit in (0,1):
187 nid = self.db.issue.create(title="spam", status='1')
188 if commit: self.db.commit()
189 self.db.issue.set(nid, status=None)
190 if commit: self.db.commit()
191 self.assertEqual(self.db.issue.get(nid, "status"), None)
193 def testMultilinkChange(self):
194 for commit in (0,1):
195 u1 = self.db.user.create(username='foo%s'%commit)
196 u2 = self.db.user.create(username='bar%s'%commit)
197 nid = self.db.issue.create(title="spam", nosy=[u1])
198 if commit: self.db.commit()
199 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
200 self.db.issue.set(nid, nosy=[])
201 if commit: self.db.commit()
202 self.assertEqual(self.db.issue.get(nid, "nosy"), [])
203 self.db.issue.set(nid, nosy=[u1,u2])
204 if commit: self.db.commit()
205 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1,u2])
207 def testDateChange(self):
208 for commit in (0,1):
209 nid = self.db.issue.create(title="spam", status='1')
210 a = self.db.issue.get(nid, "deadline")
211 if commit: self.db.commit()
212 self.db.issue.set(nid, deadline=date.Date())
213 b = self.db.issue.get(nid, "deadline")
214 if commit: self.db.commit()
215 self.assertNotEqual(a, b)
216 self.assertNotEqual(b, date.Date('1970-1-1 00:00:00'))
218 def testDateUnset(self):
219 for commit in (0,1):
220 nid = self.db.issue.create(title="spam", status='1')
221 self.db.issue.set(nid, deadline=date.Date())
222 if commit: self.db.commit()
223 self.assertNotEqual(self.db.issue.get(nid, "deadline"), None)
224 self.db.issue.set(nid, deadline=None)
225 if commit: self.db.commit()
226 self.assertEqual(self.db.issue.get(nid, "deadline"), None)
228 def testIntervalChange(self):
229 for commit in (0,1):
230 nid = self.db.issue.create(title="spam", status='1')
231 if commit: self.db.commit()
232 a = self.db.issue.get(nid, "foo")
233 i = date.Interval('-1d')
234 self.db.issue.set(nid, foo=i)
235 if commit: self.db.commit()
236 self.assertNotEqual(self.db.issue.get(nid, "foo"), a)
237 self.assertEqual(i, self.db.issue.get(nid, "foo"))
238 j = date.Interval('1y')
239 self.db.issue.set(nid, foo=j)
240 if commit: self.db.commit()
241 self.assertNotEqual(self.db.issue.get(nid, "foo"), i)
242 self.assertEqual(j, self.db.issue.get(nid, "foo"))
244 def testIntervalUnset(self):
245 for commit in (0,1):
246 nid = self.db.issue.create(title="spam", status='1')
247 self.db.issue.set(nid, foo=date.Interval('-1d'))
248 if commit: self.db.commit()
249 self.assertNotEqual(self.db.issue.get(nid, "foo"), None)
250 self.db.issue.set(nid, foo=None)
251 if commit: self.db.commit()
252 self.assertEqual(self.db.issue.get(nid, "foo"), None)
254 def testBooleanChange(self):
255 userid = self.db.user.create(username='foo', assignable=1)
256 self.assertEqual(1, self.db.user.get(userid, 'assignable'))
257 self.db.user.set(userid, assignable=0)
258 self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
260 def testBooleanUnset(self):
261 nid = self.db.user.create(username='foo', assignable=1)
262 self.db.user.set(nid, assignable=None)
263 self.assertEqual(self.db.user.get(nid, "assignable"), None)
265 def testNumberChange(self):
266 nid = self.db.user.create(username='foo', age=1)
267 self.assertEqual(1, self.db.user.get(nid, 'age'))
268 self.db.user.set(nid, age=3)
269 self.assertNotEqual(self.db.user.get(nid, 'age'), 1)
270 self.db.user.set(nid, age=1.0)
271 self.assertEqual(self.db.user.get(nid, 'age'), 1)
272 self.db.user.set(nid, age=0)
273 self.assertEqual(self.db.user.get(nid, 'age'), 0)
275 nid = self.db.user.create(username='bar', age=0)
276 self.assertEqual(self.db.user.get(nid, 'age'), 0)
278 def testNumberUnset(self):
279 nid = self.db.user.create(username='foo', age=1)
280 self.db.user.set(nid, age=None)
281 self.assertEqual(self.db.user.get(nid, "age"), None)
283 def testKeyValue(self):
284 newid = self.db.user.create(username="spam")
285 self.assertEqual(self.db.user.lookup('spam'), newid)
286 self.db.commit()
287 self.assertEqual(self.db.user.lookup('spam'), newid)
288 self.db.user.retire(newid)
289 self.assertRaises(KeyError, self.db.user.lookup, 'spam')
291 # use the key again now that the old is retired
292 newid2 = self.db.user.create(username="spam")
293 self.assertNotEqual(newid, newid2)
294 # try to restore old node. this shouldn't succeed!
295 self.assertRaises(KeyError, self.db.user.restore, newid)
297 def testRetire(self):
298 self.db.issue.create(title="spam", status='1')
299 b = self.db.status.get('1', 'name')
300 a = self.db.status.list()
301 self.db.status.retire('1')
302 # make sure the list is different
303 self.assertNotEqual(a, self.db.status.list())
304 # can still access the node if necessary
305 self.assertEqual(self.db.status.get('1', 'name'), b)
306 self.db.commit()
307 self.assertEqual(self.db.status.get('1', 'name'), b)
308 self.assertNotEqual(a, self.db.status.list())
309 # try to restore retired node
310 self.db.status.restore('1')
312 def testCacheCreateSet(self):
313 self.db.issue.create(title="spam", status='1')
314 a = self.db.issue.get('1', 'title')
315 self.assertEqual(a, 'spam')
316 self.db.issue.set('1', title='ham')
317 b = self.db.issue.get('1', 'title')
318 self.assertEqual(b, 'ham')
320 def testSerialisation(self):
321 nid = self.db.issue.create(title="spam", status='1',
322 deadline=date.Date(), foo=date.Interval('-1d'))
323 self.db.commit()
324 assert isinstance(self.db.issue.get(nid, 'deadline'), date.Date)
325 assert isinstance(self.db.issue.get(nid, 'foo'), date.Interval)
326 uid = self.db.user.create(username="fozzy",
327 password=password.Password('t. bear'))
328 self.db.commit()
329 assert isinstance(self.db.user.get(uid, 'password'), password.Password)
331 def testTransactions(self):
332 # remember the number of items we started
333 num_issues = len(self.db.issue.list())
334 num_files = self.db.numfiles()
335 self.db.issue.create(title="don't commit me!", status='1')
336 self.assertNotEqual(num_issues, len(self.db.issue.list()))
337 self.db.rollback()
338 self.assertEqual(num_issues, len(self.db.issue.list()))
339 self.db.issue.create(title="please commit me!", status='1')
340 self.assertNotEqual(num_issues, len(self.db.issue.list()))
341 self.db.commit()
342 self.assertNotEqual(num_issues, len(self.db.issue.list()))
343 self.db.rollback()
344 self.assertNotEqual(num_issues, len(self.db.issue.list()))
345 self.db.file.create(name="test", type="text/plain", content="hi")
346 self.db.rollback()
347 self.assertEqual(num_files, self.db.numfiles())
348 for i in range(10):
349 self.db.file.create(name="test", type="text/plain",
350 content="hi %d"%(i))
351 self.db.commit()
352 num_files2 = self.db.numfiles()
353 self.assertNotEqual(num_files, num_files2)
354 self.db.file.create(name="test", type="text/plain", content="hi")
355 self.db.rollback()
356 self.assertNotEqual(num_files, self.db.numfiles())
357 self.assertEqual(num_files2, self.db.numfiles())
359 # rollback / cache interaction
360 name1 = self.db.user.get('1', 'username')
361 self.db.user.set('1', username = name1+name1)
362 # get the prop so the info's forced into the cache (if there is one)
363 self.db.user.get('1', 'username')
364 self.db.rollback()
365 name2 = self.db.user.get('1', 'username')
366 self.assertEqual(name1, name2)
368 def testDestroyNoJournalling(self):
369 self.innerTestDestroy(klass=self.db.session)
371 def testDestroyJournalling(self):
372 self.innerTestDestroy(klass=self.db.issue)
374 def innerTestDestroy(self, klass):
375 newid = klass.create(title='Mr Friendly')
376 n = len(klass.list())
377 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
378 klass.destroy(newid)
379 self.assertRaises(IndexError, klass.get, newid, 'title')
380 self.assertNotEqual(len(klass.list()), n)
381 if klass.do_journal:
382 self.assertRaises(IndexError, klass.history, newid)
384 # now with a commit
385 newid = klass.create(title='Mr Friendly')
386 n = len(klass.list())
387 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
388 self.db.commit()
389 klass.destroy(newid)
390 self.assertRaises(IndexError, klass.get, newid, 'title')
391 self.db.commit()
392 self.assertRaises(IndexError, klass.get, newid, 'title')
393 self.assertNotEqual(len(klass.list()), n)
394 if klass.do_journal:
395 self.assertRaises(IndexError, klass.history, newid)
397 # now with a rollback
398 newid = klass.create(title='Mr Friendly')
399 n = len(klass.list())
400 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
401 self.db.commit()
402 klass.destroy(newid)
403 self.assertNotEqual(len(klass.list()), n)
404 self.assertRaises(IndexError, klass.get, newid, 'title')
405 self.db.rollback()
406 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
407 self.assertEqual(len(klass.list()), n)
408 if klass.do_journal:
409 self.assertNotEqual(klass.history(newid), [])
411 def testExceptions(self):
412 # this tests the exceptions that should be raised
413 ar = self.assertRaises
415 #
416 # class create
417 #
418 # string property
419 ar(TypeError, self.db.status.create, name=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.db.user.create(username='test')
626 ids = []
627 ids.append(self.db.issue.create(status="1", nosy=['1']))
628 oddid = self.db.issue.create(status="2", nosy=['2'], assignedto='2')
629 ids.append(self.db.issue.create(status="1", nosy=['1','2']))
630 self.db.issue.create(status="3", nosy=['1'], assignedto='1')
631 ids.sort()
633 # should match first and third
634 got = self.db.issue.find(status='1')
635 got.sort()
636 self.assertEqual(got, ids)
638 # none
639 self.assertEqual(self.db.issue.find(status='4'), [])
641 # should match first and third
642 got = self.db.issue.find(assignedto=None)
643 got.sort()
644 self.assertEqual(got, ids)
646 # should match first three
647 got = self.db.issue.find(status='1', nosy='2')
648 got.sort()
649 ids.append(oddid)
650 ids.sort()
651 self.assertEqual(got, ids)
653 # none
654 self.assertEqual(self.db.issue.find(status='4', nosy='3'), [])
656 def testStringFind(self):
657 ids = []
658 ids.append(self.db.issue.create(title="spam"))
659 self.db.issue.create(title="not spam")
660 ids.append(self.db.issue.create(title="spam"))
661 ids.sort()
662 got = self.db.issue.stringFind(title='spam')
663 got.sort()
664 self.assertEqual(got, ids)
665 self.assertEqual(self.db.issue.stringFind(title='fubar'), [])
667 def filteringSetup(self):
668 for user in (
669 {'username': 'bleep'},
670 {'username': 'blop'},
671 {'username': 'blorp'}):
672 self.db.user.create(**user)
673 iss = self.db.issue
674 for issue in (
675 {'title': 'issue one', 'status': '2',
676 'foo': date.Interval('1:10'),
677 'deadline': date.Date('2003-01-01.00:00')},
678 {'title': 'issue two', 'status': '1',
679 'foo': date.Interval('1d'),
680 'deadline': date.Date('2003-02-16.22:50')},
681 {'title': 'issue three', 'status': '1',
682 'nosy': ['1','2'], 'deadline': date.Date('2003-02-18')},
683 {'title': 'non four', 'status': '3',
684 'foo': date.Interval('0:10'),
685 'nosy': ['1'], 'deadline': date.Date('2004-03-08')}):
686 self.db.issue.create(**issue)
687 self.db.commit()
688 return self.assertEqual, self.db.issue.filter
690 def testFilteringID(self):
691 ae, filt = self.filteringSetup()
692 ae(filt(None, {'id': '1'}, ('+','id'), (None,None)), ['1'])
694 def testFilteringString(self):
695 ae, filt = self.filteringSetup()
696 ae(filt(None, {'title': ['one']}, ('+','id'), (None,None)), ['1'])
697 ae(filt(None, {'title': ['issue']}, ('+','id'), (None,None)),
698 ['1','2','3'])
699 ae(filt(None, {'title': ['one', 'two']}, ('+','id'), (None,None)),
700 ['1', '2'])
702 def testFilteringLink(self):
703 ae, filt = self.filteringSetup()
704 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['2','3'])
706 def testFilteringRetired(self):
707 ae, filt = self.filteringSetup()
708 self.db.issue.retire('2')
709 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['3'])
711 def testFilteringMultilink(self):
712 ae, filt = self.filteringSetup()
713 ae(filt(None, {'nosy': '2'}, ('+','id'), (None,None)), ['3'])
714 ae(filt(None, {'nosy': '-1'}, ('+','id'), (None,None)), ['1', '2'])
716 def testFilteringMany(self):
717 ae, filt = self.filteringSetup()
718 ae(filt(None, {'nosy': '2', 'status': '1'}, ('+','id'), (None,None)),
719 ['3'])
721 def testFilteringRange(self):
722 ae, filt = self.filteringSetup()
723 # Date ranges
724 ae(filt(None, {'deadline': 'from 2003-02-10 to 2003-02-23'}), ['2','3'])
725 ae(filt(None, {'deadline': '2003-02-10; 2003-02-23'}), ['2','3'])
726 ae(filt(None, {'deadline': '; 2003-02-16'}), ['1'])
727 # Lets assume people won't invent a time machine, otherwise this test
728 # may fail :)
729 ae(filt(None, {'deadline': 'from 2003-02-16'}), ['2', '3', '4'])
730 ae(filt(None, {'deadline': '2003-02-16;'}), ['2', '3', '4'])
731 # year and month granularity
732 ae(filt(None, {'deadline': '2002'}), [])
733 ae(filt(None, {'deadline': '2003'}), ['1', '2', '3'])
734 ae(filt(None, {'deadline': '2004'}), ['4'])
735 ae(filt(None, {'deadline': '2003-02'}), ['2', '3'])
736 ae(filt(None, {'deadline': '2003-03'}), [])
737 ae(filt(None, {'deadline': '2003-02-16'}), ['2'])
738 ae(filt(None, {'deadline': '2003-02-17'}), [])
739 # Interval ranges
740 ae(filt(None, {'foo': 'from 0:50 to 2:00'}), ['1'])
741 ae(filt(None, {'foo': 'from 0:50 to 1d 2:00'}), ['1', '2'])
742 ae(filt(None, {'foo': 'from 5:50'}), ['2'])
743 ae(filt(None, {'foo': 'to 0:05'}), [])
745 def testFilteringIntervalSort(self):
746 ae, filt = self.filteringSetup()
747 # ascending should sort None, 1:10, 1d
748 ae(filt(None, {}, ('+','foo'), (None,None)), ['3', '4', '1', '2'])
749 # descending should sort 1d, 1:10, None
750 ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3'])
752 # XXX add sorting tests for other types
753 # XXX test auditors and reactors
756 class ROTest(MyTestCase):
757 def setUp(self):
758 # remove previous test, ignore errors
759 if os.path.exists(config.DATABASE):
760 shutil.rmtree(config.DATABASE)
761 os.makedirs(config.DATABASE + '/files')
762 self.db = self.module.Database(config, 'admin')
763 setupSchema(self.db, 1, self.module)
764 self.db.close()
766 self.db = self.module.Database(config)
767 setupSchema(self.db, 0, self.module)
769 def testExceptions(self):
770 # this tests the exceptions that should be raised
771 ar = self.assertRaises
773 # this tests the exceptions that should be raised
774 ar(DatabaseError, self.db.status.create, name="foo")
775 ar(DatabaseError, self.db.status.set, '1', name="foo")
776 ar(DatabaseError, self.db.status.retire, '1')
779 class SchemaTest(MyTestCase):
780 def setUp(self):
781 # remove previous test, ignore errors
782 if os.path.exists(config.DATABASE):
783 shutil.rmtree(config.DATABASE)
784 os.makedirs(config.DATABASE + '/files')
786 def init_a(self):
787 self.db = self.module.Database(config, 'admin')
788 a = self.module.Class(self.db, "a", name=String())
789 a.setkey("name")
790 self.db.post_init()
792 def init_ab(self):
793 self.db = self.module.Database(config, 'admin')
794 a = self.module.Class(self.db, "a", name=String())
795 a.setkey("name")
796 b = self.module.Class(self.db, "b", name=String())
797 b.setkey("name")
798 self.db.post_init()
800 def test_addNewClass(self):
801 self.init_a()
802 aid = self.db.a.create(name='apple')
803 self.db.commit(); self.db.close()
805 # add a new class to the schema and check creation of new items
806 # (and existence of old ones)
807 self.init_ab()
808 bid = self.db.b.create(name='bear')
809 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
810 self.db.commit()
811 self.db.close()
813 # now check we can recall the added class' items
814 self.init_ab()
815 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
816 self.assertEqual(self.db.a.lookup('apple'), aid)
817 self.assertEqual(self.db.b.get(bid, 'name'), 'bear')
818 self.assertEqual(self.db.b.lookup('bear'), bid)
820 # confirm journal's ok
821 self.db.getjournal('a', aid)
822 self.db.getjournal('b', bid)
824 def init_amod(self):
825 self.db = self.module.Database(config, 'admin')
826 a = self.module.Class(self.db, "a", name=String(), fooz=String())
827 a.setkey("name")
828 b = self.module.Class(self.db, "b", name=String())
829 b.setkey("name")
830 self.db.post_init()
832 def test_modifyClass(self):
833 self.init_ab()
835 # add item to user and issue class
836 aid = self.db.a.create(name='apple')
837 bid = self.db.b.create(name='bear')
838 self.db.commit(); self.db.close()
840 # modify "a" schema
841 self.init_amod()
842 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
843 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
844 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
845 aid2 = self.db.a.create(name='aardvark', fooz='booz')
846 self.db.commit(); self.db.close()
848 # test
849 self.init_amod()
850 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
851 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
852 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
853 self.assertEqual(self.db.a.get(aid2, 'name'), 'aardvark')
854 self.assertEqual(self.db.a.get(aid2, 'fooz'), 'booz')
856 # confirm journal's ok
857 self.db.getjournal('a', aid)
858 self.db.getjournal('a', aid2)
860 def init_amodkey(self):
861 self.db = self.module.Database(config, 'admin')
862 a = self.module.Class(self.db, "a", name=String(), fooz=String())
863 a.setkey("fooz")
864 b = self.module.Class(self.db, "b", name=String())
865 b.setkey("name")
866 self.db.post_init()
868 def test_changeClassKey(self):
869 self.init_amod()
870 aid = self.db.a.create(name='apple')
871 self.assertEqual(self.db.a.lookup('apple'), aid)
872 self.db.commit(); self.db.close()
874 # change the key to fooz
875 self.init_amodkey()
876 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
877 self.assertEqual(self.db.a.get(aid, 'fooz'), None)
878 self.assertRaises(KeyError, self.db.a.lookup, 'apple')
879 aid2 = self.db.a.create(name='aardvark', fooz='booz')
880 self.db.commit(); self.db.close()
882 # check
883 self.init_amodkey()
884 self.assertEqual(self.db.a.lookup('booz'), aid2)
886 # confirm journal's ok
887 self.db.getjournal('a', aid)
889 def init_ml(self):
890 self.db = self.module.Database(config, 'admin')
891 a = self.module.Class(self.db, "a", name=String())
892 a.setkey('name')
893 b = self.module.Class(self.db, "b", name=String(),
894 fooz=Multilink('a'))
895 b.setkey("name")
896 self.db.post_init()
898 def test_makeNewMultilink(self):
899 self.init_a()
900 aid = self.db.a.create(name='apple')
901 self.assertEqual(self.db.a.lookup('apple'), aid)
902 self.db.commit(); self.db.close()
904 # add a multilink prop
905 self.init_ml()
906 bid = self.db.b.create(name='bear', fooz=[aid])
907 self.assertEqual(self.db.b.find(fooz=aid), [bid])
908 self.assertEqual(self.db.a.lookup('apple'), aid)
909 self.db.commit(); self.db.close()
911 # check
912 self.init_ml()
913 self.assertEqual(self.db.b.find(fooz=aid), [bid])
914 self.assertEqual(self.db.a.lookup('apple'), aid)
915 self.assertEqual(self.db.b.lookup('bear'), bid)
917 # confirm journal's ok
918 self.db.getjournal('a', aid)
919 self.db.getjournal('b', bid)
921 def test_removeMultilink(self):
922 # add a multilink prop
923 self.init_ml()
924 aid = self.db.a.create(name='apple')
925 bid = self.db.b.create(name='bear', fooz=[aid])
926 self.assertEqual(self.db.b.find(fooz=aid), [bid])
927 self.assertEqual(self.db.a.lookup('apple'), aid)
928 self.assertEqual(self.db.b.lookup('bear'), bid)
929 self.db.commit(); self.db.close()
931 # remove the multilink
932 self.init_ab()
933 self.assertEqual(self.db.a.lookup('apple'), aid)
934 self.assertEqual(self.db.b.lookup('bear'), bid)
936 # confirm journal's ok
937 self.db.getjournal('a', aid)
938 self.db.getjournal('b', bid)
940 def test_removeClass(self):
941 self.init_ml()
942 aid = self.db.a.create(name='apple')
943 bid = self.db.b.create(name='bear', fooz=[aid])
944 self.db.commit(); self.db.close()
946 # drop the b class
947 self.init_a()
948 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
949 self.assertEqual(self.db.a.lookup('apple'), aid)
950 self.db.commit(); self.db.close()
952 # now check we can recall the added class' items
953 self.init_a()
954 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
955 self.assertEqual(self.db.a.lookup('apple'), aid)
957 # confirm journal's ok
958 self.db.getjournal('a', aid)
961 class ClassicInitTest(unittest.TestCase):
962 count = 0
963 db = None
965 def setUp(self):
966 ClassicInitTest.count = ClassicInitTest.count + 1
967 self.dirname = '_test_init_%s'%self.count
968 try:
969 shutil.rmtree(self.dirname)
970 except OSError, error:
971 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
973 def testCreation(self):
974 ae = self.assertEqual
976 # create the instance
977 init.install(self.dirname, 'templates/classic')
978 init.write_select_db(self.dirname, self.backend)
980 if self.extra_config:
981 f = open(os.path.join(self.dirname, 'config.py'), 'a')
982 try:
983 f.write(self.extra_config)
984 finally:
985 f.close()
987 init.initialise(self.dirname, 'sekrit')
989 # check we can load the package
990 instance = imp.load_package(self.dirname, self.dirname)
992 # and open the database
993 db = self.db = instance.open()
995 # check the basics of the schema and initial data set
996 l = db.priority.list()
997 ae(l, ['1', '2', '3', '4', '5'])
998 l = db.status.list()
999 ae(l, ['1', '2', '3', '4', '5', '6', '7', '8'])
1000 l = db.keyword.list()
1001 ae(l, [])
1002 l = db.user.list()
1003 ae(l, ['1', '2'])
1004 l = db.msg.list()
1005 ae(l, [])
1006 l = db.file.list()
1007 ae(l, [])
1008 l = db.issue.list()
1009 ae(l, [])
1011 def tearDown(self):
1012 if self.db is not None:
1013 self.db.close()
1014 try:
1015 shutil.rmtree(self.dirname)
1016 except OSError, error:
1017 if error.errno not in (errno.ENOENT, errno.ESRCH): raise