comparison test/test_db.py @ 2092:332c040b82da maint-0.6

import/export test
author Richard Jones <richard@users.sourceforge.net>
date Sat, 20 Mar 2004 22:15:58 +0000
parents 6e1e717a9caa
children
comparison
equal deleted inserted replaced
2087:8e960088b44c 2092:332c040b82da
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.90.2.4 2004-03-18 02:40:08 richard Exp $ 18 # $Id: test_db.py,v 1.90.2.5 2004-03-20 22:15:58 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, Node 23 Interval, DatabaseError, Boolean, Number, Node
49 class MyTestCase(unittest.TestCase): 49 class MyTestCase(unittest.TestCase):
50 def tearDown(self): 50 def tearDown(self):
51 self.db.close() 51 self.db.close()
52 if os.path.exists('_test_dir'): 52 if os.path.exists('_test_dir'):
53 shutil.rmtree('_test_dir') 53 shutil.rmtree('_test_dir')
54 nuke_database = tearDown
54 55
55 class config: 56 class config:
56 DATABASE='_test_dir' 57 DATABASE='_test_dir'
57 MAILHOST = 'localhost' 58 MAILHOST = 'localhost'
58 MAIL_DOMAIN = 'fill.me.in.' 59 MAIL_DOMAIN = 'fill.me.in.'
76 MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD) 77 MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD)
77 78
78 class anydbmDBTestCase(MyTestCase): 79 class anydbmDBTestCase(MyTestCase):
79 def setUp(self): 80 def setUp(self):
80 from roundup.backends import anydbm 81 from roundup.backends import anydbm
82 self.module = anydbm
81 # remove previous test, ignore errors 83 # remove previous test, ignore errors
82 if os.path.exists(config.DATABASE): 84 if os.path.exists(config.DATABASE):
83 shutil.rmtree(config.DATABASE) 85 shutil.rmtree(config.DATABASE)
84 os.makedirs(config.DATABASE + '/files') 86 os.makedirs(config.DATABASE + '/files')
85 self.db = anydbm.Database(config, 'admin') 87 self.db = self.module.Database(config, 'admin')
86 setupSchema(self.db, 1, anydbm) 88 setupSchema(self.db, 1, self.module)
87 89
88 # 90 #
89 # schema mutation 91 # schema mutation
90 # 92 #
91 def testAddProperty(self): 93 def testAddProperty(self):
738 ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3']) 740 ae(filt(None, {}, ('-','foo'), (None,None)), ['2', '1', '4', '3'])
739 741
740 # XXX add sorting tests for other types 742 # XXX add sorting tests for other types
741 # XXX test auditors and reactors 743 # XXX test auditors and reactors
742 744
745 def filteringSetup(self):
746 for user in (
747 {'username': 'bleep'},
748 {'username': 'blop'},
749 {'username': 'blorp'}):
750 self.db.user.create(**user)
751 iss = self.db.issue
752 for issue in (
753 {'title': 'issue one', 'status': '2', 'assignedto': '1',
754 'foo': date.Interval('1:10'),
755 'deadline': date.Date('2003-01-01.00:00')},
756 {'title': 'issue two', 'status': '1', 'assignedto': '2',
757 'foo': date.Interval('1d'),
758 'deadline': date.Date('2003-02-16.22:50')},
759 {'title': 'issue three', 'status': '1',
760 'nosy': ['1','2'], 'deadline': date.Date('2003-02-18')},
761 {'title': 'non four', 'status': '3',
762 'foo': date.Interval('0:10'),
763 'nosy': ['1'], 'deadline': date.Date('2004-03-08')}):
764 self.db.issue.create(**issue)
765 self.db.commit()
766 return self.assertEqual, self.db.issue.filter
767
768 def testImportExport(self):
769 # use the filtering setup to create a bunch of items
770 ae, filt = self.filteringSetup()
771 self.db.user.retire('3')
772 self.db.issue.retire('2')
773
774 # grab snapshot of the current database
775 orig = {}
776 for cn,klass in self.db.classes.items():
777 cl = orig[cn] = {}
778 for id in klass.list():
779 it = cl[id] = {}
780 for name in klass.getprops().keys():
781 it[name] = klass.get(id, name)
782
783 # grab the export
784 export = {}
785 for cn,klass in self.db.classes.items():
786 names = klass.getprops().keys()
787 cl = export[cn] = [names+['is retired']]
788 for id in klass.getnodeids():
789 cl.append(klass.export_list(names, id))
790
791 # shut down this db and nuke it
792 self.db.close()
793 self.nuke_database()
794
795 # open a new, empty database
796 os.makedirs(config.DATABASE + '/files')
797 self.db = self.module.Database(config, 'admin')
798 setupSchema(self.db, 0, self.module)
799
800 # import
801 for cn, items in export.items():
802 klass = self.db.classes[cn]
803 names = items[0]
804 maxid = 1
805 for itemprops in items[1:]:
806 maxid = max(maxid, int(klass.import_list(names, itemprops)))
807 self.db.setid(cn, str(maxid+1))
808
809 # compare with snapshot of the database
810 for cn, items in orig.items():
811 klass = self.db.classes[cn]
812 # ensure retired items are retired :)
813 l = items.keys(); l.sort()
814 m = klass.list(); m.sort()
815 ae(l, m)
816 for id, props in items.items():
817 for name, value in props.items():
818 l = klass.get(id, name)
819 if isinstance(value, type([])):
820 value.sort()
821 l.sort()
822 ae(l, value)
823
824 # make sure the retired items are actually imported
825 ae(self.db.user.get('4', 'username'), 'blorp')
826 ae(self.db.issue.get('2', 'title'), 'issue two')
827
828 # make sure id counters are set correctly
829 maxid = max([int(id) for id in self.db.user.list()])
830 newid = self.db.user.create(username='testing')
831 assert newid > maxid
832
743 class anydbmReadOnlyDBTestCase(MyTestCase): 833 class anydbmReadOnlyDBTestCase(MyTestCase):
744 def setUp(self): 834 def setUp(self):
745 from roundup.backends import anydbm 835 from roundup.backends import anydbm
746 # remove previous test, ignore errors 836 # remove previous test, ignore errors
747 if os.path.exists(config.DATABASE): 837 if os.path.exists(config.DATABASE):
764 854
765 855
766 class bsddbDBTestCase(anydbmDBTestCase): 856 class bsddbDBTestCase(anydbmDBTestCase):
767 def setUp(self): 857 def setUp(self):
768 from roundup.backends import bsddb 858 from roundup.backends import bsddb
769 # remove previous test, ignore errors 859 self.module = bsddb
770 if os.path.exists(config.DATABASE): 860 anydbmDBTestCase.setUp(self)
771 shutil.rmtree(config.DATABASE)
772 os.makedirs(config.DATABASE + '/files')
773 self.db = bsddb.Database(config, 'admin')
774 setupSchema(self.db, 1, bsddb)
775 861
776 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): 862 class bsddbReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
777 def setUp(self): 863 def setUp(self):
778 from roundup.backends import bsddb 864 from roundup.backends import bsddb
779 # remove previous test, ignore errors 865 # remove previous test, ignore errors
788 874
789 875
790 class bsddb3DBTestCase(anydbmDBTestCase): 876 class bsddb3DBTestCase(anydbmDBTestCase):
791 def setUp(self): 877 def setUp(self):
792 from roundup.backends import bsddb3 878 from roundup.backends import bsddb3
793 # remove previous test, ignore errors 879 self.module = bsddb3
794 if os.path.exists(config.DATABASE): 880 anydbmDBTestCase.setUp(self)
795 shutil.rmtree(config.DATABASE)
796 os.makedirs(config.DATABASE + '/files')
797 self.db = bsddb3.Database(config, 'admin')
798 setupSchema(self.db, 1, bsddb3)
799 881
800 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): 882 class bsddb3ReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
801 def setUp(self): 883 def setUp(self):
802 from roundup.backends import bsddb3 884 from roundup.backends import bsddb3
803 # remove previous test, ignore errors 885 # remove previous test, ignore errors
812 894
813 895
814 class mysqlDBTestCase(anydbmDBTestCase): 896 class mysqlDBTestCase(anydbmDBTestCase):
815 def setUp(self): 897 def setUp(self):
816 from roundup.backends import mysql 898 from roundup.backends import mysql
899 self.module = mysql
817 mysql.db_nuke(config) 900 mysql.db_nuke(config)
818 901 anydbmDBTestCase.setUp(self)
819 # open database for testing
820 os.makedirs(config.DATABASE + '/files')
821 self.db = mysql.Database(config, 'admin')
822 setupSchema(self.db, 1, mysql)
823 902
824 def tearDown(self): 903 def tearDown(self):
825 from roundup.backends import mysql 904 from roundup.backends import mysql
826 self.db.close() 905 self.db.close()
827 mysql.db_nuke(config) 906 mysql.db_nuke(config)
907 nuke_database = tearDown
828 908
829 class mysqlReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): 909 class mysqlReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
830 def setUp(self): 910 def setUp(self):
831 from roundup.backends import mysql 911 from roundup.backends import mysql
832 mysql.db_nuke(config) 912 mysql.db_nuke(config)
842 mysql.db_nuke(config) 922 mysql.db_nuke(config)
843 923
844 class sqliteDBTestCase(anydbmDBTestCase): 924 class sqliteDBTestCase(anydbmDBTestCase):
845 def setUp(self): 925 def setUp(self):
846 from roundup.backends import sqlite 926 from roundup.backends import sqlite
847 # remove previous test, ignore errors 927 self.module = sqlite
848 if os.path.exists(config.DATABASE): 928 anydbmDBTestCase.setUp(self)
849 shutil.rmtree(config.DATABASE)
850 os.makedirs(config.DATABASE + '/files')
851 self.db = sqlite.Database(config, 'admin')
852 setupSchema(self.db, 1, sqlite)
853 929
854 class sqliteReadOnlyDBTestCase(anydbmReadOnlyDBTestCase): 930 class sqliteReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
855 def setUp(self): 931 def setUp(self):
856 from roundup.backends import sqlite 932 from roundup.backends import sqlite
857 # remove previous test, ignore errors 933 # remove previous test, ignore errors
868 class metakitDBTestCase(anydbmDBTestCase): 944 class metakitDBTestCase(anydbmDBTestCase):
869 def setUp(self): 945 def setUp(self):
870 from roundup.backends import metakit 946 from roundup.backends import metakit
871 import weakref 947 import weakref
872 metakit._instances = weakref.WeakValueDictionary() 948 metakit._instances = weakref.WeakValueDictionary()
873 # remove previous test, ignore errors 949 self.module = metakit
874 if os.path.exists(config.DATABASE): 950 anydbmDBTestCase.setUp(self)
875 shutil.rmtree(config.DATABASE)
876 os.makedirs(config.DATABASE + '/files')
877 self.db = metakit.Database(config, 'admin')
878 setupSchema(self.db, 1, metakit)
879 951
880 def testTransactions(self): 952 def testTransactions(self):
881 # remember the number of items we started 953 # remember the number of items we started
882 num_issues = len(self.db.issue.list()) 954 num_issues = len(self.db.issue.list())
883 self.db.issue.create(title="don't commit me!", status='1') 955 self.db.issue.create(title="don't commit me!", status='1')

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