1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
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 $
1919
2020import unittest , os , shutil , time
2121
@@ -51,6 +51,7 @@ def tearDown(self):
5151 self .db .close ()
5252 if os .path .exists ('_test_dir' ):
5353 shutil .rmtree ('_test_dir' )
54+ nuke_database = tearDown
5455
5556class config :
5657 DATABASE = '_test_dir'
@@ -78,12 +79,13 @@ class nodbconfig(config):
7879class anydbmDBTestCase (MyTestCase ):
7980 def setUp (self ):
8081 from roundup .backends import anydbm
82+ self .module = anydbm
8183 # remove previous test, ignore errors
8284 if os .path .exists (config .DATABASE ):
8385 shutil .rmtree (config .DATABASE )
8486 os .makedirs (config .DATABASE + '/files' )
85- self .db = anydbm .Database (config , 'admin' )
86- setupSchema (self .db , 1 , anydbm )
87+ self .db = self . module .Database (config , 'admin' )
88+ setupSchema (self .db , 1 , self . module )
8789
8890 #
8991 # schema mutation
@@ -740,6 +742,94 @@ def testFilteringIntervalSort(self):
740742# XXX add sorting tests for other types
741743# XXX test auditors and reactors
742744
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+
743833class anydbmReadOnlyDBTestCase (MyTestCase ):
744834 def setUp (self ):
745835 from roundup .backends import anydbm
@@ -766,12 +856,8 @@ def testExceptions(self):
766856class bsddbDBTestCase (anydbmDBTestCase ):
767857 def setUp (self ):
768858 from roundup .backends import bsddb
769- # remove previous test, ignore errors
770- if os .path .exists (config .DATABASE ):
771- shutil .rmtree (config .DATABASE )
772- os .makedirs (config .DATABASE + '/files' )
773- self .db = bsddb .Database (config , 'admin' )
774- setupSchema (self .db , 1 , bsddb )
859+ self .module = bsddb
860+ anydbmDBTestCase .setUp (self )
775861
776862class bsddbReadOnlyDBTestCase (anydbmReadOnlyDBTestCase ):
777863 def setUp (self ):
@@ -790,12 +876,8 @@ def setUp(self):
790876class bsddb3DBTestCase (anydbmDBTestCase ):
791877 def setUp (self ):
792878 from roundup .backends import bsddb3
793- # remove previous test, ignore errors
794- if os .path .exists (config .DATABASE ):
795- shutil .rmtree (config .DATABASE )
796- os .makedirs (config .DATABASE + '/files' )
797- self .db = bsddb3 .Database (config , 'admin' )
798- setupSchema (self .db , 1 , bsddb3 )
879+ self .module = bsddb3
880+ anydbmDBTestCase .setUp (self )
799881
800882class bsddb3ReadOnlyDBTestCase (anydbmReadOnlyDBTestCase ):
801883 def setUp (self ):
@@ -814,17 +896,15 @@ def setUp(self):
814896class mysqlDBTestCase (anydbmDBTestCase ):
815897 def setUp (self ):
816898 from roundup .backends import mysql
899+ self .module = mysql
817900 mysql .db_nuke (config )
818-
819- # open database for testing
820- os .makedirs (config .DATABASE + '/files' )
821- self .db = mysql .Database (config , 'admin' )
822- setupSchema (self .db , 1 , mysql )
901+ anydbmDBTestCase .setUp (self )
823902
824903 def tearDown (self ):
825904 from roundup .backends import mysql
826905 self .db .close ()
827906 mysql .db_nuke (config )
907+ nuke_database = tearDown
828908
829909class mysqlReadOnlyDBTestCase (anydbmReadOnlyDBTestCase ):
830910 def setUp (self ):
@@ -844,12 +924,8 @@ def tearDown(self):
844924class sqliteDBTestCase (anydbmDBTestCase ):
845925 def setUp (self ):
846926 from roundup .backends import sqlite
847- # remove previous test, ignore errors
848- if os .path .exists (config .DATABASE ):
849- shutil .rmtree (config .DATABASE )
850- os .makedirs (config .DATABASE + '/files' )
851- self .db = sqlite .Database (config , 'admin' )
852- setupSchema (self .db , 1 , sqlite )
927+ self .module = sqlite
928+ anydbmDBTestCase .setUp (self )
853929
854930class sqliteReadOnlyDBTestCase (anydbmReadOnlyDBTestCase ):
855931 def setUp (self ):
@@ -870,12 +946,8 @@ def setUp(self):
870946 from roundup .backends import metakit
871947 import weakref
872948 metakit ._instances = weakref .WeakValueDictionary ()
873- # remove previous test, ignore errors
874- if os .path .exists (config .DATABASE ):
875- shutil .rmtree (config .DATABASE )
876- os .makedirs (config .DATABASE + '/files' )
877- self .db = metakit .Database (config , 'admin' )
878- setupSchema (self .db , 1 , metakit )
949+ self .module = metakit
950+ anydbmDBTestCase .setUp (self )
879951
880952 def testTransactions (self ):
881953 # remember the number of items we started
0 commit comments