File size: 8,976 Bytes
d520909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Integration Tests for RAG Pipeline

Tests the full RAG workflow:
- Vector store operations
- Embedding generation
- Document retrieval
- Answer generation
"""

import pytest
from pathlib import Path
from unittest.mock import Mock, patch, MagicMock
import json


class TestVectorStore:
    """Test vector store functionality."""

    def test_vector_store_config(self):
        """Test VectorStoreConfig creation."""
        from src.rag.store import VectorStoreConfig

        config = VectorStoreConfig(
            collection_name="test_collection",
            default_top_k=10,
            similarity_threshold=0.8,
        )

        assert config.collection_name == "test_collection"
        assert config.default_top_k == 10

    def test_vector_search_result(self):
        """Test VectorSearchResult model."""
        from src.rag.store import VectorSearchResult

        result = VectorSearchResult(
            chunk_id="chunk_1",
            document_id="doc_1",
            text="Sample text",
            metadata={"page": 0},
            similarity=0.85,
            page=0,
            chunk_type="text",
        )

        assert result.similarity == 0.85
        assert result.chunk_id == "chunk_1"

    @pytest.mark.skipif(
        not pytest.importorskip("chromadb", reason="ChromaDB not installed"),
        reason="ChromaDB not available"
    )
    def test_chromadb_store_creation(self, tmp_path):
        """Test ChromaDB store creation."""
        from src.rag.store import ChromaVectorStore, VectorStoreConfig

        config = VectorStoreConfig(
            persist_directory=str(tmp_path / "vectorstore"),
            collection_name="test_collection",
        )

        store = ChromaVectorStore(config)
        assert store.count() == 0


class TestEmbeddings:
    """Test embedding functionality."""

    def test_embedding_config(self):
        """Test EmbeddingConfig creation."""
        from src.rag.embeddings import EmbeddingConfig

        config = EmbeddingConfig(
            adapter_type="ollama",
            ollama_model="nomic-embed-text",
            batch_size=16,
        )

        assert config.adapter_type == "ollama"
        assert config.batch_size == 16

    def test_embedding_cache_creation(self, tmp_path):
        """Test EmbeddingCache creation."""
        from src.rag.embeddings import EmbeddingCache

        cache = EmbeddingCache(str(tmp_path), "test_model")
        assert cache.cache_dir.exists()

    def test_embedding_cache_operations(self, tmp_path):
        """Test EmbeddingCache get/put operations."""
        from src.rag.embeddings import EmbeddingCache

        cache = EmbeddingCache(str(tmp_path), "test_model")

        # Test put and get
        test_text = "Hello world"
        test_embedding = [0.1, 0.2, 0.3, 0.4]

        cache.put(test_text, test_embedding)
        retrieved = cache.get(test_text)

        assert retrieved == test_embedding

    def test_ollama_embedding_dimensions(self):
        """Test OllamaEmbedding model dimensions mapping."""
        from src.rag.embeddings import OllamaEmbedding

        assert OllamaEmbedding.MODEL_DIMENSIONS["nomic-embed-text"] == 768
        assert OllamaEmbedding.MODEL_DIMENSIONS["mxbai-embed-large"] == 1024


class TestRetriever:
    """Test retriever functionality."""

    def test_retriever_config(self):
        """Test RetrieverConfig creation."""
        from src.rag.retriever import RetrieverConfig

        config = RetrieverConfig(
            default_top_k=10,
            similarity_threshold=0.75,
            include_evidence=True,
        )

        assert config.default_top_k == 10
        assert config.include_evidence is True

    def test_retrieved_chunk(self):
        """Test RetrievedChunk model."""
        from src.rag.retriever import RetrievedChunk

        chunk = RetrievedChunk(
            chunk_id="chunk_1",
            document_id="doc_1",
            text="Sample retrieved text",
            similarity=0.9,
            page=0,
            chunk_type="text",
        )

        assert chunk.similarity == 0.9


class TestGenerator:
    """Test generator functionality."""

    def test_generator_config(self):
        """Test GeneratorConfig creation."""
        from src.rag.generator import GeneratorConfig

        config = GeneratorConfig(
            llm_provider="ollama",
            ollama_model="llama3.2:3b",
            temperature=0.1,
            require_citations=True,
        )

        assert config.llm_provider == "ollama"
        assert config.require_citations is True

    def test_citation_model(self):
        """Test Citation model."""
        from src.rag.generator import Citation

        citation = Citation(
            index=1,
            chunk_id="chunk_1",
            page=0,
            text_snippet="Sample snippet",
            confidence=0.85,
        )

        assert citation.index == 1
        assert citation.confidence == 0.85

    def test_generated_answer_model(self):
        """Test GeneratedAnswer model."""
        from src.rag.generator import GeneratedAnswer, Citation

        answer = GeneratedAnswer(
            answer="This is the generated answer.",
            citations=[
                Citation(
                    index=1,
                    chunk_id="chunk_1",
                    page=0,
                    text_snippet="Evidence text",
                    confidence=0.9,
                )
            ],
            confidence=0.85,
            abstained=False,
            num_chunks_used=3,
            query="What is the answer?",
        )

        assert answer.answer == "This is the generated answer."
        assert len(answer.citations) == 1
        assert answer.abstained is False

    def test_abstention(self):
        """Test abstention behavior."""
        from src.rag.generator import GeneratedAnswer

        answer = GeneratedAnswer(
            answer="I cannot provide a confident answer.",
            citations=[],
            confidence=0.3,
            abstained=True,
            abstain_reason="Low confidence",
            num_chunks_used=2,
            query="Complex question",
        )

        assert answer.abstained is True
        assert answer.abstain_reason == "Low confidence"


class TestIndexer:
    """Test indexer functionality."""

    def test_indexer_config(self):
        """Test IndexerConfig creation."""
        from src.rag.indexer import IndexerConfig

        config = IndexerConfig(
            batch_size=64,
            include_bbox=True,
            skip_empty_chunks=True,
        )

        assert config.batch_size == 64

    def test_indexing_result(self):
        """Test IndexingResult model."""
        from src.rag.indexer import IndexingResult

        result = IndexingResult(
            document_id="doc_1",
            source_path="/path/to/doc.pdf",
            num_chunks_indexed=10,
            num_chunks_skipped=2,
            success=True,
        )

        assert result.success is True
        assert result.num_chunks_indexed == 10


class TestRAGIntegration:
    """Integration tests for full RAG pipeline."""

    @pytest.fixture
    def mock_chunks(self):
        """Create mock document chunks."""
        from src.rag.retriever import RetrievedChunk

        return [
            RetrievedChunk(
                chunk_id=f"chunk_{i}",
                document_id="doc_1",
                text=f"This is sample text from chunk {i}.",
                similarity=0.9 - (i * 0.1),
                page=i,
                chunk_type="text",
            )
            for i in range(3)
        ]

    def test_context_building(self, mock_chunks):
        """Test building context from chunks."""
        from src.rag.retriever import DocumentRetriever

        retriever = DocumentRetriever()

        context = retriever.build_context(mock_chunks, include_metadata=True)

        assert "chunk 0" in context.lower()
        assert "Page 1" in context  # Page numbers are 1-indexed in display

    def test_citation_extraction(self):
        """Test citation extraction from text."""
        from src.rag.generator import GroundedGenerator
        from src.rag.retriever import RetrievedChunk

        generator = GroundedGenerator()

        chunks = [
            RetrievedChunk(
                chunk_id="chunk_1",
                document_id="doc_1",
                text="First chunk content",
                similarity=0.9,
                page=0,
            ),
            RetrievedChunk(
                chunk_id="chunk_2",
                document_id="doc_1",
                text="Second chunk content",
                similarity=0.85,
                page=1,
            ),
        ]

        answer_text = "The answer is based on [1] and [2]."

        citations = generator._extract_citations(answer_text, chunks)

        assert len(citations) == 2
        assert citations[0].index == 1
        assert citations[1].index == 2