|
| 1 | +""" |
| 2 | +Integration tests for Pipeline Reranking Order (Issue #543, PR #544). |
| 3 | +
|
| 4 | +Tests verify that reranking happens BEFORE LLM generation, not after. |
| 5 | +
|
| 6 | +Test Strategy: |
| 7 | +- Use real PipelineService with real database |
| 8 | +- Mock vector store to return controlled 20 documents |
| 9 | +- Use SimpleReranker (no LLM needed) to rerank to top 5 |
| 10 | +- Track method calls to verify ordering |
| 11 | +- Verify LLM receives exactly 5 reranked documents |
| 12 | +
|
| 13 | +Expected Flow: |
| 14 | + Retrieval (20 docs) → Reranking (top 5) → Context Format → LLM Generation (5 docs) |
| 15 | +
|
| 16 | +Buggy Flow (before fix): |
| 17 | + Retrieval (20 docs) → Context Format → LLM Generation (20 docs) → Reranking (too late) |
| 18 | +""" |
| 19 | + |
| 20 | +from datetime import UTC, datetime |
| 21 | +from unittest.mock import Mock, patch |
| 22 | +from uuid import uuid4 |
| 23 | + |
| 24 | +import pytest |
| 25 | +from sqlalchemy.orm import Session |
| 26 | + |
| 27 | +from core.config import Settings, get_settings |
| 28 | +from rag_solution.schemas.prompt_template_schema import PromptTemplateOutput, PromptTemplateType |
| 29 | +from rag_solution.schemas.search_schema import SearchInput |
| 30 | +from rag_solution.services.pipeline_service import PipelineService |
| 31 | +from vectordbs.data_types import DocumentChunk, DocumentChunkMetadata, QueryResult, Source |
| 32 | + |
| 33 | + |
| 34 | +# ============================================================================ |
| 35 | +# FIXTURES |
| 36 | +# ============================================================================ |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def mock_vector_store_20_docs(): |
| 41 | + """Mock vector store that returns 20 documents.""" |
| 42 | + mock_results = [] |
| 43 | + for i in range(20): |
| 44 | + metadata = DocumentChunkMetadata( |
| 45 | + document_id=f"doc_{i}", |
| 46 | + chunk_index=0, |
| 47 | + total_chunks=1, |
| 48 | + source=Source.OTHER, |
| 49 | + ) |
| 50 | + chunk = DocumentChunk( |
| 51 | + id=f"chunk_{i}", |
| 52 | + text=f"This is document {i} content with relevant information about the query topic.", |
| 53 | + metadata=metadata, |
| 54 | + ) |
| 55 | + result = QueryResult( |
| 56 | + chunk=chunk, |
| 57 | + score=0.9 - (i * 0.01), # Descending scores: 0.9, 0.89, 0.88, ... |
| 58 | + collection_id="test_collection", |
| 59 | + ) |
| 60 | + mock_results.append(result) |
| 61 | + |
| 62 | + mock = Mock() |
| 63 | + mock.search = Mock(return_value=mock_results) |
| 64 | + return mock |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture |
| 68 | +def mock_rag_template(): |
| 69 | + """Mock RAG template for testing.""" |
| 70 | + now = datetime.now(UTC) |
| 71 | + return PromptTemplateOutput( |
| 72 | + id=uuid4(), |
| 73 | + name="test-rag-template", |
| 74 | + user_id=uuid4(), |
| 75 | + template_type=PromptTemplateType.RAG_QUERY, |
| 76 | + system_prompt="You are a helpful assistant.", |
| 77 | + template_format="{context}\n\n{question}", |
| 78 | + input_variables={"context": "context", "question": "question"}, |
| 79 | + example_inputs={"context": "example context", "question": "example question"}, |
| 80 | + is_default=True, |
| 81 | + created_at=now, |
| 82 | + updated_at=now, |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.fixture |
| 87 | +def settings_with_reranking(): |
| 88 | + """Settings with reranking enabled.""" |
| 89 | + settings = get_settings() |
| 90 | + settings.enable_reranking = True |
| 91 | + settings.reranker_type = "simple" # Use SimpleReranker (no LLM needed) |
| 92 | + settings.reranker_top_k = 5 # Rerank to top 5 |
| 93 | + settings.number_of_results = 20 # Retrieve 20 initially |
| 94 | + return settings |
| 95 | + |
| 96 | + |
| 97 | +# ============================================================================ |
| 98 | +# INTEGRATION TESTS |
| 99 | +# ============================================================================ |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.integration |
| 103 | +class TestPipelineRerankingOrder: |
| 104 | + """Integration tests verifying reranking happens BEFORE LLM generation.""" |
| 105 | + |
| 106 | + @pytest.fixture |
| 107 | + def pipeline_service(self, real_db_session: Session, settings_with_reranking: Settings) -> PipelineService: |
| 108 | + """Create PipelineService with real database and reranking enabled.""" |
| 109 | + return PipelineService(real_db_session, settings_with_reranking) |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_reranking_happens_before_llm_generation_integration( |
| 113 | + self, |
| 114 | + pipeline_service: PipelineService, |
| 115 | + mock_vector_store_20_docs, |
| 116 | + mock_rag_template, |
| 117 | + ): |
| 118 | + """ |
| 119 | + Integration Test: Verify reranking reduces 20 docs to 5 BEFORE LLM sees them. |
| 120 | +
|
| 121 | + Flow: |
| 122 | + 1. Vector store returns 20 documents |
| 123 | + 2. Reranking reduces to 5 documents |
| 124 | + 3. Context formatter receives 5 documents |
| 125 | + 4. LLM generation receives 5 documents |
| 126 | +
|
| 127 | + This test verifies the P0-2 fix is working end-to-end. |
| 128 | + """ |
| 129 | + # Arrange |
| 130 | + search_input = SearchInput( |
| 131 | + question="What is machine learning and how does it work?", |
| 132 | + collection_id=uuid4(), |
| 133 | + user_id=uuid4(), |
| 134 | + ) |
| 135 | + |
| 136 | + # Track what _format_context receives |
| 137 | + format_context_docs_count = None |
| 138 | + |
| 139 | + def track_format_context(template_id, query_results): |
| 140 | + nonlocal format_context_docs_count |
| 141 | + format_context_docs_count = len(query_results) |
| 142 | + return "Formatted context with relevant information" |
| 143 | + |
| 144 | + with ( |
| 145 | + patch.object(PipelineService, "_validate_configuration") as mock_validate, |
| 146 | + patch.object(PipelineService, "_get_templates") as mock_get_templates, |
| 147 | + patch.object(PipelineService, "_prepare_query") as mock_prepare, |
| 148 | + patch.object(PipelineService, "_retrieve_documents") as mock_retrieve, |
| 149 | + patch.object(PipelineService, "_format_context") as mock_format_context, |
| 150 | + patch.object(PipelineService, "_generate_answer") as mock_generate, |
| 151 | + ): |
| 152 | + # Setup mocks |
| 153 | + mock_validate.return_value = (Mock(), Mock(), Mock()) |
| 154 | + mock_get_templates.return_value = (mock_rag_template, None) |
| 155 | + mock_prepare.return_value = "prepared query" |
| 156 | + mock_retrieve.return_value = mock_vector_store_20_docs.search.return_value # Return 20 docs |
| 157 | + mock_format_context.side_effect = track_format_context |
| 158 | + mock_generate.return_value = "Generated answer based on relevant documents" |
| 159 | + |
| 160 | + # Act |
| 161 | + result = await pipeline_service.execute_pipeline( |
| 162 | + search_input=search_input, |
| 163 | + collection_name="test_collection", |
| 164 | + pipeline_id=uuid4(), |
| 165 | + ) |
| 166 | + |
| 167 | + # Assert: _retrieve_documents was called and returned 20 docs |
| 168 | + mock_retrieve.assert_called_once() |
| 169 | + |
| 170 | + # Assert: Context formatter received exactly 5 reranked documents (not 20) |
| 171 | + assert format_context_docs_count == 5, ( |
| 172 | + f"Context formatter should receive 5 reranked docs, got {format_context_docs_count}" |
| 173 | + ) |
| 174 | + |
| 175 | + # Assert: Result contains 5 reranked documents (not 20) |
| 176 | + assert len(result.query_results) == 5, ( |
| 177 | + f"Pipeline result should have 5 reranked docs, got {len(result.query_results)}" |
| 178 | + ) |
| 179 | + |
| 180 | + # Assert: Documents are the top-scored ones (SimpleReranker keeps highest scores) |
| 181 | + assert all(r.score >= 0.85 for r in result.query_results), ( |
| 182 | + "Reranked results should have high scores (top 5)" |
| 183 | + ) |
| 184 | + |
| 185 | + @pytest.mark.asyncio |
| 186 | + async def test_reranking_called_exactly_once_integration( |
| 187 | + self, |
| 188 | + pipeline_service: PipelineService, |
| 189 | + mock_vector_store_20_docs, |
| 190 | + mock_rag_template, |
| 191 | + ): |
| 192 | + """ |
| 193 | + Integration Test: Verify reranking is called exactly ONCE (no double-reranking). |
| 194 | +
|
| 195 | + Before P0-2 fix: Reranking happened in both PipelineService AND SearchService. |
| 196 | + After P0-2 fix: Reranking happens ONLY in PipelineService. |
| 197 | +
|
| 198 | + This test ensures we don't have double-reranking bugs. |
| 199 | + """ |
| 200 | + # Arrange |
| 201 | + search_input = SearchInput( |
| 202 | + question="Explain neural networks", |
| 203 | + collection_id=uuid4(), |
| 204 | + user_id=uuid4(), |
| 205 | + ) |
| 206 | + |
| 207 | + rerank_call_count = 0 |
| 208 | + |
| 209 | + def track_rerank_calls(query, results, top_k=None): |
| 210 | + nonlocal rerank_call_count |
| 211 | + rerank_call_count += 1 |
| 212 | + # SimpleReranker just returns top results by score |
| 213 | + return results[:5] |
| 214 | + |
| 215 | + with ( |
| 216 | + patch.object(PipelineService, "_validate_configuration") as mock_validate, |
| 217 | + patch.object(PipelineService, "_get_templates") as mock_get_templates, |
| 218 | + patch.object(PipelineService, "_prepare_query") as mock_prepare, |
| 219 | + patch.object(PipelineService, "_retrieve_documents") as mock_retrieve, |
| 220 | + patch.object(PipelineService, "_format_context") as mock_format_context, |
| 221 | + patch.object(PipelineService, "_generate_answer") as mock_generate, |
| 222 | + patch("rag_solution.retrieval.reranker.SimpleReranker.rerank") as mock_rerank, |
| 223 | + ): |
| 224 | + # Setup mocks |
| 225 | + mock_validate.return_value = (Mock(), Mock(), Mock()) |
| 226 | + mock_get_templates.return_value = (mock_rag_template, None) |
| 227 | + mock_prepare.return_value = "prepared query" |
| 228 | + mock_retrieve.return_value = mock_vector_store_20_docs.search.return_value # Return 20 docs |
| 229 | + mock_format_context.return_value = "formatted context" |
| 230 | + mock_generate.return_value = "generated answer" |
| 231 | + mock_rerank.side_effect = track_rerank_calls |
| 232 | + |
| 233 | + # Act |
| 234 | + await pipeline_service.execute_pipeline( |
| 235 | + search_input=search_input, |
| 236 | + collection_name="test_collection", |
| 237 | + pipeline_id=uuid4(), |
| 238 | + ) |
| 239 | + |
| 240 | + # Assert: Reranker.rerank was called exactly ONCE |
| 241 | + assert rerank_call_count == 1, ( |
| 242 | + f"Reranker.rerank should be called exactly once, got {rerank_call_count} calls" |
| 243 | + ) |
| 244 | + |
| 245 | + # Assert: Reranker was called with all 20 documents |
| 246 | + call_args = mock_rerank.call_args |
| 247 | + assert call_args is not None, "Reranker was not called" |
| 248 | + # Access results from keyword arguments |
| 249 | + results_arg = call_args.kwargs.get("results") or call_args[0][1] # Try kwargs first, then positional |
| 250 | + assert len(results_arg) == 20, ( |
| 251 | + f"Reranker should receive 20 retrieved docs, got {len(results_arg)}" |
| 252 | + ) |
| 253 | + |
| 254 | + @pytest.mark.asyncio |
| 255 | + async def test_reranking_disabled_skips_reranking_integration( |
| 256 | + self, |
| 257 | + real_db_session: Session, |
| 258 | + mock_vector_store_20_docs, |
| 259 | + mock_rag_template, |
| 260 | + ): |
| 261 | + """ |
| 262 | + Integration Test: When reranking is disabled, all 20 docs pass through. |
| 263 | +
|
| 264 | + Verifies that the reranking pipeline stage can be disabled cleanly. |
| 265 | + """ |
| 266 | + # Arrange: Settings with reranking DISABLED |
| 267 | + settings = get_settings() |
| 268 | + settings.enable_reranking = False |
| 269 | + settings.number_of_results = 20 |
| 270 | + |
| 271 | + pipeline_service = PipelineService(real_db_session, settings) |
| 272 | + |
| 273 | + search_input = SearchInput( |
| 274 | + question="What is deep learning?", |
| 275 | + collection_id=uuid4(), |
| 276 | + user_id=uuid4(), |
| 277 | + ) |
| 278 | + |
| 279 | + format_context_docs_count = None |
| 280 | + |
| 281 | + def track_format_context(template_id, query_results): |
| 282 | + nonlocal format_context_docs_count |
| 283 | + format_context_docs_count = len(query_results) |
| 284 | + return "Formatted context" |
| 285 | + |
| 286 | + with ( |
| 287 | + patch.object(PipelineService, "_validate_configuration") as mock_validate, |
| 288 | + patch.object(PipelineService, "_get_templates") as mock_get_templates, |
| 289 | + patch.object(PipelineService, "_prepare_query") as mock_prepare, |
| 290 | + patch.object(PipelineService, "_retrieve_documents") as mock_retrieve, |
| 291 | + patch.object(PipelineService, "_format_context") as mock_format_context, |
| 292 | + patch.object(PipelineService, "_generate_answer") as mock_generate, |
| 293 | + ): |
| 294 | + # Setup mocks |
| 295 | + mock_validate.return_value = (Mock(), Mock(), Mock()) |
| 296 | + mock_get_templates.return_value = (mock_rag_template, None) |
| 297 | + mock_prepare.return_value = "prepared query" |
| 298 | + mock_retrieve.return_value = mock_vector_store_20_docs.search.return_value # Return 20 docs |
| 299 | + mock_format_context.side_effect = track_format_context |
| 300 | + mock_generate.return_value = "generated answer" |
| 301 | + |
| 302 | + # Act |
| 303 | + result = await pipeline_service.execute_pipeline( |
| 304 | + search_input=search_input, |
| 305 | + collection_name="test_collection", |
| 306 | + pipeline_id=uuid4(), |
| 307 | + ) |
| 308 | + |
| 309 | + # Assert: Context formatter received all 20 documents (no reranking) |
| 310 | + assert format_context_docs_count == 20, ( |
| 311 | + f"When reranking disabled, should pass all 20 docs, got {format_context_docs_count}" |
| 312 | + ) |
| 313 | + |
| 314 | + # Assert: Result contains all 20 documents |
| 315 | + assert len(result.query_results) == 20, ( |
| 316 | + f"When reranking disabled, should return all 20 docs, got {len(result.query_results)}" |
| 317 | + ) |
0 commit comments