|
| 1 | +""" |
| 2 | +Tests for backend API startup and shutdown functionality. |
| 3 | +""" |
| 4 | + |
| 5 | +from unittest.mock import AsyncMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from fastapi.testclient import TestClient |
| 9 | + |
| 10 | +from edcpy.backend import app, lifespan |
| 11 | + |
| 12 | + |
| 13 | +class TestBackendStartup: |
| 14 | + """Test suite for backend startup functionality.""" |
| 15 | + |
| 16 | + def test_app_creation(self): |
| 17 | + """Test that the FastAPI app can be created successfully.""" |
| 18 | + |
| 19 | + assert app is not None |
| 20 | + assert hasattr(app.router, "lifespan_context") |
| 21 | + |
| 22 | + @pytest.mark.asyncio |
| 23 | + async def test_lifespan_startup_success(self, mock_messaging_app): |
| 24 | + """Test successful startup of the messaging app during lifespan.""" |
| 25 | + |
| 26 | + mock_app = AsyncMock() |
| 27 | + |
| 28 | + with patch( |
| 29 | + "edcpy.backend.start_messaging_app", return_value=mock_messaging_app |
| 30 | + ): |
| 31 | + async with lifespan(mock_app): |
| 32 | + # Verify that the messaging app was set on the app state |
| 33 | + assert mock_app.state.messaging_app == mock_messaging_app |
| 34 | + |
| 35 | + @pytest.mark.asyncio |
| 36 | + async def test_lifespan_shutdown_success(self, mock_messaging_app): |
| 37 | + """Test successful shutdown of the messaging app during lifespan.""" |
| 38 | + |
| 39 | + mock_app = AsyncMock() |
| 40 | + |
| 41 | + with patch( |
| 42 | + "edcpy.backend.start_messaging_app", return_value=mock_messaging_app |
| 43 | + ): |
| 44 | + async with lifespan(mock_app): |
| 45 | + pass |
| 46 | + |
| 47 | + # Verify that the broker was closed |
| 48 | + mock_messaging_app.broker.close.assert_called_once() |
| 49 | + |
| 50 | + @pytest.mark.asyncio |
| 51 | + async def test_lifespan_shutdown_with_exception(self, mock_messaging_app): |
| 52 | + """Test that shutdown handles exceptions gracefully.""" |
| 53 | + |
| 54 | + mock_app = AsyncMock() |
| 55 | + mock_messaging_app.broker.close.side_effect = Exception("Close error") |
| 56 | + |
| 57 | + with patch( |
| 58 | + "edcpy.backend.start_messaging_app", return_value=mock_messaging_app |
| 59 | + ): |
| 60 | + # Should not raise an exception even if broker close fails |
| 61 | + async with lifespan(mock_app): |
| 62 | + pass |
| 63 | + |
| 64 | + # Verify that the broker close was attempted |
| 65 | + mock_messaging_app.broker.close.assert_called_once() |
| 66 | + |
| 67 | + @pytest.mark.asyncio |
| 68 | + async def test_lifespan_startup_failure(self): |
| 69 | + """Test that startup failure is handled properly.""" |
| 70 | + |
| 71 | + mock_app = AsyncMock() |
| 72 | + |
| 73 | + with patch( |
| 74 | + "edcpy.backend.start_messaging_app", side_effect=Exception("Startup error") |
| 75 | + ): |
| 76 | + # Should raise the exception from start_messaging_app |
| 77 | + with pytest.raises(Exception, match="Startup error"): |
| 78 | + async with lifespan(mock_app): |
| 79 | + pass |
| 80 | + |
| 81 | + def test_test_client_creation(self, test_client): |
| 82 | + """Test that the test client can be created successfully.""" |
| 83 | + |
| 84 | + assert test_client is not None |
| 85 | + assert isinstance(test_client, TestClient) |
| 86 | + |
| 87 | + def test_app_state_access(self, test_client, mock_messaging_app): |
| 88 | + """Test that the messaging app can be accessed from app state.""" |
| 89 | + |
| 90 | + # The test_client fixture should have set up the messaging app |
| 91 | + assert hasattr(test_client.app, "state") |
| 92 | + |
| 93 | + # Since we're using mocked startup, we need to verify the mock is working |
| 94 | + with patch( |
| 95 | + "edcpy.backend.start_messaging_app", return_value=mock_messaging_app |
| 96 | + ): |
| 97 | + # Create a new test client to trigger the lifespan |
| 98 | + with TestClient(app) as client: |
| 99 | + # The app should be accessible during the lifespan |
| 100 | + assert client.app is not None |
| 101 | + |
| 102 | + |
| 103 | +class TestBackendConfiguration: |
| 104 | + """Test suite for backend configuration handling.""" |
| 105 | + |
| 106 | + def test_get_messaging_app_dependency(self, test_client, mock_messaging_app): |
| 107 | + """Test that the messaging app dependency can be resolved.""" |
| 108 | + |
| 109 | + from edcpy.backend import get_messaging_app |
| 110 | + |
| 111 | + # Create a mock request with app state |
| 112 | + mock_request = AsyncMock() |
| 113 | + mock_request.app.state.messaging_app = mock_messaging_app |
| 114 | + |
| 115 | + result = get_messaging_app(mock_request) |
| 116 | + assert result == mock_messaging_app |
| 117 | + |
| 118 | + def test_messaging_app_annotation(self): |
| 119 | + """Test that the MessagingAppDep annotation is properly defined.""" |
| 120 | + |
| 121 | + from edcpy.backend import MessagingAppDep |
| 122 | + |
| 123 | + assert MessagingAppDep is not None |
| 124 | + # The annotation should be a type annotation |
| 125 | + assert hasattr(MessagingAppDep, "__origin__") |
| 126 | + |
| 127 | + |
| 128 | +class TestBackendHealthCheck: |
| 129 | + """Test suite for basic backend health checks.""" |
| 130 | + |
| 131 | + def test_app_routes_exist(self): |
| 132 | + """Test that the expected routes are registered.""" |
| 133 | + |
| 134 | + routes = [ |
| 135 | + getattr(route, "path", None) |
| 136 | + for route in app.routes |
| 137 | + if hasattr(route, "path") |
| 138 | + ] |
| 139 | + |
| 140 | + # Check that our main endpoints are registered |
| 141 | + assert "/pull" in routes |
| 142 | + assert "/push" in routes |
| 143 | + assert "/push/{routing_key_parts:path}" in routes |
| 144 | + |
| 145 | + def test_app_lifespan_configured(self): |
| 146 | + """Test that the app has lifespan configured.""" |
| 147 | + |
| 148 | + assert app.router.lifespan_context is not None |
0 commit comments