From de33b1492914a0a93902205ca6776f58a736039c Mon Sep 17 00:00:00 2001 From: Maximiliano Bertacchini Date: Wed, 21 Aug 2024 21:32:21 +0000 Subject: [PATCH 1/3] Port test runner to pytest --- CHANGELOG.rst | 2 +- Makefile | 2 +- README.rst | 2 +- pyproject.toml | 6 ++++++ requirements-dev.txt | 2 ++ tox.ini | 2 +- 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ca608a9..06f16f6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,6 @@ Version Next - * ... + * Port test runner to pytest. Version 0.39 diff --git a/Makefile b/Makefile index 991e524..b3f5b84 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ lint: env/.done .PHONY: test test: env/.done - env/bin/python setup.py test + env/bin/pytest .PHONY: tox tox: env/bin/tox diff --git a/README.rst b/README.rst index fd793ac..f5ea5ce 100644 --- a/README.rst +++ b/README.rst @@ -152,7 +152,7 @@ Development To run a single test module invoke:: - python setup.py test --test-suite acceptable.tests.test_module + env/bin/pytest acceptable/tests/test_module.py or:: diff --git a/pyproject.toml b/pyproject.toml index b58143b..623ea06 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,3 +5,9 @@ skip-magic-trailing-comma = true [tool.isort] profile = "black" skip = ["env", ".tox"] + +[tool.pytest.ini_options] +addopts = "--cov=acceptable" + +[tool.coverage.report] +omit = ["acceptable/tests/*"] diff --git a/requirements-dev.txt b/requirements-dev.txt index 38ba335..6cede52 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,6 +3,8 @@ coveralls flake8 fixtures isort +pytest +pytest-cov testscenarios testtools responses diff --git a/tox.ini b/tox.ini index 0d76291..79c0e6f 100644 --- a/tox.ini +++ b/tox.ini @@ -15,7 +15,7 @@ extras = flask django commands = - coverage run --source acceptable --omit "acceptable/tests/*" setup.py test {posargs} + pytest {posargs} passenv = TRAVIS TRAVIS_BRANCH From 94d9fff30d266efae9ce515585ffc6f445764ae3 Mon Sep 17 00:00:00 2001 From: Maximiliano Bertacchini Date: Wed, 21 Aug 2024 21:35:00 +0000 Subject: [PATCH 2/3] Fix issue with Werkezeug 3 related to deprecated request.charset --- CHANGELOG.rst | 1 + acceptable/_validation.py | 3 ++- acceptable/tests/test_service.py | 3 ++- acceptable/tests/test_validation.py | 3 ++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 06f16f6..0559961 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,7 @@ Version Next * Port test runner to pytest. + * Fix compatibility issue with Werkezeug 3 related to deprecated ``request.charset``. Version 0.39 diff --git a/acceptable/_validation.py b/acceptable/_validation.py index 5efa375..51f9028 100644 --- a/acceptable/_validation.py +++ b/acceptable/_validation.py @@ -138,7 +138,8 @@ def wrapper(*args, **kwargs): # is not informative enough. if payload is None: try: - payload = json.loads(request.data.decode(request.charset)) + charset = request.mimetype_params.get("charset", "utf-8") + payload = json.loads(request.data.decode(charset)) except ValueError as e: raise DataValidationError( ["Error decoding JSON request body: %s" % str(e)] diff --git a/acceptable/tests/test_service.py b/acceptable/tests/test_service.py index 244369e..85ccd24 100644 --- a/acceptable/tests/test_service.py +++ b/acceptable/tests/test_service.py @@ -434,7 +434,8 @@ def match(self, response): return mismatch data = response.data if self._decode: - data = data.decode(response.charset) + charset = response.mimetype_params.get("charset", "utf-8") + data = data.decode(charset) return self.expected_content.match(data) def __str__(self): diff --git a/acceptable/tests/test_validation.py b/acceptable/tests/test_validation.py index 6e9f7d6..4f756cd 100644 --- a/acceptable/tests/test_validation.py +++ b/acceptable/tests/test_validation.py @@ -153,7 +153,8 @@ def view(): self.assertRaises(AssertionError, app.post_json, []) def assertResponseJsonEqual(self, response, expected_json): - data = json.loads(response.data.decode(response.charset)) + charset = response.mimetype_params.get("charset", "utf-8") + data = json.loads(response.data.decode(charset)) self.assertEqual(expected_json, data) def test_passes_on_good_payload_single_return_parameter(self): From eff8eab9404c6fbaa27f61ec6d490e06b54c67a9 Mon Sep 17 00:00:00 2001 From: Maximiliano Bertacchini Date: Wed, 21 Aug 2024 21:41:15 +0000 Subject: [PATCH 3/3] Make lint happy --- acceptable/openapi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/acceptable/openapi.py b/acceptable/openapi.py index 866528a..3296da3 100644 --- a/acceptable/openapi.py +++ b/acceptable/openapi.py @@ -99,7 +99,7 @@ def _to_dict(source: Any): return source._to_dict() # noqa elif isinstance(source, dict): return {key: _to_dict(value) for key, value in source.items()} - elif type(source) == list: + elif type(source) is list: return [_to_dict(value) for value in source] elif hasattr(source, "__dict__"): return {key: _to_dict(value) for key, value in source.__dict__.items()}