|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
6 | | -from deepset_mcp.api.exceptions import ResourceNotFoundError, UnexpectedAPIError |
| 6 | +from deepset_mcp.api.exceptions import BadRequestError, ResourceNotFoundError, UnexpectedAPIError |
7 | 7 | from deepset_mcp.api.indexes.models import Index, IndexList |
8 | 8 | from deepset_mcp.api.indexes.resource import IndexResource |
9 | 9 | from deepset_mcp.api.transport import TransportResponse |
@@ -97,6 +97,36 @@ def fake_get_500_response(fake_client: BaseFakeClient, workspace: str) -> None: |
97 | 97 | ) |
98 | 98 |
|
99 | 99 |
|
| 100 | +@pytest.fixture() |
| 101 | +def fake_create_400_response(fake_client: BaseFakeClient, workspace: str) -> None: |
| 102 | + """Configure fake client to return a 400 response for invalid create request.""" |
| 103 | + fake_client.responses[f"/api/v1/workspaces/{workspace}/indexes"] = TransportResponse( |
| 104 | + status_code=400, |
| 105 | + json={"detail": "Invalid request parameters"}, |
| 106 | + text=json.dumps({"detail": "Invalid request parameters"}), |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.fixture() |
| 111 | +def fake_update_404_response(fake_client: BaseFakeClient, workspace: str) -> None: |
| 112 | + """Configure fake client to return a 404 response for nonexistent index update.""" |
| 113 | + fake_client.responses[f"/api/v1/workspaces/{workspace}/indexes/nonexistent-index"] = TransportResponse( |
| 114 | + status_code=404, |
| 115 | + json={"detail": "Index not found"}, |
| 116 | + text=json.dumps({"detail": "Index not found"}), |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +@pytest.fixture() |
| 121 | +def fake_update_400_response(fake_client: BaseFakeClient, workspace: str) -> None: |
| 122 | + """Configure fake client to return a 400 response for invalid update request.""" |
| 123 | + fake_client.responses[f"/api/v1/workspaces/{workspace}/indexes/invalid-index"] = TransportResponse( |
| 124 | + status_code=400, |
| 125 | + json={"detail": "Invalid configuration format"}, |
| 126 | + text=json.dumps({"detail": "Invalid configuration format"}), |
| 127 | + ) |
| 128 | + |
| 129 | + |
100 | 130 | class TestIndexResource: |
101 | 131 | """Test the IndexResource.""" |
102 | 132 |
|
@@ -147,3 +177,77 @@ async def test_list_indexes_passes_params( |
147 | 177 | # Check the last request's parameters |
148 | 178 | last_request = fake_client.requests[-1] |
149 | 179 | assert last_request["params"] == {"limit": 20, "page_number": 2} |
| 180 | + |
| 181 | + async def test_create_index_successful( |
| 182 | + self, fake_client: BaseFakeClient, workspace: str, index_response: dict[str, Any] |
| 183 | + ) -> None: |
| 184 | + """Test creating a new index.""" |
| 185 | + fake_client.responses[f"/api/v1/workspaces/{workspace}/indexes"] = TransportResponse( |
| 186 | + status_code=201, json=index_response, text=json.dumps(index_response) |
| 187 | + ) |
| 188 | + |
| 189 | + resource = IndexResource(fake_client, workspace) |
| 190 | + result = await resource.create(name="test-index", yaml_config="yaml: content", description="Test description") |
| 191 | + |
| 192 | + assert isinstance(result, Index) |
| 193 | + assert result.name == "test-index" |
| 194 | + |
| 195 | + # Verify request |
| 196 | + last_request = fake_client.requests[-1] |
| 197 | + assert last_request["method"] == "POST" |
| 198 | + assert last_request["data"] == { |
| 199 | + "name": "test-index", |
| 200 | + "config_yaml": "yaml: content", |
| 201 | + "description": "Test description", |
| 202 | + } |
| 203 | + |
| 204 | + async def test_update_index_successful( |
| 205 | + self, fake_client: BaseFakeClient, workspace: str, index_response: dict[str, Any] |
| 206 | + ) -> None: |
| 207 | + """Test updating an existing index.""" |
| 208 | + fake_client.responses[f"/api/v1/workspaces/{workspace}/indexes/test-index"] = TransportResponse( |
| 209 | + status_code=200, json=index_response, text=json.dumps(index_response) |
| 210 | + ) |
| 211 | + |
| 212 | + resource = IndexResource(fake_client, workspace) |
| 213 | + result = await resource.update( |
| 214 | + index_name="test-index", updated_index_name="new-name", yaml_config="new: config" |
| 215 | + ) |
| 216 | + |
| 217 | + assert isinstance(result, Index) |
| 218 | + assert result.name == "test-index" |
| 219 | + |
| 220 | + # Verify request |
| 221 | + last_request = fake_client.requests[-1] |
| 222 | + assert last_request["method"] == "PATCH" |
| 223 | + assert last_request["data"] == {"name": "new-name", "config_yaml": "new: config"} |
| 224 | + |
| 225 | + async def test_update_index_without_changes_fails(self, fake_client: BaseFakeClient, workspace: str) -> None: |
| 226 | + """Test that updating an index without any changes raises ValueError.""" |
| 227 | + resource = IndexResource(fake_client, workspace) |
| 228 | + with pytest.raises(ValueError, match="At least one of updated_index_name or yaml_config must be provided"): |
| 229 | + await resource.update(index_name="test-index") |
| 230 | + |
| 231 | + async def test_create_index_invalid_request( |
| 232 | + self, fake_client: BaseFakeClient, workspace: str, fake_create_400_response: None |
| 233 | + ) -> None: |
| 234 | + """Test that creating an index with invalid parameters raises an error.""" |
| 235 | + resource = IndexResource(fake_client, workspace) |
| 236 | + with pytest.raises(BadRequestError): |
| 237 | + await resource.create(name="invalid-index", yaml_config="invalid: yaml") |
| 238 | + |
| 239 | + async def test_update_nonexistent_index( |
| 240 | + self, fake_client: BaseFakeClient, workspace: str, fake_update_404_response: None |
| 241 | + ) -> None: |
| 242 | + """Test that updating a nonexistent index raises ResourceNotFoundError.""" |
| 243 | + resource = IndexResource(fake_client, workspace) |
| 244 | + with pytest.raises(ResourceNotFoundError): |
| 245 | + await resource.update(index_name="nonexistent-index", updated_index_name="new-name") |
| 246 | + |
| 247 | + async def test_update_index_invalid_config( |
| 248 | + self, fake_client: BaseFakeClient, workspace: str, fake_update_400_response: None |
| 249 | + ) -> None: |
| 250 | + """Test that updating an index with invalid configuration raises an error.""" |
| 251 | + resource = IndexResource(fake_client, workspace) |
| 252 | + with pytest.raises(BadRequestError): |
| 253 | + await resource.update(index_name="invalid-index", yaml_config="invalid: yaml") |
0 commit comments