From 14b03df5a13de7a8e69f7b297518ddd9f9540645 Mon Sep 17 00:00:00 2001 From: Peter Schutt Date: Mon, 16 Jan 2023 20:37:44 +1000 Subject: [PATCH] fix: ignore 405s in ControllerTest utility. If a method handler isn't registered, we ignore that test. Closes #234 --- .../testing/controller_test.py | 12 +++++++++++- tests/unit/test_testing.py | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/starlite_saqlalchemy/testing/controller_test.py b/src/starlite_saqlalchemy/testing/controller_test.py index c964dde2..78ae14f6 100644 --- a/src/starlite_saqlalchemy/testing/controller_test.py +++ b/src/starlite_saqlalchemy/testing/controller_test.py @@ -4,7 +4,11 @@ import random from typing import TYPE_CHECKING -from starlite.status_codes import HTTP_200_OK, HTTP_201_CREATED +from starlite.status_codes import ( + HTTP_200_OK, + HTTP_201_CREATED, + HTTP_405_METHOD_NOT_ALLOWED, +) if TYPE_CHECKING: from collections.abc import Sequence @@ -67,6 +71,9 @@ async def _list(*_: Any, **__: Any) -> list[Any]: self.base_path, params=self.collection_filters if with_filters else None ) + if resp.status_code == HTTP_405_METHOD_NOT_ALLOWED: + return + assert resp.status_code == HTTP_200_OK assert resp.json() == self.raw_collection @@ -91,6 +98,9 @@ async def _method(*_: Any, **__: Any) -> Any: resp = self.client.request(method, url, **request_kw) + if resp.status_code == HTTP_405_METHOD_NOT_ALLOWED: + return + assert resp.status_code == exp_status assert resp.json() == raw diff --git a/tests/unit/test_testing.py b/tests/unit/test_testing.py index 519f5d5f..2474f3ba 100644 --- a/tests/unit/test_testing.py +++ b/tests/unit/test_testing.py @@ -6,7 +6,11 @@ import httpx import pytest -from starlite.status_codes import HTTP_200_OK, HTTP_404_NOT_FOUND +from starlite.status_codes import ( + HTTP_200_OK, + HTTP_404_NOT_FOUND, + HTTP_405_METHOD_NOT_ALLOWED, +) from starlite_saqlalchemy import testing from starlite_saqlalchemy.exceptions import ConflictError, StarliteSaqlalchemyError @@ -210,3 +214,12 @@ def test_tester_run_method(params: dict[str, Any] | None) -> None: ("test_member_request", ("POST", "create", 201), {}), ("test_member_request", ("DELETE", "delete", 200), {}), ] + + +def test_tester_ignores_405_response( + tester: testing.ControllerTest, mock_response: MagicMock +) -> None: + """Test that 405 responses don't raise from asserts.""" + mock_response.status_code = HTTP_405_METHOD_NOT_ALLOWED + tester.test_get_collection() + tester.test_member_request("GET", "get", HTTP_200_OK)