|
| 1 | +import pytest |
| 2 | + |
| 3 | +from ragbits.core.embeddings.noop import NoopEmbedder |
| 4 | +from ragbits.core.vector_stores.in_memory import InMemoryVectorStore |
| 5 | +from ragbits.document_search import DocumentSearch |
| 6 | +from ragbits.document_search.documents.document import Document, DocumentMeta, DocumentType |
| 7 | +from ragbits.document_search.documents.element import Element |
| 8 | +from ragbits.document_search.ingestion.parsers.base import DocumentParser |
| 9 | +from ragbits.document_search.ingestion.parsers.router import DocumentParserRouter |
| 10 | +from ragbits.document_search.ingestion.strategies.base import IngestExecutionError |
| 11 | + |
| 12 | + |
| 13 | +class FailingParser(DocumentParser): |
| 14 | + """A parser that always raises an exception.""" |
| 15 | + |
| 16 | + supported_document_types = {DocumentType.TXT} |
| 17 | + |
| 18 | + @classmethod |
| 19 | + async def parse(cls, document: Document) -> list[Element]: |
| 20 | + raise ValueError("This parser always fails") |
| 21 | + |
| 22 | + |
| 23 | +async def test_ingest_fails_on_error(): |
| 24 | + # Create a document search instance with a failing parser |
| 25 | + document_search = DocumentSearch( |
| 26 | + vector_store=InMemoryVectorStore(embedder=NoopEmbedder()), |
| 27 | + parser_router=DocumentParserRouter({DocumentType.TXT: FailingParser()}), |
| 28 | + ) |
| 29 | + |
| 30 | + # Create a test document |
| 31 | + document = DocumentMeta.create_text_document_from_literal("Test content") |
| 32 | + |
| 33 | + # Test that ingest raises IngestExecutionError when fail_on_error=True (default) |
| 34 | + with pytest.raises(IngestExecutionError) as exc_info: |
| 35 | + await document_search.ingest([document]) |
| 36 | + |
| 37 | + # Verify the error details |
| 38 | + assert len(exc_info.value.results) == 1 |
| 39 | + failed_result = exc_info.value.results[0] |
| 40 | + assert failed_result.document_uri == document.id |
| 41 | + assert failed_result.num_elements == 0 |
| 42 | + assert failed_result.error is not None |
| 43 | + assert isinstance(failed_result.error.type, type(ValueError)) |
| 44 | + assert failed_result.error.message == "This parser always fails" |
| 45 | + |
| 46 | + |
| 47 | +async def test_ingest_returns_errors_when_fail_on_error_false(): |
| 48 | + # Create a document search instance with a failing parser |
| 49 | + document_search = DocumentSearch( |
| 50 | + vector_store=InMemoryVectorStore(embedder=NoopEmbedder()), |
| 51 | + parser_router=DocumentParserRouter({DocumentType.TXT: FailingParser()}), |
| 52 | + ) |
| 53 | + |
| 54 | + # Create a test document |
| 55 | + document = DocumentMeta.create_text_document_from_literal("Test content") |
| 56 | + |
| 57 | + # Test that ingest returns errors when fail_on_error=False |
| 58 | + result = await document_search.ingest([document], fail_on_error=False) |
| 59 | + |
| 60 | + # Verify the result details |
| 61 | + assert len(result.successful) == 0 |
| 62 | + assert len(result.failed) == 1 |
| 63 | + failed_result = result.failed[0] |
| 64 | + assert failed_result.document_uri == document.id |
| 65 | + assert failed_result.num_elements == 0 |
| 66 | + assert failed_result.error is not None |
| 67 | + assert isinstance(failed_result.error.type, type(ValueError)) |
| 68 | + assert failed_result.error.message == "This parser always fails" |
0 commit comments