comparison test/test_db.py @ 1176:bd3b57859c37

On second thought, that last checkin was dumb. The old, nasty, for-purely-historical-reasons journaltag-as-username has gone away now. The code should handle existing journaltag-as-username entries, but will use userid from now on.
author Richard Jones <richard@users.sourceforge.net>
date Fri, 20 Sep 2002 05:08:00 +0000
parents 8e318dfaf479
children 18bb36e4f62f
comparison
equal deleted inserted replaced
1175:762f48bfbc0b 1176:bd3b57859c37
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 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" 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, 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 # 17 #
18 # $Id: test_db.py,v 1.51 2002-09-20 01:20:32 richard Exp $ 18 # $Id: test_db.py,v 1.52 2002-09-20 05:08:00 richard Exp $
19 19
20 import unittest, os, shutil, time 20 import unittest, os, shutil, time
21 21
22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \ 22 from roundup.hyperdb import String, Password, Link, Multilink, Date, \
23 Interval, DatabaseError, Boolean, Number 23 Interval, DatabaseError, Boolean, Number
29 status.setkey("name") 29 status.setkey("name")
30 user = module.Class(db, "user", username=String(), password=Password(), 30 user = module.Class(db, "user", username=String(), password=Password(),
31 assignable=Boolean(), age=Number(), roles=String()) 31 assignable=Boolean(), age=Number(), roles=String())
32 user.setkey("username") 32 user.setkey("username")
33 file = module.FileClass(db, "file", name=String(), type=String(), 33 file = module.FileClass(db, "file", name=String(), type=String(),
34 comment=String(indexme="yes")) 34 comment=String(indexme="yes"), fooz=Password())
35 issue = module.IssueClass(db, "issue", title=String(indexme="yes"), 35 issue = module.IssueClass(db, "issue", title=String(indexme="yes"),
36 status=Link("status"), nosy=Multilink("user"), deadline=Date(), 36 status=Link("status"), nosy=Multilink("user"), deadline=Date(),
37 foo=Interval(), files=Multilink("file"), assignedto=Link('user')) 37 foo=Interval(), files=Multilink("file"), assignedto=Link('user'))
38 session = module.Class(db, 'session', title=String()) 38 session = module.Class(db, 'session', title=String())
39 session.disableJournalling() 39 session.disableJournalling()
40 db.post_init() 40 db.post_init()
41 if create: 41 if create:
42 user.create(username="admin", roles='Admin')
42 status.create(name="unread") 43 status.create(name="unread")
43 status.create(name="in-progress") 44 status.create(name="in-progress")
44 status.create(name="testing") 45 status.create(name="testing")
45 status.create(name="resolved") 46 status.create(name="resolved")
46 db.commit() 47 db.commit()
72 from roundup.backends import anydbm 73 from roundup.backends import anydbm
73 # remove previous test, ignore errors 74 # remove previous test, ignore errors
74 if os.path.exists(config.DATABASE): 75 if os.path.exists(config.DATABASE):
75 shutil.rmtree(config.DATABASE) 76 shutil.rmtree(config.DATABASE)
76 os.makedirs(config.DATABASE + '/files') 77 os.makedirs(config.DATABASE + '/files')
77 self.db = anydbm.Database(config, 'test') 78 self.db = anydbm.Database(config, 'admin')
78 setupSchema(self.db, 1, anydbm) 79 setupSchema(self.db, 1, anydbm)
79 self.db2 = anydbm.Database(config, 'test') 80 self.db2 = anydbm.Database(config, 'admin')
80 setupSchema(self.db2, 0, anydbm) 81 setupSchema(self.db2, 0, anydbm)
81 82
82 def testStringChange(self): 83 def testStringChange(self):
83 # test set & retrieve 84 # test set & retrieve
84 self.db.issue.create(title="spam", status='1') 85 self.db.issue.create(title="spam", status='1')
148 self.assertEqual(self.db.user.get(userid, 'assignable'), 0) 149 self.assertEqual(self.db.user.get(userid, 'assignable'), 0)
149 self.db.user.set(userid, assignable=None) 150 self.db.user.set(userid, assignable=None)
150 self.assertEqual(self.db.user.get('1', "assignable"), None) 151 self.assertEqual(self.db.user.get('1', "assignable"), None)
151 152
152 def testNumberChange(self): 153 def testNumberChange(self):
153 self.db.user.create(username='foo', age=1) 154 nid = self.db.user.create(username='foo', age=1)
154 self.assertEqual(1, self.db.user.get('1', 'age')) 155 self.assertEqual(1, self.db.user.get(nid, 'age'))
155 self.db.user.set('1', age=3) 156 self.db.user.set('1', age=3)
156 self.assertNotEqual(self.db.user.get('1', 'age'), 1) 157 self.assertNotEqual(self.db.user.get('1', 'age'), 1)
157 self.db.user.set('1', age=1.0) 158 self.db.user.set('1', age=1.0)
158 self.db.user.set('1', age=None) 159 self.db.user.set('1', age=None)
159 self.assertEqual(self.db.user.get('1', "age"), None) 160 self.assertEqual(self.db.user.get('1', "age"), None)
303 304
304 # 305 #
305 # key property 306 # key property
306 # 307 #
307 # key must be a String 308 # key must be a String
308 ar(TypeError, self.db.user.setkey, 'password') 309 ar(TypeError, self.db.file.setkey, 'fooz')
309 # key must exist 310 # key must exist
310 ar(KeyError, self.db.user.setkey, 'fubar') 311 ar(KeyError, self.db.file.setkey, 'fubar')
311 312
312 # 313 #
313 # class get 314 # class get
314 # 315 #
315 # invalid node id 316 # invalid node id
345 nosy=['10']) 346 nosy=['10'])
346 # invalid number value 347 # invalid number value
347 ar(TypeError, self.db.user.create, username='foo', age='a') 348 ar(TypeError, self.db.user.create, username='foo', age='a')
348 # invalid boolean value 349 # invalid boolean value
349 ar(TypeError, self.db.user.create, username='foo', assignable='true') 350 ar(TypeError, self.db.user.create, username='foo', assignable='true')
350 self.db.user.create(username='foo') 351 nid = self.db.user.create(username='foo')
351 # invalid number value 352 # invalid number value
352 ar(TypeError, self.db.user.set, '3', username='foo', age='a') 353 ar(TypeError, self.db.user.set, nid, username='foo', age='a')
353 # invalid boolean value 354 # invalid boolean value
354 ar(TypeError, self.db.user.set, '3', username='foo', assignable='true') 355 ar(TypeError, self.db.user.set, nid, username='foo', assignable='true')
355 356
356 def testJournals(self): 357 def testJournals(self):
357 self.db.user.create(username="mary") 358 self.db.user.create(username="mary")
358 self.db.user.create(username="pete") 359 self.db.user.create(username="pete")
359 self.db.issue.create(title="spam", status='1') 360 self.db.issue.create(title="spam", status='1')
362 # journal entry for issue create 363 # journal entry for issue create
363 journal = self.db.getjournal('issue', '1') 364 journal = self.db.getjournal('issue', '1')
364 self.assertEqual(1, len(journal)) 365 self.assertEqual(1, len(journal))
365 (nodeid, date_stamp, journaltag, action, params) = journal[0] 366 (nodeid, date_stamp, journaltag, action, params) = journal[0]
366 self.assertEqual(nodeid, '1') 367 self.assertEqual(nodeid, '1')
367 self.assertEqual(journaltag, 'test') 368 self.assertEqual(journaltag, self.db.user.lookup('admin'))
368 self.assertEqual(action, 'create') 369 self.assertEqual(action, 'create')
369 keys = params.keys() 370 keys = params.keys()
370 keys.sort() 371 keys.sort()
371 self.assertEqual(keys, ['assignedto', 'deadline', 'files', 372 self.assertEqual(keys, ['assignedto', 'deadline', 'files',
372 'foo', 'messages', 'nosy', 'status', 'superseder', 'title']) 373 'foo', 'messages', 'nosy', 'status', 'superseder', 'title'])
383 self.db.commit() 384 self.db.commit()
384 journal = self.db.getjournal('user', '1') 385 journal = self.db.getjournal('user', '1')
385 self.assertEqual(2, len(journal)) 386 self.assertEqual(2, len(journal))
386 (nodeid, date_stamp, journaltag, action, params) = journal[1] 387 (nodeid, date_stamp, journaltag, action, params) = journal[1]
387 self.assertEqual('1', nodeid) 388 self.assertEqual('1', nodeid)
388 self.assertEqual('test', journaltag) 389 self.assertEqual('1', journaltag)
389 self.assertEqual('link', action) 390 self.assertEqual('link', action)
390 self.assertEqual(('issue', '1', 'assignedto'), params) 391 self.assertEqual(('issue', '1', 'assignedto'), params)
391 392
392 # journal entry for unlink 393 # journal entry for unlink
393 self.db.issue.set('1', assignedto='2') 394 self.db.issue.set('1', assignedto='2')
394 self.db.commit() 395 self.db.commit()
395 journal = self.db.getjournal('user', '1') 396 journal = self.db.getjournal('user', '1')
396 self.assertEqual(3, len(journal)) 397 self.assertEqual(3, len(journal))
397 (nodeid, date_stamp, journaltag, action, params) = journal[2] 398 (nodeid, date_stamp, journaltag, action, params) = journal[2]
398 self.assertEqual('1', nodeid) 399 self.assertEqual('1', nodeid)
399 self.assertEqual('test', journaltag) 400 self.assertEqual('1', journaltag)
400 self.assertEqual('unlink', action) 401 self.assertEqual('unlink', action)
401 self.assertEqual(('issue', '1', 'assignedto'), params) 402 self.assertEqual(('issue', '1', 'assignedto'), params)
402 403
403 # test disabling journalling 404 # test disabling journalling
404 # ... get the last entry 405 # ... get the last entry
523 from roundup.backends import anydbm 524 from roundup.backends import anydbm
524 # remove previous test, ignore errors 525 # remove previous test, ignore errors
525 if os.path.exists(config.DATABASE): 526 if os.path.exists(config.DATABASE):
526 shutil.rmtree(config.DATABASE) 527 shutil.rmtree(config.DATABASE)
527 os.makedirs(config.DATABASE + '/files') 528 os.makedirs(config.DATABASE + '/files')
528 db = anydbm.Database(config, 'test') 529 db = anydbm.Database(config, 'admin')
529 setupSchema(db, 1, anydbm) 530 setupSchema(db, 1, anydbm)
530 self.db = anydbm.Database(config) 531 self.db = anydbm.Database(config)
531 setupSchema(self.db, 0, anydbm) 532 setupSchema(self.db, 0, anydbm)
532 self.db2 = anydbm.Database(config, 'test') 533 self.db2 = anydbm.Database(config, 'admin')
533 setupSchema(self.db2, 0, anydbm) 534 setupSchema(self.db2, 0, anydbm)
534 535
535 def testExceptions(self): 536 def testExceptions(self):
536 # this tests the exceptions that should be raised 537 # this tests the exceptions that should be raised
537 ar = self.assertRaises 538 ar = self.assertRaises
547 from roundup.backends import bsddb 548 from roundup.backends import bsddb
548 # remove previous test, ignore errors 549 # remove previous test, ignore errors
549 if os.path.exists(config.DATABASE): 550 if os.path.exists(config.DATABASE):
550 shutil.rmtree(config.DATABASE) 551 shutil.rmtree(config.DATABASE)
551 os.makedirs(config.DATABASE + '/files') 552 os.makedirs(config.DATABASE + '/files')
552 self.db = bsddb.Database(config, 'test') 553 self.db = bsddb.Database(config, 'admin')
553 setupSchema(self.db, 1, bsddb) 554 setupSchema(self.db, 1, bsddb)
554 self.db2 = bsddb.Database(config, 'test') 555 self.db2 = bsddb.Database(config, 'admin')
555 setupSchema(self.db2, 0, bsddb) 556 setupSchema(self.db2, 0, bsddb)
556 557
557 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): 558 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
558 def setUp(self): 559 def setUp(self):
559 from roundup.backends import bsddb 560 from roundup.backends import bsddb
560 # remove previous test, ignore errors 561 # remove previous test, ignore errors
561 if os.path.exists(config.DATABASE): 562 if os.path.exists(config.DATABASE):
562 shutil.rmtree(config.DATABASE) 563 shutil.rmtree(config.DATABASE)
563 os.makedirs(config.DATABASE + '/files') 564 os.makedirs(config.DATABASE + '/files')
564 db = bsddb.Database(config, 'test') 565 db = bsddb.Database(config, 'admin')
565 setupSchema(db, 1, bsddb) 566 setupSchema(db, 1, bsddb)
566 self.db = bsddb.Database(config) 567 self.db = bsddb.Database(config)
567 setupSchema(self.db, 0, bsddb) 568 setupSchema(self.db, 0, bsddb)
568 self.db2 = bsddb.Database(config, 'test') 569 self.db2 = bsddb.Database(config, 'admin')
569 setupSchema(self.db2, 0, bsddb) 570 setupSchema(self.db2, 0, bsddb)
570 571
571 572
572 class bsddb3DBTestCase(anydbmDBTestCase): 573 class bsddb3DBTestCase(anydbmDBTestCase):
573 def setUp(self): 574 def setUp(self):
574 from roundup.backends import bsddb3 575 from roundup.backends import bsddb3
575 # remove previous test, ignore errors 576 # remove previous test, ignore errors
576 if os.path.exists(config.DATABASE): 577 if os.path.exists(config.DATABASE):
577 shutil.rmtree(config.DATABASE) 578 shutil.rmtree(config.DATABASE)
578 os.makedirs(config.DATABASE + '/files') 579 os.makedirs(config.DATABASE + '/files')
579 self.db = bsddb3.Database(config, 'test') 580 self.db = bsddb3.Database(config, 'admin')
580 setupSchema(self.db, 1, bsddb3) 581 setupSchema(self.db, 1, bsddb3)
581 self.db2 = bsddb3.Database(config, 'test') 582 self.db2 = bsddb3.Database(config, 'admin')
582 setupSchema(self.db2, 0, bsddb3) 583 setupSchema(self.db2, 0, bsddb3)
583 584
584 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): 585 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
585 def setUp(self): 586 def setUp(self):
586 from roundup.backends import bsddb3 587 from roundup.backends import bsddb3
587 # remove previous test, ignore errors 588 # remove previous test, ignore errors
588 if os.path.exists(config.DATABASE): 589 if os.path.exists(config.DATABASE):
589 shutil.rmtree(config.DATABASE) 590 shutil.rmtree(config.DATABASE)
590 os.makedirs(config.DATABASE + '/files') 591 os.makedirs(config.DATABASE + '/files')
591 db = bsddb3.Database(config, 'test') 592 db = bsddb3.Database(config, 'admin')
592 setupSchema(db, 1, bsddb3) 593 setupSchema(db, 1, bsddb3)
593 self.db = bsddb3.Database(config) 594 self.db = bsddb3.Database(config)
594 setupSchema(self.db, 0, bsddb3) 595 setupSchema(self.db, 0, bsddb3)
595 self.db2 = bsddb3.Database(config, 'test') 596 self.db2 = bsddb3.Database(config, 'admin')
596 setupSchema(self.db2, 0, bsddb3) 597 setupSchema(self.db2, 0, bsddb3)
597 598
598 599
599 class gadflyDBTestCase(anydbmDBTestCase): 600 class gadflyDBTestCase(anydbmDBTestCase):
600 ''' Gadfly doesn't support multiple connections to the one local 601 ''' Gadfly doesn't support multiple connections to the one local
605 # remove previous test, ignore errors 606 # remove previous test, ignore errors
606 if os.path.exists(config.DATABASE): 607 if os.path.exists(config.DATABASE):
607 shutil.rmtree(config.DATABASE) 608 shutil.rmtree(config.DATABASE)
608 config.GADFLY_DATABASE = ('test', config.DATABASE) 609 config.GADFLY_DATABASE = ('test', config.DATABASE)
609 os.makedirs(config.DATABASE + '/files') 610 os.makedirs(config.DATABASE + '/files')
610 self.db = gadfly.Database(config, 'test') 611 self.db = gadfly.Database(config, 'admin')
611 setupSchema(self.db, 1, gadfly) 612 setupSchema(self.db, 1, gadfly)
612 613
613 def testIDGeneration(self): 614 def testIDGeneration(self):
614 id1 = self.db.issue.create(title="spam", status='1') 615 id1 = self.db.issue.create(title="spam", status='1')
615 id2 = self.db.issue.create(title="eggs", status='2') 616 id2 = self.db.issue.create(title="eggs", status='2')
621 # remove previous test, ignore errors 622 # remove previous test, ignore errors
622 if os.path.exists(config.DATABASE): 623 if os.path.exists(config.DATABASE):
623 shutil.rmtree(config.DATABASE) 624 shutil.rmtree(config.DATABASE)
624 config.GADFLY_DATABASE = ('test', config.DATABASE) 625 config.GADFLY_DATABASE = ('test', config.DATABASE)
625 os.makedirs(config.DATABASE + '/files') 626 os.makedirs(config.DATABASE + '/files')
626 db = gadfly.Database(config, 'test') 627 db = gadfly.Database(config, 'admin')
627 setupSchema(db, 1, gadfly) 628 setupSchema(db, 1, gadfly)
628 self.db = gadfly.Database(config) 629 self.db = gadfly.Database(config)
629 setupSchema(self.db, 0, gadfly) 630 setupSchema(self.db, 0, gadfly)
630 631
631 632
634 from roundup.backends import sqlite 635 from roundup.backends import sqlite
635 # remove previous test, ignore errors 636 # remove previous test, ignore errors
636 if os.path.exists(config.DATABASE): 637 if os.path.exists(config.DATABASE):
637 shutil.rmtree(config.DATABASE) 638 shutil.rmtree(config.DATABASE)
638 os.makedirs(config.DATABASE + '/files') 639 os.makedirs(config.DATABASE + '/files')
639 self.db = sqlite.Database(config, 'test') 640 self.db = sqlite.Database(config, 'admin')
640 setupSchema(self.db, 1, sqlite) 641 setupSchema(self.db, 1, sqlite)
641 642
642 def testIDGeneration(self): 643 def testIDGeneration(self):
643 pass 644 pass
644 645
647 from roundup.backends import sqlite 648 from roundup.backends import sqlite
648 # remove previous test, ignore errors 649 # remove previous test, ignore errors
649 if os.path.exists(config.DATABASE): 650 if os.path.exists(config.DATABASE):
650 shutil.rmtree(config.DATABASE) 651 shutil.rmtree(config.DATABASE)
651 os.makedirs(config.DATABASE + '/files') 652 os.makedirs(config.DATABASE + '/files')
652 db = sqlite.Database(config, 'test') 653 db = sqlite.Database(config, 'admin')
653 setupSchema(db, 1, sqlite) 654 setupSchema(db, 1, sqlite)
654 self.db = sqlite.Database(config) 655 self.db = sqlite.Database(config)
655 setupSchema(self.db, 0, sqlite) 656 setupSchema(self.db, 0, sqlite)
656 657
657 658
662 metakit._instances = weakref.WeakValueDictionary() 663 metakit._instances = weakref.WeakValueDictionary()
663 # remove previous test, ignore errors 664 # remove previous test, ignore errors
664 if os.path.exists(config.DATABASE): 665 if os.path.exists(config.DATABASE):
665 shutil.rmtree(config.DATABASE) 666 shutil.rmtree(config.DATABASE)
666 os.makedirs(config.DATABASE + '/files') 667 os.makedirs(config.DATABASE + '/files')
667 self.db = metakit.Database(config, 'test') 668 self.db = metakit.Database(config, 'admin')
668 setupSchema(self.db, 1, metakit) 669 setupSchema(self.db, 1, metakit)
669 #self.db2 = metakit.Database(config, 'test')
670 #setupSchema(self.db2, 0, metakit)
671 670
672 def testIDGeneration(self): 671 def testIDGeneration(self):
673 id1 = self.db.issue.create(title="spam", status='1') 672 id1 = self.db.issue.create(title="spam", status='1')
674 id2 = self.db.issue.create(title="eggs", status='2') 673 id2 = self.db.issue.create(title="eggs", status='2')
675 self.assertNotEqual(id1, id2) 674 self.assertNotEqual(id1, id2)
706 metakit._instances = weakref.WeakValueDictionary() 705 metakit._instances = weakref.WeakValueDictionary()
707 # remove previous test, ignore errors 706 # remove previous test, ignore errors
708 if os.path.exists(config.DATABASE): 707 if os.path.exists(config.DATABASE):
709 shutil.rmtree(config.DATABASE) 708 shutil.rmtree(config.DATABASE)
710 os.makedirs(config.DATABASE + '/files') 709 os.makedirs(config.DATABASE + '/files')
711 db = metakit.Database(config, 'test') 710 db = metakit.Database(config, 'admin')
712 setupSchema(db, 1, metakit) 711 setupSchema(db, 1, metakit)
713 self.db = metakit.Database(config) 712 self.db = metakit.Database(config)
714 setupSchema(self.db, 0, metakit) 713 setupSchema(self.db, 0, metakit)
715 # self.db2 = metakit.Database(config, 'test')
716 # setupSchema(self.db2, 0, metakit)
717 714
718 def suite(): 715 def suite():
719 l = [ 716 l = [
720 unittest.makeSuite(anydbmDBTestCase, 'test'), 717 unittest.makeSuite(anydbmDBTestCase, 'test'),
721 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test') 718 unittest.makeSuite(anydbmReadOnlyDBTestCase, 'test')
722 ] 719 ]
723 # return unittest.TestSuite(l) 720 # return unittest.TestSuite(l)
724 721
725 try: 722 try:
723 import gadfly
724 l.append(unittest.makeSuite(gadflyDBTestCase, 'test'))
725 l.append(unittest.makeSuite(gadflyReadOnlyDBTestCase, 'test'))
726 except:
727 print 'gadfly module not found, skipping gadfly DBTestCase'
728
729 try:
726 import sqlite 730 import sqlite
727 l.append(unittest.makeSuite(sqliteDBTestCase, 'test')) 731 l.append(unittest.makeSuite(sqliteDBTestCase, 'test'))
728 l.append(unittest.makeSuite(sqliteReadOnlyDBTestCase, 'test')) 732 l.append(unittest.makeSuite(sqliteReadOnlyDBTestCase, 'test'))
729 except: 733 except:
730 print 'sqlite module not found, skipping gadfly DBTestCase' 734 print 'sqlite module not found, skipping gadfly DBTestCase'
731 # return unittest.TestSuite(l)
732
733 try:
734 import gadfly
735 l.append(unittest.makeSuite(gadflyDBTestCase, 'test'))
736 l.append(unittest.makeSuite(gadflyReadOnlyDBTestCase, 'test'))
737 except:
738 print 'gadfly module not found, skipping gadfly DBTestCase'
739 735
740 try: 736 try:
741 import bsddb 737 import bsddb
742 l.append(unittest.makeSuite(bsddbDBTestCase, 'test')) 738 l.append(unittest.makeSuite(bsddbDBTestCase, 'test'))
743 l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test')) 739 l.append(unittest.makeSuite(bsddbReadOnlyDBTestCase, 'test'))

Roundup Issue Tracker: http://roundup-tracker.org/