-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconftest.py
More file actions
102 lines (80 loc) · 3.17 KB
/
conftest.py
File metadata and controls
102 lines (80 loc) · 3.17 KB
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
import os
import pytest
import tempfile
from unittest.mock import patch, MagicMock
from typing import List, Dict, Any, Optional, Union
import numpy as np
from langchain.schema import Document, AIMessage
from document_processor import DocumentProcessor
from vector_store import VectorStore
from rag import RAG
class MockDocument(Document):
"""Mock LangChain document for testing."""
def __init__(self, page_content: str, metadata: Optional[Dict[str, Any]] = None):
super().__init__(page_content=page_content, metadata=metadata or {})
@pytest.fixture
def sample_documents() -> List[Document]:
"""Create sample documents for testing."""
return [
MockDocument(
page_content="This is a test document about artificial intelligence.",
metadata={"source": "test1.pdf", "page": 1}
),
MockDocument(
page_content="FAISS is a library for efficient similarity search.",
metadata={"source": "test1.pdf", "page": 2}
),
MockDocument(
page_content="RAG combines retrieval with generative models.",
metadata={"source": "test2.pdf", "page": 1}
),
]
@pytest.fixture
def mock_pdf_file():
"""Create a temporary mock PDF file for testing."""
# Create a simple PDF-like content
content = b"%PDF-1.4\n1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
# Create a temporary file
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as temp_file:
temp_file.write(content)
temp_file_path = temp_file.name
yield temp_file_path
# Clean up the temporary file
if os.path.exists(temp_file_path):
os.remove(temp_file_path)
@pytest.fixture
def document_processor() -> DocumentProcessor:
"""Create a DocumentProcessor instance for testing."""
return DocumentProcessor(chunk_size=100, chunk_overlap=20)
@pytest.fixture
def mock_sentence_transformer():
"""Create a mock SentenceTransformer for testing."""
with patch('sentence_transformers.SentenceTransformer', autospec=True) as mock:
# Configure the mock
instance = MagicMock()
instance.get_sentence_embedding_dimension.return_value = 384
instance.encode = MagicMock(return_value=np.array([
[0.1] * 384,
[0.2] * 384,
[0.3] * 384,
]))
mock.return_value = instance
yield mock
@pytest.fixture
def vector_store(mock_sentence_transformer) -> VectorStore:
"""Create a VectorStore instance with mocked embeddings for testing."""
return VectorStore()
@pytest.fixture
def mock_openai():
"""Create a mock OpenAI client for testing."""
with patch('langchain_openai.ChatOpenAI', autospec=True) as mock:
# Configure the mock response
instance = MagicMock()
response = AIMessage(content="This is a mock response from the language model.")
instance.invoke = MagicMock(return_value=response)
mock.return_value = instance
yield mock
@pytest.fixture
def rag_instance(vector_store, mock_openai) -> RAG:
"""Create a RAG instance with a mocked vector store and language model."""
return RAG(vector_store)