Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testAccess.py 5.6KB

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