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.101 2008-08-19 01:40:59 richard Exp $
20 import unittest, os, shutil, errno, imp, sys, time, pprint, base64, os.path
21 # Python 2.3 ... 2.6 compatibility:
22 from roundup.anypy.sets_ import set
24 from roundup.hyperdb import String, Password, Link, Multilink, Date, \
25 Interval, DatabaseError, Boolean, Number, Node
26 from roundup.mailer import Mailer
27 from roundup import date, password, init, instance, configuration, \
28 roundupdb, i18n
30 from mocknull import MockNull
32 config = configuration.CoreConfig()
33 config.DATABASE = "db"
34 config.RDBMS_NAME = "rounduptest"
35 config.RDBMS_HOST = "localhost"
36 config.RDBMS_USER = "rounduptest"
37 config.RDBMS_PASSWORD = "rounduptest"
38 #config.logging = MockNull()
39 # these TRACKER_WEB and MAIL_DOMAIN values are used in mailgw tests
40 config.MAIL_DOMAIN = "your.tracker.email.domain.example"
41 config.TRACKER_WEB = "http://tracker.example/cgi-bin/roundup.cgi/bugs/"
42 # uncomment the following to have excessive debug output from test cases
43 # FIXME: tracker logging level should be increased by -v arguments
44 # to 'run_tests.py' script
45 #config.LOGGING_FILENAME = "/tmp/logfile"
46 #config.LOGGING_LEVEL = "DEBUG"
47 config.init_logging()
49 def setupTracker(dirname, backend="anydbm"):
50 """Install and initialize new tracker in dirname; return tracker instance.
52 If the directory exists, it is wiped out before the operation.
54 """
55 global config
56 try:
57 shutil.rmtree(dirname)
58 except OSError, error:
59 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
60 # create the instance
61 init.install(dirname, os.path.join(os.path.dirname(__file__),
62 '..',
63 'share',
64 'roundup',
65 'templates',
66 'classic'))
67 init.write_select_db(dirname, backend)
68 config.save(os.path.join(dirname, 'config.ini'))
69 tracker = instance.open(dirname)
70 if tracker.exists():
71 tracker.nuke()
72 init.write_select_db(dirname, backend)
73 tracker.init(password.Password('sekrit'))
74 return tracker
76 def setupSchema(db, create, module):
77 status = module.Class(db, "status", name=String())
78 status.setkey("name")
79 priority = module.Class(db, "priority", name=String(), order=String())
80 priority.setkey("name")
81 user = module.Class(db, "user", username=String(), password=Password(),
82 assignable=Boolean(), age=Number(), roles=String(), address=String(),
83 supervisor=Link('user'),realname=String())
84 user.setkey("username")
85 file = module.FileClass(db, "file", name=String(), type=String(),
86 comment=String(indexme="yes"), fooz=Password())
87 file_nidx = module.FileClass(db, "file_nidx", content=String(indexme='no'))
88 issue = module.IssueClass(db, "issue", title=String(indexme="yes"),
89 status=Link("status"), nosy=Multilink("user"), deadline=Date(),
90 foo=Interval(), files=Multilink("file"), assignedto=Link('user'),
91 priority=Link('priority'), spam=Multilink('msg'),
92 feedback=Link('msg'))
93 stuff = module.Class(db, "stuff", stuff=String())
94 session = module.Class(db, 'session', title=String())
95 msg = module.FileClass(db, "msg", date=Date(),
96 author=Link("user", do_journal='no'),
97 files=Multilink('file'), inreplyto=String(),
98 messageid=String(),
99 recipients=Multilink("user", do_journal='no')
100 )
101 session.disableJournalling()
102 db.post_init()
103 if create:
104 user.create(username="admin", roles='Admin',
105 password=password.Password('sekrit'))
106 user.create(username="fred", roles='User',
107 password=password.Password('sekrit'), address='fred@example.com')
108 status.create(name="unread")
109 status.create(name="in-progress")
110 status.create(name="testing")
111 status.create(name="resolved")
112 priority.create(name="feature", order="2")
113 priority.create(name="wish", order="3")
114 priority.create(name="bug", order="1")
115 db.commit()
117 # nosy tests require this
118 db.security.addPermissionToRole('User', 'View', 'msg')
120 class MyTestCase(unittest.TestCase):
121 def tearDown(self):
122 if hasattr(self, 'db'):
123 self.db.close()
124 if os.path.exists(config.DATABASE):
125 shutil.rmtree(config.DATABASE)
127 if os.environ.has_key('LOGGING_LEVEL'):
128 from roundup import rlog
129 config.logging = rlog.BasicLogging()
130 config.logging.setLevel(os.environ['LOGGING_LEVEL'])
131 config.logging.getLogger('hyperdb').setFormat('%(message)s')
133 class DBTest(MyTestCase):
134 def setUp(self):
135 # remove previous test, ignore errors
136 if os.path.exists(config.DATABASE):
137 shutil.rmtree(config.DATABASE)
138 os.makedirs(config.DATABASE + '/files')
139 self.open_database()
140 setupSchema(self.db, 1, self.module)
142 def open_database(self):
143 self.db = self.module.Database(config, 'admin')
145 def testRefresh(self):
146 self.db.refresh_database()
148 #
149 # automatic properties (well, the two easy ones anyway)
150 #
151 def testCreatorProperty(self):
152 i = self.db.issue
153 id1 = i.create(title='spam')
154 self.db.journaltag = 'fred'
155 id2 = i.create(title='spam')
156 self.assertNotEqual(id1, id2)
157 self.assertNotEqual(i.get(id1, 'creator'), i.get(id2, 'creator'))
159 def testActorProperty(self):
160 i = self.db.issue
161 id1 = i.create(title='spam')
162 self.db.journaltag = 'fred'
163 i.set(id1, title='asfasd')
164 self.assertNotEqual(i.get(id1, 'creator'), i.get(id1, 'actor'))
166 # ID number controls
167 def testIDGeneration(self):
168 id1 = self.db.issue.create(title="spam", status='1')
169 id2 = self.db.issue.create(title="eggs", status='2')
170 self.assertNotEqual(id1, id2)
171 def testIDSetting(self):
172 # XXX numeric ids
173 self.db.setid('issue', 10)
174 id2 = self.db.issue.create(title="eggs", status='2')
175 self.assertEqual('11', id2)
177 #
178 # basic operations
179 #
180 def testEmptySet(self):
181 id1 = self.db.issue.create(title="spam", status='1')
182 self.db.issue.set(id1)
184 # String
185 def testStringChange(self):
186 for commit in (0,1):
187 # test set & retrieve
188 nid = self.db.issue.create(title="spam", status='1')
189 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
191 # change and make sure we retrieve the correct value
192 self.db.issue.set(nid, title='eggs')
193 if commit: self.db.commit()
194 self.assertEqual(self.db.issue.get(nid, 'title'), 'eggs')
196 def testStringUnset(self):
197 for commit in (0,1):
198 nid = self.db.issue.create(title="spam", status='1')
199 if commit: self.db.commit()
200 self.assertEqual(self.db.issue.get(nid, 'title'), 'spam')
201 # make sure we can unset
202 self.db.issue.set(nid, title=None)
203 if commit: self.db.commit()
204 self.assertEqual(self.db.issue.get(nid, "title"), None)
206 # FileClass "content" property (no unset test)
207 def testFileClassContentChange(self):
208 for commit in (0,1):
209 # test set & retrieve
210 nid = self.db.file.create(content="spam")
211 self.assertEqual(self.db.file.get(nid, 'content'), 'spam')
213 # change and make sure we retrieve the correct value
214 self.db.file.set(nid, content='eggs')
215 if commit: self.db.commit()
216 self.assertEqual(self.db.file.get(nid, 'content'), 'eggs')
218 def testStringUnicode(self):
219 # test set & retrieve
220 ustr = u'\xe4\xf6\xfc\u20ac'.encode('utf8')
221 nid = self.db.issue.create(title=ustr, status='1')
222 self.assertEqual(self.db.issue.get(nid, 'title'), ustr)
224 # change and make sure we retrieve the correct value
225 ustr2 = u'change \u20ac change'.encode('utf8')
226 self.db.issue.set(nid, title=ustr2)
227 self.db.commit()
228 self.assertEqual(self.db.issue.get(nid, 'title'), ustr2)
230 # Link
231 def testLinkChange(self):
232 self.assertRaises(IndexError, self.db.issue.create, title="spam",
233 status='100')
234 for commit in (0,1):
235 nid = self.db.issue.create(title="spam", status='1')
236 if commit: self.db.commit()
237 self.assertEqual(self.db.issue.get(nid, "status"), '1')
238 self.db.issue.set(nid, status='2')
239 if commit: self.db.commit()
240 self.assertEqual(self.db.issue.get(nid, "status"), '2')
242 def testLinkUnset(self):
243 for commit in (0,1):
244 nid = self.db.issue.create(title="spam", status='1')
245 if commit: self.db.commit()
246 self.db.issue.set(nid, status=None)
247 if commit: self.db.commit()
248 self.assertEqual(self.db.issue.get(nid, "status"), None)
250 # Multilink
251 def testMultilinkChange(self):
252 for commit in (0,1):
253 self.assertRaises(IndexError, self.db.issue.create, title="spam",
254 nosy=['foo%s'%commit])
255 u1 = self.db.user.create(username='foo%s'%commit)
256 u2 = self.db.user.create(username='bar%s'%commit)
257 nid = self.db.issue.create(title="spam", nosy=[u1])
258 if commit: self.db.commit()
259 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
260 self.db.issue.set(nid, nosy=[])
261 if commit: self.db.commit()
262 self.assertEqual(self.db.issue.get(nid, "nosy"), [])
263 self.db.issue.set(nid, nosy=[u1,u2])
264 if commit: self.db.commit()
265 l = [u1,u2]; l.sort()
266 m = self.db.issue.get(nid, "nosy"); m.sort()
267 self.assertEqual(l, m)
269 # verify that when we pass None to an Multilink it sets
270 # it to an empty list
271 self.db.issue.set(nid, nosy=None)
272 if commit: self.db.commit()
273 self.assertEqual(self.db.issue.get(nid, "nosy"), [])
275 def testMultilinkChangeIterable(self):
276 for commit in (0,1):
277 # invalid nosy value assertion
278 self.assertRaises(IndexError, self.db.issue.create, title='spam',
279 nosy=['foo%s'%commit])
280 # invalid type for nosy create
281 self.assertRaises(TypeError, self.db.issue.create, title='spam',
282 nosy=1)
283 u1 = self.db.user.create(username='foo%s'%commit)
284 u2 = self.db.user.create(username='bar%s'%commit)
285 # try a couple of the built-in iterable types to make
286 # sure that we accept them and handle them properly
287 # try a set as input for the multilink
288 nid = self.db.issue.create(title="spam", nosy=set(u1))
289 if commit: self.db.commit()
290 self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
291 self.assertRaises(TypeError, self.db.issue.set, nid,
292 nosy='invalid type')
293 # test with a tuple
294 self.db.issue.set(nid, nosy=tuple())
295 if commit: self.db.commit()
296 self.assertEqual(self.db.issue.get(nid, "nosy"), [])
297 # make sure we accept a frozen set
298 self.db.issue.set(nid, nosy=set([u1,u2]))
299 if commit: self.db.commit()
300 l = [u1,u2]; l.sort()
301 m = self.db.issue.get(nid, "nosy"); m.sort()
302 self.assertEqual(l, m)
305 # XXX one day, maybe...
306 # def testMultilinkOrdering(self):
307 # for i in range(10):
308 # self.db.user.create(username='foo%s'%i)
309 # i = self.db.issue.create(title="spam", nosy=['5','3','12','4'])
310 # self.db.commit()
311 # l = self.db.issue.get(i, "nosy")
312 # # all backends should return the Multilink numeric-id-sorted
313 # self.assertEqual(l, ['3', '4', '5', '12'])
315 # Date
316 def testDateChange(self):
317 self.assertRaises(TypeError, self.db.issue.create,
318 title='spam', deadline=1)
319 for commit in (0,1):
320 nid = self.db.issue.create(title="spam", status='1')
321 self.assertRaises(TypeError, self.db.issue.set, nid, deadline=1)
322 a = self.db.issue.get(nid, "deadline")
323 if commit: self.db.commit()
324 self.db.issue.set(nid, deadline=date.Date())
325 b = self.db.issue.get(nid, "deadline")
326 if commit: self.db.commit()
327 self.assertNotEqual(a, b)
328 self.assertNotEqual(b, date.Date('1970-1-1.00:00:00'))
329 # The 1970 date will fail for metakit -- it is used
330 # internally for storing NULL. The others would, too
331 # because metakit tries to convert date.timestamp to an int
332 # for storing and fails with an overflow.
333 for d in [date.Date (x) for x in '2038', '1970', '0033', '9999']:
334 self.db.issue.set(nid, deadline=d)
335 if commit: self.db.commit()
336 c = self.db.issue.get(nid, "deadline")
337 self.assertEqual(c, d)
339 def testDateLeapYear(self):
340 nid = self.db.issue.create(title='spam', status='1',
341 deadline=date.Date('2008-02-29'))
342 self.assertEquals(str(self.db.issue.get(nid, 'deadline')),
343 '2008-02-29.00:00:00')
344 self.assertEquals(self.db.issue.filter(None,
345 {'deadline': '2008-02-29'}), [nid])
346 self.db.issue.set(nid, deadline=date.Date('2008-03-01'))
347 self.assertEquals(str(self.db.issue.get(nid, 'deadline')),
348 '2008-03-01.00:00:00')
349 self.assertEquals(self.db.issue.filter(None,
350 {'deadline': '2008-02-29'}), [])
352 def testDateUnset(self):
353 for commit in (0,1):
354 nid = self.db.issue.create(title="spam", status='1')
355 self.db.issue.set(nid, deadline=date.Date())
356 if commit: self.db.commit()
357 self.assertNotEqual(self.db.issue.get(nid, "deadline"), None)
358 self.db.issue.set(nid, deadline=None)
359 if commit: self.db.commit()
360 self.assertEqual(self.db.issue.get(nid, "deadline"), None)
362 # Interval
363 def testIntervalChange(self):
364 self.assertRaises(TypeError, self.db.issue.create,
365 title='spam', foo=1)
366 for commit in (0,1):
367 nid = self.db.issue.create(title="spam", status='1')
368 self.assertRaises(TypeError, self.db.issue.set, nid, foo=1)
369 if commit: self.db.commit()
370 a = self.db.issue.get(nid, "foo")
371 i = date.Interval('-1d')
372 self.db.issue.set(nid, foo=i)
373 if commit: self.db.commit()
374 self.assertNotEqual(self.db.issue.get(nid, "foo"), a)
375 self.assertEqual(i, self.db.issue.get(nid, "foo"))
376 j = date.Interval('1y')
377 self.db.issue.set(nid, foo=j)
378 if commit: self.db.commit()
379 self.assertNotEqual(self.db.issue.get(nid, "foo"), i)
380 self.assertEqual(j, self.db.issue.get(nid, "foo"))
382 def testIntervalUnset(self):
383 for commit in (0,1):
384 nid = self.db.issue.create(title="spam", status='1')
385 self.db.issue.set(nid, foo=date.Interval('-1d'))
386 if commit: self.db.commit()
387 self.assertNotEqual(self.db.issue.get(nid, "foo"), None)
388 self.db.issue.set(nid, foo=None)
389 if commit: self.db.commit()
390 self.assertEqual(self.db.issue.get(nid, "foo"), None)
392 # Boolean
393 def testBooleanSet(self):
394 nid = self.db.user.create(username='one', assignable=1)
395 self.assertEqual(self.db.user.get(nid, "assignable"), 1)
396 nid = self.db.user.create(username='two', assignable=0)
397 self.assertEqual(self.db.user.get(nid, "assignable"), 0)
399 def testBooleanChange(self):
400 userid = self.db.user.create(username='foo', assignable=1)
401 self.assertEqual(1, self.db.user.get(userid, 'assignable'))
402 self.db.user.set(userid, assignable=0)
403 self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
404 self.db.user.set(userid, assignable=1)
405 self.assertEqual(self.db.user.get(userid, 'assignable'), 1)
407 def testBooleanUnset(self):
408 nid = self.db.user.create(username='foo', assignable=1)
409 self.db.user.set(nid, assignable=None)
410 self.assertEqual(self.db.user.get(nid, "assignable"), None)
412 # Number
413 def testNumberChange(self):
414 nid = self.db.user.create(username='foo', age=1)
415 self.assertEqual(1, self.db.user.get(nid, 'age'))
416 self.db.user.set(nid, age=3)
417 self.assertNotEqual(self.db.user.get(nid, 'age'), 1)
418 self.db.user.set(nid, age=1.0)
419 self.assertEqual(self.db.user.get(nid, 'age'), 1)
420 self.db.user.set(nid, age=0)
421 self.assertEqual(self.db.user.get(nid, 'age'), 0)
423 nid = self.db.user.create(username='bar', age=0)
424 self.assertEqual(self.db.user.get(nid, 'age'), 0)
426 def testNumberUnset(self):
427 nid = self.db.user.create(username='foo', age=1)
428 self.db.user.set(nid, age=None)
429 self.assertEqual(self.db.user.get(nid, "age"), None)
431 # Password
432 def testPasswordChange(self):
433 x = password.Password('x')
434 userid = self.db.user.create(username='foo', password=x)
435 self.assertEqual(x, self.db.user.get(userid, 'password'))
436 self.assertEqual(self.db.user.get(userid, 'password'), 'x')
437 y = password.Password('y')
438 self.db.user.set(userid, password=y)
439 self.assertEqual(self.db.user.get(userid, 'password'), 'y')
440 self.assertRaises(TypeError, self.db.user.create, userid,
441 username='bar', password='x')
442 self.assertRaises(TypeError, self.db.user.set, userid, password='x')
444 def testPasswordUnset(self):
445 x = password.Password('x')
446 nid = self.db.user.create(username='foo', password=x)
447 self.db.user.set(nid, assignable=None)
448 self.assertEqual(self.db.user.get(nid, "assignable"), None)
450 # key value
451 def testKeyValue(self):
452 self.assertRaises(ValueError, self.db.user.create)
454 newid = self.db.user.create(username="spam")
455 self.assertEqual(self.db.user.lookup('spam'), newid)
456 self.db.commit()
457 self.assertEqual(self.db.user.lookup('spam'), newid)
458 self.db.user.retire(newid)
459 self.assertRaises(KeyError, self.db.user.lookup, 'spam')
461 # use the key again now that the old is retired
462 newid2 = self.db.user.create(username="spam")
463 self.assertNotEqual(newid, newid2)
464 # try to restore old node. this shouldn't succeed!
465 self.assertRaises(KeyError, self.db.user.restore, newid)
467 self.assertRaises(TypeError, self.db.issue.lookup, 'fubar')
469 # label property
470 def testLabelProp(self):
471 # key prop
472 self.assertEqual(self.db.status.labelprop(), 'name')
473 self.assertEqual(self.db.user.labelprop(), 'username')
474 # title
475 self.assertEqual(self.db.issue.labelprop(), 'title')
476 # name
477 self.assertEqual(self.db.file.labelprop(), 'name')
478 # id
479 self.assertEqual(self.db.stuff.labelprop(default_to_id=1), 'id')
481 # retirement
482 def testRetire(self):
483 self.db.issue.create(title="spam", status='1')
484 b = self.db.status.get('1', 'name')
485 a = self.db.status.list()
486 nodeids = self.db.status.getnodeids()
487 self.db.status.retire('1')
488 others = nodeids[:]
489 others.remove('1')
491 self.assertEqual(set(self.db.status.getnodeids()),
492 set(nodeids))
493 self.assertEqual(set(self.db.status.getnodeids(retired=True)),
494 set(['1']))
495 self.assertEqual(set(self.db.status.getnodeids(retired=False)),
496 set(others))
498 self.assert_(self.db.status.is_retired('1'))
500 # make sure the list is different
501 self.assertNotEqual(a, self.db.status.list())
503 # can still access the node if necessary
504 self.assertEqual(self.db.status.get('1', 'name'), b)
505 self.assertRaises(IndexError, self.db.status.set, '1', name='hello')
506 self.db.commit()
507 self.assert_(self.db.status.is_retired('1'))
508 self.assertEqual(self.db.status.get('1', 'name'), b)
509 self.assertNotEqual(a, self.db.status.list())
511 # try to restore retired node
512 self.db.status.restore('1')
514 self.assert_(not self.db.status.is_retired('1'))
516 def testCacheCreateSet(self):
517 self.db.issue.create(title="spam", status='1')
518 a = self.db.issue.get('1', 'title')
519 self.assertEqual(a, 'spam')
520 self.db.issue.set('1', title='ham')
521 b = self.db.issue.get('1', 'title')
522 self.assertEqual(b, 'ham')
524 def testSerialisation(self):
525 nid = self.db.issue.create(title="spam", status='1',
526 deadline=date.Date(), foo=date.Interval('-1d'))
527 self.db.commit()
528 assert isinstance(self.db.issue.get(nid, 'deadline'), date.Date)
529 assert isinstance(self.db.issue.get(nid, 'foo'), date.Interval)
530 uid = self.db.user.create(username="fozzy",
531 password=password.Password('t. bear'))
532 self.db.commit()
533 assert isinstance(self.db.user.get(uid, 'password'), password.Password)
535 def testTransactions(self):
536 # remember the number of items we started
537 num_issues = len(self.db.issue.list())
538 num_files = self.db.numfiles()
539 self.db.issue.create(title="don't commit me!", status='1')
540 self.assertNotEqual(num_issues, len(self.db.issue.list()))
541 self.db.rollback()
542 self.assertEqual(num_issues, len(self.db.issue.list()))
543 self.db.issue.create(title="please commit me!", status='1')
544 self.assertNotEqual(num_issues, len(self.db.issue.list()))
545 self.db.commit()
546 self.assertNotEqual(num_issues, len(self.db.issue.list()))
547 self.db.rollback()
548 self.assertNotEqual(num_issues, len(self.db.issue.list()))
549 self.db.file.create(name="test", type="text/plain", content="hi")
550 self.db.rollback()
551 self.assertEqual(num_files, self.db.numfiles())
552 for i in range(10):
553 self.db.file.create(name="test", type="text/plain",
554 content="hi %d"%(i))
555 self.db.commit()
556 num_files2 = self.db.numfiles()
557 self.assertNotEqual(num_files, num_files2)
558 self.db.file.create(name="test", type="text/plain", content="hi")
559 self.db.rollback()
560 self.assertNotEqual(num_files, self.db.numfiles())
561 self.assertEqual(num_files2, self.db.numfiles())
563 # rollback / cache interaction
564 name1 = self.db.user.get('1', 'username')
565 self.db.user.set('1', username = name1+name1)
566 # get the prop so the info's forced into the cache (if there is one)
567 self.db.user.get('1', 'username')
568 self.db.rollback()
569 name2 = self.db.user.get('1', 'username')
570 self.assertEqual(name1, name2)
572 def testDestroyBlob(self):
573 # destroy an uncommitted blob
574 f1 = self.db.file.create(content='hello', type="text/plain")
575 self.db.commit()
576 fn = self.db.filename('file', f1)
577 self.db.file.destroy(f1)
578 self.db.commit()
579 self.assertEqual(os.path.exists(fn), False)
581 def testDestroyNoJournalling(self):
582 self.innerTestDestroy(klass=self.db.session)
584 def testDestroyJournalling(self):
585 self.innerTestDestroy(klass=self.db.issue)
587 def innerTestDestroy(self, klass):
588 newid = klass.create(title='Mr Friendly')
589 n = len(klass.list())
590 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
591 count = klass.count()
592 klass.destroy(newid)
593 self.assertNotEqual(count, klass.count())
594 self.assertRaises(IndexError, klass.get, newid, 'title')
595 self.assertNotEqual(len(klass.list()), n)
596 if klass.do_journal:
597 self.assertRaises(IndexError, klass.history, newid)
599 # now with a commit
600 newid = klass.create(title='Mr Friendly')
601 n = len(klass.list())
602 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
603 self.db.commit()
604 count = klass.count()
605 klass.destroy(newid)
606 self.assertNotEqual(count, klass.count())
607 self.assertRaises(IndexError, klass.get, newid, 'title')
608 self.db.commit()
609 self.assertRaises(IndexError, klass.get, newid, 'title')
610 self.assertNotEqual(len(klass.list()), n)
611 if klass.do_journal:
612 self.assertRaises(IndexError, klass.history, newid)
614 # now with a rollback
615 newid = klass.create(title='Mr Friendly')
616 n = len(klass.list())
617 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
618 self.db.commit()
619 count = klass.count()
620 klass.destroy(newid)
621 self.assertNotEqual(len(klass.list()), n)
622 self.assertRaises(IndexError, klass.get, newid, 'title')
623 self.db.rollback()
624 self.assertEqual(count, klass.count())
625 self.assertEqual(klass.get(newid, 'title'), 'Mr Friendly')
626 self.assertEqual(len(klass.list()), n)
627 if klass.do_journal:
628 self.assertNotEqual(klass.history(newid), [])
630 def testExceptions(self):
631 # this tests the exceptions that should be raised
632 ar = self.assertRaises
634 ar(KeyError, self.db.getclass, 'fubar')
636 #
637 # class create
638 #
639 # string property
640 ar(TypeError, self.db.status.create, name=1)
641 # id, creation, creator and activity properties are reserved
642 ar(KeyError, self.db.status.create, id=1)
643 ar(KeyError, self.db.status.create, creation=1)
644 ar(KeyError, self.db.status.create, creator=1)
645 ar(KeyError, self.db.status.create, activity=1)
646 ar(KeyError, self.db.status.create, actor=1)
647 # invalid property name
648 ar(KeyError, self.db.status.create, foo='foo')
649 # key name clash
650 ar(ValueError, self.db.status.create, name='unread')
651 # invalid link index
652 ar(IndexError, self.db.issue.create, title='foo', status='bar')
653 # invalid link value
654 ar(ValueError, self.db.issue.create, title='foo', status=1)
655 # invalid multilink type
656 ar(TypeError, self.db.issue.create, title='foo', status='1',
657 nosy='hello')
658 # invalid multilink index type
659 ar(ValueError, self.db.issue.create, title='foo', status='1',
660 nosy=[1])
661 # invalid multilink index
662 ar(IndexError, self.db.issue.create, title='foo', status='1',
663 nosy=['10'])
665 #
666 # key property
667 #
668 # key must be a String
669 ar(TypeError, self.db.file.setkey, 'fooz')
670 # key must exist
671 ar(KeyError, self.db.file.setkey, 'fubar')
673 #
674 # class get
675 #
676 # invalid node id
677 ar(IndexError, self.db.issue.get, '99', 'title')
678 # invalid property name
679 ar(KeyError, self.db.status.get, '2', 'foo')
681 #
682 # class set
683 #
684 # invalid node id
685 ar(IndexError, self.db.issue.set, '99', title='foo')
686 # invalid property name
687 ar(KeyError, self.db.status.set, '1', foo='foo')
688 # string property
689 ar(TypeError, self.db.status.set, '1', name=1)
690 # key name clash
691 ar(ValueError, self.db.status.set, '2', name='unread')
692 # set up a valid issue for me to work on
693 id = self.db.issue.create(title="spam", status='1')
694 # invalid link index
695 ar(IndexError, self.db.issue.set, id, title='foo', status='bar')
696 # invalid link value
697 ar(ValueError, self.db.issue.set, id, title='foo', status=1)
698 # invalid multilink type
699 ar(TypeError, self.db.issue.set, id, title='foo', status='1',
700 nosy='hello')
701 # invalid multilink index type
702 ar(ValueError, self.db.issue.set, id, title='foo', status='1',
703 nosy=[1])
704 # invalid multilink index
705 ar(IndexError, self.db.issue.set, id, title='foo', status='1',
706 nosy=['10'])
707 # NOTE: the following increment the username to avoid problems
708 # within metakit's backend (it creates the node, and then sets the
709 # info, so the create (and by a fluke the username set) go through
710 # before the age/assignable/etc. set, which raises the exception)
711 # invalid number value
712 ar(TypeError, self.db.user.create, username='foo', age='a')
713 # invalid boolean value
714 ar(TypeError, self.db.user.create, username='foo2', assignable='true')
715 nid = self.db.user.create(username='foo3')
716 # invalid number value
717 ar(TypeError, self.db.user.set, nid, age='a')
718 # invalid boolean value
719 ar(TypeError, self.db.user.set, nid, assignable='true')
721 def testAuditors(self):
722 class test:
723 called = False
724 def call(self, *args): self.called = True
725 create = test()
727 self.db.user.audit('create', create.call)
728 self.db.user.create(username="mary")
729 self.assertEqual(create.called, True)
731 set = test()
732 self.db.user.audit('set', set.call)
733 self.db.user.set('1', username="joe")
734 self.assertEqual(set.called, True)
736 retire = test()
737 self.db.user.audit('retire', retire.call)
738 self.db.user.retire('1')
739 self.assertEqual(retire.called, True)
741 def testAuditorTwo(self):
742 class test:
743 n = 0
744 def a(self, *args): self.call_a = self.n; self.n += 1
745 def b(self, *args): self.call_b = self.n; self.n += 1
746 def c(self, *args): self.call_c = self.n; self.n += 1
747 test = test()
748 self.db.user.audit('create', test.b, 1)
749 self.db.user.audit('create', test.a, 1)
750 self.db.user.audit('create', test.c, 2)
751 self.db.user.create(username="mary")
752 self.assertEqual(test.call_a, 0)
753 self.assertEqual(test.call_b, 1)
754 self.assertEqual(test.call_c, 2)
756 def testJournals(self):
757 muid = self.db.user.create(username="mary")
758 self.db.user.create(username="pete")
759 self.db.issue.create(title="spam", status='1')
760 self.db.commit()
762 # journal entry for issue create
763 journal = self.db.getjournal('issue', '1')
764 self.assertEqual(1, len(journal))
765 (nodeid, date_stamp, journaltag, action, params) = journal[0]
766 self.assertEqual(nodeid, '1')
767 self.assertEqual(journaltag, self.db.user.lookup('admin'))
768 self.assertEqual(action, 'create')
769 keys = params.keys()
770 keys.sort()
771 self.assertEqual(keys, [])
773 # journal entry for link
774 journal = self.db.getjournal('user', '1')
775 self.assertEqual(1, len(journal))
776 self.db.issue.set('1', assignedto='1')
777 self.db.commit()
778 journal = self.db.getjournal('user', '1')
779 self.assertEqual(2, len(journal))
780 (nodeid, date_stamp, journaltag, action, params) = journal[1]
781 self.assertEqual('1', nodeid)
782 self.assertEqual('1', journaltag)
783 self.assertEqual('link', action)
784 self.assertEqual(('issue', '1', 'assignedto'), params)
786 # wait a bit to keep proper order of journal entries
787 time.sleep(0.01)
788 # journal entry for unlink
789 self.db.setCurrentUser('mary')
790 self.db.issue.set('1', assignedto='2')
791 self.db.commit()
792 journal = self.db.getjournal('user', '1')
793 self.assertEqual(3, len(journal))
794 (nodeid, date_stamp, journaltag, action, params) = journal[2]
795 self.assertEqual('1', nodeid)
796 self.assertEqual(muid, journaltag)
797 self.assertEqual('unlink', action)
798 self.assertEqual(('issue', '1', 'assignedto'), params)
800 # test disabling journalling
801 # ... get the last entry
802 jlen = len(self.db.getjournal('user', '1'))
803 self.db.issue.disableJournalling()
804 self.db.issue.set('1', title='hello world')
805 self.db.commit()
806 # see if the change was journalled when it shouldn't have been
807 self.assertEqual(jlen, len(self.db.getjournal('user', '1')))
808 jlen = len(self.db.getjournal('issue', '1'))
809 self.db.issue.enableJournalling()
810 self.db.issue.set('1', title='hello world 2')
811 self.db.commit()
812 # see if the change was journalled
813 self.assertNotEqual(jlen, len(self.db.getjournal('issue', '1')))
815 def testJournalPreCommit(self):
816 id = self.db.user.create(username="mary")
817 self.assertEqual(len(self.db.getjournal('user', id)), 1)
818 self.db.commit()
820 def testPack(self):
821 id = self.db.issue.create(title="spam", status='1')
822 self.db.commit()
823 time.sleep(1)
824 self.db.issue.set(id, status='2')
825 self.db.commit()
827 # sleep for at least a second, then get a date to pack at
828 time.sleep(1)
829 pack_before = date.Date('.')
831 # wait another second and add one more entry
832 time.sleep(1)
833 self.db.issue.set(id, status='3')
834 self.db.commit()
835 jlen = len(self.db.getjournal('issue', id))
837 # pack
838 self.db.pack(pack_before)
840 # we should have the create and last set entries now
841 self.assertEqual(jlen-1, len(self.db.getjournal('issue', id)))
843 def testIndexerSearching(self):
844 f1 = self.db.file.create(content='hello', type="text/plain")
845 # content='world' has the wrong content-type and won't be indexed
846 f2 = self.db.file.create(content='world', type="text/frozz",
847 comment='blah blah')
848 i1 = self.db.issue.create(files=[f1, f2], title="flebble plop")
849 i2 = self.db.issue.create(title="flebble the frooz")
850 self.db.commit()
851 self.assertEquals(self.db.indexer.search([], self.db.issue), {})
852 self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
853 {i1: {'files': [f1]}})
854 # content='world' has the wrong content-type and shouldn't be indexed
855 self.assertEquals(self.db.indexer.search(['world'], self.db.issue), {})
856 self.assertEquals(self.db.indexer.search(['frooz'], self.db.issue),
857 {i2: {}})
858 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
859 {i1: {}, i2: {}})
861 # test AND'ing of search terms
862 self.assertEquals(self.db.indexer.search(['frooz', 'flebble'],
863 self.db.issue), {i2: {}})
865 # unindexed stopword
866 self.assertEquals(self.db.indexer.search(['the'], self.db.issue), {})
868 def testIndexerSearchingLink(self):
869 m1 = self.db.msg.create(content="one two")
870 i1 = self.db.issue.create(messages=[m1])
871 m2 = self.db.msg.create(content="two three")
872 i2 = self.db.issue.create(feedback=m2)
873 self.db.commit()
874 self.assertEquals(self.db.indexer.search(['two'], self.db.issue),
875 {i1: {'messages': [m1]}, i2: {'feedback': [m2]}})
877 def testIndexerSearchMulti(self):
878 m1 = self.db.msg.create(content="one two")
879 m2 = self.db.msg.create(content="two three")
880 i1 = self.db.issue.create(messages=[m1])
881 i2 = self.db.issue.create(spam=[m2])
882 self.db.commit()
883 self.assertEquals(self.db.indexer.search([], self.db.issue), {})
884 self.assertEquals(self.db.indexer.search(['one'], self.db.issue),
885 {i1: {'messages': [m1]}})
886 self.assertEquals(self.db.indexer.search(['two'], self.db.issue),
887 {i1: {'messages': [m1]}, i2: {'spam': [m2]}})
888 self.assertEquals(self.db.indexer.search(['three'], self.db.issue),
889 {i2: {'spam': [m2]}})
891 def testReindexingChange(self):
892 search = self.db.indexer.search
893 issue = self.db.issue
894 i1 = issue.create(title="flebble plop")
895 i2 = issue.create(title="flebble frooz")
896 self.db.commit()
897 self.assertEquals(search(['plop'], issue), {i1: {}})
898 self.assertEquals(search(['flebble'], issue), {i1: {}, i2: {}})
900 # change i1's title
901 issue.set(i1, title="plop")
902 self.db.commit()
903 self.assertEquals(search(['plop'], issue), {i1: {}})
904 self.assertEquals(search(['flebble'], issue), {i2: {}})
906 def testReindexingClear(self):
907 search = self.db.indexer.search
908 issue = self.db.issue
909 i1 = issue.create(title="flebble plop")
910 i2 = issue.create(title="flebble frooz")
911 self.db.commit()
912 self.assertEquals(search(['plop'], issue), {i1: {}})
913 self.assertEquals(search(['flebble'], issue), {i1: {}, i2: {}})
915 # unset i1's title
916 issue.set(i1, title="")
917 self.db.commit()
918 self.assertEquals(search(['plop'], issue), {})
919 self.assertEquals(search(['flebble'], issue), {i2: {}})
921 def testFileClassReindexing(self):
922 f1 = self.db.file.create(content='hello')
923 f2 = self.db.file.create(content='hello, world')
924 i1 = self.db.issue.create(files=[f1, f2])
925 self.db.commit()
926 d = self.db.indexer.search(['hello'], self.db.issue)
927 self.assert_(d.has_key(i1))
928 d[i1]['files'].sort()
929 self.assertEquals(d, {i1: {'files': [f1, f2]}})
930 self.assertEquals(self.db.indexer.search(['world'], self.db.issue),
931 {i1: {'files': [f2]}})
932 self.db.file.set(f1, content="world")
933 self.db.commit()
934 d = self.db.indexer.search(['world'], self.db.issue)
935 d[i1]['files'].sort()
936 self.assertEquals(d, {i1: {'files': [f1, f2]}})
937 self.assertEquals(self.db.indexer.search(['hello'], self.db.issue),
938 {i1: {'files': [f2]}})
940 def testFileClassIndexingNoNoNo(self):
941 f1 = self.db.file.create(content='hello')
942 self.db.commit()
943 self.assertEquals(self.db.indexer.search(['hello'], self.db.file),
944 {'1': {}})
946 f1 = self.db.file_nidx.create(content='hello')
947 self.db.commit()
948 self.assertEquals(self.db.indexer.search(['hello'], self.db.file_nidx),
949 {})
951 def testForcedReindexing(self):
952 self.db.issue.create(title="flebble frooz")
953 self.db.commit()
954 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
955 {'1': {}})
956 self.db.indexer.quiet = 1
957 self.db.indexer.force_reindex()
958 self.db.post_init()
959 self.db.indexer.quiet = 9
960 self.assertEquals(self.db.indexer.search(['flebble'], self.db.issue),
961 {'1': {}})
963 def testIndexingPropertiesOnImport(self):
964 # import an issue
965 title = 'Bzzt'
966 nodeid = self.db.issue.import_list(['title', 'messages', 'files',
967 'spam', 'nosy', 'superseder'], [repr(title), '[]', '[]',
968 '[]', '[]', '[]'])
969 self.db.commit()
971 # Content of title attribute is indexed
972 self.assertEquals(self.db.indexer.search([title], self.db.issue),
973 {str(nodeid):{}})
976 #
977 # searching tests follow
978 #
979 def testFindIncorrectProperty(self):
980 self.assertRaises(TypeError, self.db.issue.find, title='fubar')
982 def _find_test_setup(self):
983 self.db.file.create(content='')
984 self.db.file.create(content='')
985 self.db.user.create(username='')
986 one = self.db.issue.create(status="1", nosy=['1'])
987 two = self.db.issue.create(status="2", nosy=['2'], files=['1'],
988 assignedto='2')
989 three = self.db.issue.create(status="1", nosy=['1','2'])
990 four = self.db.issue.create(status="3", assignedto='1',
991 files=['1','2'])
992 return one, two, three, four
994 def testFindLink(self):
995 one, two, three, four = self._find_test_setup()
996 got = self.db.issue.find(status='1')
997 got.sort()
998 self.assertEqual(got, [one, three])
999 got = self.db.issue.find(status={'1':1})
1000 got.sort()
1001 self.assertEqual(got, [one, three])
1003 def testFindLinkFail(self):
1004 self._find_test_setup()
1005 self.assertEqual(self.db.issue.find(status='4'), [])
1006 self.assertEqual(self.db.issue.find(status={'4':1}), [])
1008 def testFindLinkUnset(self):
1009 one, two, three, four = self._find_test_setup()
1010 got = self.db.issue.find(assignedto=None)
1011 got.sort()
1012 self.assertEqual(got, [one, three])
1013 got = self.db.issue.find(assignedto={None:1})
1014 got.sort()
1015 self.assertEqual(got, [one, three])
1017 def testFindMultipleLink(self):
1018 one, two, three, four = self._find_test_setup()
1019 l = self.db.issue.find(status={'1':1, '3':1})
1020 l.sort()
1021 self.assertEqual(l, [one, three, four])
1022 l = self.db.issue.find(assignedto={None:1, '1':1})
1023 l.sort()
1024 self.assertEqual(l, [one, three, four])
1026 def testFindMultilink(self):
1027 one, two, three, four = self._find_test_setup()
1028 got = self.db.issue.find(nosy='2')
1029 got.sort()
1030 self.assertEqual(got, [two, three])
1031 got = self.db.issue.find(nosy={'2':1})
1032 got.sort()
1033 self.assertEqual(got, [two, three])
1034 got = self.db.issue.find(nosy={'2':1}, files={})
1035 got.sort()
1036 self.assertEqual(got, [two, three])
1038 def testFindMultiMultilink(self):
1039 one, two, three, four = self._find_test_setup()
1040 got = self.db.issue.find(nosy='2', files='1')
1041 got.sort()
1042 self.assertEqual(got, [two, three, four])
1043 got = self.db.issue.find(nosy={'2':1}, files={'1':1})
1044 got.sort()
1045 self.assertEqual(got, [two, three, four])
1047 def testFindMultilinkFail(self):
1048 self._find_test_setup()
1049 self.assertEqual(self.db.issue.find(nosy='3'), [])
1050 self.assertEqual(self.db.issue.find(nosy={'3':1}), [])
1052 def testFindMultilinkUnset(self):
1053 self._find_test_setup()
1054 self.assertEqual(self.db.issue.find(nosy={}), [])
1056 def testFindLinkAndMultilink(self):
1057 one, two, three, four = self._find_test_setup()
1058 got = self.db.issue.find(status='1', nosy='2')
1059 got.sort()
1060 self.assertEqual(got, [one, two, three])
1061 got = self.db.issue.find(status={'1':1}, nosy={'2':1})
1062 got.sort()
1063 self.assertEqual(got, [one, two, three])
1065 def testFindRetired(self):
1066 one, two, three, four = self._find_test_setup()
1067 self.assertEqual(len(self.db.issue.find(status='1')), 2)
1068 self.db.issue.retire(one)
1069 self.assertEqual(len(self.db.issue.find(status='1')), 1)
1071 def testStringFind(self):
1072 self.assertRaises(TypeError, self.db.issue.stringFind, status='1')
1074 ids = []
1075 ids.append(self.db.issue.create(title="spam"))
1076 self.db.issue.create(title="not spam")
1077 ids.append(self.db.issue.create(title="spam"))
1078 ids.sort()
1079 got = self.db.issue.stringFind(title='spam')
1080 got.sort()
1081 self.assertEqual(got, ids)
1082 self.assertEqual(self.db.issue.stringFind(title='fubar'), [])
1084 # test retiring a node
1085 self.db.issue.retire(ids[0])
1086 self.assertEqual(len(self.db.issue.stringFind(title='spam')), 1)
1088 def filteringSetup(self):
1089 for user in (
1090 {'username': 'bleep', 'age': 1, 'assignable': True},
1091 {'username': 'blop', 'age': 1.5, 'assignable': True},
1092 {'username': 'blorp', 'age': 2, 'assignable': False}):
1093 self.db.user.create(**user)
1094 iss = self.db.issue
1095 file_content = ''.join([chr(i) for i in range(255)])
1096 f = self.db.file.create(content=file_content)
1097 for issue in (
1098 {'title': 'issue one', 'status': '2', 'assignedto': '1',
1099 'foo': date.Interval('1:10'), 'priority': '3',
1100 'deadline': date.Date('2003-02-16.22:50')},
1101 {'title': 'issue two', 'status': '1', 'assignedto': '2',
1102 'foo': date.Interval('1d'), 'priority': '3',
1103 'deadline': date.Date('2003-01-01.00:00')},
1104 {'title': 'issue three', 'status': '1', 'priority': '2',
1105 'nosy': ['1','2'], 'deadline': date.Date('2003-02-18')},
1106 {'title': 'non four', 'status': '3',
1107 'foo': date.Interval('0:10'), 'priority': '2',
1108 'nosy': ['1','2','3'], 'deadline': date.Date('2004-03-08'),
1109 'files': [f]}):
1110 self.db.issue.create(**issue)
1111 self.db.commit()
1112 return self.assertEqual, self.db.issue.filter
1114 def testFilteringID(self):
1115 ae, filt = self.filteringSetup()
1116 ae(filt(None, {'id': '1'}, ('+','id'), (None,None)), ['1'])
1117 ae(filt(None, {'id': '2'}, ('+','id'), (None,None)), ['2'])
1118 ae(filt(None, {'id': '100'}, ('+','id'), (None,None)), [])
1120 def testFilteringBoolean(self):
1121 self.filteringSetup()
1122 ae, filt = self.assertEqual, self.db.user.filter
1123 a = 'assignable'
1124 ae(filt(None, {a: '1'}, ('+','id'), (None,None)), ['3','4'])
1125 ae(filt(None, {a: '0'}, ('+','id'), (None,None)), ['5'])
1126 ae(filt(None, {a: ['1']}, ('+','id'), (None,None)), ['3','4'])
1127 ae(filt(None, {a: ['0']}, ('+','id'), (None,None)), ['5'])
1128 ae(filt(None, {a: ['0','1']}, ('+','id'), (None,None)), ['3','4','5'])
1129 ae(filt(None, {a: 'True'}, ('+','id'), (None,None)), ['3','4'])
1130 ae(filt(None, {a: 'False'}, ('+','id'), (None,None)), ['5'])
1131 ae(filt(None, {a: ['True']}, ('+','id'), (None,None)), ['3','4'])
1132 ae(filt(None, {a: ['False']}, ('+','id'), (None,None)), ['5'])
1133 ae(filt(None, {a: ['False','True']}, ('+','id'), (None,None)),
1134 ['3','4','5'])
1135 ae(filt(None, {a: True}, ('+','id'), (None,None)), ['3','4'])
1136 ae(filt(None, {a: False}, ('+','id'), (None,None)), ['5'])
1137 ae(filt(None, {a: 1}, ('+','id'), (None,None)), ['3','4'])
1138 ae(filt(None, {a: 0}, ('+','id'), (None,None)), ['5'])
1139 ae(filt(None, {a: [1]}, ('+','id'), (None,None)), ['3','4'])
1140 ae(filt(None, {a: [0]}, ('+','id'), (None,None)), ['5'])
1141 ae(filt(None, {a: [0,1]}, ('+','id'), (None,None)), ['3','4','5'])
1142 ae(filt(None, {a: [True]}, ('+','id'), (None,None)), ['3','4'])
1143 ae(filt(None, {a: [False]}, ('+','id'), (None,None)), ['5'])
1144 ae(filt(None, {a: [False,True]}, ('+','id'), (None,None)),
1145 ['3','4','5'])
1147 def testFilteringNumber(self):
1148 self.filteringSetup()
1149 ae, filt = self.assertEqual, self.db.user.filter
1150 ae(filt(None, {'age': '1'}, ('+','id'), (None,None)), ['3'])
1151 ae(filt(None, {'age': '1.5'}, ('+','id'), (None,None)), ['4'])
1152 ae(filt(None, {'age': '2'}, ('+','id'), (None,None)), ['5'])
1153 ae(filt(None, {'age': ['1','2']}, ('+','id'), (None,None)), ['3','5'])
1154 ae(filt(None, {'age': 2}, ('+','id'), (None,None)), ['5'])
1155 ae(filt(None, {'age': [1,2]}, ('+','id'), (None,None)), ['3','5'])
1157 def testFilteringString(self):
1158 ae, filt = self.filteringSetup()
1159 ae(filt(None, {'title': ['one']}, ('+','id'), (None,None)), ['1'])
1160 ae(filt(None, {'title': ['issue one']}, ('+','id'), (None,None)),
1161 ['1'])
1162 ae(filt(None, {'title': ['issue', 'one']}, ('+','id'), (None,None)),
1163 ['1'])
1164 ae(filt(None, {'title': ['issue']}, ('+','id'), (None,None)),
1165 ['1','2','3'])
1166 ae(filt(None, {'title': ['one', 'two']}, ('+','id'), (None,None)),
1167 [])
1169 def testFilteringLink(self):
1170 ae, filt = self.filteringSetup()
1171 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['2','3'])
1172 ae(filt(None, {'assignedto': '-1'}, ('+','id'), (None,None)), ['3','4'])
1173 ae(filt(None, {'assignedto': None}, ('+','id'), (None,None)), ['3','4'])
1174 ae(filt(None, {'assignedto': [None]}, ('+','id'), (None,None)),
1175 ['3','4'])
1176 ae(filt(None, {'assignedto': ['-1', None]}, ('+','id'), (None,None)),
1177 ['3','4'])
1178 ae(filt(None, {'assignedto': ['1', None]}, ('+','id'), (None,None)),
1179 ['1', '3','4'])
1181 def testFilteringMultilinkAndGroup(self):
1182 """testFilteringMultilinkAndGroup:
1183 See roundup Bug 1541128: apparently grouping by something and
1184 searching a Multilink failed with MySQL 5.0
1185 """
1186 ae, filt = self.filteringSetup()
1187 ae(filt(None, {'files': '1'}, ('-','activity'), ('+','status')), ['4'])
1189 def testFilteringRetired(self):
1190 ae, filt = self.filteringSetup()
1191 self.db.issue.retire('2')
1192 ae(filt(None, {'status': '1'}, ('+','id'), (None,None)), ['3'])
1194 def testFilteringMultilink(self):
1195 ae, filt = self.filteringSetup()
1196 ae(filt(None, {'nosy': '3'}, ('+','id'), (None,None)), ['4'])
1197 ae(filt(None, {'nosy': '-1'}, ('+','id'), (None,None)), ['1', '2'])
1198 ae(filt(None, {'nosy': ['1','2']}, ('+', 'status'),
1199 ('-', 'deadline')), ['4', '3'])
1201 def testFilteringMany(self):
1202 ae, filt = self.filteringSetup()
1203 ae(filt(None, {'nosy': '2', 'status': '1'}, ('+','id'), (None,None)),
1204 ['3'])
1206 def testFilteringRangeBasic(self):
1207 ae, filt = self.filteringSetup()
1208 ae(filt(None, {'deadline': 'from 2003-02-10 to 2003-02-23'}), ['1','3'])
1209 ae(filt(None, {'deadline': '2003-02-10; 2003-02-23'}), ['1','3'])
1210 ae(filt(None, {'deadline': '; 2003-02-16'}), ['2'])
1212 def testFilteringRangeTwoSyntaxes(self):
1213 ae, filt = self.filteringSetup()
1214 ae(filt(None, {'deadline': 'from 2003-02-16'}), ['1', '3', '4'])
1215 ae(filt(None, {'deadline': '2003-02-16;'}), ['1', '3', '4'])
1217 def testFilteringRangeYearMonthDay(self):
1218 ae, filt = self.filteringSetup()
1219 ae(filt(None, {'deadline': '2002'}), [])
1220 ae(filt(None, {'deadline': '2003'}), ['1', '2', '3'])
1221 ae(filt(None, {'deadline': '2004'}), ['4'])
1222 ae(filt(None, {'deadline': '2003-02-16'}), ['1'])
1223 ae(filt(None, {'deadline': '2003-02-17'}), [])
1225 def testFilteringRangeMonths(self):
1226 ae, filt = self.filteringSetup()
1227 for month in range(1, 13):
1228 for n in range(1, month+1):
1229 i = self.db.issue.create(title='%d.%d'%(month, n),
1230 deadline=date.Date('2001-%02d-%02d.00:00'%(month, n)))
1231 self.db.commit()
1233 for month in range(1, 13):
1234 r = filt(None, dict(deadline='2001-%02d'%month))
1235 assert len(r) == month, 'month %d != length %d'%(month, len(r))
1237 def testFilteringRangeInterval(self):
1238 ae, filt = self.filteringSetup()
1239 ae(filt(None, {'foo': 'from 0:50 to 2:00'}), ['1'])
1240 ae(filt(None, {'foo': 'from 0:50 to 1d 2:00'}), ['1', '2'])
1241 ae(filt(None, {'foo': 'from 5:50'}), ['2'])
1242 ae(filt(None, {'foo': 'to 0:05'}), [])
1244 def testFilteringRangeGeekInterval(self):
1245 ae, filt = self.filteringSetup()
1246 for issue in (
1247 { 'deadline': date.Date('. -2d')},
1248 { 'deadline': date.Date('. -1d')},
1249 { 'deadline': date.Date('. -8d')},
1250 ):
1251 self.db.issue.create(**issue)
1252 ae(filt(None, {'deadline': '-2d;'}), ['5', '6'])
1253 ae(filt(None, {'deadline': '-1d;'}), ['6'])
1254 ae(filt(None, {'deadline': '-1w;'}), ['5', '6'])
1256 def testFilteringIntervalSort(self):
1257 # 1: '1:10'
1258 # 2: '1d'
1259 # 3: None
1260 # 4: '0:10'
1261 ae, filt = self.filteringSetup()
1262 # ascending should sort None, 1:10, 1d
1263 ae(filt(None, {}, ('+','foo'), (None,None)), ['3', '4', '1', '2'])
1264 # descending should sort 1d, 1:10, None
1265 ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3'])
1267 def testFilteringStringSort(self):
1268 # 1: 'issue one'
1269 # 2: 'issue two'
1270 # 3: 'issue three'
1271 # 4: 'non four'
1272 ae, filt = self.filteringSetup()
1273 ae(filt(None, {}, ('+','title')), ['1', '3', '2', '4'])
1274 ae(filt(None, {}, ('-','title')), ['4', '2', '3', '1'])
1275 # Test string case: For now allow both, w/wo case matching.
1276 # 1: 'issue one'
1277 # 2: 'issue two'
1278 # 3: 'Issue three'
1279 # 4: 'non four'
1280 self.db.issue.set('3', title='Issue three')
1281 ae(filt(None, {}, ('+','title')), ['1', '3', '2', '4'])
1282 ae(filt(None, {}, ('-','title')), ['4', '2', '3', '1'])
1283 # Obscure bug in anydbm backend trying to convert to number
1284 # 1: '1st issue'
1285 # 2: '2'
1286 # 3: 'Issue three'
1287 # 4: 'non four'
1288 self.db.issue.set('1', title='1st issue')
1289 self.db.issue.set('2', title='2')
1290 ae(filt(None, {}, ('+','title')), ['1', '2', '3', '4'])
1291 ae(filt(None, {}, ('-','title')), ['4', '3', '2', '1'])
1293 def testFilteringMultilinkSort(self):
1294 # 1: [] Reverse: 1: []
1295 # 2: [] 2: []
1296 # 3: ['admin','fred'] 3: ['fred','admin']
1297 # 4: ['admin','bleep','fred'] 4: ['fred','bleep','admin']
1298 # Note the sort order for the multilink doen't change when
1299 # reversing the sort direction due to the re-sorting of the
1300 # multilink!
1301 ae, filt = self.filteringSetup()
1302 ae(filt(None, {}, ('+','nosy'), (None,None)), ['1', '2', '4', '3'])
1303 ae(filt(None, {}, ('-','nosy'), (None,None)), ['4', '3', '1', '2'])
1305 def testFilteringMultilinkSortGroup(self):
1306 # 1: status: 2 "in-progress" nosy: []
1307 # 2: status: 1 "unread" nosy: []
1308 # 3: status: 1 "unread" nosy: ['admin','fred']
1309 # 4: status: 3 "testing" nosy: ['admin','bleep','fred']
1310 ae, filt = self.filteringSetup()
1311 ae(filt(None, {}, ('+','nosy'), ('+','status')), ['1', '4', '2', '3'])
1312 ae(filt(None, {}, ('-','nosy'), ('+','status')), ['1', '4', '3', '2'])
1313 ae(filt(None, {}, ('+','nosy'), ('-','status')), ['2', '3', '4', '1'])
1314 ae(filt(None, {}, ('-','nosy'), ('-','status')), ['3', '2', '4', '1'])
1315 ae(filt(None, {}, ('+','status'), ('+','nosy')), ['1', '2', '4', '3'])
1316 ae(filt(None, {}, ('-','status'), ('+','nosy')), ['2', '1', '4', '3'])
1317 ae(filt(None, {}, ('+','status'), ('-','nosy')), ['4', '3', '1', '2'])
1318 ae(filt(None, {}, ('-','status'), ('-','nosy')), ['4', '3', '2', '1'])
1320 def testFilteringLinkSortGroup(self):
1321 # 1: status: 2 -> 'i', priority: 3 -> 1
1322 # 2: status: 1 -> 'u', priority: 3 -> 1
1323 # 3: status: 1 -> 'u', priority: 2 -> 3
1324 # 4: status: 3 -> 't', priority: 2 -> 3
1325 ae, filt = self.filteringSetup()
1326 ae(filt(None, {}, ('+','status'), ('+','priority')),
1327 ['1', '2', '4', '3'])
1328 ae(filt(None, {'priority':'2'}, ('+','status'), ('+','priority')),
1329 ['4', '3'])
1330 ae(filt(None, {'priority.order':'3'}, ('+','status'), ('+','priority')),
1331 ['4', '3'])
1332 ae(filt(None, {'priority':['2','3']}, ('+','priority'), ('+','status')),
1333 ['1', '4', '2', '3'])
1334 ae(filt(None, {}, ('+','priority'), ('+','status')),
1335 ['1', '4', '2', '3'])
1337 def testFilteringDateSort(self):
1338 # '1': '2003-02-16.22:50'
1339 # '2': '2003-01-01.00:00'
1340 # '3': '2003-02-18'
1341 # '4': '2004-03-08'
1342 ae, filt = self.filteringSetup()
1343 # ascending
1344 ae(filt(None, {}, ('+','deadline'), (None,None)), ['2', '1', '3', '4'])
1345 # descending
1346 ae(filt(None, {}, ('-','deadline'), (None,None)), ['4', '3', '1', '2'])
1348 def testFilteringDateSortPriorityGroup(self):
1349 # '1': '2003-02-16.22:50' 1 => 2
1350 # '2': '2003-01-01.00:00' 3 => 1
1351 # '3': '2003-02-18' 2 => 3
1352 # '4': '2004-03-08' 1 => 2
1353 ae, filt = self.filteringSetup()
1355 # ascending
1356 ae(filt(None, {}, ('+','deadline'), ('+','priority')),
1357 ['2', '1', '3', '4'])
1358 ae(filt(None, {}, ('-','deadline'), ('+','priority')),
1359 ['1', '2', '4', '3'])
1360 # descending
1361 ae(filt(None, {}, ('+','deadline'), ('-','priority')),
1362 ['3', '4', '2', '1'])
1363 ae(filt(None, {}, ('-','deadline'), ('-','priority')),
1364 ['4', '3', '1', '2'])
1366 def filteringSetupTransitiveSearch(self):
1367 u_m = {}
1368 k = 30
1369 for user in (
1370 {'username': 'ceo', 'age': 129},
1371 {'username': 'grouplead1', 'age': 29, 'supervisor': '3'},
1372 {'username': 'grouplead2', 'age': 29, 'supervisor': '3'},
1373 {'username': 'worker1', 'age': 25, 'supervisor' : '4'},
1374 {'username': 'worker2', 'age': 24, 'supervisor' : '4'},
1375 {'username': 'worker3', 'age': 23, 'supervisor' : '5'},
1376 {'username': 'worker4', 'age': 22, 'supervisor' : '5'},
1377 {'username': 'worker5', 'age': 21, 'supervisor' : '5'}):
1378 u = self.db.user.create(**user)
1379 u_m [u] = self.db.msg.create(author = u, content = ' '
1380 , date = date.Date ('2006-01-%s' % k))
1381 k -= 1
1382 iss = self.db.issue
1383 for issue in (
1384 {'title': 'ts1', 'status': '2', 'assignedto': '6',
1385 'priority': '3', 'messages' : [u_m ['6']], 'nosy' : ['4']},
1386 {'title': 'ts2', 'status': '1', 'assignedto': '6',
1387 'priority': '3', 'messages' : [u_m ['6']], 'nosy' : ['5']},
1388 {'title': 'ts4', 'status': '2', 'assignedto': '7',
1389 'priority': '3', 'messages' : [u_m ['7']]},
1390 {'title': 'ts5', 'status': '1', 'assignedto': '8',
1391 'priority': '3', 'messages' : [u_m ['8']]},
1392 {'title': 'ts6', 'status': '2', 'assignedto': '9',
1393 'priority': '3', 'messages' : [u_m ['9']]},
1394 {'title': 'ts7', 'status': '1', 'assignedto': '10',
1395 'priority': '3', 'messages' : [u_m ['10']]},
1396 {'title': 'ts8', 'status': '2', 'assignedto': '10',
1397 'priority': '3', 'messages' : [u_m ['10']]},
1398 {'title': 'ts9', 'status': '1', 'assignedto': '10',
1399 'priority': '3', 'messages' : [u_m ['10'], u_m ['9']]}):
1400 self.db.issue.create(**issue)
1401 return self.assertEqual, self.db.issue.filter
1403 def testFilteringTransitiveLinkUser(self):
1404 ae, filt = self.filteringSetupTransitiveSearch()
1405 ufilt = self.db.user.filter
1406 ae(ufilt(None, {'supervisor.username': 'ceo'}, ('+','username')),
1407 ['4', '5'])
1408 ae(ufilt(None, {'supervisor.supervisor.username': 'ceo'},
1409 ('+','username')), ['6', '7', '8', '9', '10'])
1410 ae(ufilt(None, {'supervisor.supervisor': '3'}, ('+','username')),
1411 ['6', '7', '8', '9', '10'])
1412 ae(ufilt(None, {'supervisor.supervisor.id': '3'}, ('+','username')),
1413 ['6', '7', '8', '9', '10'])
1414 ae(ufilt(None, {'supervisor.username': 'grouplead1'}, ('+','username')),
1415 ['6', '7'])
1416 ae(ufilt(None, {'supervisor.username': 'grouplead2'}, ('+','username')),
1417 ['8', '9', '10'])
1418 ae(ufilt(None, {'supervisor.username': 'grouplead2',
1419 'supervisor.supervisor.username': 'ceo'}, ('+','username')),
1420 ['8', '9', '10'])
1421 ae(ufilt(None, {'supervisor.supervisor': '3', 'supervisor': '4'},
1422 ('+','username')), ['6', '7'])
1424 def testFilteringTransitiveLinkSort(self):
1425 ae, filt = self.filteringSetupTransitiveSearch()
1426 ufilt = self.db.user.filter
1427 # Need to make ceo his own (and first two users') supervisor,
1428 # otherwise we will depend on sorting order of NULL values.
1429 # Leave that to a separate test.
1430 self.db.user.set('1', supervisor = '3')
1431 self.db.user.set('2', supervisor = '3')
1432 self.db.user.set('3', supervisor = '3')
1433 ae(ufilt(None, {'supervisor':'3'}, []), ['1', '2', '3', '4', '5'])
1434 ae(ufilt(None, {}, [('+','supervisor.supervisor.supervisor'),
1435 ('+','supervisor.supervisor'), ('+','supervisor'),
1436 ('+','username')]),
1437 ['1', '3', '2', '4', '5', '6', '7', '8', '9', '10'])
1438 ae(ufilt(None, {}, [('+','supervisor.supervisor.supervisor'),
1439 ('-','supervisor.supervisor'), ('-','supervisor'),
1440 ('+','username')]),
1441 ['8', '9', '10', '6', '7', '1', '3', '2', '4', '5'])
1442 ae(filt(None, {}, [('+','assignedto.supervisor.supervisor.supervisor'),
1443 ('+','assignedto.supervisor.supervisor'),
1444 ('+','assignedto.supervisor'), ('+','assignedto')]),
1445 ['1', '2', '3', '4', '5', '6', '7', '8'])
1446 ae(filt(None, {}, [('+','assignedto.supervisor.supervisor.supervisor'),
1447 ('+','assignedto.supervisor.supervisor'),
1448 ('-','assignedto.supervisor'), ('+','assignedto')]),
1449 ['4', '5', '6', '7', '8', '1', '2', '3'])
1450 ae(filt(None, {}, [('+','assignedto.supervisor.supervisor.supervisor'),
1451 ('+','assignedto.supervisor.supervisor'),
1452 ('+','assignedto.supervisor'), ('+','assignedto'),
1453 ('-','status')]),
1454 ['2', '1', '3', '4', '5', '6', '8', '7'])
1455 ae(filt(None, {}, [('+','assignedto.supervisor.supervisor.supervisor'),
1456 ('+','assignedto.supervisor.supervisor'),
1457 ('+','assignedto.supervisor'), ('+','assignedto'),
1458 ('+','status')]),
1459 ['1', '2', '3', '4', '5', '7', '6', '8'])
1460 ae(filt(None, {}, [('+','assignedto.supervisor.supervisor.supervisor'),
1461 ('+','assignedto.supervisor.supervisor'),
1462 ('-','assignedto.supervisor'), ('+','assignedto'), ('+','status')]),
1463 ['4', '5', '7', '6', '8', '1', '2', '3'])
1464 ae(filt(None, {'assignedto':['6','7','8','9','10']},
1465 [('+','assignedto.supervisor.supervisor.supervisor'),
1466 ('+','assignedto.supervisor.supervisor'),
1467 ('-','assignedto.supervisor'), ('+','assignedto'), ('+','status')]),
1468 ['4', '5', '7', '6', '8', '1', '2', '3'])
1469 ae(filt(None, {'assignedto':['6','7','8','9']},
1470 [('+','assignedto.supervisor.supervisor.supervisor'),
1471 ('+','assignedto.supervisor.supervisor'),
1472 ('-','assignedto.supervisor'), ('+','assignedto'), ('+','status')]),
1473 ['4', '5', '1', '2', '3'])
1475 def testFilteringTransitiveLinkSortNull(self):
1476 """Check sorting of NULL values"""
1477 ae, filt = self.filteringSetupTransitiveSearch()
1478 ufilt = self.db.user.filter
1479 ae(ufilt(None, {}, [('+','supervisor.supervisor.supervisor'),
1480 ('+','supervisor.supervisor'), ('+','supervisor'),
1481 ('+','username')]),
1482 ['1', '3', '2', '4', '5', '6', '7', '8', '9', '10'])
1483 ae(ufilt(None, {}, [('+','supervisor.supervisor.supervisor'),
1484 ('-','supervisor.supervisor'), ('-','supervisor'),
1485 ('+','username')]),
1486 ['8', '9', '10', '6', '7', '4', '5', '1', '3', '2'])
1487 ae(filt(None, {}, [('+','assignedto.supervisor.supervisor.supervisor'),
1488 ('+','assignedto.supervisor.supervisor'),
1489 ('+','assignedto.supervisor'), ('+','assignedto')]),
1490 ['1', '2', '3', '4', '5', '6', '7', '8'])
1491 ae(filt(None, {}, [('+','assignedto.supervisor.supervisor.supervisor'),
1492 ('+','assignedto.supervisor.supervisor'),
1493 ('-','assignedto.supervisor'), ('+','assignedto')]),
1494 ['4', '5', '6', '7', '8', '1', '2', '3'])
1496 def testFilteringTransitiveLinkIssue(self):
1497 ae, filt = self.filteringSetupTransitiveSearch()
1498 ae(filt(None, {'assignedto.supervisor.username': 'grouplead1'},
1499 ('+','id')), ['1', '2', '3'])
1500 ae(filt(None, {'assignedto.supervisor.username': 'grouplead2'},
1501 ('+','id')), ['4', '5', '6', '7', '8'])
1502 ae(filt(None, {'assignedto.supervisor.username': 'grouplead2',
1503 'status': '1'}, ('+','id')), ['4', '6', '8'])
1504 ae(filt(None, {'assignedto.supervisor.username': 'grouplead2',
1505 'status': '2'}, ('+','id')), ['5', '7'])
1506 ae(filt(None, {'assignedto.supervisor.username': ['grouplead2'],
1507 'status': '2'}, ('+','id')), ['5', '7'])
1508 ae(filt(None, {'assignedto.supervisor': ['4', '5'], 'status': '2'},
1509 ('+','id')), ['1', '3', '5', '7'])
1511 def testFilteringTransitiveMultilink(self):
1512 ae, filt = self.filteringSetupTransitiveSearch()
1513 ae(filt(None, {'messages.author.username': 'grouplead1'},
1514 ('+','id')), [])
1515 ae(filt(None, {'messages.author': '6'},
1516 ('+','id')), ['1', '2'])
1517 ae(filt(None, {'messages.author.id': '6'},
1518 ('+','id')), ['1', '2'])
1519 ae(filt(None, {'messages.author.username': 'worker1'},
1520 ('+','id')), ['1', '2'])
1521 ae(filt(None, {'messages.author': '10'},
1522 ('+','id')), ['6', '7', '8'])
1523 ae(filt(None, {'messages.author': '9'},
1524 ('+','id')), ['5', '8'])
1525 ae(filt(None, {'messages.author': ['9', '10']},
1526 ('+','id')), ['5', '6', '7', '8'])
1527 ae(filt(None, {'messages.author': ['8', '9']},
1528 ('+','id')), ['4', '5', '8'])
1529 ae(filt(None, {'messages.author': ['8', '9'], 'status' : '1'},
1530 ('+','id')), ['4', '8'])
1531 ae(filt(None, {'messages.author': ['8', '9'], 'status' : '2'},
1532 ('+','id')), ['5'])
1533 ae(filt(None, {'messages.author': ['8', '9', '10'],
1534 'messages.date': '2006-01-22.21:00;2006-01-23'}, ('+','id')),
1535 ['6', '7', '8'])
1536 ae(filt(None, {'nosy.supervisor.username': 'ceo'},
1537 ('+','id')), ['1', '2'])
1538 ae(filt(None, {'messages.author': ['6', '9']},
1539 ('+','id')), ['1', '2', '5', '8'])
1540 ae(filt(None, {'messages': ['5', '7']},
1541 ('+','id')), ['3', '5', '8'])
1542 ae(filt(None, {'messages.author': ['6', '9'], 'messages': ['5', '7']},
1543 ('+','id')), ['5', '8'])
1545 def testFilteringTransitiveMultilinkSort(self):
1546 ae, filt = self.filteringSetupTransitiveSearch()
1547 ae(filt(None, {}, [('+','messages.author')]),
1548 ['1', '2', '3', '4', '5', '8', '6', '7'])
1549 ae(filt(None, {}, [('-','messages.author')]),
1550 ['8', '6', '7', '5', '4', '3', '1', '2'])
1551 ae(filt(None, {}, [('+','messages.date')]),
1552 ['6', '7', '8', '5', '4', '3', '1', '2'])
1553 ae(filt(None, {}, [('-','messages.date')]),
1554 ['1', '2', '3', '4', '8', '5', '6', '7'])
1555 ae(filt(None, {}, [('+','messages.author'),('+','messages.date')]),
1556 ['1', '2', '3', '4', '5', '8', '6', '7'])
1557 ae(filt(None, {}, [('-','messages.author'),('+','messages.date')]),
1558 ['8', '6', '7', '5', '4', '3', '1', '2'])
1559 ae(filt(None, {}, [('+','messages.author'),('-','messages.date')]),
1560 ['1', '2', '3', '4', '5', '8', '6', '7'])
1561 ae(filt(None, {}, [('-','messages.author'),('-','messages.date')]),
1562 ['8', '6', '7', '5', '4', '3', '1', '2'])
1563 ae(filt(None, {}, [('+','messages.author'),('+','assignedto')]),
1564 ['1', '2', '3', '4', '5', '8', '6', '7'])
1565 ae(filt(None, {}, [('+','messages.author'),
1566 ('-','assignedto.supervisor'),('-','assignedto')]),
1567 ['1', '2', '3', '4', '5', '8', '6', '7'])
1568 ae(filt(None, {},
1569 [('+','messages.author.supervisor.supervisor.supervisor'),
1570 ('+','messages.author.supervisor.supervisor'),
1571 ('+','messages.author.supervisor'), ('+','messages.author')]),
1572 ['1', '2', '3', '4', '5', '6', '7', '8'])
1573 self.db.user.setorderprop('age')
1574 self.db.msg.setorderprop('date')
1575 ae(filt(None, {}, [('+','messages'), ('+','messages.author')]),
1576 ['6', '7', '8', '5', '4', '3', '1', '2'])
1577 ae(filt(None, {}, [('+','messages.author'), ('+','messages')]),
1578 ['6', '7', '8', '5', '4', '3', '1', '2'])
1579 self.db.msg.setorderprop('author')
1580 # Orderprop is a Link/Multilink:
1581 # messages are sorted by orderprop().labelprop(), i.e. by
1582 # author.username, *not* by author.orderprop() (author.age)!
1583 ae(filt(None, {}, [('+','messages')]),
1584 ['1', '2', '3', '4', '5', '8', '6', '7'])
1585 ae(filt(None, {}, [('+','messages.author'), ('+','messages')]),
1586 ['6', '7', '8', '5', '4', '3', '1', '2'])
1587 # The following will sort by
1588 # author.supervisor.username and then by
1589 # author.username
1590 # I've resited the tempation to implement recursive orderprop
1591 # here: There could even be loops if several classes specify a
1592 # Link or Multilink as the orderprop...
1593 # msg: 4: worker1 (id 5) : grouplead1 (id 4) ceo (id 3)
1594 # msg: 5: worker2 (id 7) : grouplead1 (id 4) ceo (id 3)
1595 # msg: 6: worker3 (id 8) : grouplead2 (id 5) ceo (id 3)
1596 # msg: 7: worker4 (id 9) : grouplead2 (id 5) ceo (id 3)
1597 # msg: 8: worker5 (id 10) : grouplead2 (id 5) ceo (id 3)
1598 # issue 1: messages 4 sortkey:[[grouplead1], [worker1], 1]
1599 # issue 2: messages 4 sortkey:[[grouplead1], [worker1], 2]
1600 # issue 3: messages 5 sortkey:[[grouplead1], [worker2], 3]
1601 # issue 4: messages 6 sortkey:[[grouplead2], [worker3], 4]
1602 # issue 5: messages 7 sortkey:[[grouplead2], [worker4], 5]
1603 # issue 6: messages 8 sortkey:[[grouplead2], [worker5], 6]
1604 # issue 7: messages 8 sortkey:[[grouplead2], [worker5], 7]
1605 # issue 8: messages 7,8 sortkey:[[grouplead2, grouplead2], ...]
1606 self.db.user.setorderprop('supervisor')
1607 ae(filt(None, {}, [('+','messages.author'), ('-','messages')]),
1608 ['3', '1', '2', '6', '7', '5', '4', '8'])
1610 def testFilteringSortId(self):
1611 ae, filt = self.filteringSetupTransitiveSearch()
1612 ae(self.db.user.filter(None, {}, ('+','id')),
1613 ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'])
1615 # XXX add sorting tests for other types
1617 # nuke and re-create db for restore
1618 def nukeAndCreate(self):
1619 # shut down this db and nuke it
1620 self.db.close()
1621 self.nuke_database()
1623 # open a new, empty database
1624 os.makedirs(config.DATABASE + '/files')
1625 self.db = self.module.Database(config, 'admin')
1626 setupSchema(self.db, 0, self.module)
1628 def testImportExport(self):
1629 # use the filtering setup to create a bunch of items
1630 ae, filt = self.filteringSetup()
1631 # Get some stuff into the journal for testing import/export of
1632 # journal data:
1633 self.db.user.set('4', password = password.Password('xyzzy'))
1634 self.db.user.set('4', age = 3)
1635 self.db.user.set('4', assignable = True)
1636 self.db.issue.set('1', title = 'i1', status = '3')
1637 self.db.issue.set('1', deadline = date.Date('2007'))
1638 self.db.issue.set('1', foo = date.Interval('1:20'))
1639 p = self.db.priority.create(name = 'some_prio_without_order')
1640 self.db.commit()
1641 self.db.user.set('4', password = password.Password('123xyzzy'))
1642 self.db.user.set('4', assignable = False)
1643 self.db.priority.set(p, order = '4711')
1644 self.db.commit()
1646 self.db.user.retire('3')
1647 self.db.issue.retire('2')
1649 # grab snapshot of the current database
1650 orig = {}
1651 origj = {}
1652 for cn,klass in self.db.classes.items():
1653 cl = orig[cn] = {}
1654 jn = origj[cn] = {}
1655 for id in klass.list():
1656 it = cl[id] = {}
1657 jn[id] = self.db.getjournal(cn, id)
1658 for name in klass.getprops().keys():
1659 it[name] = klass.get(id, name)
1661 os.mkdir('_test_export')
1662 try:
1663 # grab the export
1664 export = {}
1665 journals = {}
1666 for cn,klass in self.db.classes.items():
1667 names = klass.export_propnames()
1668 cl = export[cn] = [names+['is retired']]
1669 for id in klass.getnodeids():
1670 cl.append(klass.export_list(names, id))
1671 if hasattr(klass, 'export_files'):
1672 klass.export_files('_test_export', id)
1673 journals[cn] = klass.export_journals()
1675 self.nukeAndCreate()
1677 # import
1678 for cn, items in export.items():
1679 klass = self.db.classes[cn]
1680 names = items[0]
1681 maxid = 1
1682 for itemprops in items[1:]:
1683 id = int(klass.import_list(names, itemprops))
1684 if hasattr(klass, 'import_files'):
1685 klass.import_files('_test_export', str(id))
1686 maxid = max(maxid, id)
1687 self.db.setid(cn, str(maxid+1))
1688 klass.import_journals(journals[cn])
1689 # This is needed, otherwise journals won't be there for anydbm
1690 self.db.commit()
1691 finally:
1692 shutil.rmtree('_test_export')
1694 # compare with snapshot of the database
1695 for cn, items in orig.iteritems():
1696 klass = self.db.classes[cn]
1697 propdefs = klass.getprops(1)
1698 # ensure retired items are retired :)
1699 l = items.keys(); l.sort()
1700 m = klass.list(); m.sort()
1701 ae(l, m, '%s id list wrong %r vs. %r'%(cn, l, m))
1702 for id, props in items.items():
1703 for name, value in props.items():
1704 l = klass.get(id, name)
1705 if isinstance(value, type([])):
1706 value.sort()
1707 l.sort()
1708 try:
1709 ae(l, value)
1710 except AssertionError:
1711 if not isinstance(propdefs[name], Date):
1712 raise
1713 # don't get hung up on rounding errors
1714 assert not l.__cmp__(value, int_seconds=1)
1715 for jc, items in origj.iteritems():
1716 for id, oj in items.iteritems():
1717 rj = self.db.getjournal(jc, id)
1718 # Both mysql and postgresql have some minor issues with
1719 # rounded seconds on export/import, so we compare only
1720 # the integer part.
1721 for j in oj:
1722 j[1].second = float(int(j[1].second))
1723 for j in rj:
1724 j[1].second = float(int(j[1].second))
1725 oj.sort()
1726 rj.sort()
1727 ae(oj, rj)
1729 # make sure the retired items are actually imported
1730 ae(self.db.user.get('4', 'username'), 'blop')
1731 ae(self.db.issue.get('2', 'title'), 'issue two')
1733 # make sure id counters are set correctly
1734 maxid = max([int(id) for id in self.db.user.list()])
1735 newid = self.db.user.create(username='testing')
1736 assert newid > maxid
1738 # test import/export via admin interface
1739 def testAdminImportExport(self):
1740 import roundup.admin
1741 import csv
1742 # use the filtering setup to create a bunch of items
1743 ae, filt = self.filteringSetup()
1744 # create large field
1745 self.db.priority.create(name = 'X' * 500)
1746 self.db.config.CSV_FIELD_SIZE = 400
1747 self.db.commit()
1748 output = []
1749 # ugly hack to get stderr output and disable stdout output
1750 # during regression test. Depends on roundup.admin not using
1751 # anything but stdout/stderr from sys (which is currently the
1752 # case)
1753 def stderrwrite(s):
1754 output.append(s)
1755 roundup.admin.sys = MockNull ()
1756 try:
1757 roundup.admin.sys.stderr.write = stderrwrite
1758 tool = roundup.admin.AdminTool()
1759 home = '.'
1760 tool.tracker_home = home
1761 tool.db = self.db
1762 tool.verbose = False
1763 tool.do_export (['_test_export'])
1764 self.assertEqual(len(output), 2)
1765 self.assertEqual(output [1], '\n')
1766 self.failUnless(output [0].startswith
1767 ('Warning: config csv_field_size should be at least'))
1768 self.failUnless(int(output[0].split()[-1]) > 500)
1770 if hasattr(roundup.admin.csv, 'field_size_limit'):
1771 self.nukeAndCreate()
1772 self.db.config.CSV_FIELD_SIZE = 400
1773 tool = roundup.admin.AdminTool()
1774 tool.tracker_home = home
1775 tool.db = self.db
1776 tool.verbose = False
1777 self.assertRaises(csv.Error, tool.do_import, ['_test_export'])
1779 self.nukeAndCreate()
1780 self.db.config.CSV_FIELD_SIZE = 3200
1781 tool = roundup.admin.AdminTool()
1782 tool.tracker_home = home
1783 tool.db = self.db
1784 tool.verbose = False
1785 tool.do_import(['_test_export'])
1786 finally:
1787 roundup.admin.sys = sys
1788 shutil.rmtree('_test_export')
1790 def testAddProperty(self):
1791 self.db.issue.create(title="spam", status='1')
1792 self.db.commit()
1794 self.db.issue.addprop(fixer=Link("user"))
1795 # force any post-init stuff to happen
1796 self.db.post_init()
1797 props = self.db.issue.getprops()
1798 keys = props.keys()
1799 keys.sort()
1800 self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
1801 'creator', 'deadline', 'feedback', 'files', 'fixer', 'foo', 'id', 'messages',
1802 'nosy', 'priority', 'spam', 'status', 'superseder', 'title'])
1803 self.assertEqual(self.db.issue.get('1', "fixer"), None)
1805 def testRemoveProperty(self):
1806 self.db.issue.create(title="spam", status='1')
1807 self.db.commit()
1809 del self.db.issue.properties['title']
1810 self.db.post_init()
1811 props = self.db.issue.getprops()
1812 keys = props.keys()
1813 keys.sort()
1814 self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
1815 'creator', 'deadline', 'feedback', 'files', 'foo', 'id', 'messages',
1816 'nosy', 'priority', 'spam', 'status', 'superseder'])
1817 self.assertEqual(self.db.issue.list(), ['1'])
1819 def testAddRemoveProperty(self):
1820 self.db.issue.create(title="spam", status='1')
1821 self.db.commit()
1823 self.db.issue.addprop(fixer=Link("user"))
1824 del self.db.issue.properties['title']
1825 self.db.post_init()
1826 props = self.db.issue.getprops()
1827 keys = props.keys()
1828 keys.sort()
1829 self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
1830 'creator', 'deadline', 'feedback', 'files', 'fixer', 'foo', 'id',
1831 'messages', 'nosy', 'priority', 'spam', 'status', 'superseder'])
1832 self.assertEqual(self.db.issue.list(), ['1'])
1834 def testNosyMail(self) :
1835 """Creates one issue with two attachments, one smaller and one larger
1836 than the set max_attachment_size.
1837 """
1838 old_translate_ = roundupdb._
1839 roundupdb._ = i18n.get_translation(language='C').gettext
1840 db = self.db
1841 db.config.NOSY_MAX_ATTACHMENT_SIZE = 4096
1842 res = dict(mail_to = None, mail_msg = None)
1843 def dummy_snd(s, to, msg, res=res) :
1844 res["mail_to"], res["mail_msg"] = to, msg
1845 backup, Mailer.smtp_send = Mailer.smtp_send, dummy_snd
1846 try :
1847 f1 = db.file.create(name="test1.txt", content="x" * 20)
1848 f2 = db.file.create(name="test2.txt", content="y" * 5000)
1849 m = db.msg.create(content="one two", author="admin",
1850 files = [f1, f2])
1851 i = db.issue.create(title='spam', files = [f1, f2],
1852 messages = [m], nosy = [db.user.lookup("fred")])
1854 db.issue.nosymessage(i, m, {})
1855 mail_msg = str(res["mail_msg"])
1856 self.assertEqual(res["mail_to"], ["fred@example.com"])
1857 self.assert_("From: admin" in mail_msg)
1858 self.assert_("Subject: [issue1] spam" in mail_msg)
1859 self.assert_("New submission from admin" in mail_msg)
1860 self.assert_("one two" in mail_msg)
1861 self.assert_("File 'test1.txt' not attached" not in mail_msg)
1862 self.assert_(base64.encodestring("xxx").rstrip() in mail_msg)
1863 self.assert_("File 'test2.txt' not attached" in mail_msg)
1864 self.assert_(base64.encodestring("yyy").rstrip() not in mail_msg)
1865 finally :
1866 roundupdb._ = old_translate_
1867 Mailer.smtp_send = backup
1869 class ROTest(MyTestCase):
1870 def setUp(self):
1871 # remove previous test, ignore errors
1872 if os.path.exists(config.DATABASE):
1873 shutil.rmtree(config.DATABASE)
1874 os.makedirs(config.DATABASE + '/files')
1875 self.db = self.module.Database(config, 'admin')
1876 setupSchema(self.db, 1, self.module)
1877 self.db.close()
1879 self.db = self.module.Database(config)
1880 setupSchema(self.db, 0, self.module)
1882 def testExceptions(self):
1883 # this tests the exceptions that should be raised
1884 ar = self.assertRaises
1886 # this tests the exceptions that should be raised
1887 ar(DatabaseError, self.db.status.create, name="foo")
1888 ar(DatabaseError, self.db.status.set, '1', name="foo")
1889 ar(DatabaseError, self.db.status.retire, '1')
1892 class SchemaTest(MyTestCase):
1893 def setUp(self):
1894 # remove previous test, ignore errors
1895 if os.path.exists(config.DATABASE):
1896 shutil.rmtree(config.DATABASE)
1897 os.makedirs(config.DATABASE + '/files')
1899 def open_database(self):
1900 self.db = self.module.Database(config, 'admin')
1902 def test_reservedProperties(self):
1903 self.open_database()
1904 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1905 creation=String())
1906 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1907 activity=String())
1908 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1909 creator=String())
1910 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1911 actor=String())
1913 def init_a(self):
1914 self.open_database()
1915 a = self.module.Class(self.db, "a", name=String())
1916 a.setkey("name")
1917 self.db.post_init()
1919 def test_fileClassProps(self):
1920 self.open_database()
1921 a = self.module.FileClass(self.db, 'a')
1922 l = a.getprops().keys()
1923 l.sort()
1924 self.assert_(l, ['activity', 'actor', 'content', 'created',
1925 'creation', 'type'])
1927 def init_ab(self):
1928 self.open_database()
1929 a = self.module.Class(self.db, "a", name=String())
1930 a.setkey("name")
1931 b = self.module.Class(self.db, "b", name=String(),
1932 fooz=Multilink('a'))
1933 b.setkey("name")
1934 self.db.post_init()
1936 def test_addNewClass(self):
1937 self.init_a()
1939 self.assertRaises(ValueError, self.module.Class, self.db, "a",
1940 name=String())
1942 aid = self.db.a.create(name='apple')
1943 self.db.commit(); self.db.close()
1945 # add a new class to the schema and check creation of new items
1946 # (and existence of old ones)
1947 self.init_ab()
1948 bid = self.db.b.create(name='bear', fooz=[aid])
1949 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1950 self.db.commit()
1951 self.db.close()
1953 # now check we can recall the added class' items
1954 self.init_ab()
1955 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1956 self.assertEqual(self.db.a.lookup('apple'), aid)
1957 self.assertEqual(self.db.b.get(bid, 'name'), 'bear')
1958 self.assertEqual(self.db.b.get(bid, 'fooz'), [aid])
1959 self.assertEqual(self.db.b.lookup('bear'), bid)
1961 # confirm journal's ok
1962 self.db.getjournal('a', aid)
1963 self.db.getjournal('b', bid)
1965 def init_amod(self):
1966 self.open_database()
1967 a = self.module.Class(self.db, "a", name=String(), newstr=String(),
1968 newint=Interval(), newnum=Number(), newbool=Boolean(),
1969 newdate=Date())
1970 a.setkey("name")
1971 b = self.module.Class(self.db, "b", name=String())
1972 b.setkey("name")
1973 self.db.post_init()
1975 def test_modifyClass(self):
1976 self.init_ab()
1978 # add item to user and issue class
1979 aid = self.db.a.create(name='apple')
1980 bid = self.db.b.create(name='bear')
1981 self.db.commit(); self.db.close()
1983 # modify "a" schema
1984 self.init_amod()
1985 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
1986 self.assertEqual(self.db.a.get(aid, 'newstr'), None)
1987 self.assertEqual(self.db.a.get(aid, 'newint'), None)
1988 # hack - metakit can't return None for missing values, and we're not
1989 # really checking for that behavior here anyway
1990 self.assert_(not self.db.a.get(aid, 'newnum'))
1991 self.assert_(not self.db.a.get(aid, 'newbool'))
1992 self.assertEqual(self.db.a.get(aid, 'newdate'), None)
1993 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
1994 aid2 = self.db.a.create(name='aardvark', newstr='booz')
1995 self.db.commit(); self.db.close()
1997 # test
1998 self.init_amod()
1999 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
2000 self.assertEqual(self.db.a.get(aid, 'newstr'), None)
2001 self.assertEqual(self.db.b.get(aid, 'name'), 'bear')
2002 self.assertEqual(self.db.a.get(aid2, 'name'), 'aardvark')
2003 self.assertEqual(self.db.a.get(aid2, 'newstr'), 'booz')
2005 # confirm journal's ok
2006 self.db.getjournal('a', aid)
2007 self.db.getjournal('a', aid2)
2009 def init_amodkey(self):
2010 self.open_database()
2011 a = self.module.Class(self.db, "a", name=String(), newstr=String())
2012 a.setkey("newstr")
2013 b = self.module.Class(self.db, "b", name=String())
2014 b.setkey("name")
2015 self.db.post_init()
2017 def test_changeClassKey(self):
2018 self.init_amod()
2019 aid = self.db.a.create(name='apple')
2020 self.assertEqual(self.db.a.lookup('apple'), aid)
2021 self.db.commit(); self.db.close()
2023 # change the key to newstr on a
2024 self.init_amodkey()
2025 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
2026 self.assertEqual(self.db.a.get(aid, 'newstr'), None)
2027 self.assertRaises(KeyError, self.db.a.lookup, 'apple')
2028 aid2 = self.db.a.create(name='aardvark', newstr='booz')
2029 self.db.commit(); self.db.close()
2031 # check
2032 self.init_amodkey()
2033 self.assertEqual(self.db.a.lookup('booz'), aid2)
2035 # confirm journal's ok
2036 self.db.getjournal('a', aid)
2038 def test_removeClassKey(self):
2039 self.init_amod()
2040 aid = self.db.a.create(name='apple')
2041 self.assertEqual(self.db.a.lookup('apple'), aid)
2042 self.db.commit(); self.db.close()
2044 self.db = self.module.Database(config, 'admin')
2045 a = self.module.Class(self.db, "a", name=String(), newstr=String())
2046 self.db.post_init()
2048 aid2 = self.db.a.create(name='apple', newstr='booz')
2049 self.db.commit()
2052 def init_amodml(self):
2053 self.open_database()
2054 a = self.module.Class(self.db, "a", name=String(),
2055 newml=Multilink('a'))
2056 a.setkey('name')
2057 self.db.post_init()
2059 def test_makeNewMultilink(self):
2060 self.init_a()
2061 aid = self.db.a.create(name='apple')
2062 self.assertEqual(self.db.a.lookup('apple'), aid)
2063 self.db.commit(); self.db.close()
2065 # add a multilink prop
2066 self.init_amodml()
2067 bid = self.db.a.create(name='bear', newml=[aid])
2068 self.assertEqual(self.db.a.find(newml=aid), [bid])
2069 self.assertEqual(self.db.a.lookup('apple'), aid)
2070 self.db.commit(); self.db.close()
2072 # check
2073 self.init_amodml()
2074 self.assertEqual(self.db.a.find(newml=aid), [bid])
2075 self.assertEqual(self.db.a.lookup('apple'), aid)
2076 self.assertEqual(self.db.a.lookup('bear'), bid)
2078 # confirm journal's ok
2079 self.db.getjournal('a', aid)
2080 self.db.getjournal('a', bid)
2082 def test_removeMultilink(self):
2083 # add a multilink prop
2084 self.init_amodml()
2085 aid = self.db.a.create(name='apple')
2086 bid = self.db.a.create(name='bear', newml=[aid])
2087 self.assertEqual(self.db.a.find(newml=aid), [bid])
2088 self.assertEqual(self.db.a.lookup('apple'), aid)
2089 self.assertEqual(self.db.a.lookup('bear'), bid)
2090 self.db.commit(); self.db.close()
2092 # remove the multilink
2093 self.init_a()
2094 self.assertEqual(self.db.a.lookup('apple'), aid)
2095 self.assertEqual(self.db.a.lookup('bear'), bid)
2097 # confirm journal's ok
2098 self.db.getjournal('a', aid)
2099 self.db.getjournal('a', bid)
2101 def test_removeClass(self):
2102 self.init_ab()
2103 aid = self.db.a.create(name='apple')
2104 bid = self.db.b.create(name='bear')
2105 self.db.commit(); self.db.close()
2107 # drop the b class
2108 self.init_a()
2109 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
2110 self.assertEqual(self.db.a.lookup('apple'), aid)
2111 self.db.commit(); self.db.close()
2113 # now check we can recall the added class' items
2114 self.init_a()
2115 self.assertEqual(self.db.a.get(aid, 'name'), 'apple')
2116 self.assertEqual(self.db.a.lookup('apple'), aid)
2118 # confirm journal's ok
2119 self.db.getjournal('a', aid)
2121 class RDBMSTest:
2122 """ tests specific to RDBMS backends """
2123 def test_indexTest(self):
2124 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_id_idx'), 1)
2125 self.assertEqual(self.db.sql_index_exists('_issue', '_issue_x_idx'), 0)
2128 class ClassicInitTest(unittest.TestCase):
2129 count = 0
2130 db = None
2132 def setUp(self):
2133 ClassicInitTest.count = ClassicInitTest.count + 1
2134 self.dirname = '_test_init_%s'%self.count
2135 try:
2136 shutil.rmtree(self.dirname)
2137 except OSError, error:
2138 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
2140 def testCreation(self):
2141 ae = self.assertEqual
2143 # set up and open a tracker
2144 tracker = setupTracker(self.dirname, self.backend)
2145 # open the database
2146 db = self.db = tracker.open('test')
2148 # check the basics of the schema and initial data set
2149 l = db.priority.list()
2150 l.sort()
2151 ae(l, ['1', '2', '3', '4', '5'])
2152 l = db.status.list()
2153 l.sort()
2154 ae(l, ['1', '2', '3', '4', '5', '6', '7', '8'])
2155 l = db.keyword.list()
2156 ae(l, [])
2157 l = db.user.list()
2158 l.sort()
2159 ae(l, ['1', '2'])
2160 l = db.msg.list()
2161 ae(l, [])
2162 l = db.file.list()
2163 ae(l, [])
2164 l = db.issue.list()
2165 ae(l, [])
2167 def tearDown(self):
2168 if self.db is not None:
2169 self.db.close()
2170 try:
2171 shutil.rmtree(self.dirname)
2172 except OSError, error:
2173 if error.errno not in (errno.ENOENT, errno.ESRCH): raise
2175 # vim: set et sts=4 sw=4 :