adodbapitestconfig.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Configure this to _YOUR_ environment in order to run the testcases.
  2. "testADOdbapiConfig.py v 2.6.2.B00"
  3. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  4. # #
  5. # # TESTERS:
  6. # #
  7. # # You will need to make numerous modifications to this file
  8. # # to adapt it to your own testing environment.
  9. # #
  10. # # Skip down to the next "# #" line --
  11. # # -- the things you need to change are below it.
  12. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  13. import platform
  14. import sys
  15. import random
  16. import is64bit
  17. import setuptestframework
  18. import tryconnection
  19. print("\nPython", sys.version)
  20. node = platform.node()
  21. try: print('node=%s, is64bit.os()= %s, is64bit.Python()= %s' % (node, is64bit.os(), is64bit.Python()))
  22. except: pass
  23. if '--help' in sys.argv:
  24. print("""Valid command-line switches are:
  25. --package - create a temporary test package, run 2to3 if needed.
  26. --all - run all possible tests
  27. --time - loop over time format tests (including mxdatetime if present)
  28. --nojet - do not test against an ACCESS database file
  29. --mssql - test against Microsoft SQL server
  30. --pg - test against PostgreSQL
  31. --mysql - test against MariaDB
  32. --remote= - test unsing remote server at= (experimental)
  33. """)
  34. exit()
  35. try:
  36. onWindows = bool(sys.getwindowsversion()) # seems to work on all versions of Python
  37. except:
  38. onWindows = False
  39. # create a random name for temporary table names
  40. _alphabet = "PYFGCRLAOEUIDHTNSQJKXBMWVZ" # why, yes, I do happen to use a dvorak keyboard
  41. tmp = ''.join([random.choice(_alphabet) for x in range(9)])
  42. mdb_name = 'xx_' + tmp + '.mdb' # generate a non-colliding name for the temporary .mdb
  43. testfolder = setuptestframework.maketemp()
  44. if '--package' in sys.argv:
  45. # create a new adodbapi module -- running 2to3 if needed.
  46. pth = setuptestframework.makeadopackage(testfolder)
  47. else:
  48. # use the adodbapi module in which this file appears
  49. pth = setuptestframework.find_ado_path()
  50. if pth not in sys.path:
  51. # look here _first_ to find modules
  52. sys.path.insert(1,pth)
  53. proxy_host = None
  54. for arg in sys.argv:
  55. if arg.startswith('--remote='):
  56. proxy_host = arg.split("=")[1]
  57. import adodbapi.remote as remote
  58. break
  59. # function to clean up the temporary folder -- calling program must run this function before exit.
  60. cleanup = setuptestframework.getcleanupfunction()
  61. try:
  62. import adodbapi # will (hopefully) be imported using the "pth" discovered above
  63. except SyntaxError:
  64. print('\n* * * Are you trying to run Python2 code using Python3? Re-run this test using the "--package" switch.')
  65. sys.exit(11)
  66. try:
  67. print(adodbapi.version) # show version
  68. except:
  69. print('"adodbapi.version" not present or not working.')
  70. print(__doc__)
  71. verbose = False
  72. for a in sys.argv:
  73. if a.startswith('--verbose'):
  74. arg = True
  75. try: arg = int(a.split("=")[1])
  76. except IndexError: pass
  77. adodbapi.adodbapi.verbose = arg
  78. verbose = arg
  79. doAllTests = '--all' in sys.argv
  80. doAccessTest = not ('--nojet' in sys.argv)
  81. doSqlServerTest = '--mssql' in sys.argv or doAllTests
  82. doMySqlTest = '--mysql' in sys.argv or doAllTests
  83. doPostgresTest = '--pg' in sys.argv or doAllTests
  84. iterateOverTimeTests = ('--time' in sys.argv or doAllTests) and onWindows
  85. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  86. # # start your environment setup here v v v
  87. SQL_HOST_NODE = 'testsql.2txt.us,1430'
  88. try: #If mx extensions are installed, use mxDateTime
  89. import mx.DateTime
  90. doMxDateTimeTest=True
  91. except:
  92. doMxDateTimeTest=False #Requires eGenixMXExtensions
  93. doTimeTest = True # obsolete python time format
  94. if doAccessTest:
  95. if proxy_host: # determine the (probably remote) database file folder
  96. c = {'macro_find_temp_test_path': ['mdb', mdb_name],
  97. 'proxy_host': proxy_host }
  98. else:
  99. c = {'mdb': setuptestframework.makemdb(testfolder, mdb_name)}
  100. # macro definition for keyword "provider" using macro "is64bit" -- see documentation
  101. # is64bit will return true for 64 bit versions of Python, so the macro will select the ACE provider
  102. # (If running a remote ADO service, this will test the 64-bitedness of the ADO server.)
  103. c['macro_is64bit'] = ['provider',
  104. "Microsoft.ACE.OLEDB.12.0", # 64 bit provider
  105. "Microsoft.Jet.OLEDB.4.0"] # 32 bit provider
  106. connStrAccess = "Provider=%(provider)s;Data Source=%(mdb)s" # ;Mode=ReadWrite;Persist Security Info=False;Jet OLEDB:Bypass UserInfo Validation=True"
  107. print(' ...Testing ACCESS connection to {} file...'.format(c.get('mdb', 'remote .mdb')))
  108. doAccessTest, connStrAccess, dbAccessconnect = tryconnection.try_connection(verbose, connStrAccess, 10, **c)
  109. if doSqlServerTest:
  110. c = {'host': SQL_HOST_NODE, # name of computer with SQL Server
  111. 'database': "adotest",
  112. 'user' : 'adotestuser', # None implies Windows security
  113. 'password' : "Sq1234567",
  114. # macro definition for keyword "security" using macro "auto_security"
  115. 'macro_auto_security' : 'security',
  116. 'provider' : 'MSOLEDBSQL; MARS Connection=True'
  117. }
  118. if proxy_host:
  119. c['proxy_host'] = proxy_host
  120. connStr = "Provider=%(provider)s; Initial Catalog=%(database)s; Data Source=%(host)s; %(security)s;"
  121. print(' ...Testing MS-SQL login to {}...'.format(c['host']))
  122. doSqlServerTest, connStrSQLServer, dbSqlServerconnect = tryconnection.try_connection(verbose, connStr, 30, **c)
  123. if doMySqlTest:
  124. c = {'host' : "testmysql.2txt.us",
  125. 'database' : 'adodbapitest',
  126. 'user' : 'adotest',
  127. 'password' : '12345678',
  128. 'port' : '3330', # note the nonstandard port for obfuscation
  129. 'driver' : "MySQL ODBC 5.1 Driver"} # or _driver="MySQL ODBC 3.51 Driver
  130. if proxy_host:
  131. c['proxy_host'] = proxy_host
  132. c['macro_is64bit'] = ['provider', 'Provider=MSDASQL;'] # turn on the 64 bit ODBC adapter only if needed
  133. cs = '%(provider)sDriver={%(driver)s};Server=%(host)s;Port=3330;' + \
  134. 'Database=%(database)s;user=%(user)s;password=%(password)s;Option=3;'
  135. print(' ...Testing MySql login to {}...'.format(c['host']))
  136. doMySqlTest, connStrMySql, dbMySqlconnect = tryconnection.try_connection(verbose, cs, 5, **c)
  137. if doPostgresTest:
  138. _computername = "testpg.2txt.us"
  139. _databasename='adotest'
  140. _username = 'adotestuser'
  141. _password = '12345678'
  142. kws = {'timeout': 4}
  143. kws['macro_is64bit'] = ['prov_drv',
  144. 'Provider=MSDASQL;Driver={PostgreSQL Unicode(x64)}',
  145. 'Driver=PostgreSQL Unicode']
  146. # get driver from http://www.postgresql.org/ftp/odbc/versions/
  147. # test using positional and keyword arguments (bad example for real code)
  148. if proxy_host:
  149. kws['proxy_host'] = proxy_host
  150. print(' ...Testing PostgreSQL login to {}...'.format(_computername))
  151. doPostgresTest, connStrPostgres, dbPostgresConnect = tryconnection.try_connection(verbose,
  152. '%(prov_drv)s;Server=%(host)s;Database=%(database)s;uid=%(user)s;pwd=%(password)s;port=5430;', # note nonstandard port
  153. _username, _password, _computername, _databasename, **kws)
  154. assert doAccessTest or doSqlServerTest or doMySqlTest or doPostgresTest, 'No database engine found for testing'