Spaces:
Paused
Paused
File size: 11,744 Bytes
ad33df7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
import os
from unittest.mock import patch
import pytest
from elastic_transport import ApiResponseMeta
from kotaemon.base import Document
from kotaemon.storages import (
ElasticsearchDocumentStore,
InMemoryDocumentStore,
SimpleFileDocumentStore,
)
meta_success = ApiResponseMeta(
status=200,
http_version="1.1",
headers={"x-elastic-product": "Elasticsearch"},
duration=1.0,
node=None,
)
meta_fail = ApiResponseMeta(
status=404,
http_version="1.1",
headers={"x-elastic-product": "Elasticsearch"},
duration=1.0,
node=None,
)
_elastic_search_responses = [
# check exist
(meta_fail, None),
# create index
(
meta_success,
{"acknowledged": True, "shards_acknowledged": True, "index": "test"},
),
# count API
(
meta_success,
[{"epoch": "1700474422", "timestamp": "10:00:22", "count": "0"}],
),
# add documents
(
meta_success,
{
"took": 50,
"errors": False,
"items": [
{
"index": {
"_index": "test",
"_id": "a3774dab-b8f1-43ba-adb8-842cb7a76eeb",
"_version": 1,
"result": "created",
"_shards": {"total": 2, "successful": 1, "failed": 0},
"_seq_no": 0,
"_primary_term": 1,
"status": 201,
}
},
{
"index": {
"_index": "test",
"_id": "b44f5593-7587-4f91-afd0-5736e5bd5bfe",
"_version": 1,
"result": "created",
"_shards": {"total": 2, "successful": 1, "failed": 0},
"_seq_no": 1,
"_primary_term": 1,
"status": 201,
}
},
{
"index": {
"_index": "test",
"_id": "13ae7825-eef9-4214-a164-983c2e6bbeaa",
"_version": 1,
"result": "created",
"_shards": {"total": 2, "successful": 1, "failed": 0},
"_seq_no": 2,
"_primary_term": 1,
"status": 201,
}
},
],
},
),
# check exist
(
meta_success,
{"_shards": {"total": 2, "successful": 1, "failed": 0}},
),
# count
(
meta_success,
[{"epoch": "1700474422", "timestamp": "10:00:22", "count": "3"}],
),
# get_all
(
meta_success,
{
"took": 1,
"timed_out": False,
"_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0},
"hits": {
"total": {"value": 3, "relation": "eq"},
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_id": "a3774dab-b8f1-43ba-adb8-842cb7a76eeb",
"_score": 1.0,
"_source": {"content": "Sample text 0", "metadata": {}},
},
{
"_index": "test",
"_id": "b44f5593-7587-4f91-afd0-5736e5bd5bfe",
"_score": 1.0,
"_source": {"content": "Sample text 1", "metadata": {}},
},
{
"_index": "test",
"_id": "13ae7825-eef9-4214-a164-983c2e6bbeaa",
"_score": 1.0,
"_source": {"content": "Sample text 2", "metadata": {}},
},
],
},
},
),
# get by-id
(
meta_success,
{
"took": 1,
"timed_out": False,
"_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0},
"hits": {
"total": {"value": 1, "relation": "eq"},
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_id": "a3774dab-b8f1-43ba-adb8-842cb7a76eeb",
"_score": 1.0,
"_source": {"content": "Sample text 0", "metadata": {}},
}
],
},
},
),
# query
(
meta_success,
{
"took": 2,
"timed_out": False,
"_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0},
"hits": {
"total": {"value": 3, "relation": "eq"},
"max_score": 0.13353139,
"hits": [
{
"_index": "test",
"_id": "a3774dab-b8f1-43ba-adb8-842cb7a76eeb",
"_score": 0.13353139,
"_source": {"content": "Sample text 0", "metadata": {}},
},
{
"_index": "test",
"_id": "b44f5593-7587-4f91-afd0-5736e5bd5bfe",
"_score": 0.13353139,
"_source": {"content": "Sample text 1", "metadata": {}},
},
{
"_index": "test",
"_id": "13ae7825-eef9-4214-a164-983c2e6bbeaa",
"_score": 0.13353139,
"_source": {"content": "Sample text 2", "metadata": {}},
},
],
},
},
),
# delete
(
meta_success,
{
"took": 10,
"timed_out": False,
"total": 1,
"deleted": 1,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {"bulk": 0, "search": 0},
"throttled_millis": 0,
"requests_per_second": -1.0,
"throttled_until_millis": 0,
"failures": [],
},
),
# check exists
(
meta_success,
{"_shards": {"total": 2, "successful": 1, "failed": 0}},
),
# count
(
meta_success,
[{"epoch": "1700549363", "timestamp": "06:49:23", "count": "2"}],
),
]
def test_inmemory_document_store_base_interfaces(tmp_path):
"""Test all interfaces of a a document store"""
store = InMemoryDocumentStore()
docs = [
Document(text=f"Sample text {idx}", meta={"meta_key": f"meta_value_{idx}"})
for idx in range(10)
]
# Test add and get all
assert len(store.get_all()) == 0, "Document store should be empty"
store.add(docs)
assert len(store.get_all()) == 10, "Document store should have 10 documents"
# Test add with provided ids
store.add(docs=docs, ids=[f"doc_{idx}" for idx in range(10)])
assert len(store.get_all()) == 20, "Document store should have 20 documents"
# Test add without exist_ok
with pytest.raises(ValueError):
store.add(docs=docs, ids=[f"doc_{idx}" for idx in range(10)])
# Update ok with add exist_ok
store.add(docs=docs, ids=[f"doc_{idx}" for idx in range(10)], exist_ok=True)
assert len(store.get_all()) == 20, "Document store should have 20 documents"
# Test get with str id
matched = store.get(docs[0].doc_id)
assert len(matched) == 1, "Should return 1 document"
assert matched[0].text == docs[0].text, "Should return the correct document"
# Test get with list of ids
matched = store.get([docs[0].doc_id, docs[1].doc_id])
assert len(matched) == 2, "Should return 2 documents"
assert [doc.text for doc in matched] == [doc.text for doc in docs[:2]]
# Test delete with str id
store.delete(docs[0].doc_id)
assert len(store.get_all()) == 19, "Document store should have 19 documents"
# Test delete with list of ids
store.delete([docs[1].doc_id, docs[2].doc_id])
assert len(store.get_all()) == 17, "Document store should have 17 documents"
# Test save
store.save(tmp_path / "store.json")
assert (tmp_path / "store.json").exists(), "File should exist"
# Test load
store2 = InMemoryDocumentStore()
store2.load(tmp_path / "store.json")
assert len(store2.get_all()) == 17, "Laded document store should have 17 documents"
os.remove(tmp_path / "store.json")
def test_simplefile_document_store_base_interfaces(tmp_path):
"""Test all interfaces of a a document store"""
store = SimpleFileDocumentStore(path=tmp_path)
docs = [
Document(text=f"Sample text {idx}", meta={"meta_key": f"meta_value_{idx}"})
for idx in range(10)
]
# Test add and get all
assert len(store.get_all()) == 0, "Document store should be empty"
store.add(docs)
assert len(store.get_all()) == 10, "Document store should have 10 documents"
# Test add with provided ids
store.add(docs=docs, ids=[f"doc_{idx}" for idx in range(10)])
assert len(store.get_all()) == 20, "Document store should have 20 documents"
# Test add without exist_ok
with pytest.raises(ValueError):
store.add(docs=docs, ids=[f"doc_{idx}" for idx in range(10)])
# Update ok with add exist_ok
store.add(docs=docs, ids=[f"doc_{idx}" for idx in range(10)], exist_ok=True)
assert len(store.get_all()) == 20, "Document store should have 20 documents"
# Test get with str id
matched = store.get(docs[0].doc_id)
assert len(matched) == 1, "Should return 1 document"
assert matched[0].text == docs[0].text, "Should return the correct document"
# Test get with list of ids
matched = store.get([docs[0].doc_id, docs[1].doc_id])
assert len(matched) == 2, "Should return 2 documents"
assert [doc.text for doc in matched] == [doc.text for doc in docs[:2]]
# Test delete with str id
store.delete(docs[0].doc_id)
assert len(store.get_all()) == 19, "Document store should have 19 documents"
# Test delete with list of ids
store.delete([docs[1].doc_id, docs[2].doc_id])
assert len(store.get_all()) == 17, "Document store should have 17 documents"
# Test save
assert (tmp_path / "default.json").exists(), "File should exist"
# Test load
store2 = SimpleFileDocumentStore(path=tmp_path)
assert len(store2.get_all()) == 17, "Laded document store should have 17 documents"
os.remove(tmp_path / "default.json")
@patch(
"elastic_transport.Transport.perform_request",
side_effect=_elastic_search_responses,
)
def test_elastic_document_store(elastic_api):
store = ElasticsearchDocumentStore(collection_name="test")
docs = [
Document(text=f"Sample text {idx}", meta={"meta_key": f"meta_value_{idx}"})
for idx in range(3)
]
# Test add and get all
assert store.count() == 0, "Document store should be empty"
store.add(docs)
assert store.count() == 3, "Document store count should changed after adding docs"
docs = store.get_all()
first_doc = docs[0]
assert len(docs) == 3, "Document store get_all() failed"
doc_by_ids = store.get(first_doc.doc_id)
assert doc_by_ids[0].doc_id == first_doc.doc_id, "Document store get() failed"
docs = store.query("text")
assert len(docs) == 3, "Document store query() failed"
# delete test
store.delete(first_doc.doc_id)
assert store.count() == 2, "Document store delete() failed"
elastic_api.assert_called()
|