|
| 1 | +import os |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import httpx |
| 5 | +import pytest |
| 6 | +from lif.query_planner_client import QueryPlannerException, fetch_query_from_query_planner |
| 7 | +from lif.query_planner_client.core import ( |
| 8 | + DEFAULT_QUERY_PLANNER_CLIENT_TIMEOUT_SECONDS, |
| 9 | + _get_query_planner_timeout_seconds, |
| 10 | +) |
| 11 | + |
| 12 | +_BASE_URL = "http://localhost:8008" |
| 13 | +_QUERY = {"query": "{ Person { firstName } }"} |
| 14 | + |
| 15 | + |
| 16 | +def _make_http_mock(status_code: int, json_return=None): |
| 17 | + mock_response = mock.Mock() |
| 18 | + mock_response.status_code = status_code |
| 19 | + mock_response.json.return_value = json_return or [] |
| 20 | + mock_response.text = str(json_return or []) |
| 21 | + |
| 22 | + mock_client = mock.AsyncMock() |
| 23 | + mock_client.post.return_value = mock_response |
| 24 | + |
| 25 | + mock_cls = mock.MagicMock() |
| 26 | + mock_cls.return_value.__aenter__ = mock.AsyncMock(return_value=mock_client) |
| 27 | + mock_cls.return_value.__aexit__ = mock.AsyncMock(return_value=False) |
| 28 | + return mock_cls, mock_client |
| 29 | + |
| 30 | + |
| 31 | +async def test_successful_query_returns_json(): |
| 32 | + mock_cls, _ = _make_http_mock(200, [{"firstName": "John"}]) |
| 33 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient", mock_cls): |
| 34 | + result = await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 35 | + assert result == [{"firstName": "John"}] |
| 36 | + |
| 37 | + |
| 38 | +async def test_url_is_constructed_from_base(): |
| 39 | + mock_cls, mock_client = _make_http_mock(200, []) |
| 40 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient", mock_cls): |
| 41 | + await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 42 | + mock_client.post.assert_called_once() |
| 43 | + call_url = mock_client.post.call_args[0][0] |
| 44 | + assert call_url == f"{_BASE_URL}/query" |
| 45 | + |
| 46 | + |
| 47 | +async def test_trailing_slash_stripped_from_base_url(): |
| 48 | + mock_cls, mock_client = _make_http_mock(200, []) |
| 49 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient", mock_cls): |
| 50 | + await fetch_query_from_query_planner(_BASE_URL + "/", _QUERY) |
| 51 | + call_url = mock_client.post.call_args[0][0] |
| 52 | + assert call_url == f"{_BASE_URL}/query" |
| 53 | + |
| 54 | + |
| 55 | +async def test_query_sent_as_json_body(): |
| 56 | + mock_cls, mock_client = _make_http_mock(200, []) |
| 57 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient", mock_cls): |
| 58 | + await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 59 | + assert mock_client.post.call_args[1]["json"] == _QUERY |
| 60 | + |
| 61 | + |
| 62 | +async def test_non_200_raises_query_planner_exception(): |
| 63 | + mock_cls, _ = _make_http_mock(500) |
| 64 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient", mock_cls): |
| 65 | + with pytest.raises(QueryPlannerException, match="HTTP 500"): |
| 66 | + await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 67 | + |
| 68 | + |
| 69 | +async def test_timeout_raises_query_planner_exception(): |
| 70 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient") as mock_cls: |
| 71 | + mock_cls.return_value.__aenter__ = mock.AsyncMock(side_effect=httpx.TimeoutException("timed out")) |
| 72 | + mock_cls.return_value.__aexit__ = mock.AsyncMock(return_value=False) |
| 73 | + with pytest.raises(QueryPlannerException, match="timed out"): |
| 74 | + await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 75 | + |
| 76 | + |
| 77 | +async def test_connect_error_raises_query_planner_exception(): |
| 78 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient") as mock_cls: |
| 79 | + mock_cls.return_value.__aenter__ = mock.AsyncMock(side_effect=httpx.ConnectError("connection refused")) |
| 80 | + mock_cls.return_value.__aexit__ = mock.AsyncMock(return_value=False) |
| 81 | + with pytest.raises(QueryPlannerException, match="connection refused"): |
| 82 | + await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 83 | + |
| 84 | + |
| 85 | +def test_timeout_defaults_when_env_var_absent(): |
| 86 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 87 | + assert _get_query_planner_timeout_seconds() == DEFAULT_QUERY_PLANNER_CLIENT_TIMEOUT_SECONDS |
| 88 | + |
| 89 | + |
| 90 | +def test_timeout_reads_from_env_var(): |
| 91 | + with mock.patch.dict(os.environ, {"QUERY_PLANNER_CLIENT_TIMEOUT_SECONDS": "5"}): |
| 92 | + assert _get_query_planner_timeout_seconds() == 5 |
| 93 | + |
| 94 | + |
| 95 | +async def test_default_timeout_passed_to_async_client(): |
| 96 | + mock_cls, _ = _make_http_mock(200, []) |
| 97 | + with mock.patch.dict(os.environ, {}, clear=True): |
| 98 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient", mock_cls): |
| 99 | + await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 100 | + assert mock_cls.call_args.kwargs["timeout"] == DEFAULT_QUERY_PLANNER_CLIENT_TIMEOUT_SECONDS |
| 101 | + |
| 102 | + |
| 103 | +async def test_configured_timeout_passed_to_async_client(): |
| 104 | + mock_cls, _ = _make_http_mock(200, []) |
| 105 | + with mock.patch.dict(os.environ, {"QUERY_PLANNER_CLIENT_TIMEOUT_SECONDS": "7"}): |
| 106 | + with mock.patch("lif.query_planner_client.core.httpx.AsyncClient", mock_cls): |
| 107 | + await fetch_query_from_query_planner(_BASE_URL, _QUERY) |
| 108 | + assert mock_cls.call_args.kwargs["timeout"] == 7 |
0 commit comments