"""Tests for proxy class helper methods.""" from typing import Any, Dict, Type from unittest.mock import AsyncMock import pytest from fastapi import HTTPException from httpx import HTTPStatusError, Request, Response from pdp.connectors.ows_account import ( LookupVendorsResponse, ) from pdp.proxies.helpers import lookup_with_error_handling from pdp.proxies.multi_tenant_proxy import ( TenantHierarchyLookupError, ) # Constants for test_lookup_with_error_handling ERROR_400_MSG = "A 400 error happened!!" ERROR_500_MSG = "A 500 error happened!!" ERROR_UNKNOWN = "I don't know what happened!!" class HelperException(Exception): """Test exception class for unit tests.""" ... @pytest.mark.parametrize( "side_effect, error_class, expected_response, expected_exception_type, expected_error_message", # noqa: E501 [ pytest.param( {"test": "response"}, TenantHierarchyLookupError, {"test": "response"}, None, "", id="The function should return the expected dict type value.", ), pytest.param( LookupVendorsResponse(vendors=[]), TenantHierarchyLookupError, LookupVendorsResponse(vendors=[]), None, "", id="The function should return the expected LookupVendorsResponse type value.", # noqa: E501 ), pytest.param( HTTPStatusError( request=Request("GET", "https://no.such/endpoint"), response=Response(status_code=401), message="testing...", ), TenantHierarchyLookupError, None, HTTPException, ERROR_400_MSG, id="A 400 error should raise an HTTPException", ), pytest.param( HTTPStatusError( request=Request("GET", "https://no.such/endpoint"), response=Response(status_code=500), message="testing...", ), TenantHierarchyLookupError, None, TenantHierarchyLookupError, ERROR_500_MSG, id="A 500 error should raise a TenantHierarchyLookupError", ), pytest.param( ValueError("Game over!!!"), TenantHierarchyLookupError, None, TenantHierarchyLookupError, ERROR_UNKNOWN, id="An unhandled error should raise a TenantHierarchyLookupError", ), pytest.param( ValueError("Game over!!!"), HelperException, None, HelperException, ERROR_UNKNOWN, id="An unhandled error should raise a TestHelperException", ), ], ) async def test_lookup_with_error_handling( side_effect: Any, error_class: Type[Exception], expected_response: Any, expected_exception_type: Type[Exception], expected_error_message: str, ) -> None: """Test lookup_with_error_handling.""" lookup_cb = AsyncMock(side_effect=[side_effect]) if expected_exception_type: with pytest.raises(expected_exception_type) as exc_info: _ = await lookup_with_error_handling( lookup_cb=lookup_cb(a=1, b=2, c="3"), http_error_message_400_string=ERROR_400_MSG, http_error_message_500_string=ERROR_500_MSG, unhandled_exception_string=ERROR_UNKNOWN, lookup_error_cls=error_class, ) assert expected_error_message in str(exc_info.value) else: result = await lookup_with_error_handling( lookup_cb=lookup_cb(a=1, b=2, c="3"), http_error_message_400_string=ERROR_400_MSG, http_error_message_500_string=ERROR_500_MSG, unhandled_exception_string=ERROR_UNKNOWN, lookup_error_cls=TenantHierarchyLookupError, ) assert result == expected_response async def test_lookup_with_error_handling_sync() -> None: """Test lookup_with_error_handling with a sync function.""" def sync_lookup_cb(a: int, b: int, c: str) -> Dict[Any, Any]: return {"a": a, "b": b, "c": c} result = await lookup_with_error_handling( lookup_cb=sync_lookup_cb(a=1, b=2, c="3"), http_error_message_400_string=ERROR_400_MSG, http_error_message_500_string=ERROR_500_MSG, unhandled_exception_string=ERROR_UNKNOWN, lookup_error_cls=TenantHierarchyLookupError, ) assert result == {"a": 1, "b": 2, "c": "3"}