adodbapitestconfig.py 7.2 KB

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