From 2b6b835da7aea6bc4c99eafe3548479124b89e22 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Thu, 31 Oct 2024 14:09:33 +0100 Subject: [PATCH] Testing: Refactor software tests into dedicated directory `tests` git mv src/crate/client/test* tests/client/ git mv src/crate/testing/test* tests/testing/ --- CHANGES.txt | 2 + DEVELOP.rst | 24 ++++--- bin/test | 6 +- docs/by-example/connection.rst | 2 +- docs/by-example/cursor.rst | 2 +- src/crate/client/test_util.py | 69 ------------------ src/crate/testing/util.py | 71 +++++++++++++++++++ tests/__init__.py | 0 .../data => tests/assets/import}/test_a.json | 0 .../assets}/mappings/locations.sql | 0 .../assets}/pki/cacert_invalid.pem | 0 .../assets}/pki/cacert_valid.pem | 0 .../assets}/pki/client_invalid.pem | 0 .../assets}/pki/client_valid.pem | 0 .../client => tests/assets}/pki/readme.rst | 0 .../assets}/pki/server_valid.pem | 0 .../assets}/settings/test_a.json | 0 tests/client/__init__.py | 0 .../test_support.py => tests/client/layer.py | 34 ++++----- .../testing => tests/client}/settings.py | 23 +++--- .../crate => tests}/client/test_connection.py | 6 +- {src/crate => tests}/client/test_cursor.py | 2 +- .../crate => tests}/client/test_exceptions.py | 0 {src/crate => tests}/client/test_http.py | 4 +- {src/crate => tests}/client/tests.py | 8 +-- tests/testing/__init__.py | 0 tests/testing/settings.py | 9 +++ {src/crate => tests}/testing/test_layer.py | 2 +- {src/crate => tests}/testing/tests.py | 0 tox.ini | 2 +- 30 files changed, 134 insertions(+), 132 deletions(-) delete mode 100644 src/crate/client/test_util.py create mode 100644 tests/__init__.py rename {src/crate/testing/testdata/data => tests/assets/import}/test_a.json (100%) rename {src/crate/testing/testdata => tests/assets}/mappings/locations.sql (100%) rename {src/crate/client => tests/assets}/pki/cacert_invalid.pem (100%) rename {src/crate/client => tests/assets}/pki/cacert_valid.pem (100%) rename {src/crate/client => tests/assets}/pki/client_invalid.pem (100%) rename {src/crate/client => tests/assets}/pki/client_valid.pem (100%) rename {src/crate/client => tests/assets}/pki/readme.rst (100%) rename {src/crate/client => tests/assets}/pki/server_valid.pem (100%) rename {src/crate/testing/testdata => tests/assets}/settings/test_a.json (100%) create mode 100644 tests/client/__init__.py rename src/crate/client/test_support.py => tests/client/layer.py (88%) rename {src/crate/testing => tests/client}/settings.py (77%) rename {src/crate => tests}/client/test_connection.py (96%) rename {src/crate => tests}/client/test_cursor.py (99%) rename {src/crate => tests}/client/test_exceptions.py (100%) rename {src/crate => tests}/client/test_http.py (99%) rename {src/crate => tests}/client/tests.py (85%) create mode 100644 tests/testing/__init__.py create mode 100644 tests/testing/settings.py rename {src/crate => tests}/testing/test_layer.py (99%) rename {src/crate => tests}/testing/tests.py (100%) diff --git a/CHANGES.txt b/CHANGES.txt index 4a0f0a48..4c71ea4a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -12,6 +12,8 @@ Unreleased "Threads may share the module, but not connections." - Added ``error_trace`` to string representation of an Error to relay server stacktraces into exception messages. +- Refactoring: The module namespace ``crate.client.test_util`` has been + renamed to ``crate.testing.util``. .. _Migrate from crate.client to sqlalchemy-cratedb: https://cratedb.com/docs/sqlalchemy-cratedb/migrate-from-crate-client.html .. _sqlalchemy-cratedb: https://pypi.org/project/sqlalchemy-cratedb/ diff --git a/DEVELOP.rst b/DEVELOP.rst index 41373f18..b523a4bf 100644 --- a/DEVELOP.rst +++ b/DEVELOP.rst @@ -26,34 +26,40 @@ see, for example, `useful command-line options for zope-testrunner`_. Run all tests:: - ./bin/test -vvvv + bin/test Run specific tests:: - ./bin/test -vvvv -t test_score + # Select modules. + bin/test -t test_cursor + bin/test -t client + bin/test -t testing + + # Select doctests. + bin/test -t http.rst Ignore specific test directories:: - ./bin/test -vvvv --ignore_dir=testing + bin/test --ignore_dir=testing The ``LayerTest`` test cases have quite some overhead. Omitting them will save a few cycles (~70 seconds runtime):: - ./bin/test -t '!LayerTest' + bin/test -t '!LayerTest' -Invoke all tests without integration tests (~15 seconds runtime):: +Invoke all tests without integration tests (~10 seconds runtime):: - ./bin/test --layer '!crate.testing.layer.crate' --test '!LayerTest' + bin/test --layer '!crate.testing.layer.crate' --test '!LayerTest' -Yet ~130 test cases, but only ~5 seconds runtime:: +Yet ~60 test cases, but only ~1 second runtime:: - ./bin/test --layer '!crate.testing.layer.crate' --test '!LayerTest' \ + bin/test --layer '!crate.testing.layer.crate' --test '!LayerTest' \ -t '!test_client_threaded' -t '!test_no_retry_on_read_timeout' \ -t '!test_wait_for_http' -t '!test_table_clustered_by' To inspect the whole list of test cases, run:: - ./bin/test --list-tests + bin/test --list-tests You can run the tests against multiple Python interpreters with `tox`_:: diff --git a/bin/test b/bin/test index 05407417..749ec64b 100755 --- a/bin/test +++ b/bin/test @@ -12,6 +12,6 @@ sys.argv[0] = os.path.abspath(sys.argv[0]) if __name__ == '__main__': zope.testrunner.run([ - '-vvv', '--auto-color', - '--test-path', join(base, 'src')], - ) + '-vvvv', '--auto-color', + '--path', join(base, 'tests'), + ]) diff --git a/docs/by-example/connection.rst b/docs/by-example/connection.rst index 4b89db7d..108166a3 100644 --- a/docs/by-example/connection.rst +++ b/docs/by-example/connection.rst @@ -21,7 +21,7 @@ connect() This section sets up a connection object, and inspects some of its attributes. >>> from crate.client import connect - >>> from crate.client.test_util import ClientMocked + >>> from crate.testing.util import ClientMocked >>> connection = connect(client=ClientMocked()) >>> connection.lowest_server_version.version diff --git a/docs/by-example/cursor.rst b/docs/by-example/cursor.rst index 7fc7da7d..c649ee8c 100644 --- a/docs/by-example/cursor.rst +++ b/docs/by-example/cursor.rst @@ -23,7 +23,7 @@ up the response for subsequent cursor operations. >>> from crate.client import connect >>> from crate.client.converter import DefaultTypeConverter >>> from crate.client.cursor import Cursor - >>> from crate.client.test_util import ClientMocked + >>> from crate.testing.util import ClientMocked >>> connection = connect(client=ClientMocked()) >>> cursor = connection.cursor() diff --git a/src/crate/client/test_util.py b/src/crate/client/test_util.py deleted file mode 100644 index 823a44e3..00000000 --- a/src/crate/client/test_util.py +++ /dev/null @@ -1,69 +0,0 @@ -# -*- coding: utf-8; -*- -# -# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor -# license agreements. See the NOTICE file distributed with this work for -# additional information regarding copyright ownership. Crate licenses -# this file to you under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. You may -# obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# -# However, if you have executed another commercial license agreement -# with Crate these terms will supersede the license and you may use the -# software solely pursuant to the terms of the relevant commercial agreement. -import unittest - - -class ClientMocked(object): - - active_servers = ["http://localhost:4200"] - - def __init__(self): - self.response = {} - self._server_infos = ("http://localhost:4200", "my server", "2.0.0") - - def sql(self, stmt=None, parameters=None, bulk_parameters=None): - return self.response - - def server_infos(self, server): - return self._server_infos - - def set_next_response(self, response): - self.response = response - - def set_next_server_infos(self, server, server_name, version): - self._server_infos = (server, server_name, version) - - def close(self): - pass - - -class ParametrizedTestCase(unittest.TestCase): - """ - TestCase classes that want to be parametrized should - inherit from this class. - - https://eli.thegreenplace.net/2011/08/02/python-unit-testing-parametrized-test-cases - """ - def __init__(self, methodName="runTest", param=None): - super(ParametrizedTestCase, self).__init__(methodName) - self.param = param - - @staticmethod - def parametrize(testcase_klass, param=None): - """ Create a suite containing all tests taken from the given - subclass, passing them the parameter 'param'. - """ - testloader = unittest.TestLoader() - testnames = testloader.getTestCaseNames(testcase_klass) - suite = unittest.TestSuite() - for name in testnames: - suite.addTest(testcase_klass(name, param=param)) - return suite diff --git a/src/crate/testing/util.py b/src/crate/testing/util.py index 3e9885d6..54f9098c 100644 --- a/src/crate/testing/util.py +++ b/src/crate/testing/util.py @@ -1,3 +1,74 @@ +# -*- coding: utf-8; -*- +# +# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. Crate licenses +# this file to you under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. You may +# obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# However, if you have executed another commercial license agreement +# with Crate these terms will supersede the license and you may use the +# software solely pursuant to the terms of the relevant commercial agreement. +import unittest + + +class ClientMocked(object): + + active_servers = ["http://localhost:4200"] + + def __init__(self): + self.response = {} + self._server_infos = ("http://localhost:4200", "my server", "2.0.0") + + def sql(self, stmt=None, parameters=None, bulk_parameters=None): + return self.response + + def server_infos(self, server): + return self._server_infos + + def set_next_response(self, response): + self.response = response + + def set_next_server_infos(self, server, server_name, version): + self._server_infos = (server, server_name, version) + + def close(self): + pass + + +class ParametrizedTestCase(unittest.TestCase): + """ + TestCase classes that want to be parametrized should + inherit from this class. + + https://eli.thegreenplace.net/2011/08/02/python-unit-testing-parametrized-test-cases + """ + def __init__(self, methodName="runTest", param=None): + super(ParametrizedTestCase, self).__init__(methodName) + self.param = param + + @staticmethod + def parametrize(testcase_klass, param=None): + """ Create a suite containing all tests taken from the given + subclass, passing them the parameter 'param'. + """ + testloader = unittest.TestLoader() + testnames = testloader.getTestCaseNames(testcase_klass) + suite = unittest.TestSuite() + for name in testnames: + suite.addTest(testcase_klass(name, param=param)) + return suite + + class ExtraAssertions: """ Additional assert methods for unittest. diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/crate/testing/testdata/data/test_a.json b/tests/assets/import/test_a.json similarity index 100% rename from src/crate/testing/testdata/data/test_a.json rename to tests/assets/import/test_a.json diff --git a/src/crate/testing/testdata/mappings/locations.sql b/tests/assets/mappings/locations.sql similarity index 100% rename from src/crate/testing/testdata/mappings/locations.sql rename to tests/assets/mappings/locations.sql diff --git a/src/crate/client/pki/cacert_invalid.pem b/tests/assets/pki/cacert_invalid.pem similarity index 100% rename from src/crate/client/pki/cacert_invalid.pem rename to tests/assets/pki/cacert_invalid.pem diff --git a/src/crate/client/pki/cacert_valid.pem b/tests/assets/pki/cacert_valid.pem similarity index 100% rename from src/crate/client/pki/cacert_valid.pem rename to tests/assets/pki/cacert_valid.pem diff --git a/src/crate/client/pki/client_invalid.pem b/tests/assets/pki/client_invalid.pem similarity index 100% rename from src/crate/client/pki/client_invalid.pem rename to tests/assets/pki/client_invalid.pem diff --git a/src/crate/client/pki/client_valid.pem b/tests/assets/pki/client_valid.pem similarity index 100% rename from src/crate/client/pki/client_valid.pem rename to tests/assets/pki/client_valid.pem diff --git a/src/crate/client/pki/readme.rst b/tests/assets/pki/readme.rst similarity index 100% rename from src/crate/client/pki/readme.rst rename to tests/assets/pki/readme.rst diff --git a/src/crate/client/pki/server_valid.pem b/tests/assets/pki/server_valid.pem similarity index 100% rename from src/crate/client/pki/server_valid.pem rename to tests/assets/pki/server_valid.pem diff --git a/src/crate/testing/testdata/settings/test_a.json b/tests/assets/settings/test_a.json similarity index 100% rename from src/crate/testing/testdata/settings/test_a.json rename to tests/assets/settings/test_a.json diff --git a/tests/client/__init__.py b/tests/client/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/crate/client/test_support.py b/tests/client/layer.py similarity index 88% rename from src/crate/client/test_support.py rename to tests/client/layer.py index f9d5b7ff..b2d521e7 100644 --- a/src/crate/client/test_support.py +++ b/tests/client/layer.py @@ -34,11 +34,11 @@ import stopit -from crate.testing.layer import CrateLayer -from crate.testing.settings import \ - crate_host, crate_path, crate_port, \ - crate_transport_port, docs_path, localhost from crate.client import connect +from crate.testing.layer import CrateLayer +from .settings import \ + assets_path, crate_host, crate_path, crate_port, \ + crate_transport_port, localhost makeSuite = unittest.TestLoader().loadTestsFromTestCase @@ -104,7 +104,7 @@ def setUpCrateLayerBaseline(test): with connect(crate_host) as conn: cursor = conn.cursor() - with open(docs_path('testing/testdata/mappings/locations.sql')) as s: + with open(assets_path('mappings/locations.sql')) as s: stmt = s.read() cursor.execute(stmt) stmt = ("select count(*) from information_schema.tables " @@ -112,7 +112,7 @@ def setUpCrateLayerBaseline(test): cursor.execute(stmt) assert cursor.fetchall()[0][0] == 1 - data_path = docs_path('testing/testdata/data/test_a.json') + data_path = assets_path('import/test_a.json') # load testing data into crate cursor.execute("copy locations from ?", (data_path,)) # refresh location table so imported data is visible immediately @@ -145,10 +145,8 @@ def tearDownDropEntitiesBaseline(test): class HttpsTestServerLayer: PORT = 65534 HOST = "localhost" - CERT_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__), - "pki/server_valid.pem")) - CACERT_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__), - "pki/cacert_valid.pem")) + CERT_FILE = assets_path("pki/server_valid.pem") + CACERT_FILE = assets_path("pki/cacert_valid.pem") __name__ = "httpsserver" __bases__ = tuple() @@ -237,18 +235,10 @@ def setUpWithHttps(test): test.globs['pprint'] = pprint test.globs['print'] = cprint - test.globs['cacert_valid'] = os.path.abspath( - os.path.join(os.path.dirname(__file__), "pki/cacert_valid.pem") - ) - test.globs['cacert_invalid'] = os.path.abspath( - os.path.join(os.path.dirname(__file__), "pki/cacert_invalid.pem") - ) - test.globs['clientcert_valid'] = os.path.abspath( - os.path.join(os.path.dirname(__file__), "pki/client_valid.pem") - ) - test.globs['clientcert_invalid'] = os.path.abspath( - os.path.join(os.path.dirname(__file__), "pki/client_invalid.pem") - ) + test.globs['cacert_valid'] = assets_path("pki/cacert_valid.pem") + test.globs['cacert_invalid'] = assets_path("pki/cacert_invalid.pem") + test.globs['clientcert_valid'] = assets_path("pki/client_valid.pem") + test.globs['clientcert_invalid'] = assets_path("pki/client_invalid.pem") def _execute_statements(statements, on_error="ignore"): diff --git a/src/crate/testing/settings.py b/tests/client/settings.py similarity index 77% rename from src/crate/testing/settings.py rename to tests/client/settings.py index 34793cc6..228222fd 100644 --- a/src/crate/testing/settings.py +++ b/tests/client/settings.py @@ -21,27 +21,20 @@ # software solely pursuant to the terms of the relevant commercial agreement. from __future__ import absolute_import -import os +from pathlib import Path -def docs_path(*parts): - return os.path.abspath( - os.path.join( - os.path.dirname(os.path.dirname(__file__)), *parts - ) - ) +def assets_path(*parts) -> str: + return str((project_root() / "tests" / "assets").joinpath(*parts).absolute()) -def project_root(*parts): - return os.path.abspath( - os.path.join(docs_path("..", ".."), *parts) - ) +def crate_path() -> str: + return str(project_root() / "parts" / "crate") -def crate_path(*parts): - return os.path.abspath( - project_root("parts", "crate", *parts) - ) +def project_root() -> Path: + return Path(__file__).parent.parent.parent + crate_port = 44209 diff --git a/src/crate/client/test_connection.py b/tests/client/test_connection.py similarity index 96% rename from src/crate/client/test_connection.py rename to tests/client/test_connection.py index 93510864..5badfab2 100644 --- a/src/crate/client/test_connection.py +++ b/tests/client/test_connection.py @@ -2,12 +2,12 @@ from urllib3 import Timeout -from .connection import Connection -from .http import Client +from crate.client.connection import Connection +from crate.client.http import Client from crate.client import connect from unittest import TestCase -from ..testing.settings import crate_host +from .settings import crate_host class ConnectionTest(TestCase): diff --git a/src/crate/client/test_cursor.py b/tests/client/test_cursor.py similarity index 99% rename from src/crate/client/test_cursor.py rename to tests/client/test_cursor.py index 79e7ddd6..318c172b 100644 --- a/src/crate/client/test_cursor.py +++ b/tests/client/test_cursor.py @@ -33,7 +33,7 @@ from crate.client import connect from crate.client.converter import DataType, DefaultTypeConverter from crate.client.http import Client -from crate.client.test_util import ClientMocked +from crate.testing.util import ClientMocked class CursorTest(TestCase): diff --git a/src/crate/client/test_exceptions.py b/tests/client/test_exceptions.py similarity index 100% rename from src/crate/client/test_exceptions.py rename to tests/client/test_exceptions.py diff --git a/src/crate/client/test_http.py b/tests/client/test_http.py similarity index 99% rename from src/crate/client/test_http.py rename to tests/client/test_http.py index 76e6ade6..fd538fc1 100644 --- a/src/crate/client/test_http.py +++ b/tests/client/test_http.py @@ -43,8 +43,8 @@ import uuid import certifi -from .http import Client, CrateJsonEncoder, _get_socket_opts, _remove_certs_for_non_https -from .exceptions import ConnectionError, ProgrammingError, IntegrityError +from crate.client.http import Client, CrateJsonEncoder, _get_socket_opts, _remove_certs_for_non_https +from crate.client.exceptions import ConnectionError, ProgrammingError, IntegrityError REQUEST = 'crate.client.http.Server.request' CA_CERT_PATH = certifi.where() diff --git a/src/crate/client/tests.py b/tests/client/tests.py similarity index 85% rename from src/crate/client/tests.py rename to tests/client/tests.py index 476d37aa..10c2f03d 100644 --- a/src/crate/client/tests.py +++ b/tests/client/tests.py @@ -1,12 +1,12 @@ import doctest import unittest -from crate.client.test_connection import ConnectionTest -from crate.client.test_cursor import CursorTest -from crate.client.test_http import HttpClientTest, KeepAliveClientTest, ThreadSafeHttpClientTest, ParamsTest, \ +from .test_connection import ConnectionTest +from .test_cursor import CursorTest +from .test_http import HttpClientTest, KeepAliveClientTest, ThreadSafeHttpClientTest, ParamsTest, \ RetryOnTimeoutServerTest, RequestsCaBundleTest, TestUsernameSentAsHeader, TestCrateJsonEncoder, \ TestDefaultSchemaHeader -from crate.client.test_support import makeSuite, setUpWithHttps, HttpsTestServerLayer, setUpCrateLayerBaseline, \ +from .layer import makeSuite, setUpWithHttps, HttpsTestServerLayer, setUpCrateLayerBaseline, \ tearDownDropEntitiesBaseline, ensure_cratedb_layer diff --git a/tests/testing/__init__.py b/tests/testing/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/testing/settings.py b/tests/testing/settings.py new file mode 100644 index 00000000..eb99a055 --- /dev/null +++ b/tests/testing/settings.py @@ -0,0 +1,9 @@ +from pathlib import Path + + +def crate_path() -> str: + return str(project_root() / "parts" / "crate") + + +def project_root() -> Path: + return Path(__file__).parent.parent.parent diff --git a/src/crate/testing/test_layer.py b/tests/testing/test_layer.py similarity index 99% rename from src/crate/testing/test_layer.py rename to tests/testing/test_layer.py index aaeca336..38d53922 100644 --- a/src/crate/testing/test_layer.py +++ b/tests/testing/test_layer.py @@ -29,7 +29,7 @@ import urllib3 import crate -from .layer import CrateLayer, prepend_http, http_url_from_host_port, wait_for_http_url +from crate.testing.layer import CrateLayer, prepend_http, http_url_from_host_port, wait_for_http_url from .settings import crate_path diff --git a/src/crate/testing/tests.py b/tests/testing/tests.py similarity index 100% rename from src/crate/testing/tests.py rename to tests/testing/tests.py diff --git a/tox.ini b/tox.ini index 978bd90c..1ea931fa 100644 --- a/tox.ini +++ b/tox.ini @@ -11,4 +11,4 @@ deps = mock urllib3 commands = - zope-testrunner -c --test-path=src + zope-testrunner -c --path=tests