From e3fb6cebe6161462adc8e3425f706fab7eb49f35 Mon Sep 17 00:00:00 2001 From: Karel van Klink <karel.vanklink@geant.org> Date: Thu, 20 Jul 2023 12:27:48 +0200 Subject: [PATCH] pass mypy linting, and make flake8 stricter --- pyproject.toml | 1 - test/conftest.py | 7 ++++--- test/routes/__init__.py | 14 ++++++++------ test/routes/test_device.py | 5 +++-- test/routes/test_ip_trunk.py | 13 +++++++------ test/test_config.py | 4 ++-- tox.ini | 3 +-- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d5f9e2e..99627a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,6 @@ exclude = ''' [tool.mypy] exclude = [ "venv", - "test/*", "docs" ] ignore_missing_imports = true diff --git a/test/conftest.py b/test/conftest.py index 5083af8..bc4781a 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,6 +1,7 @@ import json import os import tempfile +from typing import Any, Generator import pytest from fastapi.testclient import TestClient @@ -11,13 +12,13 @@ TEST_CONFIG = {"collection-name": "kvklink.echo", "test-role": "kvklink.echo.ech @pytest.fixture -def config_data(): +def config_data() -> dict[str, str]: """Start the server with valid configuration data.""" return {"ansible_playbooks_root_dir": "/"} @pytest.fixture -def config_file(config_data): +def config_file(config_data: dict[str, str]) -> Generator[str, Any, None]: """Fixture that will yield a filename that contains a valid configuration. :return: Path to valid configuration file @@ -29,7 +30,7 @@ def config_file(config_data): @pytest.fixture -def client(config_file): +def client(config_file: str) -> Generator[TestClient, Any, None]: """Return a client that can be used to test the server.""" os.environ["SETTINGS_FILENAME"] = config_file app = lso.create_app() diff --git a/test/routes/__init__.py b/test/routes/__init__.py index b9b2d1b..b149057 100644 --- a/test/routes/__init__.py +++ b/test/routes/__init__.py @@ -1,13 +1,15 @@ from io import StringIO +from typing import Any TEST_CALLBACK_URL = "https://fqdn.abc.xyz/api/resume" -def test_ansible_runner_run(**kwargs): - class Runner: - def __init__(self): - self.status = "success" - self.rc = 0 - self.stdout = StringIO("some initial text data") +class Runner: + def __init__(self) -> None: + self.status = "success" + self.rc = 0 + self.stdout = StringIO("[{'step one': 'results'}, {'step two': 2}]") + +def test_ansible_runner_run(**kwargs: Any) -> Runner: return Runner() diff --git a/test/routes/test_device.py b/test/routes/test_device.py index e42172d..63fb7c7 100644 --- a/test/routes/test_device.py +++ b/test/routes/test_device.py @@ -3,13 +3,14 @@ from unittest.mock import patch import jsonschema import responses +from starlette.testclient import TestClient from lso.playbook import PlaybookLaunchResponse from test.routes import TEST_CALLBACK_URL, test_ansible_runner_run @responses.activate -def test_router_provisioning(client): +def test_router_provisioning(client: TestClient) -> None: responses.put(url=TEST_CALLBACK_URL, status=204) params = { @@ -43,7 +44,7 @@ def test_router_provisioning(client): # wait two seconds for the run thread to finish time.sleep(2) - jsonschema.validate(response, PlaybookLaunchResponse.schema()) + jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) responses.assert_call_count(TEST_CALLBACK_URL, 1) assert response["status"] == "ok" diff --git a/test/routes/test_ip_trunk.py b/test/routes/test_ip_trunk.py index bd44e1d..af00375 100644 --- a/test/routes/test_ip_trunk.py +++ b/test/routes/test_ip_trunk.py @@ -3,6 +3,7 @@ from unittest.mock import patch import jsonschema import responses +from starlette.testclient import TestClient from lso.playbook import PlaybookLaunchResponse from test.routes import TEST_CALLBACK_URL, test_ansible_runner_run @@ -91,7 +92,7 @@ _SUBSCRIPTION_OBJECT = { @responses.activate -def test_ip_trunk_provisioning(client): +def test_ip_trunk_provisioning(client: TestClient) -> None: responses.put(url=TEST_CALLBACK_URL, status=204) params = { @@ -109,14 +110,14 @@ def test_ip_trunk_provisioning(client): # wait a second for the run thread to finish time.sleep(1) - jsonschema.validate(response, PlaybookLaunchResponse.schema()) + jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) responses.assert_call_count(TEST_CALLBACK_URL, 1) assert response["status"] == "ok" @responses.activate -def test_ip_trunk_modification(client): +def test_ip_trunk_modification(client: TestClient) -> None: responses.put(url=TEST_CALLBACK_URL, status=204) params = { @@ -134,14 +135,14 @@ def test_ip_trunk_modification(client): # wait a second for the run thread to finish time.sleep(1) - jsonschema.validate(response, PlaybookLaunchResponse.schema()) + jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) responses.assert_call_count(TEST_CALLBACK_URL, 1) assert response["status"] == "ok" @responses.activate -def test_ip_trunk_deletion(client): +def test_ip_trunk_deletion(client: TestClient) -> None: responses.put(url=TEST_CALLBACK_URL, status=204) params = {"callback": TEST_CALLBACK_URL, "dry_run": True, "verb": "terminate", "subscription": _SUBSCRIPTION_OBJECT} @@ -153,7 +154,7 @@ def test_ip_trunk_deletion(client): # wait a second for the run thread to finish time.sleep(1) - jsonschema.validate(response, PlaybookLaunchResponse.schema()) + jsonschema.validate(response, PlaybookLaunchResponse.model_json_schema()) responses.assert_call_count(TEST_CALLBACK_URL, 1) assert response["status"] == "ok" diff --git a/test/test_config.py b/test/test_config.py index 60836be..83a47d1 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -9,7 +9,7 @@ import pytest from lso import config -def test_validate_testenv_config(config_file): +def test_validate_testenv_config(config_file: str) -> None: """Load a configuration from a file. :param config_file: Configuration file pytest fixture @@ -22,7 +22,7 @@ def test_validate_testenv_config(config_file): @pytest.mark.parametrize( "bad_config", [{"name": "bad version", "version": 123}, {"name": "missing version"}, {"version": "missing name"}] ) -def test_bad_config(bad_config): +def test_bad_config(bad_config: dict) -> None: with io.StringIO(json.dumps(bad_config)) as file: file.seek(0) # rewind file position to the beginning with pytest.raises(jsonschema.ValidationError): diff --git a/tox.ini b/tox.ini index ec1dbc5..3563a2e 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,5 @@ [flake8] -ignore = D100,D101,D102,D103,D104,D105,D106,D107,D202,E501,RST301,RST304,W503,E203,C417,T202,S101 -; extend-ignore = E203 +ignore = W503 exclude = .git,.*_cache,.eggs,*.egg-info,__pycache__,venv,.tox,docs enable-extensions = G select = B,C,D,E,F,G,I,N,S,T,W,B902,B903,R -- GitLab