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