Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: Refactor software tests into dedicated directory tests #659

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
24 changes: 15 additions & 9 deletions DEVELOP.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,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`_::

Expand Down
6 changes: 3 additions & 3 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
])
2 changes: 1 addition & 1 deletion bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# set -x

# Default variables.
CRATEDB_VERSION=${CRATEDB_VERSION:-5.8.3}
CRATEDB_VERSION=${CRATEDB_VERSION:-5.9.2}


function print_header() {
Expand Down
2 changes: 1 addition & 1 deletion docs/by-example/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/by-example/cursor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
69 changes: 0 additions & 69 deletions src/crate/client/test_util.py

This file was deleted.

71 changes: 71 additions & 0 deletions src/crate/testing/util.py
Original file line number Diff line number Diff line change
@@ -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)

Check warning on line 42 in src/crate/testing/util.py

View check run for this annotation

Codecov / codecov/patch

src/crate/testing/util.py#L42

Added line #L42 was not covered by tests

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

Check warning on line 57 in src/crate/testing/util.py

View check run for this annotation

Codecov / codecov/patch

src/crate/testing/util.py#L56-L57

Added lines #L56 - L57 were not covered by tests

@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

Check warning on line 69 in src/crate/testing/util.py

View check run for this annotation

Codecov / codecov/patch

src/crate/testing/util.py#L64-L69

Added lines #L64 - L69 were not covered by tests


class ExtraAssertions:
"""
Additional assert methods for unittest.
Expand Down
Empty file added tests/__init__.py
Empty file.
File renamed without changes.
Empty file added tests/client/__init__.py
Empty file.
Loading
Loading