|
| 1 | +from typing import Any |
| 2 | +from unittest.mock import AsyncMock |
| 3 | + |
| 4 | +import pytest |
| 5 | +import pytest_asyncio |
| 6 | +from httpx import ASGITransport, AsyncClient |
| 7 | +from src.core.authentication import authenticate_user |
| 8 | +from src.main import app |
| 9 | +from src.modules.account.account_model import Account, AccountRole |
| 10 | +from src.modules.location.location_model import AutocompleteResult |
| 11 | +from src.modules.location.location_service import LocationService |
| 12 | + |
| 13 | + |
| 14 | +@pytest_asyncio.fixture |
| 15 | +async def mock_location_service(): |
| 16 | + """Create a mock LocationService for testing""" |
| 17 | + return AsyncMock(spec=LocationService) |
| 18 | + |
| 19 | + |
| 20 | +@pytest_asyncio.fixture |
| 21 | +async def override_dependencies(mock_location_service: AsyncMock): |
| 22 | + """Override dependencies to provide mock service and auth""" |
| 23 | + |
| 24 | + async def _fake_user(): |
| 25 | + return Account( |
| 26 | + id=1, |
| 27 | + |
| 28 | + first_name="Test", |
| 29 | + last_name="User", |
| 30 | + pid="123456789", |
| 31 | + role=AccountRole.STUDENT, |
| 32 | + ) |
| 33 | + |
| 34 | + def _get_mock_location_service(): |
| 35 | + return mock_location_service |
| 36 | + |
| 37 | + app.dependency_overrides[authenticate_user] = _fake_user |
| 38 | + app.dependency_overrides[LocationService] = _get_mock_location_service |
| 39 | + yield |
| 40 | + app.dependency_overrides.clear() |
| 41 | + |
| 42 | + |
| 43 | +@pytest_asyncio.fixture |
| 44 | +async def override_dependencies_no_auth(mock_location_service: AsyncMock): |
| 45 | + """Override dependencies without authentication""" |
| 46 | + |
| 47 | + def _get_mock_location_service(): |
| 48 | + return mock_location_service |
| 49 | + |
| 50 | + app.dependency_overrides[LocationService] = _get_mock_location_service |
| 51 | + yield |
| 52 | + app.dependency_overrides.clear() |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_autocomplete_success( |
| 57 | + override_dependencies: Any, mock_location_service: AsyncMock |
| 58 | +): |
| 59 | + """Test that the endpoint returns multiple address suggestions successfully""" |
| 60 | + mock_results = [ |
| 61 | + AutocompleteResult( |
| 62 | + formatted_address="123 Main St, Chapel Hill, NC 27514, USA", |
| 63 | + place_id="ChIJTest123", |
| 64 | + ), |
| 65 | + AutocompleteResult( |
| 66 | + formatted_address="123 Main St, Durham, NC 27701, USA", |
| 67 | + place_id="ChIJTest456", |
| 68 | + ), |
| 69 | + ] |
| 70 | + mock_location_service.autocomplete_address.return_value = mock_results |
| 71 | + |
| 72 | + async with AsyncClient( |
| 73 | + transport=ASGITransport(app=app), base_url="http://test" |
| 74 | + ) as client: |
| 75 | + response = await client.post( |
| 76 | + "/api/locations/autocomplete", json={"address": "123 Main St"} |
| 77 | + ) |
| 78 | + |
| 79 | + assert response.status_code == 200 |
| 80 | + data = response.json() |
| 81 | + assert len(data) == 2 |
| 82 | + assert data[0]["formatted_address"] == "123 Main St, Chapel Hill, NC 27514, USA" |
| 83 | + assert data[0]["place_id"] == "ChIJTest123" |
| 84 | + mock_location_service.autocomplete_address.assert_called_once_with( |
| 85 | + "123 Main St" |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.asyncio |
| 90 | +async def test_autocomplete_empty_results( |
| 91 | + override_dependencies: Any, mock_location_service: AsyncMock |
| 92 | +): |
| 93 | + """Test that the endpoint returns an empty list when no addresses match""" |
| 94 | + mock_location_service.autocomplete_address.return_value = [] |
| 95 | + |
| 96 | + async with AsyncClient( |
| 97 | + transport=ASGITransport(app=app), base_url="http://test" |
| 98 | + ) as client: |
| 99 | + response = await client.post( |
| 100 | + "/api/locations/autocomplete", |
| 101 | + json={"address": "nonexistentaddress12345xyz"}, |
| 102 | + ) |
| 103 | + |
| 104 | + assert response.status_code == 200 |
| 105 | + assert response.json() == [] |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.asyncio |
| 109 | +async def test_autocomplete_missing_address(override_dependencies: Any): |
| 110 | + """Test that the endpoint returns 422 when address field is missing""" |
| 111 | + async with AsyncClient( |
| 112 | + transport=ASGITransport(app=app), base_url="http://test" |
| 113 | + ) as client: |
| 114 | + response = await client.post("/api/locations/autocomplete", json={}) |
| 115 | + assert response.status_code == 422 |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.asyncio |
| 119 | +async def test_autocomplete_empty_string( |
| 120 | + override_dependencies: Any, mock_location_service: AsyncMock |
| 121 | +): |
| 122 | + """Test that the endpoint handles empty string gracefully""" |
| 123 | + mock_location_service.autocomplete_address.return_value = [] |
| 124 | + |
| 125 | + async with AsyncClient( |
| 126 | + transport=ASGITransport(app=app), base_url="http://test" |
| 127 | + ) as client: |
| 128 | + response = await client.post( |
| 129 | + "/api/locations/autocomplete", json={"address": ""} |
| 130 | + ) |
| 131 | + assert response.status_code == 200 |
| 132 | + assert response.json() == [] |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.asyncio |
| 136 | +async def test_autocomplete_unauthenticated(override_dependencies_no_auth: Any): |
| 137 | + """Test that the endpoint requires authentication""" |
| 138 | + async with AsyncClient( |
| 139 | + transport=ASGITransport(app=app), base_url="http://test" |
| 140 | + ) as client: |
| 141 | + response = await client.post( |
| 142 | + "/api/locations/autocomplete", json={"address": "123 Main St"} |
| 143 | + ) |
| 144 | + assert response.status_code == 401 |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.asyncio |
| 148 | +async def test_autocomplete_service_exception( |
| 149 | + override_dependencies: Any, mock_location_service: AsyncMock |
| 150 | +): |
| 151 | + """Test that service exceptions are handled correctly""" |
| 152 | + mock_location_service.autocomplete_address.side_effect = Exception( |
| 153 | + "Service temporarily unavailable" |
| 154 | + ) |
| 155 | + |
| 156 | + async with AsyncClient( |
| 157 | + transport=ASGITransport(app=app), base_url="http://test" |
| 158 | + ) as client: |
| 159 | + response = await client.post( |
| 160 | + "/api/locations/autocomplete", |
| 161 | + json={"address": "123 Test St"}, |
| 162 | + ) |
| 163 | + |
| 164 | + assert response.status_code == 500 |
| 165 | + |
| 166 | + |
| 167 | +@pytest.mark.asyncio |
| 168 | +async def test_autocomplete_value_error( |
| 169 | + override_dependencies: Any, mock_location_service: AsyncMock |
| 170 | +): |
| 171 | + """Test that ValueError from API is handled correctly""" |
| 172 | + mock_location_service.autocomplete_address.side_effect = ValueError( |
| 173 | + "Invalid API key provided" |
| 174 | + ) |
| 175 | + |
| 176 | + async with AsyncClient( |
| 177 | + transport=ASGITransport(app=app), base_url="http://test" |
| 178 | + ) as client: |
| 179 | + response = await client.post( |
| 180 | + "/api/locations/autocomplete", |
| 181 | + json={"address": "123 Test St"}, |
| 182 | + ) |
| 183 | + |
| 184 | + assert response.status_code == 400 |
0 commit comments