Fix code review issues - use PropertyMock instead of exec
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -517,24 +517,23 @@ class TestAzureTestConnectionIntegration:
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_azure_connection_logs_outer_exception(self, mock_logger, mock_settings):
|
async def test_azure_connection_logs_outer_exception(self, mock_logger, mock_settings):
|
||||||
"""Test that exceptions in outer try block are logged with exception()."""
|
"""Test that exceptions in outer try block are logged with exception()."""
|
||||||
|
from unittest.mock import PropertyMock
|
||||||
from app.api.azure import test_azure_connection
|
from app.api.azure import test_azure_connection
|
||||||
|
|
||||||
# Trigger an exception in the outer try block
|
# Trigger an exception in the outer try block
|
||||||
mock_settings.azure_endpoint = "https://test.cognitiveservices.azure.com/"
|
# Use PropertyMock to raise exception when azure_endpoint is accessed
|
||||||
mock_settings.azure_ai_key = "test-key"
|
type(mock_settings).azure_endpoint = PropertyMock(side_effect=RuntimeError("Outer error"))
|
||||||
|
type(mock_settings).azure_ai_key = PropertyMock(return_value="test-key")
|
||||||
# Make settings raise an exception when accessed
|
|
||||||
type(mock_settings).azure_endpoint = property(lambda self: exec('raise RuntimeError("Outer error")'))
|
|
||||||
|
|
||||||
mock_request = Mock()
|
mock_request = Mock()
|
||||||
|
result = await test_azure_connection(mock_request)
|
||||||
|
|
||||||
try:
|
# Should catch the exception and return error
|
||||||
await test_azure_connection(mock_request)
|
assert result["status"] == "error"
|
||||||
except Exception:
|
assert "unexpected error" in result["message"].lower()
|
||||||
pass # We expect this to be handled
|
|
||||||
|
|
||||||
# Verify exception was logged with logger.exception
|
# Verify exception was logged with logger.exception
|
||||||
# Note: This might not work perfectly due to property access, but tests the concept
|
mock_logger.exception.assert_called_once()
|
||||||
|
|
||||||
@patch("app.api.azure.DocumentIntelligenceAdministrationClient")
|
@patch("app.api.azure.DocumentIntelligenceAdministrationClient")
|
||||||
@patch("app.api.azure.AzureKeyCredential")
|
@patch("app.api.azure.AzureKeyCredential")
|
||||||
@@ -545,6 +544,7 @@ class TestAzureTestConnectionIntegration:
|
|||||||
self, mock_logger, mock_settings, mock_credential, mock_admin_client_class
|
self, mock_logger, mock_settings, mock_credential, mock_admin_client_class
|
||||||
):
|
):
|
||||||
"""Test that warning is logged when operations parsing fails."""
|
"""Test that warning is logged when operations parsing fails."""
|
||||||
|
from unittest.mock import PropertyMock
|
||||||
from app.api.azure import test_azure_connection
|
from app.api.azure import test_azure_connection
|
||||||
|
|
||||||
mock_settings.azure_endpoint = "https://test.cognitiveservices.azure.com/"
|
mock_settings.azure_endpoint = "https://test.cognitiveservices.azure.com/"
|
||||||
@@ -554,8 +554,8 @@ class TestAzureTestConnectionIntegration:
|
|||||||
# Create an operation that will raise exception during attribute access
|
# Create an operation that will raise exception during attribute access
|
||||||
mock_op = MagicMock()
|
mock_op = MagicMock()
|
||||||
mock_op.operation_id = "valid-id"
|
mock_op.operation_id = "valid-id"
|
||||||
# Make status property raise an exception
|
# Make status property raise an exception using PropertyMock
|
||||||
type(mock_op).status = property(lambda self: (_ for _ in ()).throw(RuntimeError("Status error")))
|
type(mock_op).status = PropertyMock(side_effect=RuntimeError("Status error"))
|
||||||
mock_client.list_operations.return_value = iter([mock_op])
|
mock_client.list_operations.return_value = iter([mock_op])
|
||||||
mock_admin_client_class.return_value = mock_client
|
mock_admin_client_class.return_value = mock_client
|
||||||
|
|
||||||
@@ -574,10 +574,12 @@ class TestAzureTestConnectionIntegration:
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_azure_connection_outer_exception_handler(self, mock_settings):
|
async def test_azure_connection_outer_exception_handler(self, mock_settings):
|
||||||
"""Test the outer exception handler catches unexpected errors."""
|
"""Test the outer exception handler catches unexpected errors."""
|
||||||
|
from unittest.mock import PropertyMock
|
||||||
from app.api.azure import test_azure_connection
|
from app.api.azure import test_azure_connection
|
||||||
|
|
||||||
# Create a mock that raises exception when azure_endpoint is accessed
|
# Create a mock that raises exception when azure_endpoint is accessed using PropertyMock
|
||||||
mock_settings.azure_endpoint = property(lambda self: (_ for _ in ()).throw(RuntimeError("Outer error")))
|
type(mock_settings).azure_endpoint = PropertyMock(side_effect=RuntimeError("Outer error"))
|
||||||
|
type(mock_settings).azure_ai_key = PropertyMock(return_value="test-key")
|
||||||
|
|
||||||
mock_request = Mock()
|
mock_request = Mock()
|
||||||
result = await test_azure_connection(mock_request)
|
result = await test_azure_connection(mock_request)
|
||||||
|
|||||||
Reference in New Issue
Block a user