Skip to content

Commit

Permalink
use argparse instead of optparse (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
keitherskine authored Aug 7, 2022
1 parent c717c78 commit 2071e94
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 125 deletions.
8 changes: 4 additions & 4 deletions tests3/accesstests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python

usage="""\
usage: %prog [options] filename
%(prog)s [options] filename
Unit tests for Microsoft Access
Expand Down Expand Up @@ -591,9 +591,9 @@ def test_autocommit(self):
def main():
from argparse import ArgumentParser
parser = ArgumentParser(usage=usage)
parser.add_argument("-v", "--verbose", default=0, action="count", help="Increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="Print debugging items")
parser.add_argument("-t", "--test", help="Run only the named test")
parser.add_argument("-v", "--verbose", action="count", default=0, help="increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="print debugging items")
parser.add_argument("-t", "--test", help="run only the named test")
parser.add_argument('type', choices=['accdb', 'mdb'], help='Which type of file to test')

args = parser.parse_args()
Expand Down
18 changes: 10 additions & 8 deletions tests3/dbapitests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ def main():
add_to_path()
import pyodbc

from optparse import OptionParser
parser = OptionParser(usage="usage: %prog [options] connection_string")
parser.add_option("-v", "--verbose", action="count", help="Increment test verbosity (can be used multiple times)")
parser.add_option("-d", "--debug", action="store_true", default=False, help="Print debugging items")
from argparse import ArgumentParser
parser = ArgumentParser(usage="%(prog)s [options] connection_string")
parser.add_argument("-v", "--verbose", action="count", help="increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="print debugging items")
parser.add_argument("conn_str", nargs="*", help="database connection string")

(options, args) = parser.parse_args()
if len(args) > 1:
args = parser.parse_args()

if len(args.conn_str) > 1:
parser.error('Only one argument is allowed. Do you need quotes around the connection string?')

if not args:
Expand All @@ -23,7 +25,7 @@ def main():
parser.print_help()
raise SystemExit()
else:
connection_string = args[0]
connection_string = args.conn_str[0]

class test_pyodbc(dbapi20.DatabaseAPI20Test):
driver = pyodbc
Expand All @@ -35,7 +37,7 @@ def test_setoutputsize(self): pass
def test_ExceptionsAsConnectionAttributes(self): pass

suite = unittest.makeSuite(test_pyodbc, 'test')
testRunner = unittest.TextTestRunner(verbosity=(options.verbose > 1) and 9 or 0)
testRunner = unittest.TextTestRunner(verbosity=(args.verbose > 1) and 9 or 0)
result = testRunner.run(suite)

return result
Expand Down
21 changes: 11 additions & 10 deletions tests3/exceltests.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ def test_tables(self):


def main():
from optparse import OptionParser
parser = OptionParser() #usage=usage)
parser.add_option("-v", "--verbose", action="count", help="Increment test verbosity (can be used multiple times)")
parser.add_option("-d", "--debug", action="store_true", default=False, help="Print debugging items")
parser.add_option("-t", "--test", help="Run only the named test")
from argparse import ArgumentParser
parser = ArgumentParser() #usage=usage)
parser.add_argument("-v", "--verbose", action="count", help="increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="print debugging items")
parser.add_argument("-t", "--test", help="run only the named test")
parser.add_argument("conn_str", nargs="*", help="connection string for Excel")

(options, args) = parser.parse_args()
args = parser.parse_args()

if args:
if args.conn_str:
parser.error('no arguments expected')

global CNXNSTRING
Expand All @@ -122,14 +123,14 @@ def main():
assert os.path.exists(filename)
CNXNSTRING = 'Driver={Microsoft Excel Driver (*.xls)};DBQ=%s;READONLY=FALSE' % filename

if options.verbose:
if args.verbose:
cnxn = pyodbc.connect(CNXNSTRING, autocommit=True)
print_library_info(cnxn)
cnxn.close()

suite = load_tests(ExcelTestCase, options.test)
suite = load_tests(ExcelTestCase, args.test)

testRunner = unittest.TextTestRunner(verbosity=options.verbose)
testRunner = unittest.TextTestRunner(verbosity=args.verbose)
result = testRunner.run(suite)

return result
Expand Down
27 changes: 14 additions & 13 deletions tests3/informixtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: latin-1 -*-

usage = """\
usage: %prog [options] connection_string
%(prog)s [options] connection_string
Unit tests for Informix DB. To use, pass a connection string as the parameter.
The tests will create and drop tables t1 and t2 as necessary.
Expand Down Expand Up @@ -1221,34 +1221,35 @@ def test_drivers(self):


def main():
from optparse import OptionParser
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose", action="count", help="Increment test verbosity (can be used multiple times)")
parser.add_option("-d", "--debug", action="store_true", default=False, help="Print debugging items")
parser.add_option("-t", "--test", help="Run only the named test")
from argparse import ArgumentParser
parser = ArgumentParser(usage=usage)
parser.add_argument("-v", "--verbose", action="count", help="increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="print debugging items")
parser.add_argument("-t", "--test", help="run only the named test")
parser.add_argument("conn_str", nargs="*", help="connection string for Informix")

(options, args) = parser.parse_args()
args = parser.parse_args()

if len(args) > 1:
if len(args.conn_str) > 1:
parser.error('Only one argument is allowed. Do you need quotes around the connection string?')

if not args:
if not args.conn_str:
connection_string = load_setup_connection_string('informixtests')

if not connection_string:
parser.print_help()
raise SystemExit()
else:
connection_string = args[0]
connection_string = args.conn_str[0]

if options.verbose:
if args.verbose:
cnxn = pyodbc.connect(connection_string)
print_library_info(cnxn)
cnxn.close()

suite = load_tests(InformixTestCase, options.test, connection_string)
suite = load_tests(InformixTestCase, args.test, connection_string)

testRunner = unittest.TextTestRunner(verbosity=options.verbose)
testRunner = unittest.TextTestRunner(verbosity=args.verbose)
result = testRunner.run(suite)

return result
Expand Down
27 changes: 14 additions & 13 deletions tests3/mysqltests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

usage = """\
usage: %prog [options] connection_string
%(prog)s [options] connection_string
Unit tests for MySQL. To use, pass a connection string as the parameter.
The tests will create and drop tables t1 and t2 as necessary.
Expand Down Expand Up @@ -747,18 +747,19 @@ def test_emoticons_as_literal(self):
self.assertEqual(result, v)

def main():
from optparse import OptionParser
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose", action="count", default=0, help="Increment test verbosity (can be used multiple times)")
parser.add_option("-d", "--debug", action="store_true", default=False, help="Print debugging items")
parser.add_option("-t", "--test", help="Run only the named test")
from argparse import ArgumentParser
parser = ArgumentParser(usage=usage)
parser.add_argument("-v", "--verbose", action="count", default=0, help="increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="print debugging items")
parser.add_argument("-t", "--test", help="run only the named test")
parser.add_argument("conn_str", nargs="*", help="connection string for MySQL")

(options, args) = parser.parse_args()
args = parser.parse_args()

if len(args) > 1:
if len(args.conn_str) > 1:
parser.error('Only one argument is allowed. Do you need quotes around the connection string?')

if not args:
if not args.conn_str:
filename = basename(sys.argv[0])
assert filename.endswith('.py')
connection_string = load_setup_connection_string(filename[:-3])
Expand All @@ -767,16 +768,16 @@ def main():
parser.print_help()
raise SystemExit()
else:
connection_string = args[0]
connection_string = args.conn_str[0]

if options.verbose:
if args.verbose:
cnxn = pyodbc.connect(connection_string)
print_library_info(cnxn)
cnxn.close()

suite = load_tests(MySqlTestCase, options.test, connection_string)
suite = load_tests(MySqlTestCase, args.test, connection_string)

testRunner = unittest.TextTestRunner(verbosity=options.verbose)
testRunner = unittest.TextTestRunner(verbosity=args.verbose)
result = testRunner.run(suite)

return result
Expand Down
39 changes: 20 additions & 19 deletions tests3/pgtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

usage = """\
usage: %prog [options] connection_string
%(prog)s [options] connection_string
Unit tests for PostgreSQL. To use, pass a connection string as the parameter.
The tests will create and drop tables t1 and t2 as necessary.
Expand Down Expand Up @@ -707,46 +707,47 @@ def convert(value):
self.assertEqual(value, '123.45')

def main():
from optparse import OptionParser
parser = OptionParser(usage="usage: %prog [options] connection_string")
parser.add_option("-v", "--verbose", default=0, action="count", help="Increment test verbosity (can be used multiple times)")
parser.add_option("-d", "--debug", action="store_true", default=False, help="Print debugging items")
parser.add_option("-t", "--test", help="Run only the named test")
parser.add_option('-a', '--ansi', help='ANSI only', default=False, action='store_true')
from argparse import ArgumentParser
parser = ArgumentParser(usage=usage)
parser.add_argument("-v", "--verbose", action="count", default=0, help="increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="print debugging items")
parser.add_argument("-t", "--test", help="run only the named test")
parser.add_argument("-a", "--ansi", help="ANSI only", default=False, action="store_true")
parser.add_argument("conn_str", nargs="*", help="connection string for PostgreSQL")

(options, args) = parser.parse_args()
args = parser.parse_args()

if len(args) > 1:
if len(args.conn_str) > 1:
parser.error('Only one argument is allowed. Do you need quotes around the connection string?')

if not args:
if not args.conn_str:
connection_string = load_setup_connection_string('pgtests')

if not connection_string:
parser.print_help()
raise SystemExit()
else:
connection_string = args[0]
connection_string = args.conn_str[0]

if options.verbose:
cnxn = pyodbc.connect(connection_string, ansi=options.ansi)
if args.verbose:
cnxn = pyodbc.connect(connection_string, ansi=args.ansi)
print_library_info(cnxn)
cnxn.close()

if options.test:
if args.test:
# Run a single test
if not options.test.startswith('test_'):
options.test = 'test_%s' % (options.test)
if not args.test.startswith('test_'):
args.test = 'test_%s' % (args.test)

s = unittest.TestSuite([ PGTestCase(connection_string, options.ansi, options.test) ])
s = unittest.TestSuite([ PGTestCase(connection_string, args.ansi, args.test) ])
else:
# Run all tests in the class

methods = [ m for m in dir(PGTestCase) if m.startswith('test_') ]
methods.sort()
s = unittest.TestSuite([ PGTestCase(connection_string, options.ansi, m) for m in methods ])
s = unittest.TestSuite([ PGTestCase(connection_string, args.ansi, m) for m in methods ])

testRunner = unittest.TextTestRunner(verbosity=options.verbose)
testRunner = unittest.TextTestRunner(verbosity=args.verbose)
result = testRunner.run(s)

return result
Expand Down
39 changes: 20 additions & 19 deletions tests3/sparktests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-

usage = """\
usage: %prog [options] connection_string
%(prog)s [options] connection_string
Unit tests for Apache Spark. To use, pass a connection string as the parameter.
The tests will create and drop tables t1 and t2 as necessary.
Expand Down Expand Up @@ -489,46 +489,47 @@ def test_emoticons_as_parameter(self):
self.assertEqual(result, v)

def main():
from optparse import OptionParser
parser = OptionParser(usage="usage: %prog [options] connection_string")
parser.add_option("-v", "--verbose", default=0, action="count", help="Increment test verbosity (can be used multiple times)")
parser.add_option("-d", "--debug", action="store_true", default=False, help="Print debugging items")
parser.add_option("-t", "--test", help="Run only the named test")
parser.add_option('-a', '--ansi', help='ANSI only', default=False, action='store_true')
from argparse import ArgumentParser
parser = ArgumentParser(usage=usage)
parser.add_argument("-v", "--verbose", action="count", default=0, help="increment test verbosity (can be used multiple times)")
parser.add_argument("-d", "--debug", action="store_true", default=False, help="print debugging items")
parser.add_argument("-t", "--test", help="run only the named test")
parser.add_argument("-a", "--ansi", help="ANSI only", default=False, action="store_true")
parser.add_argument("conn_str", nargs="*", help="connection string for Spark")

(options, args) = parser.parse_args()
args = parser.parse_args()

if len(args) > 1:
if len(args.conn_str) > 1:
parser.error('Only one argument is allowed. Do you need quotes around the connection string?')

if not args:
if not args.conn_str:
connection_string = load_setup_connection_string('sparktests')

if not connection_string:
parser.print_help()
raise SystemExit()
else:
connection_string = args[0]
connection_string = args.conn_str[0]

if options.verbose:
cnxn = pyodbc.connect(connection_string, ansi=options.ansi)
if args.verbose:
cnxn = pyodbc.connect(connection_string, ansi=args.ansi)
print_library_info(cnxn)
cnxn.close()

if options.test:
if args.test:
# Run a single test
if not options.test.startswith('test_'):
options.test = 'test_%s' % (options.test)
if not args.test.startswith('test_'):
args.test = 'test_%s' % (args.test)

s = unittest.TestSuite([ SparkTestCase(connection_string, options.ansi, options.test) ])
s = unittest.TestSuite([ SparkTestCase(connection_string, args.ansi, args.test) ])
else:
# Run all tests in the class

methods = [ m for m in dir(SparkTestCase) if m.startswith('test_') ]
methods.sort()
s = unittest.TestSuite([ SparkTestCase(connection_string, options.ansi, m) for m in methods ])
s = unittest.TestSuite([ SparkTestCase(connection_string, args.ansi, m) for m in methods ])

testRunner = unittest.TextTestRunner(verbosity=options.verbose)
testRunner = unittest.TextTestRunner(verbosity=args.verbose)
result = testRunner.run(s)

return result
Expand Down
Loading

0 comments on commit 2071e94

Please sign in to comment.