Fixing tests

This commit is contained in:
Cyber MacGeddon 2026-03-07 23:36:02 +00:00
parent 2d47102d70
commit 35dac42c73
2 changed files with 192 additions and 127 deletions

View file

@ -8,10 +8,33 @@ from unittest.mock import MagicMock, AsyncMock
from trustgraph.retrieval.document_rag.document_rag import DocumentRag, Query from trustgraph.retrieval.document_rag.document_rag import DocumentRag, Query
# Sample chunk content mapping for tests
CHUNK_CONTENT = {
"doc/c1": "Document 1 content",
"doc/c2": "Document 2 content",
"doc/c3": "Relevant document content",
"doc/c4": "Another document",
"doc/c5": "Default doc",
"doc/c6": "Verbose test doc",
"doc/c7": "Verbose doc content",
"doc/ml1": "Machine learning is a subset of artificial intelligence...",
"doc/ml2": "ML algorithms learn patterns from data to make predictions...",
"doc/ml3": "Common ML techniques include supervised and unsupervised learning...",
}
@pytest.fixture
def mock_fetch_chunk():
"""Create a mock fetch_chunk function"""
async def fetch(chunk_id, user):
return CHUNK_CONTENT.get(chunk_id, f"Content for {chunk_id}")
return fetch
class TestDocumentRag: class TestDocumentRag:
"""Test cases for DocumentRag class""" """Test cases for DocumentRag class"""
def test_document_rag_initialization_with_defaults(self): def test_document_rag_initialization_with_defaults(self, mock_fetch_chunk):
"""Test DocumentRag initialization with default verbose setting""" """Test DocumentRag initialization with default verbose setting"""
# Create mock clients # Create mock clients
mock_prompt_client = MagicMock() mock_prompt_client = MagicMock()
@ -22,16 +45,18 @@ class TestDocumentRag:
document_rag = DocumentRag( document_rag = DocumentRag(
prompt_client=mock_prompt_client, prompt_client=mock_prompt_client,
embeddings_client=mock_embeddings_client, embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client doc_embeddings_client=mock_doc_embeddings_client,
fetch_chunk=mock_fetch_chunk
) )
# Verify initialization # Verify initialization
assert document_rag.prompt_client == mock_prompt_client assert document_rag.prompt_client == mock_prompt_client
assert document_rag.embeddings_client == mock_embeddings_client assert document_rag.embeddings_client == mock_embeddings_client
assert document_rag.doc_embeddings_client == mock_doc_embeddings_client assert document_rag.doc_embeddings_client == mock_doc_embeddings_client
assert document_rag.fetch_chunk == mock_fetch_chunk
assert document_rag.verbose is False # Default value assert document_rag.verbose is False # Default value
def test_document_rag_initialization_with_verbose(self): def test_document_rag_initialization_with_verbose(self, mock_fetch_chunk):
"""Test DocumentRag initialization with verbose enabled""" """Test DocumentRag initialization with verbose enabled"""
# Create mock clients # Create mock clients
mock_prompt_client = MagicMock() mock_prompt_client = MagicMock()
@ -43,6 +68,7 @@ class TestDocumentRag:
prompt_client=mock_prompt_client, prompt_client=mock_prompt_client,
embeddings_client=mock_embeddings_client, embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client, doc_embeddings_client=mock_doc_embeddings_client,
fetch_chunk=mock_fetch_chunk,
verbose=True verbose=True
) )
@ -50,6 +76,7 @@ class TestDocumentRag:
assert document_rag.prompt_client == mock_prompt_client assert document_rag.prompt_client == mock_prompt_client
assert document_rag.embeddings_client == mock_embeddings_client assert document_rag.embeddings_client == mock_embeddings_client
assert document_rag.doc_embeddings_client == mock_doc_embeddings_client assert document_rag.doc_embeddings_client == mock_doc_embeddings_client
assert document_rag.fetch_chunk == mock_fetch_chunk
assert document_rag.verbose is True assert document_rag.verbose is True
@ -137,13 +164,18 @@ class TestQuery:
mock_rag.embeddings_client = mock_embeddings_client mock_rag.embeddings_client = mock_embeddings_client
mock_rag.doc_embeddings_client = mock_doc_embeddings_client mock_rag.doc_embeddings_client = mock_doc_embeddings_client
# Mock fetch_chunk function
async def mock_fetch(chunk_id, user):
return CHUNK_CONTENT.get(chunk_id, f"Content for {chunk_id}")
mock_rag.fetch_chunk = mock_fetch
# Mock the embedding and document query responses # Mock the embedding and document query responses
test_vectors = [[0.1, 0.2, 0.3]] test_vectors = [[0.1, 0.2, 0.3]]
mock_embeddings_client.embed.return_value = test_vectors mock_embeddings_client.embed.return_value = test_vectors
# Mock document results # Mock document embeddings returns chunk_ids
test_docs = ["Document 1 content", "Document 2 content"] test_chunk_ids = ["doc/c1", "doc/c2"]
mock_doc_embeddings_client.query.return_value = test_docs mock_doc_embeddings_client.query.return_value = test_chunk_ids
# Initialize Query # Initialize Query
query = Query( query = Query(
@ -169,24 +201,25 @@ class TestQuery:
collection="test_collection" collection="test_collection"
) )
# Verify result is list of documents # Verify result is list of fetched document content
assert result == test_docs assert "Document 1 content" in result
assert "Document 2 content" in result
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_document_rag_query_method(self): async def test_document_rag_query_method(self, mock_fetch_chunk):
"""Test DocumentRag.query method orchestrates full document RAG pipeline""" """Test DocumentRag.query method orchestrates full document RAG pipeline"""
# Create mock clients # Create mock clients
mock_prompt_client = AsyncMock() mock_prompt_client = AsyncMock()
mock_embeddings_client = AsyncMock() mock_embeddings_client = AsyncMock()
mock_doc_embeddings_client = AsyncMock() mock_doc_embeddings_client = AsyncMock()
# Mock embeddings and document responses # Mock embeddings and document embeddings responses
test_vectors = [[0.1, 0.2, 0.3]] test_vectors = [[0.1, 0.2, 0.3]]
test_docs = ["Relevant document content", "Another document"] test_chunk_ids = ["doc/c3", "doc/c4"]
expected_response = "This is the document RAG response" expected_response = "This is the document RAG response"
mock_embeddings_client.embed.return_value = test_vectors mock_embeddings_client.embed.return_value = test_vectors
mock_doc_embeddings_client.query.return_value = test_docs mock_doc_embeddings_client.query.return_value = test_chunk_ids
mock_prompt_client.document_prompt.return_value = expected_response mock_prompt_client.document_prompt.return_value = expected_response
# Initialize DocumentRag # Initialize DocumentRag
@ -194,6 +227,7 @@ class TestQuery:
prompt_client=mock_prompt_client, prompt_client=mock_prompt_client,
embeddings_client=mock_embeddings_client, embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client, doc_embeddings_client=mock_doc_embeddings_client,
fetch_chunk=mock_fetch_chunk,
verbose=False verbose=False
) )
@ -216,17 +250,20 @@ class TestQuery:
collection="test_collection" collection="test_collection"
) )
# Verify prompt client was called with documents and query # Verify prompt client was called with fetched documents and query
mock_prompt_client.document_prompt.assert_called_once_with( mock_prompt_client.document_prompt.assert_called_once()
query="test query", call_args = mock_prompt_client.document_prompt.call_args
documents=test_docs assert call_args.kwargs["query"] == "test query"
) # Documents should be fetched content, not chunk_ids
docs = call_args.kwargs["documents"]
assert "Relevant document content" in docs
assert "Another document" in docs
# Verify result # Verify result
assert result == expected_response assert result == expected_response
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_document_rag_query_with_defaults(self): async def test_document_rag_query_with_defaults(self, mock_fetch_chunk):
"""Test DocumentRag.query method with default parameters""" """Test DocumentRag.query method with default parameters"""
# Create mock clients # Create mock clients
mock_prompt_client = AsyncMock() mock_prompt_client = AsyncMock()
@ -235,14 +272,15 @@ class TestQuery:
# Mock responses # Mock responses
mock_embeddings_client.embed.return_value = [[0.1, 0.2]] mock_embeddings_client.embed.return_value = [[0.1, 0.2]]
mock_doc_embeddings_client.query.return_value = ["Default doc"] mock_doc_embeddings_client.query.return_value = ["doc/c5"]
mock_prompt_client.document_prompt.return_value = "Default response" mock_prompt_client.document_prompt.return_value = "Default response"
# Initialize DocumentRag # Initialize DocumentRag
document_rag = DocumentRag( document_rag = DocumentRag(
prompt_client=mock_prompt_client, prompt_client=mock_prompt_client,
embeddings_client=mock_embeddings_client, embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client doc_embeddings_client=mock_doc_embeddings_client,
fetch_chunk=mock_fetch_chunk
) )
# Call DocumentRag.query with minimal parameters # Call DocumentRag.query with minimal parameters
@ -268,9 +306,14 @@ class TestQuery:
mock_rag.embeddings_client = mock_embeddings_client mock_rag.embeddings_client = mock_embeddings_client
mock_rag.doc_embeddings_client = mock_doc_embeddings_client mock_rag.doc_embeddings_client = mock_doc_embeddings_client
# Mock fetch_chunk
async def mock_fetch(chunk_id, user):
return CHUNK_CONTENT.get(chunk_id, f"Content for {chunk_id}")
mock_rag.fetch_chunk = mock_fetch
# Mock responses # Mock responses
mock_embeddings_client.embed.return_value = [[0.7, 0.8]] mock_embeddings_client.embed.return_value = [[0.7, 0.8]]
mock_doc_embeddings_client.query.return_value = ["Verbose test doc"] mock_doc_embeddings_client.query.return_value = ["doc/c6"]
# Initialize Query with verbose=True # Initialize Query with verbose=True
query = Query( query = Query(
@ -288,11 +331,11 @@ class TestQuery:
mock_embeddings_client.embed.assert_called_once_with("verbose test") mock_embeddings_client.embed.assert_called_once_with("verbose test")
mock_doc_embeddings_client.query.assert_called_once() mock_doc_embeddings_client.query.assert_called_once()
# Verify result # Verify result contains fetched content
assert result == ["Verbose test doc"] assert "Verbose test doc" in result
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_document_rag_query_with_verbose(self): async def test_document_rag_query_with_verbose(self, mock_fetch_chunk):
"""Test DocumentRag.query method with verbose logging enabled""" """Test DocumentRag.query method with verbose logging enabled"""
# Create mock clients # Create mock clients
mock_prompt_client = AsyncMock() mock_prompt_client = AsyncMock()
@ -301,7 +344,7 @@ class TestQuery:
# Mock responses # Mock responses
mock_embeddings_client.embed.return_value = [[0.3, 0.4]] mock_embeddings_client.embed.return_value = [[0.3, 0.4]]
mock_doc_embeddings_client.query.return_value = ["Verbose doc content"] mock_doc_embeddings_client.query.return_value = ["doc/c7"]
mock_prompt_client.document_prompt.return_value = "Verbose RAG response" mock_prompt_client.document_prompt.return_value = "Verbose RAG response"
# Initialize DocumentRag with verbose=True # Initialize DocumentRag with verbose=True
@ -309,6 +352,7 @@ class TestQuery:
prompt_client=mock_prompt_client, prompt_client=mock_prompt_client,
embeddings_client=mock_embeddings_client, embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client, doc_embeddings_client=mock_doc_embeddings_client,
fetch_chunk=mock_fetch_chunk,
verbose=True verbose=True
) )
@ -318,10 +362,11 @@ class TestQuery:
# Verify all clients were called # Verify all clients were called
mock_embeddings_client.embed.assert_called_once_with("verbose query test") mock_embeddings_client.embed.assert_called_once_with("verbose query test")
mock_doc_embeddings_client.query.assert_called_once() mock_doc_embeddings_client.query.assert_called_once()
mock_prompt_client.document_prompt.assert_called_once_with(
query="verbose query test", # Verify prompt client was called with fetched content
documents=["Verbose doc content"] call_args = mock_prompt_client.document_prompt.call_args
) assert call_args.kwargs["query"] == "verbose query test"
assert "Verbose doc content" in call_args.kwargs["documents"]
assert result == "Verbose RAG response" assert result == "Verbose RAG response"
@ -335,9 +380,14 @@ class TestQuery:
mock_rag.embeddings_client = mock_embeddings_client mock_rag.embeddings_client = mock_embeddings_client
mock_rag.doc_embeddings_client = mock_doc_embeddings_client mock_rag.doc_embeddings_client = mock_doc_embeddings_client
# Mock responses - empty document list # Mock fetch_chunk (won't be called if no chunk_ids)
async def mock_fetch(chunk_id, user):
return f"Content for {chunk_id}"
mock_rag.fetch_chunk = mock_fetch
# Mock responses - empty chunk_id list
mock_embeddings_client.embed.return_value = [[0.1, 0.2]] mock_embeddings_client.embed.return_value = [[0.1, 0.2]]
mock_doc_embeddings_client.query.return_value = [] # No documents found mock_doc_embeddings_client.query.return_value = [] # No chunk_ids found
# Initialize Query # Initialize Query
query = Query( query = Query(
@ -358,16 +408,16 @@ class TestQuery:
assert result == [] assert result == []
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_document_rag_query_with_empty_documents(self): async def test_document_rag_query_with_empty_documents(self, mock_fetch_chunk):
"""Test DocumentRag.query method when no documents are retrieved""" """Test DocumentRag.query method when no documents are retrieved"""
# Create mock clients # Create mock clients
mock_prompt_client = AsyncMock() mock_prompt_client = AsyncMock()
mock_embeddings_client = AsyncMock() mock_embeddings_client = AsyncMock()
mock_doc_embeddings_client = AsyncMock() mock_doc_embeddings_client = AsyncMock()
# Mock responses - no documents found # Mock responses - no chunk_ids found
mock_embeddings_client.embed.return_value = [[0.5, 0.6]] mock_embeddings_client.embed.return_value = [[0.5, 0.6]]
mock_doc_embeddings_client.query.return_value = [] # Empty document list mock_doc_embeddings_client.query.return_value = [] # Empty chunk_id list
mock_prompt_client.document_prompt.return_value = "No documents found response" mock_prompt_client.document_prompt.return_value = "No documents found response"
# Initialize DocumentRag # Initialize DocumentRag
@ -375,6 +425,7 @@ class TestQuery:
prompt_client=mock_prompt_client, prompt_client=mock_prompt_client,
embeddings_client=mock_embeddings_client, embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client, doc_embeddings_client=mock_doc_embeddings_client,
fetch_chunk=mock_fetch_chunk,
verbose=False verbose=False
) )
@ -419,7 +470,7 @@ class TestQuery:
assert result == expected_vectors assert result == expected_vectors
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_document_rag_integration_flow(self): async def test_document_rag_integration_flow(self, mock_fetch_chunk):
"""Test complete DocumentRag integration with realistic data flow""" """Test complete DocumentRag integration with realistic data flow"""
# Create mock clients # Create mock clients
mock_prompt_client = AsyncMock() mock_prompt_client = AsyncMock()
@ -429,15 +480,11 @@ class TestQuery:
# Mock realistic responses # Mock realistic responses
query_text = "What is machine learning?" query_text = "What is machine learning?"
query_vectors = [[0.1, 0.2, 0.3, 0.4, 0.5]] query_vectors = [[0.1, 0.2, 0.3, 0.4, 0.5]]
retrieved_docs = [ retrieved_chunk_ids = ["doc/ml1", "doc/ml2", "doc/ml3"]
"Machine learning is a subset of artificial intelligence...",
"ML algorithms learn patterns from data to make predictions...",
"Common ML techniques include supervised and unsupervised learning..."
]
final_response = "Machine learning is a field of AI that enables computers to learn and improve from experience without being explicitly programmed." final_response = "Machine learning is a field of AI that enables computers to learn and improve from experience without being explicitly programmed."
mock_embeddings_client.embed.return_value = query_vectors mock_embeddings_client.embed.return_value = query_vectors
mock_doc_embeddings_client.query.return_value = retrieved_docs mock_doc_embeddings_client.query.return_value = retrieved_chunk_ids
mock_prompt_client.document_prompt.return_value = final_response mock_prompt_client.document_prompt.return_value = final_response
# Initialize DocumentRag # Initialize DocumentRag
@ -445,6 +492,7 @@ class TestQuery:
prompt_client=mock_prompt_client, prompt_client=mock_prompt_client,
embeddings_client=mock_embeddings_client, embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client, doc_embeddings_client=mock_doc_embeddings_client,
fetch_chunk=mock_fetch_chunk,
verbose=False verbose=False
) )
@ -466,10 +514,16 @@ class TestQuery:
collection="ml_knowledge" collection="ml_knowledge"
) )
mock_prompt_client.document_prompt.assert_called_once_with( # Verify prompt client was called with fetched document content
query=query_text, mock_prompt_client.document_prompt.assert_called_once()
documents=retrieved_docs call_args = mock_prompt_client.document_prompt.call_args
) assert call_args.kwargs["query"] == query_text
# Verify documents were fetched from chunk_ids
docs = call_args.kwargs["documents"]
assert "Machine learning is a subset of artificial intelligence..." in docs
assert "ML algorithms learn patterns from data to make predictions..." in docs
assert "Common ML techniques include supervised and unsupervised learning..." in docs
# Verify final result # Verify final result
assert result == final_response assert result == final_response

View file

@ -148,7 +148,7 @@ class TestMilvusDocEmbeddingsStorageProcessor:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_store_document_embeddings_none_chunk(self, processor): async def test_store_document_embeddings_none_chunk(self, processor):
"""Test storing document embeddings with None chunk_id (should be skipped)""" """Test storing document embeddings with None chunk_id"""
message = MagicMock() message = MagicMock()
message.metadata = MagicMock() message.metadata = MagicMock()
message.metadata.user = 'test_user' message.metadata.user = 'test_user'
@ -162,12 +162,14 @@ class TestMilvusDocEmbeddingsStorageProcessor:
await processor.store_document_embeddings(message) await processor.store_document_embeddings(message)
# Verify no insert was called for None chunk_id # Note: Implementation passes through None chunk_ids (only skips empty string "")
processor.vecstore.insert.assert_not_called() processor.vecstore.insert.assert_called_once_with(
[0.1, 0.2, 0.3], None, 'test_user', 'test_collection'
)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_store_document_embeddings_mixed_valid_invalid_chunks(self, processor): async def test_store_document_embeddings_mixed_valid_invalid_chunks(self, processor):
"""Test storing document embeddings with mix of valid and invalid chunks""" """Test storing document embeddings with mix of valid and empty chunks"""
message = MagicMock() message = MagicMock()
message.metadata = MagicMock() message.metadata = MagicMock()
message.metadata.user = 'test_user' message.metadata.user = 'test_user'
@ -181,18 +183,27 @@ class TestMilvusDocEmbeddingsStorageProcessor:
chunk_id="", chunk_id="",
vectors=[[0.4, 0.5, 0.6]] vectors=[[0.4, 0.5, 0.6]]
) )
none_chunk = ChunkEmbeddings( another_valid = ChunkEmbeddings(
chunk_id=None, chunk_id="Another valid chunk",
vectors=[[0.7, 0.8, 0.9]] vectors=[[0.7, 0.8, 0.9]]
) )
message.chunks = [valid_chunk, empty_chunk, none_chunk] message.chunks = [valid_chunk, empty_chunk, another_valid]
await processor.store_document_embeddings(message) await processor.store_document_embeddings(message)
# Verify only valid chunk was inserted with user/collection parameters # Verify valid chunks were inserted, empty string chunk was skipped
processor.vecstore.insert.assert_called_once_with( expected_calls = [
[0.1, 0.2, 0.3], "Valid document content", 'test_user', 'test_collection' ([0.1, 0.2, 0.3], "Valid document content", 'test_user', 'test_collection'),
) ([0.7, 0.8, 0.9], "Another valid chunk", 'test_user', 'test_collection'),
]
assert processor.vecstore.insert.call_count == 2
for i, (expected_vec, expected_chunk_id, expected_user, expected_collection) in enumerate(expected_calls):
actual_call = processor.vecstore.insert.call_args_list[i]
assert actual_call[0][0] == expected_vec
assert actual_call[0][1] == expected_chunk_id
assert actual_call[0][2] == expected_user
assert actual_call[0][3] == expected_collection
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_store_document_embeddings_empty_chunks_list(self, processor): async def test_store_document_embeddings_empty_chunks_list(self, processor):