From 3e19409d6443c66a6a7d62f58b2bb4e8330e56c4 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 13 Sep 2021 14:16:26 +0300 Subject: [PATCH] bpo-45181: Simplify loading sqlite3 tests (GH-28304) Use unittest discover instead of manually enumerating all test modules and classes. Also add support for filtering them by pattern. --- .../test/{backup.py => test_backup.py} | 3 --- Lib/sqlite3/test/{dbapi.py => test_dbapi.py} | 25 +------------------ Lib/sqlite3/test/{dump.py => test_dump.py} | 13 +--------- .../test/{factory.py => test_factory.py} | 18 +------------ Lib/sqlite3/test/{hooks.py => test_hooks.py} | 18 ++----------- .../{regression.py => test_regression.py} | 14 +---------- .../{transactions.py => test_transactions.py} | 15 +---------- Lib/sqlite3/test/{types.py => test_types.py} | 19 +------------- ...userfunctions.py => test_userfunctions.py} | 19 +------------- Lib/test/test_sqlite.py | 16 ++++-------- 10 files changed, 14 insertions(+), 146 deletions(-) rename Lib/sqlite3/test/{backup.py => test_backup.py} (98%) rename Lib/sqlite3/test/{dbapi.py => test_dbapi.py} (98%) rename Lib/sqlite3/test/{dump.py => test_dump.py} (91%) rename Lib/sqlite3/test/{factory.py => test_factory.py} (96%) rename Lib/sqlite3/test/{hooks.py => test_hooks.py} (96%) rename Lib/sqlite3/test/{regression.py => test_regression.py} (98%) rename Lib/sqlite3/test/{transactions.py => test_transactions.py} (95%) rename Lib/sqlite3/test/{types.py => test_types.py} (97%) rename Lib/sqlite3/test/{userfunctions.py => test_userfunctions.py} (98%) diff --git a/Lib/sqlite3/test/backup.py b/Lib/sqlite3/test/test_backup.py similarity index 98% rename from Lib/sqlite3/test/backup.py rename to Lib/sqlite3/test/test_backup.py index 4e30594bec6..384dd9229ab 100644 --- a/Lib/sqlite3/test/backup.py +++ b/Lib/sqlite3/test/test_backup.py @@ -160,8 +160,5 @@ class BackupTests(unittest.TestCase): self.verify_backup(bck) -def suite(): - return unittest.TestLoader().loadTestsFromTestCase(BackupTests) - if __name__ == "__main__": unittest.main() diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/test_dbapi.py similarity index 98% rename from Lib/sqlite3/test/dbapi.py rename to Lib/sqlite3/test/test_dbapi.py index 89f773daf24..34cadeebfa2 100644 --- a/Lib/sqlite3/test/dbapi.py +++ b/Lib/sqlite3/test/test_dbapi.py @@ -1156,28 +1156,5 @@ class MultiprocessTests(unittest.TestCase): self.assertEqual(proc.returncode, 0) -def suite(): - tests = [ - ClosedConTests, - ClosedCurTests, - ConnectionTests, - ConstructorTests, - CursorTests, - ExtensionTests, - ModuleTests, - MultiprocessTests, - OpenTests, - SqliteOnConflictTests, - ThreadTests, - UninitialisedConnectionTests, - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) - if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/sqlite3/test/dump.py b/Lib/sqlite3/test/test_dump.py similarity index 91% rename from Lib/sqlite3/test/dump.py rename to Lib/sqlite3/test/test_dump.py index 618a7fd31c1..1f14c620f0d 100644 --- a/Lib/sqlite3/test/dump.py +++ b/Lib/sqlite3/test/test_dump.py @@ -70,17 +70,6 @@ class DumpTests(unittest.TestCase): got = list(self.cx.iterdump()) self.assertEqual(expected, got) -def suite(): - tests = [ - DumpTests, - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/test_factory.py similarity index 96% rename from Lib/sqlite3/test/factory.py rename to Lib/sqlite3/test/test_factory.py index 9e7a7e28c6e..420855ba34b 100644 --- a/Lib/sqlite3/test/factory.py +++ b/Lib/sqlite3/test/test_factory.py @@ -305,22 +305,6 @@ class TextFactoryTestsWithEmbeddedZeroBytes(unittest.TestCase): def tearDown(self): self.con.close() -def suite(): - tests = [ - ConnectionFactoryTests, - CursorFactoryTests, - RowFactoryTests, - RowFactoryTestsBackwardsCompat, - TextFactoryTests, - TextFactoryTestsWithEmbeddedZeroBytes, - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/sqlite3/test/hooks.py b/Lib/sqlite3/test/test_hooks.py similarity index 96% rename from Lib/sqlite3/test/hooks.py rename to Lib/sqlite3/test/test_hooks.py index 43e3810d13d..bf454b2aa88 100644 --- a/Lib/sqlite3/test/hooks.py +++ b/Lib/sqlite3/test/test_hooks.py @@ -24,7 +24,7 @@ import unittest import sqlite3 as sqlite from test.support.os_helper import TESTFN, unlink -from .userfunctions import with_tracebacks +from .test_userfunctions import with_tracebacks class CollationTests(unittest.TestCase): def test_create_collation_not_string(self): @@ -290,19 +290,5 @@ class TraceCallbackTests(unittest.TestCase): self.assertEqual(traced_statements, queries) -def suite(): - tests = [ - CollationTests, - ProgressTests, - TraceCallbackTests, - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) - if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/test_regression.py similarity index 98% rename from Lib/sqlite3/test/regression.py rename to Lib/sqlite3/test/test_regression.py index 4a422c8d43d..f75e48f1560 100644 --- a/Lib/sqlite3/test/regression.py +++ b/Lib/sqlite3/test/test_regression.py @@ -436,17 +436,5 @@ class RegressionTests(unittest.TestCase): self.assertEqual(val, b'') -def suite(): - tests = [ - RegressionTests - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) - if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/sqlite3/test/transactions.py b/Lib/sqlite3/test/test_transactions.py similarity index 95% rename from Lib/sqlite3/test/transactions.py rename to Lib/sqlite3/test/test_transactions.py index 80284902a1a..8ea352b9ef0 100644 --- a/Lib/sqlite3/test/transactions.py +++ b/Lib/sqlite3/test/test_transactions.py @@ -195,19 +195,6 @@ class TransactionalDDL(unittest.TestCase): def tearDown(self): self.con.close() -def suite(): - tests = [ - SpecialCommandTests, - TransactionTests, - TransactionalDDL, - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/sqlite3/test/types.py b/Lib/sqlite3/test/test_types.py similarity index 97% rename from Lib/sqlite3/test/types.py rename to Lib/sqlite3/test/test_types.py index b8926ffee22..396f153a75f 100644 --- a/Lib/sqlite3/test/types.py +++ b/Lib/sqlite3/test/test_types.py @@ -530,23 +530,6 @@ class DateTimeTests(unittest.TestCase): ts2 = self.cur.fetchone()[0] self.assertEqual(ts, ts2) -def suite(): - tests = [ - BinaryConverterTests, - ColNamesTests, - CommonTableExpressionTests, - DateTimeTests, - DeclTypesTests, - ObjectAdaptationTests, - SqliteTypeTests, - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/sqlite3/test/userfunctions.py b/Lib/sqlite3/test/test_userfunctions.py similarity index 98% rename from Lib/sqlite3/test/userfunctions.py rename to Lib/sqlite3/test/test_userfunctions.py index b4d5181777e..ad408475b73 100644 --- a/Lib/sqlite3/test/userfunctions.py +++ b/Lib/sqlite3/test/test_userfunctions.py @@ -661,22 +661,5 @@ class AuthorizerLargeIntegerTests(AuthorizerTests): return sqlite.SQLITE_OK -def suite(): - tests = [ - AggregateTests, - AuthorizerIllegalTypeTests, - AuthorizerLargeIntegerTests, - AuthorizerRaiseExceptionTests, - AuthorizerTests, - FunctionTests, - ] - return unittest.TestSuite( - [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] - ) - -def test(): - runner = unittest.TextTestRunner() - runner.run(suite()) - if __name__ == "__main__": - test() + unittest.main() diff --git a/Lib/test/test_sqlite.py b/Lib/test/test_sqlite.py index 73002f228fa..9992a02e5d9 100644 --- a/Lib/test/test_sqlite.py +++ b/Lib/test/test_sqlite.py @@ -1,26 +1,20 @@ import test.support from test.support import import_helper +from test.support import load_package_tests # Skip test if _sqlite3 module not installed import_helper.import_module('_sqlite3') import unittest -import sqlite3 -from sqlite3.test import (dbapi, types, userfunctions, - factory, transactions, hooks, regression, - dump, backup) +import os +import sqlite3.test -def load_tests(*args): +def load_tests(loader, tests, pattern): if test.support.verbose: print("test_sqlite: testing with version", "{!r}, sqlite_version {!r}".format(sqlite3.version, sqlite3.sqlite_version)) - return unittest.TestSuite([dbapi.suite(), types.suite(), - userfunctions.suite(), - factory.suite(), transactions.suite(), - hooks.suite(), regression.suite(), - dump.suite(), - backup.suite()]) + return load_package_tests(os.path.dirname(sqlite3.test.__file__), loader, tests, pattern) if __name__ == "__main__": unittest.main()