0001 # 0002 # This assumes that you have MSAccess and DAO installed. 0003 # You need to run makepy.py over "msaccess.tlb" and 0004 # "dao3032.dll", and ensure the generated files are on the 0005 # path. 0006 0007 # You can run this with no args, and a test database will be generated. 0008 # You can optionally pass a dbname on the command line, in which case it will be dumped. 0009 0010 import pythoncom 0011 from win32com.client import gencache, constants, Dispatch 0012 import win32api 0013 import os, sys 0014 0015 def CreateTestAccessDatabase(dbname = None): 0016 # Creates a test access database - returns the filename. 0017 if dbname is None: 0018 dbname = os.path.join( win32api.GetTempPath(), "COMTestSuiteTempDatabase.mdb" ) 0019 0020 access = Dispatch("Access.Application") 0021 dbEngine = access.DBEngine 0022 workspace = dbEngine.Workspaces(0) 0023 0024 try: 0025 os.unlink(dbname) 0026 except os.error: 0027 print "WARNING - Unable to delete old test database - expect a COM exception RSN!" 0028 0029 newdb = workspace.CreateDatabase( dbname, constants.dbLangGeneral, constants.dbEncrypt ) 0030 0031 # Create one test table. 0032 table = newdb.CreateTableDef("Test Table 1") 0033 table.Fields.Append( table.CreateField("First Name", constants.dbText ) ) 0034 table.Fields.Append( table.CreateField("Last Name", constants.dbText ) ) 0035 0036 index = table.CreateIndex("UniqueIndex") 0037 index.Fields.Append( index.CreateField("First Name") ) 0038 index.Fields.Append( index.CreateField("Last Name") ) 0039 index.Unique = -1 0040 table.Indexes.Append(index) 0041 0042 newdb.TableDefs.Append( table ) 0043 0044 # Create a second test table. 0045 table = newdb.CreateTableDef("Test Table 2") 0046 table.Fields.Append( table.CreateField("First Name", constants.dbText ) ) 0047 table.Fields.Append( table.CreateField("Last Name", constants.dbText ) ) 0048 0049 newdb.TableDefs.Append( table ) 0050 0051 # Create a relationship between them 0052 relation = newdb.CreateRelation("TestRelationship") 0053 relation.Table = "Test Table 1" 0054 relation.ForeignTable = "Test Table 2" 0055 0056 field = relation.CreateField("First Name") 0057 field.ForeignName = "First Name" 0058 relation.Fields.Append( field ) 0059 0060 field = relation.CreateField("Last Name") 0061 field.ForeignName = "Last Name" 0062 relation.Fields.Append( field ) 0063 0064 relation.Attributes = constants.dbRelationDeleteCascade + constants.dbRelationUpdateCascade 0065 0066 newdb.Relations.Append(relation) 0067 0068 # Finally we can add some data to the table. 0069 tab1 = newdb.OpenRecordset("Test Table 1") 0070 tab1.AddNew() 0071 tab1.Fields("First Name").Value = "Mark" 0072 tab1.Fields("Last Name").Value = "Hammond" 0073 tab1.Update() 0074 0075 tab1.MoveFirst() 0076 # We do a simple bookmark test which tests our optimized VT_SAFEARRAY|VT_UI1 support. 0077 # The bookmark will be a buffer object - remember it for later. 0078 bk = tab1.Bookmark 0079 0080 # Add a second record. 0081 tab1.AddNew() 0082 tab1.Fields("First Name").Value = "Second" 0083 tab1.Fields("Last Name").Value = "Person" 0084 tab1.Update() 0085 0086 # Reset the bookmark to the one we saved. 0087 # But first check the test is actually doing something! 0088 tab1.MoveLast() 0089 if tab1.Fields("First Name").Value != "Second": 0090 raise RuntimeError, "Unexpected record is last - makes bookmark test pointless!" 0091 0092 tab1.Bookmark = bk 0093 if tab1.Bookmark != bk: 0094 raise RuntimeError, "The bookmark data is not the same" 0095 0096 if tab1.Fields("First Name").Value != "Mark": 0097 raise RuntimeError, "The bookmark did not reset the record pointer correctly" 0098 0099 return dbname 0100 0101 0102 def DoDumpAccessInfo(dbname): 0103 import daodump 0104 a = forms = None 0105 try: 0106 sys.stderr.write("Creating Access Application...\n") 0107 a=Dispatch("Access.Application") 0108 print "Opening database %s" % dbname 0109 a.OpenCurrentDatabase(dbname) 0110 db = a.CurrentDb() 0111 daodump.DumpDB(db,1) 0112 forms = a.Forms 0113 print "There are %d forms open." % (len(forms)) 0114 # Uncommenting these lines means Access remains open. 0115 # for form in forms: 0116 # print " %s" % form.Name 0117 reports = a.Reports 0118 print "There are %d reports open" % (len(reports)) 0119 finally: 0120 if not a is None: 0121 sys.stderr.write("Closing database\n") 0122 try: 0123 a.CloseCurrentDatabase() 0124 except pythoncom.com_error: 0125 pass 0126 0127 # Generate all the support we can. 0128 def GenerateSupport(): 0129 # dao 0130 gencache.EnsureModule("{00025E01-0000-0000-C000-000000000046}", 0, 4, 0) 0131 # Access 0132 # gencache.EnsureModule("{4AFFC9A0-5F99-101B-AF4E-00AA003F0F07}", 0, 8, 0) 0133 gencache.EnsureDispatch("Access.Application") 0134 0135 def DumpAccessInfo(dbname): 0136 amod = gencache.GetModuleForProgID("Access.Application") 0137 dmod = gencache.GetModuleForProgID("DAO.DBEngine.35") 0138 if amod is None and dmod is None: 0139 DoDumpAccessInfo(dbname) 0140 # Now generate all the support we can. 0141 GenerateSupport() 0142 else: 0143 sys.stderr.write("testAccess not doing dynamic test, as generated code already exists\n") 0144 # Now a generated version. 0145 DoDumpAccessInfo(dbname) 0146 0147 def test(dbname = None): 0148 if dbname is None: 0149 # We need makepy support to create a database (just for the constants!) 0150 try: 0151 GenerateSupport() 0152 except pythoncom.com_error: 0153 print "*** Can not import the MSAccess type libraries - tests skipped" 0154 return 0155 dbname = CreateTestAccessDatabase() 0156 print "A test database at '%s' was created" % dbname 0157 0158 DumpAccessInfo(dbname) 0159 0160 if __name__=='__main__': 0161 import sys 0162 from util import CheckClean 0163 dbname = None 0164 if len(sys.argv)>1: 0165 dbname = sys.argv[1] 0166 0167 test(dbname) 0168 0169 CheckClean() 0170
Generated by PyXR 0.9.4