diff --git a/src/typesense/async_/operations.py b/src/typesense/async_/operations.py index 4a36608..ad02327 100644 --- a/src/typesense/async_/operations.py +++ b/src/typesense/async_/operations.py @@ -204,16 +204,23 @@ async def perform( >>> response = await operations.perform("vote") >>> health = await operations.is_healthy() """ - response = await self.api_call.post( - self._endpoint_path(operation_name), + endpoint = self._endpoint_path(operation_name) + if operation_name == "schema_changes": + changes: typing.List[SchemaChangesResponse] = await self.api_call.get( + endpoint, + params=query_params, + as_json=True, + entity_type=typing.List[SchemaChangesResponse], + ) + return changes + + response: OperationResponse = await self.api_call.post( + endpoint, params=query_params, as_json=True, - entity_type=object, - ) - return typing.cast( - typing.Union[OperationResponse, typing.List[SchemaChangesResponse]], - response, + entity_type=OperationResponse, ) + return response async def is_healthy(self) -> bool: """ diff --git a/src/typesense/sync/operations.py b/src/typesense/sync/operations.py index 450c345..f963553 100644 --- a/src/typesense/sync/operations.py +++ b/src/typesense/sync/operations.py @@ -204,16 +204,23 @@ def perform( >>> response = await operations.perform("vote") >>> health = await operations.is_healthy() """ - response = self.api_call.post( - self._endpoint_path(operation_name), + endpoint = self._endpoint_path(operation_name) + if operation_name == "schema_changes": + changes: typing.List[SchemaChangesResponse] = self.api_call.get( + endpoint, + params=query_params, + as_json=True, + entity_type=typing.List[SchemaChangesResponse], + ) + return changes + + response: OperationResponse = self.api_call.post( + endpoint, params=query_params, as_json=True, - entity_type=object, - ) - return typing.cast( - typing.Union[OperationResponse, typing.List[SchemaChangesResponse]], - response, + entity_type=OperationResponse, ) + return response def is_healthy(self) -> bool: """ diff --git a/tests/operations_test.py b/tests/operations_test.py index 71c134e..a1b72c9 100644 --- a/tests/operations_test.py +++ b/tests/operations_test.py @@ -1,7 +1,9 @@ """Tests for the Operations class.""" +import httpx import pytest +import respx from tests.utils.object_assertions import assert_match_object, assert_object_lists_match from typesense.sync.api_call import ApiCall @@ -47,6 +49,58 @@ def test_init_async(fake_async_api_call: AsyncApiCall) -> None: ) +def test_schema_changes_uses_get(fake_operations: Operations) -> None: + """Test that performing schema_changes sends a GET request.""" + expected = [{"collection": "companies", "validated_docs": 10, "altered_docs": 5}] + with respx.mock: + route = respx.get("http://nearest:8108/operations/schema_changes").mock( + return_value=httpx.Response(200, json=expected), + ) + response = fake_operations.perform("schema_changes") + + assert route.called + assert response == expected + + +async def test_schema_changes_uses_get_async( + fake_async_operations: AsyncOperations, +) -> None: + """Test that performing schema_changes sends a GET request (async).""" + expected = [{"collection": "companies", "validated_docs": 10, "altered_docs": 5}] + with respx.mock: + route = respx.get("http://nearest:8108/operations/schema_changes").mock( + return_value=httpx.Response(200, json=expected), + ) + response = await fake_async_operations.perform("schema_changes") + + assert route.called + assert response == expected + + +def test_vote_uses_post(fake_operations: Operations) -> None: + """Test that non-GET operations still send a POST request.""" + with respx.mock: + route = respx.post("http://nearest:8108/operations/vote").mock( + return_value=httpx.Response(200, json={"success": True}), + ) + response = fake_operations.perform("vote") + + assert route.called + assert response == {"success": True} + + +async def test_vote_uses_post_async(fake_async_operations: AsyncOperations) -> None: + """Test that non-GET operations still send a POST request (async).""" + with respx.mock: + route = respx.post("http://nearest:8108/operations/vote").mock( + return_value=httpx.Response(200, json={"success": True}), + ) + response = await fake_async_operations.perform("vote") + + assert route.called + assert response == {"success": True} + + def test_vote(actual_operations: Operations) -> None: """Test that the Operations object can perform the vote operation.""" response = actual_operations.perform("vote") @@ -155,3 +209,17 @@ async def test_invalid_operation_async(actual_async_operations: AsyncOperations) """Test that the AsyncOperations object throws an error for an invalid operation.""" with pytest.raises(ObjectNotFound): await actual_async_operations.perform("invalid") + + +def test_schema_changes(actual_operations: Operations) -> None: + """Test that the Operations object can retrieve schema changes.""" + response = actual_operations.perform("schema_changes") + + assert isinstance(response, list) + + +async def test_schema_changes_async(actual_async_operations: AsyncOperations) -> None: + """Test that the AsyncOperations object can retrieve schema changes.""" + response = await actual_async_operations.perform("schema_changes") + + assert isinstance(response, list)