Skip to content

Commit 8db340c

Browse files
authored
feat: add delete method to IndexResource with comprehensive tests (#79)
* feat: add delete method to IndexResource * test: add unit tests for delete method in IndexResource * test: add integration test for delete method in IndexResource * fix: correct key for request endpoint in delete test Changed from 'url' to 'endpoint' to match how BaseFakeClient records requests * fix: formatting
1 parent 1eecf48 commit 8db340c

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

src/deepset_mcp/api/indexes/resource.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,12 @@ async def update(
9494
raise_for_status(response)
9595

9696
return Index.model_validate(response.json)
97+
98+
async def delete(self, index_name: str) -> None:
99+
"""Delete an index.
100+
101+
:param index_name: Name of the index to delete.
102+
"""
103+
response = await self._client.request(f"/v1/workspaces/{self._workspace}/indexes/{index_name}", method="DELETE")
104+
105+
raise_for_status(response)

test/integration/test_integration_index_resource.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,27 @@ async def test_get_nonexistent_index(
316316
# Trying to get a non-existent index should raise an exception
317317
with pytest.raises(ResourceNotFoundError):
318318
await index_resource.get(index_name=non_existent_name)
319+
320+
321+
@pytest.mark.asyncio
322+
async def test_delete_index(
323+
index_resource: IndexResource,
324+
valid_index_config: str,
325+
) -> None:
326+
"""Test deleting an index."""
327+
index_name = "test-delete-index"
328+
329+
# Create an index to delete
330+
config = json.loads(valid_index_config)
331+
await index_resource.create(name=index_name, yaml_config=config["config_yaml"])
332+
333+
# Verify the index exists
334+
index: Index = await index_resource.get(index_name=index_name)
335+
assert index.name == index_name
336+
337+
# Delete the index
338+
await index_resource.delete(index_name=index_name)
339+
340+
# Verify the index no longer exists
341+
with pytest.raises(ResourceNotFoundError):
342+
await index_resource.get(index_name=index_name)

test/unit/api/indexes/test_index_resource.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,41 @@ async def test_update_index_invalid_config(
251251
resource = IndexResource(fake_client, workspace)
252252
with pytest.raises(BadRequestError):
253253
await resource.update(index_name="invalid-index", yaml_config="invalid: yaml")
254+
255+
async def test_delete_index_successful(self, fake_client: BaseFakeClient, workspace: str) -> None:
256+
"""Test deleting an index successfully."""
257+
fake_client.responses[f"/v1/workspaces/{workspace}/indexes/test-index"] = TransportResponse(
258+
status_code=204, json={}, text=""
259+
)
260+
261+
resource = IndexResource(fake_client, workspace)
262+
await resource.delete("test-index")
263+
264+
# Verify request
265+
last_request = fake_client.requests[-1]
266+
assert last_request["method"] == "DELETE"
267+
assert last_request["endpoint"] == f"/v1/workspaces/{workspace}/indexes/test-index"
268+
269+
async def test_delete_nonexistent_index_raises_404(self, fake_client: BaseFakeClient, workspace: str) -> None:
270+
"""Test that deleting a nonexistent index raises ResourceNotFoundError."""
271+
fake_client.responses[f"/v1/workspaces/{workspace}/indexes/nonexistent-index"] = TransportResponse(
272+
status_code=404,
273+
json={"detail": "Index not found"},
274+
text=json.dumps({"detail": "Index not found"}),
275+
)
276+
277+
resource = IndexResource(fake_client, workspace)
278+
with pytest.raises(ResourceNotFoundError):
279+
await resource.delete("nonexistent-index")
280+
281+
async def test_delete_server_error_raises_500(self, fake_client: BaseFakeClient, workspace: str) -> None:
282+
"""Test that server error during delete raises UnexpectedAPIError."""
283+
fake_client.responses[f"/v1/workspaces/{workspace}/indexes/server-error-index"] = TransportResponse(
284+
status_code=500,
285+
json={"detail": "Internal server error"},
286+
text=json.dumps({"detail": "Internal server error"}),
287+
)
288+
289+
resource = IndexResource(fake_client, workspace)
290+
with pytest.raises(UnexpectedAPIError):
291+
await resource.delete("server-error-index")

0 commit comments

Comments
 (0)