|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import asyncio |
2 | 4 | from pathlib import Path |
3 | | -from types import SimpleNamespace |
4 | 5 |
|
5 | | -from qdrant_client import models |
| 6 | +from qdrant_client.async_qdrant_client import AsyncQdrantClient |
6 | 7 |
|
7 | 8 | from mcp_plex import loader |
8 | 9 |
|
9 | 10 |
|
10 | | -class DummyTextEmbedding: |
11 | | - def __init__(self, name: str): |
12 | | - self.embedding_size = 3 |
13 | | - |
14 | | - def embed(self, texts): |
15 | | - for _ in texts: |
16 | | - yield [0.1, 0.2, 0.3] |
17 | | - |
18 | | - |
19 | | -class DummyArray(list): |
20 | | - def tolist(self): |
21 | | - return list(self) |
22 | | - |
23 | | - |
24 | | -class DummySparseVector: |
25 | | - def __init__(self, indices, values): |
26 | | - self.indices = DummyArray(indices) |
27 | | - self.values = DummyArray(values) |
28 | | - |
29 | | - |
30 | | -class DummySparseEmbedding: |
31 | | - def __init__(self, name: str): |
32 | | - pass |
33 | | - |
34 | | - def passage_embed(self, texts): |
35 | | - for i, _ in enumerate(texts): |
36 | | - yield DummySparseVector([i], [1.0]) |
37 | | - |
38 | | - |
39 | | -class DummyQdrantClient: |
40 | | - instance = None |
41 | | - |
42 | | - def __init__(self, url: str | None = None, api_key: str | None = None, **kwargs): |
43 | | - self.collections = {} |
44 | | - self.upserted = [] |
45 | | - self.kwargs = kwargs |
46 | | - DummyQdrantClient.instance = self |
47 | | - |
48 | | - async def collection_exists(self, name: str) -> bool: |
49 | | - return name in self.collections |
50 | | - |
51 | | - async def get_collection(self, name: str): |
52 | | - return self.collections[name] |
53 | | - |
54 | | - async def delete_collection(self, name: str): |
55 | | - self.collections.pop(name, None) |
56 | | - |
57 | | - async def create_collection(self, collection_name: str, vectors_config, sparse_vectors_config): |
58 | | - size = vectors_config["dense"].size |
59 | | - params = SimpleNamespace(vectors={"dense": models.VectorParams(size=size, distance=models.Distance.COSINE)}) |
60 | | - self.collections[collection_name] = SimpleNamespace(config=SimpleNamespace(params=params)) |
61 | | - |
62 | | - async def create_payload_index(self, **kwargs): |
63 | | - return None |
64 | | - |
65 | | - async def upsert(self, collection_name: str, points): |
66 | | - self.upserted.extend(points) |
| 11 | +class CaptureClient(AsyncQdrantClient): |
| 12 | + instance: "CaptureClient" | None = None |
67 | 13 |
|
| 14 | + def __init__(self, *args, **kwargs): |
| 15 | + super().__init__(*args, **kwargs) |
| 16 | + CaptureClient.instance = self |
68 | 17 |
|
69 | | -class TrackingQdrantClient(DummyQdrantClient): |
70 | | - """Qdrant client that starts with a mismatched collection size.""" |
71 | 18 |
|
72 | | - def __init__(self, url: str | None = None, api_key: str | None = None, **kwargs): |
73 | | - super().__init__(url, api_key, **kwargs) |
74 | | - # Pre-create a collection with the wrong vector size to force recreation |
75 | | - wrong_params = SimpleNamespace( |
76 | | - vectors={ |
77 | | - "dense": models.VectorParams(size=99, distance=models.Distance.COSINE) |
78 | | - } |
79 | | - ) |
80 | | - self.collections["media-items"] = SimpleNamespace( |
81 | | - config=SimpleNamespace(params=wrong_params) |
82 | | - ) |
83 | | - self.deleted = False |
84 | | - |
85 | | - async def delete_collection(self, name: str): |
86 | | - self.deleted = True |
87 | | - await super().delete_collection(name) |
88 | | - |
89 | | - |
90 | | -async def _run_loader(sample_dir: Path): |
91 | | - await loader.run(None, None, None, sample_dir, None, None) |
| 19 | +async def _run_loader(sample_dir: Path) -> None: |
| 20 | + await loader.run( |
| 21 | + None, |
| 22 | + None, |
| 23 | + None, |
| 24 | + sample_dir, |
| 25 | + None, |
| 26 | + None, |
| 27 | + ) |
92 | 28 |
|
93 | 29 |
|
94 | 30 | def test_run_writes_points(monkeypatch): |
95 | | - monkeypatch.setattr(loader, "TextEmbedding", DummyTextEmbedding) |
96 | | - monkeypatch.setattr(loader, "SparseTextEmbedding", DummySparseEmbedding) |
97 | | - monkeypatch.setattr(loader, "AsyncQdrantClient", DummyQdrantClient) |
98 | | - sample_dir = Path(__file__).resolve().parents[1] / "sample-data" |
99 | | - asyncio.run(_run_loader(sample_dir)) |
100 | | - client = DummyQdrantClient.instance |
101 | | - assert client is not None |
102 | | - assert len(client.upserted) == 2 |
103 | | - payloads = [p.payload for p in client.upserted] |
104 | | - assert all("title" in p and "type" in p for p in payloads) |
105 | | - |
106 | | - |
107 | | -def test_run_recreates_mismatched_collection(monkeypatch): |
108 | | - monkeypatch.setattr(loader, "TextEmbedding", DummyTextEmbedding) |
109 | | - monkeypatch.setattr(loader, "SparseTextEmbedding", DummySparseEmbedding) |
110 | | - monkeypatch.setattr(loader, "AsyncQdrantClient", TrackingQdrantClient) |
| 31 | + monkeypatch.setattr(loader, "AsyncQdrantClient", CaptureClient) |
111 | 32 | sample_dir = Path(__file__).resolve().parents[1] / "sample-data" |
112 | 33 | asyncio.run(_run_loader(sample_dir)) |
113 | | - client = TrackingQdrantClient.instance |
| 34 | + client = CaptureClient.instance |
114 | 35 | assert client is not None |
115 | | - # The pre-created collection should have been deleted and recreated |
116 | | - assert client.deleted is True |
117 | | - assert ( |
118 | | - client.collections["media-items"].config.params.vectors["dense"].size |
119 | | - == 3 |
120 | | - ) |
121 | | - |
122 | | - |
123 | | -def test_run_uses_connection_options(monkeypatch): |
124 | | - monkeypatch.setattr(loader, "TextEmbedding", DummyTextEmbedding) |
125 | | - monkeypatch.setattr(loader, "SparseTextEmbedding", DummySparseEmbedding) |
| 36 | + points, _ = asyncio.run(client.scroll("media-items", limit=10, with_payload=True)) |
| 37 | + assert len(points) == 2 |
| 38 | + assert all("title" in p.payload and "type" in p.payload for p in points) |
126 | 39 |
|
127 | | - captured = {} |
128 | 40 |
|
129 | | - class CaptureClient(DummyQdrantClient): |
130 | | - def __init__(self, url: str | None = None, api_key: str | None = None, **kwargs): |
131 | | - super().__init__(url, api_key, **kwargs) |
132 | | - captured.update(kwargs) |
133 | | - |
134 | | - monkeypatch.setattr(loader, "AsyncQdrantClient", CaptureClient) |
135 | | - sample_dir = Path(__file__).resolve().parents[1] / "sample-data" |
136 | | - asyncio.run( |
137 | | - loader.run( |
138 | | - None, |
139 | | - None, |
140 | | - None, |
141 | | - sample_dir, |
142 | | - None, |
143 | | - None, |
144 | | - qdrant_host="example", |
145 | | - qdrant_port=1111, |
146 | | - qdrant_grpc_port=2222, |
147 | | - qdrant_https=True, |
148 | | - qdrant_prefer_grpc=True, |
149 | | - ) |
150 | | - ) |
151 | | - assert captured["host"] == "example" |
152 | | - assert captured["port"] == 1111 |
153 | | - assert captured["grpc_port"] == 2222 |
154 | | - assert captured["https"] is True |
155 | | - assert captured["prefer_grpc"] is True |
0 commit comments