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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
import asyncio
import io
import uuid
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock, Mock
import pytest
from fastapi import UploadFile
from r2r.base import (
Document,
DocumentInfo,
R2RDocumentProcessingError,
R2RException,
generate_id_from_label,
)
from r2r.main import R2RPipelines, R2RProviders
from r2r.main.services.ingestion_service import IngestionService
@pytest.fixture(scope="session", autouse=True)
def event_loop_policy():
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
@pytest.fixture(scope="function")
def event_loop():
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
asyncio.set_event_loop(None)
@pytest.fixture(scope="session", autouse=True)
async def cleanup_tasks():
yield
for task in asyncio.all_tasks():
if task is not asyncio.current_task():
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
@pytest.fixture
def mock_vector_db():
mock_db = MagicMock()
mock_db.get_documents_overview.return_value = [] # Default to empty list
return mock_db
@pytest.fixture
def mock_embedding_model():
return Mock()
@pytest.fixture
def ingestion_service(mock_vector_db, mock_embedding_model):
config = MagicMock()
config.app.get.return_value = 32 # Default max file size
providers = Mock(spec=R2RProviders)
providers.vector_db = mock_vector_db
providers.embedding_model = mock_embedding_model
pipelines = Mock(spec=R2RPipelines)
pipelines.ingestion_pipeline = AsyncMock()
pipelines.ingestion_pipeline.run.return_value = {
"embedding_pipeline_output": []
}
run_manager = Mock()
run_manager.run_info = {"mock_run_id": {}}
logging_connection = AsyncMock()
return IngestionService(
config, providers, pipelines, run_manager, logging_connection
)
@pytest.mark.asyncio
async def test_ingest_single_document(ingestion_service, mock_vector_db):
try:
document = Document(
id=generate_id_from_label("test_id"),
data="Test content",
type="txt",
metadata={},
)
ingestion_service.pipelines.ingestion_pipeline.run.return_value = {
"embedding_pipeline_output": [(document.id, None)]
}
mock_vector_db.get_documents_overview.return_value = (
[]
) # No existing documents
result = await ingestion_service.ingest_documents([document])
assert result["processed_documents"] == [
f"Document '{document.id}' processed successfully."
]
assert not result["failed_documents"]
assert not result["skipped_documents"]
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_ingest_duplicate_document(ingestion_service, mock_vector_db):
try:
document = Document(
id=generate_id_from_label("test_id"),
data="Test content",
type="txt",
metadata={},
)
mock_vector_db.get_documents_overview.return_value = [
DocumentInfo(
document_id=document.id,
version="v0",
size_in_bytes=len(document.data),
metadata={},
title=str(document.id),
user_id=None,
created_at=datetime.now(),
updated_at=datetime.now(),
status="success",
)
]
with pytest.raises(R2RException) as exc_info:
await ingestion_service.ingest_documents([document])
assert (
f"Document with ID {document.id} was already successfully processed"
in str(exc_info.value)
)
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_ingest_file(ingestion_service):
try:
file_content = b"Test content"
file_mock = UploadFile(
filename="test.txt", file=io.BytesIO(file_content)
)
file_mock.file.seek(0)
file_mock.size = len(file_content) # Set file size manually
ingestion_service.pipelines.ingestion_pipeline.run.return_value = {
"embedding_pipeline_output": [
(generate_id_from_label("test.txt"), None)
]
}
result = await ingestion_service.ingest_files([file_mock])
assert len(result["processed_documents"]) == 1
assert not result["failed_documents"]
assert not result["skipped_documents"]
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_ingest_mixed_success_and_failure(
ingestion_service, mock_vector_db
):
try:
documents = [
Document(
id=generate_id_from_label("success_id"),
data="Success content",
type="txt",
metadata={},
),
Document(
id=generate_id_from_label("failure_id"),
data="Failure content",
type="txt",
metadata={},
),
]
ingestion_service.pipelines.ingestion_pipeline.run.return_value = {
"embedding_pipeline_output": [
(
documents[0].id,
f"Processed 1 vectors for document {documents[0].id}.",
),
(
documents[1].id,
R2RDocumentProcessingError(
error_message="Embedding failed",
document_id=documents[1].id,
),
),
]
}
result = await ingestion_service.ingest_documents(documents)
assert len(result["processed_documents"]) == 1
assert len(result["failed_documents"]) == 1
assert str(documents[0].id) in result["processed_documents"][0]
assert str(documents[1].id) in result["failed_documents"][0]
assert "Embedding failed" in result["failed_documents"][0]
assert mock_vector_db.upsert_documents_overview.call_count == 2
upserted_docs = mock_vector_db.upsert_documents_overview.call_args[0][
0
]
assert len(upserted_docs) == 2
assert upserted_docs[0].document_id == documents[0].id
assert upserted_docs[0].status == "success"
assert upserted_docs[1].document_id == documents[1].id
assert upserted_docs[1].status == "failure"
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_ingest_unsupported_file_type(ingestion_service):
try:
file_mock = UploadFile(
filename="test.unsupported", file=io.BytesIO(b"Test content")
)
file_mock.file.seek(0)
file_mock.size = 12 # Set file size manually
with pytest.raises(R2RException) as exc_info:
await ingestion_service.ingest_files([file_mock])
assert "is not a valid DocumentType" in str(exc_info.value)
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_ingest_large_file(ingestion_service):
try:
large_content = b"Large content" * 1000000 # 12MB content
file_mock = UploadFile(
filename="large_file.txt", file=io.BytesIO(large_content)
)
file_mock.file.seek(0)
file_mock.size = len(large_content) # Set file size manually
ingestion_service.config.app.get.return_value = (
10 # Set max file size to 10MB
)
with pytest.raises(R2RException) as exc_info:
await ingestion_service.ingest_files([file_mock])
assert "File size exceeds maximum allowed size" in str(exc_info.value)
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_partial_ingestion_success(ingestion_service, mock_vector_db):
try:
documents = [
Document(
id=generate_id_from_label("success_1"),
data="Success content 1",
type="txt",
metadata={},
),
Document(
id=generate_id_from_label("fail"),
data="Fail content",
type="txt",
metadata={},
),
Document(
id=generate_id_from_label("success_2"),
data="Success content 2",
type="txt",
metadata={},
),
]
ingestion_service.pipelines.ingestion_pipeline.run.return_value = {
"embedding_pipeline_output": [
(documents[0].id, None),
(
documents[1].id,
R2RDocumentProcessingError(
error_message="Embedding failed",
document_id=documents[1].id,
),
),
(documents[2].id, None),
]
}
result = await ingestion_service.ingest_documents(documents)
assert len(result["processed_documents"]) == 2
assert len(result["failed_documents"]) == 1
assert str(documents[1].id) in result["failed_documents"][0]
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_version_increment(ingestion_service, mock_vector_db):
try:
document = Document(
id=generate_id_from_label("test_id"),
data="Test content",
type="txt",
metadata={},
)
mock_vector_db.get_documents_overview.return_value = [
DocumentInfo(
document_id=document.id,
version="v2",
status="success",
size_in_bytes=0,
metadata={},
)
]
file_mock = UploadFile(
filename="test.txt", file=io.BytesIO(b"Updated content")
)
await ingestion_service.update_files([file_mock], [document.id])
calls = mock_vector_db.upsert_documents_overview.call_args_list
assert len(calls) == 2
assert calls[1][0][0][0].version == "v3"
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_process_ingestion_results_error_handling(ingestion_service):
try:
document_infos = [
DocumentInfo(
document_id=uuid.uuid4(),
version="v0",
status="processing",
size_in_bytes=0,
metadata={},
)
]
ingestion_results = {
"embedding_pipeline_output": [
(
document_infos[0].document_id,
R2RDocumentProcessingError(
"Unexpected error",
document_id=document_infos[0].document_id,
),
)
]
}
result = await ingestion_service._process_ingestion_results(
ingestion_results,
document_infos,
[],
{document_infos[0].document_id: "test"},
)
assert len(result["failed_documents"]) == 1
assert "Unexpected error" in result["failed_documents"][0]
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_file_size_limit_edge_cases(ingestion_service):
try:
ingestion_service.config.app.get.return_value = 1 # 1MB limit
just_under_limit = b"x" * (1024 * 1024 - 1)
at_limit = b"x" * (1024 * 1024)
over_limit = b"x" * (1024 * 1024 + 1)
file_under = UploadFile(
filename="under.txt",
file=io.BytesIO(just_under_limit),
size=1024 * 1024 - 1,
)
file_at = UploadFile(
filename="at.txt", file=io.BytesIO(at_limit), size=1024 * 1024
)
file_over = UploadFile(
filename="over.txt",
file=io.BytesIO(over_limit),
size=1024 * 1024 + 1,
)
await ingestion_service.ingest_files([file_under]) # Should succeed
await ingestion_service.ingest_files([file_at]) # Should succeed
with pytest.raises(
R2RException, match="File size exceeds maximum allowed size"
):
await ingestion_service.ingest_files([file_over])
except asyncio.CancelledError:
pass
@pytest.mark.asyncio
async def test_document_status_update_after_ingestion(
ingestion_service, mock_vector_db
):
try:
document = Document(
id=generate_id_from_label("test_id"),
data="Test content",
type="txt",
metadata={},
)
ingestion_service.pipelines.ingestion_pipeline.run.return_value = {
"embedding_pipeline_output": [(document.id, None)]
}
mock_vector_db.get_documents_overview.return_value = (
[]
) # No existing documents
await ingestion_service.ingest_documents([document])
# Check that upsert_documents_overview was called twice
assert mock_vector_db.upsert_documents_overview.call_count == 2
# Check the second call to upsert_documents_overview (status update)
second_call_args = (
mock_vector_db.upsert_documents_overview.call_args_list[1][0][0]
)
assert len(second_call_args) == 1
assert second_call_args[0].document_id == document.id
assert second_call_args[0].status == "success"
except asyncio.CancelledError:
pass
|