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