|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | +from uuid import UUID |
| 3 | +from rag.ingestion_pipeline import IngestionPipeline |
| 4 | +from langchain_core.documents import Document |
| 5 | + |
| 6 | + |
| 7 | +def test_load_document_returns_documents(): |
| 8 | + with patch("rag.ingestion_pipeline.PyPDFLoader") as mock_loader: |
| 9 | + mock_loader.return_value.load.return_value = [ |
| 10 | + Document(page_content="Test") |
| 11 | + ] |
| 12 | + pipeline = IngestionPipeline(vector_store=MagicMock()) |
| 13 | + docs = pipeline.load_document("fake_path.pdf") |
| 14 | + assert isinstance(docs, list) |
| 15 | + assert isinstance(docs[0], Document) |
| 16 | + assert docs[0].page_content == "Test" |
| 17 | + |
| 18 | + |
| 19 | +def test_chunk_documents_returns_chunks(): |
| 20 | + pipeline = IngestionPipeline(vector_store=MagicMock()) |
| 21 | + dummy_doc = Document( |
| 22 | + page_content="This is a long text. " * 100, |
| 23 | + metadata={} |
| 24 | + ) |
| 25 | + chunks = pipeline.chunk_documents([dummy_doc], filename="sample.pdf") |
| 26 | + assert isinstance(chunks, list) |
| 27 | + assert all(isinstance(doc, Document) for doc in chunks) |
| 28 | + assert all(doc.metadata["source"] == "sample.pdf" for doc in chunks) |
| 29 | + |
| 30 | + |
| 31 | +def test_store_documents_calls_add_documents_with_uuids(): |
| 32 | + mock_vector_store = MagicMock() |
| 33 | + pipeline = IngestionPipeline(vector_store=mock_vector_store) |
| 34 | + docs = [Document(page_content="Chunk", metadata={}) for _ in range(3)] |
| 35 | + pipeline.store_documents(docs) |
| 36 | + args, kwargs = mock_vector_store.add_documents.call_args |
| 37 | + passed_docs = args[0] |
| 38 | + passed_ids = kwargs["ids"] |
| 39 | + assert len(passed_docs) == 3 |
| 40 | + assert len(passed_ids) == 3 |
| 41 | + assert all(UUID(uid) for uid in passed_ids) |
| 42 | + |
| 43 | + |
| 44 | +def test_ingest_calls_all_steps(): |
| 45 | + pipeline = IngestionPipeline(vector_store=MagicMock()) |
| 46 | + |
| 47 | + with patch.object(pipeline, "load_document") as mock_load, \ |
| 48 | + patch.object(pipeline, "chunk_documents") as mock_chunk, \ |
| 49 | + patch.object(pipeline, "store_documents") as mock_store, \ |
| 50 | + patch("rag.ingestion_pipeline.logger") as mock_logger, \ |
| 51 | + patch("rag.ingestion_pipeline.file_ingestion_duration.observe"), \ |
| 52 | + patch("rag.ingestion_pipeline.file_ingested_counter.inc"): |
| 53 | + |
| 54 | + mock_load.return_value = [Document(page_content="Doc")] |
| 55 | + mock_chunk.return_value = [Document(page_content="Chunk")] |
| 56 | + |
| 57 | + pipeline.ingest("test.pdf", "testfile.pdf") |
| 58 | + |
| 59 | + mock_load.assert_called_once_with("test.pdf") |
| 60 | + mock_chunk.assert_called_once() |
| 61 | + mock_store.assert_called_once() |
| 62 | + mock_logger.info.assert_any_call( |
| 63 | + "Documents are loaded for file %s", |
| 64 | + "testfile.pdf" |
| 65 | + ) |
0 commit comments