home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_testAccess.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  5.1 KB  |  168 lines

  1. #
  2. # This assumes that you have MSAccess and DAO installed.
  3. #  You need to run makepy.py over "msaccess.tlb" and
  4. #  "dao3032.dll", and ensure the generated files are on the
  5. #  path.
  6.  
  7. # You can run this with no args, and a test database will be generated.
  8. # You can optionally pass a dbname on the command line, in which case it will be dumped.
  9.  
  10. import pythoncom
  11. from win32com.client import gencache, constants, Dispatch
  12. import win32api
  13. import os, sys
  14.  
  15. def CreateTestAccessDatabase(dbname = None):
  16.     # Creates a test access database - returns the filename.
  17.     if dbname is None:
  18.         dbname = os.path.join( win32api.GetTempPath(), "COMTestSuiteTempDatabase.mdb" )
  19.  
  20.     access = Dispatch("Access.Application")
  21.     dbEngine = access.DBEngine
  22.     workspace = dbEngine.Workspaces(0)
  23.  
  24.     try:
  25.         os.unlink(dbname)
  26.     except os.error:
  27.         print "WARNING - Unable to delete old test database - expect a COM exception RSN!"
  28.  
  29.     newdb = workspace.CreateDatabase( dbname, constants.dbLangGeneral, constants.dbEncrypt )
  30.  
  31.     # Create one test table.
  32.     table = newdb.CreateTableDef("Test Table 1")
  33.     table.Fields.Append( table.CreateField("First Name", constants.dbText ) )
  34.     table.Fields.Append( table.CreateField("Last Name", constants.dbText ) )
  35.  
  36.     index = table.CreateIndex("UniqueIndex")
  37.     index.Fields.Append( index.CreateField("First Name") )
  38.     index.Fields.Append( index.CreateField("Last Name") )
  39.     index.Unique = -1
  40.     table.Indexes.Append(index)
  41.  
  42.     newdb.TableDefs.Append( table )
  43.  
  44.     # Create a second test table.
  45.     table = newdb.CreateTableDef("Test Table 2")
  46.     table.Fields.Append( table.CreateField("First Name", constants.dbText ) )
  47.     table.Fields.Append( table.CreateField("Last Name", constants.dbText ) )
  48.  
  49.     newdb.TableDefs.Append( table )
  50.  
  51.     # Create a relationship between them
  52.     relation = newdb.CreateRelation("TestRelationship")
  53.     relation.Table = "Test Table 1"
  54.     relation.ForeignTable = "Test Table 2"
  55.  
  56.     field = relation.CreateField("First Name")
  57.     field.ForeignName = "First Name"
  58.     relation.Fields.Append( field )
  59.  
  60.     field = relation.CreateField("Last Name")
  61.     field.ForeignName = "Last Name"
  62.     relation.Fields.Append( field )
  63.  
  64.     relation.Attributes = constants.dbRelationDeleteCascade + constants.dbRelationUpdateCascade
  65.  
  66.     newdb.Relations.Append(relation)
  67.  
  68.     # Finally we can add some data to the table.
  69.     tab1 = newdb.OpenRecordset("Test Table 1")
  70.     tab1.AddNew()
  71.     tab1.Fields("First Name").Value = "Mark"
  72.     tab1.Fields("Last Name").Value = "Hammond"
  73.     tab1.Update()
  74.  
  75.     tab1.MoveFirst()
  76.     # We do a simple bookmark test which tests our optimized VT_SAFEARRAY|VT_UI1 support.
  77.     # The bookmark will be a buffer object - remember it for later.
  78.     bk = tab1.Bookmark
  79.  
  80.     # Add a second record.
  81.     tab1.AddNew()
  82.     tab1.Fields("First Name").Value = "Second"
  83.     tab1.Fields("Last Name").Value = "Person"
  84.     tab1.Update()
  85.  
  86.     # Reset the bookmark to the one we saved.
  87.     # But first check the test is actually doing something!
  88.     tab1.MoveLast()
  89.     if tab1.Fields("First Name").Value != "Second":
  90.         raise RuntimeError, "Unexpected record is last - makes bookmark test pointless!"
  91.  
  92.     tab1.Bookmark = bk
  93.     if tab1.Fields("First Name").Value != "Mark":
  94.         raise RuntimeError, "The bookmark did not reset the record pointer correctly"
  95.  
  96.     return dbname
  97.  
  98.  
  99. def DoDumpAccessInfo(dbname):
  100.     import daodump
  101.     a = forms = None
  102.     try:
  103.         sys.stderr.write("Creating Access Application...\n")
  104.         a=Dispatch("Access.Application")
  105.         print "Opening database %s" % dbname
  106.         a.OpenCurrentDatabase(dbname)
  107.         db = a.CurrentDb()
  108.         daodump.DumpDB(db,1)
  109.         forms = a.Forms
  110.         print "There are %d forms open." % (len(forms))
  111. # Uncommenting these lines means Access remains open.
  112. #        for form in forms:
  113. #            print " %s" % form.Name
  114.         reports = a.Reports
  115.         print "There are %d reports open" % (len(reports))
  116.     finally:
  117.         if not a is None: 
  118.             sys.stderr.write("Closing database\n")
  119.             try:
  120.                 a.CloseCurrentDatabase()
  121.             except pythoncom.com_error:
  122.                 pass
  123.  
  124. # Generate all the support we can.
  125. def GenerateSupport():
  126.     # dao
  127.     gencache.EnsureModule("{00025E01-0000-0000-C000-000000000046}", 0, 4, 0)
  128.     # Access
  129. #    gencache.EnsureModule("{4AFFC9A0-5F99-101B-AF4E-00AA003F0F07}", 0, 8, 0)
  130.     gencache.EnsureDispatch("Access.Application")
  131.  
  132. def DumpAccessInfo(dbname):
  133.     amod = gencache.GetModuleForProgID("Access.Application")
  134.     dmod = gencache.GetModuleForProgID("DAO.DBEngine.35")
  135.     if amod is None and dmod is None:
  136.         DoDumpAccessInfo(dbname)
  137.         # Now generate all the support we can.
  138.         GenerateSupport()
  139.     else:
  140.         sys.stderr.write("testAccess not doing dynamic test, as generated code already exists\n")
  141.     # Now a generated version.
  142.     DoDumpAccessInfo(dbname)
  143.  
  144. def test(dbname = None):
  145.     if dbname is None:
  146.         # We need makepy support to create a database (just for the constants!)
  147.         try:
  148.             GenerateSupport()
  149.         except pythoncom.com_error:
  150.             print "*** Can not import the MSAccess type libraries - tests skipped"
  151.             return
  152.         dbname = CreateTestAccessDatabase()
  153.         print "A test database at '%s' was created" % dbname
  154.  
  155.     DumpAccessInfo(dbname)
  156.  
  157. if __name__=='__main__':
  158.     import sys
  159.     from util import CheckClean
  160.     dbname = None
  161.     if len(sys.argv)>1:
  162.         dbname = sys.argv[1]
  163.  
  164.     test(dbname)
  165.     
  166.     CheckClean()
  167.  
  168.