|
| 1 | +from types import TracebackType |
| 2 | +from typing import Any, Protocol, Self |
| 3 | + |
| 4 | +import pytest |
| 5 | +from httpx import Response |
| 6 | + |
| 7 | +from deepset_mcp.api.protocols import AsyncClientProtocol |
| 8 | + |
| 9 | + |
| 10 | +class FakeClient(AsyncClientProtocol): |
| 11 | + """Fake client for testing.""" |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.responses: list[Response] = [] |
| 15 | + self._pipeline_template_responses = { |
| 16 | + "test_template": { |
| 17 | + "author": "deepset", |
| 18 | + "available_to_all_organization_types": True, |
| 19 | + "best_for": ["qa"], |
| 20 | + "deepset_cloud_version": "v2", |
| 21 | + "description": "Test template", |
| 22 | + "expected_output": ["answers"], |
| 23 | + "pipeline_name": "test_template", |
| 24 | + "pipeline_template_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6", |
| 25 | + "pipeline_type": "query", |
| 26 | + "potential_applications": ["qa"], |
| 27 | + "query_yaml": "test: yaml", |
| 28 | + "recommended_dataset": ["test"], |
| 29 | + "tags": [{"name": "test", "tag_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"}] |
| 30 | + }, |
| 31 | + "list": { |
| 32 | + "data": [ |
| 33 | + { |
| 34 | + "author": "deepset", |
| 35 | + "available_to_all_organization_types": True, |
| 36 | + "best_for": ["qa"], |
| 37 | + "deepset_cloud_version": "v2", |
| 38 | + "description": "Template 1", |
| 39 | + "expected_output": ["answers"], |
| 40 | + "pipeline_name": "template_1", |
| 41 | + "pipeline_template_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6", |
| 42 | + "pipeline_type": "query", |
| 43 | + "potential_applications": ["qa"], |
| 44 | + "query_yaml": "test: yaml", |
| 45 | + "recommended_dataset": ["test"], |
| 46 | + "tags": [{"name": "test", "tag_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"}] |
| 47 | + }, |
| 48 | + { |
| 49 | + "author": "deepset", |
| 50 | + "available_to_all_organization_types": True, |
| 51 | + "best_for": ["qa"], |
| 52 | + "deepset_cloud_version": "v2", |
| 53 | + "description": "Template 2", |
| 54 | + "expected_output": ["answers"], |
| 55 | + "pipeline_name": "template_2", |
| 56 | + "pipeline_template_id": "3fa85f64-5717-4562-b3fc-2c963f66afa7", |
| 57 | + "pipeline_type": "query", |
| 58 | + "potential_applications": ["qa"], |
| 59 | + "query_yaml": "test: yaml", |
| 60 | + "recommended_dataset": ["test"], |
| 61 | + "tags": [{"name": "test", "tag_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"}] |
| 62 | + } |
| 63 | + ], |
| 64 | + "has_more": False, |
| 65 | + "total": 2 |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + async def request( |
| 70 | + self, |
| 71 | + endpoint: str, |
| 72 | + method: str = "GET", |
| 73 | + data: dict[str, Any] | None = None, |
| 74 | + headers: dict[str, str] | None = None, |
| 75 | + ) -> Response: |
| 76 | + """Fake a request by returning a predefined response.""" |
| 77 | + if "pipeline_templates" in endpoint: |
| 78 | + if endpoint.endswith("test_template"): |
| 79 | + return Response(200, json=self._pipeline_template_responses["test_template"]) |
| 80 | + return Response(200, json=self._pipeline_template_responses["list"]) |
| 81 | + return self.responses.pop(0) |
| 82 | + |
| 83 | + async def close(self) -> None: |
| 84 | + """Clean up resources.""" |
| 85 | + pass |
| 86 | + |
| 87 | + async def __aenter__(self) -> Self: |
| 88 | + """Enter the async context.""" |
| 89 | + return self |
| 90 | + |
| 91 | + async def __aexit__( |
| 92 | + self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None |
| 93 | + ) -> bool: |
| 94 | + """Exit the async context.""" |
| 95 | + await self.close() |
| 96 | + return False |
| 97 | + |
| 98 | + def pipelines(self, workspace: str) -> Protocol: |
| 99 | + """Mock pipeline resource.""" |
| 100 | + return Protocol() |
| 101 | + |
| 102 | + def haystack_service(self) -> Protocol: |
| 103 | + """Mock haystack service.""" |
| 104 | + return Protocol() |
| 105 | + |
| 106 | + def pipeline_templates(self, workspace: str) -> Protocol: |
| 107 | + """Mock pipeline template resource.""" |
| 108 | + return Protocol() |
| 109 | + |
| 110 | + |
| 111 | +@pytest.fixture |
| 112 | +def fake_client() -> FakeClient: |
| 113 | + """Create a fake client for testing.""" |
| 114 | + return FakeClient() |
0 commit comments