Skip to content

Commit 1b704d0

Browse files
committed
feat(debug): add async support for debug operations
- add AsyncDebug class for async debug information retrieval - add async tests for debug functionality - add async fixtures for testing async debug operations - remove future annotations imports from test files
1 parent e81edec commit 1b704d0

3 files changed

Lines changed: 114 additions & 1 deletion

File tree

src/typesense/async_debug.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
This module provides async functionality for accessing debug information in Typesense.
3+
4+
It contains the AsyncDebug class, which allows for retrieving debug information
5+
asynchronously.
6+
7+
Classes:
8+
AsyncDebug: Manages async operations for accessing debug information in the Typesense API.
9+
10+
Dependencies:
11+
- typesense.async_api_call: Provides the AsyncApiCall class for making async API requests.
12+
- typesense.types.debug: Provides DebugResponseSchema type.
13+
14+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
15+
"""
16+
17+
import sys
18+
19+
if sys.version_info >= (3, 11):
20+
import typing
21+
else:
22+
import typing_extensions as typing
23+
24+
from typesense.async_api_call import AsyncApiCall
25+
from typesense.types.debug import DebugResponseSchema
26+
27+
28+
class AsyncDebug:
29+
"""
30+
Manages async operations for accessing debug information in the Typesense API.
31+
32+
This class provides async methods to retrieve debug information from the Typesense server,
33+
which can be useful for system diagnostics and troubleshooting.
34+
35+
Attributes:
36+
resource_path (str): The API resource path for debug operations.
37+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
38+
"""
39+
40+
resource_path: typing.Final[str] = "/debug"
41+
42+
def __init__(self, api_call: AsyncApiCall) -> None:
43+
"""
44+
Initialize the AsyncDebug instance.
45+
46+
Args:
47+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
48+
"""
49+
self.api_call = api_call
50+
51+
async def retrieve(self) -> DebugResponseSchema:
52+
"""
53+
Retrieve debug information from the Typesense server.
54+
55+
This method sends an async GET request to the debug endpoint and returns
56+
the server's debug information.
57+
58+
Returns:
59+
DebugResponseSchema: A schema containing the debug information.
60+
61+
Example:
62+
>>> debug = AsyncDebug(async_api_call)
63+
>>> info = await debug.retrieve()
64+
>>> print(info["version"])
65+
"""
66+
response: DebugResponseSchema = await self.api_call.get(
67+
AsyncDebug.resource_path,
68+
as_json=True,
69+
entity_type=DebugResponseSchema,
70+
)
71+
return response

tests/debug_test.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Tests for the Debug class."""
22

3-
from __future__ import annotations
43
from tests.utils.object_assertions import assert_match_object, assert_object_lists_match
54
from typesense.api_call import ApiCall
5+
from typesense.async_api_call import AsyncApiCall
6+
from typesense.async_debug import AsyncDebug
67
from typesense.debug import Debug
78

89

@@ -24,6 +25,22 @@ def test_init(fake_api_call: ApiCall) -> None:
2425
assert debug.resource_path == "/debug" # noqa: WPS437
2526

2627

28+
def test_init_async(fake_async_api_call: AsyncApiCall) -> None:
29+
"""Test that the AsyncDebug object is initialized correctly."""
30+
debug = AsyncDebug(fake_async_api_call)
31+
32+
assert_match_object(debug.api_call, fake_async_api_call)
33+
assert_object_lists_match(
34+
debug.api_call.node_manager.nodes,
35+
fake_async_api_call.node_manager.nodes,
36+
)
37+
assert_match_object(
38+
debug.api_call.config.nearest_node,
39+
fake_async_api_call.config.nearest_node,
40+
)
41+
assert debug.resource_path == "/debug" # noqa: WPS437
42+
43+
2744
def test_actual_retrieve(actual_debug: Debug) -> None:
2845
"""Test that the Debug object can retrieve a debug on Typesense server and verify response structure."""
2946
response = actual_debug.retrieve()
@@ -33,3 +50,14 @@ def test_actual_retrieve(actual_debug: Debug) -> None:
3350

3451
assert isinstance(response["state"], int)
3552
assert isinstance(response["version"], str)
53+
54+
55+
async def test_actual_retrieve_async(actual_async_debug: AsyncDebug) -> None:
56+
"""Test that the AsyncDebug object can retrieve a debug on Typesense server and verify response structure."""
57+
response = await actual_async_debug.retrieve()
58+
59+
assert "state" in response
60+
assert "version" in response
61+
62+
assert isinstance(response["state"], int)
63+
assert isinstance(response["version"], str)

tests/fixtures/debug_fixtures.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44

55
from typesense.api_call import ApiCall
6+
from typesense.async_api_call import AsyncApiCall
7+
from typesense.async_debug import AsyncDebug
68
from typesense.debug import Debug
79

810

@@ -16,3 +18,15 @@ def actual_debug_fixture(actual_api_call: ApiCall) -> Debug:
1618
def fake_debug_fixture(fake_api_call: ApiCall) -> Debug:
1719
"""Return a debug object with test values."""
1820
return Debug(fake_api_call)
21+
22+
23+
@pytest.fixture(scope="function", name="actual_async_debug")
24+
def actual_async_debug_fixture(actual_async_api_call: AsyncApiCall) -> AsyncDebug:
25+
"""Return a AsyncDebug object using a real API."""
26+
return AsyncDebug(actual_async_api_call)
27+
28+
29+
@pytest.fixture(scope="function", name="fake_async_debug")
30+
def fake_async_debug_fixture(fake_async_api_call: AsyncApiCall) -> AsyncDebug:
31+
"""Return a AsyncDebug object with test values."""
32+
return AsyncDebug(fake_async_api_call)

0 commit comments

Comments
 (0)