Skip to content

Commit 1f227e3

Browse files
committed
test(api_call): add tests for API call parameter normalization and node selection
- Implement tests for `ApiCall.normalize_params` to ensure correct handling of non-dictionary inputs and mixed data types, improving input validation and robustness. - Add tests for node selection logic in `ApiCall`: - Verify behavior when no healthy nodes are available, ensuring proper logging and fallback node selection. - Ensure exception is raised when no nodes are healthy during a request. - Add a docstring to `tests/__init__.py` to clarify the purpose of the tests module. These tests enhance coverage and ensure stability in key areas of the API call logic.
1 parent 5f46d82 commit 1f227e3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/api_call_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
from __future__ import annotations
44

5+
import logging
56
import sys
67
import time
78

9+
from pytest_mock import MockFixture
10+
811
if sys.version_info >= (3, 11):
912
import typing
1013
else:
@@ -19,6 +22,7 @@
1922
from typesense import exceptions
2023
from typesense.api_call import ApiCall
2124
from typesense.configuration import Configuration, Node
25+
from typesense.logger import logger
2226

2327

2428
@pytest.fixture(scope="function", name="config")
@@ -142,6 +146,14 @@ def test_normalize_params_with_booleans() -> None:
142146
assert parameter_dict == {"key1": "true", "key2": "false"}
143147

144148

149+
def test_normalize_params_with_non_dict() -> None:
150+
"""Test that it raises when a non-dictionary is passed."""
151+
parameter_non_dict = "string"
152+
153+
with pytest.raises(ValueError):
154+
ApiCall.normalize_params(parameter_non_dict)
155+
156+
145157
def test_normalize_params_with_mixed_types() -> None:
146158
"""Test that it correctly normalizes boolean values to strings."""
147159
parameter_dict = {"key1": True, "key2": False, "key3": "value", "key4": 123}
@@ -438,6 +450,32 @@ def test_selects_next_available_node_on_timeout(
438450
assert request_mocker.call_count == 3
439451

440452

453+
def test_get_node_no_healthy_nodes(
454+
api_call: ApiCall,
455+
mocker: MockFixture,
456+
caplog: pytest.LogCaptureFixture,
457+
) -> None:
458+
"""Test that it logs a message if no healthy nodes are found."""
459+
for api_node in api_call.nodes:
460+
api_node.healthy = False
461+
462+
api_call.config.nearest_node.healthy = False
463+
464+
mocker.patch.object(api_call, "node_due_for_health_check", return_value=False)
465+
466+
# Need to set the logger level to DEBUG to capture the message
467+
logger.setLevel(logging.DEBUG)
468+
469+
selected_node = api_call.get_node()
470+
471+
with caplog.at_level(logging.DEBUG):
472+
assert "No healthy nodes were found. Returning the next node." in caplog.text
473+
474+
assert selected_node == api_call.nodes[api_call.node_index]
475+
476+
assert api_call.node_index == 0
477+
478+
441479
def test_raises_if_no_nodes_are_healthy_with_the_last_exception(
442480
api_call: ApiCall,
443481
) -> None:

0 commit comments

Comments
 (0)