-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathconftest.py
More file actions
380 lines (321 loc) · 11.8 KB
/
Copy pathconftest.py
File metadata and controls
380 lines (321 loc) · 11.8 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import os
import sys
import tempfile
from typing import Any, Dict, List
from unittest.mock import MagicMock, Mock, patch
import pytest
from fastapi.testclient import TestClient
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from config import Config
from models import Course, CourseChunk, Lesson
from vector_store import SearchResults
@pytest.fixture
def sample_course():
"""Create a sample course for testing"""
lessons = [
Lesson(
lesson_number=1,
title="Introduction",
lesson_link="https://example.com/lesson1",
),
Lesson(
lesson_number=2,
title="Advanced Topics",
lesson_link="https://example.com/lesson2",
),
Lesson(
lesson_number=3,
title="Conclusion",
lesson_link="https://example.com/lesson3",
),
]
return Course(
title="Building Towards Computer Use with Anthropic",
course_link="https://example.com/course",
instructor="Colt Steele",
lessons=lessons,
)
@pytest.fixture
def sample_course_chunks():
"""Create sample course chunks for testing"""
return [
CourseChunk(
content="Welcome to Building Toward Computer Use with Anthropic. This course covers computer automation.",
course_title="Building Towards Computer Use with Anthropic",
lesson_number=1,
chunk_index=0,
),
CourseChunk(
content="In this lesson, we'll explore advanced topics including tool calling and agent workflows.",
course_title="Building Towards Computer Use with Anthropic",
lesson_number=2,
chunk_index=1,
),
CourseChunk(
content="Computer use capability is built by using many features of large language models.",
course_title="Building Towards Computer Use with Anthropic",
lesson_number=1,
chunk_index=2,
),
]
@pytest.fixture
def sample_search_results():
"""Create sample search results for testing"""
return SearchResults(
documents=[
"Welcome to Building Toward Computer Use with Anthropic. This course covers computer automation.",
"In this lesson, we'll explore advanced topics including tool calling and agent workflows.",
],
metadata=[
{
"course_title": "Building Towards Computer Use with Anthropic",
"lesson_number": 1,
"chunk_index": 0,
},
{
"course_title": "Building Towards Computer Use with Anthropic",
"lesson_number": 2,
"chunk_index": 1,
},
],
distances=[0.1, 0.2],
)
@pytest.fixture
def empty_search_results():
"""Create empty search results for testing"""
return SearchResults(documents=[], metadata=[], distances=[])
@pytest.fixture
def error_search_results():
"""Create error search results for testing"""
return SearchResults.empty("Search error: Database connection failed")
@pytest.fixture
def mock_vector_store():
"""Create a mock vector store for testing"""
mock = Mock()
mock.search.return_value = SearchResults(
documents=["Sample document content"],
metadata=[{"course_title": "Test Course", "lesson_number": 1}],
distances=[0.1],
)
mock._resolve_course_name.return_value = "Test Course"
mock.get_lesson_link.return_value = "https://example.com/lesson1"
return mock
@pytest.fixture
def mock_anthropic_client():
"""Create a mock Anthropic client for testing"""
mock_client = Mock()
# Mock response for direct text response
mock_response = Mock()
mock_response.content = [Mock()]
mock_response.content[0].text = "This is a test response from Claude."
mock_response.stop_reason = "end_turn"
# Mock response for tool use
mock_tool_response = Mock()
mock_tool_response.stop_reason = "tool_use"
mock_tool_content = Mock()
mock_tool_content.type = "tool_use"
mock_tool_content.name = "search_course_content"
mock_tool_content.id = "tool_123"
mock_tool_content.input = {"query": "test query"}
mock_tool_response.content = [mock_tool_content]
mock_client.messages.create.return_value = mock_response
return mock_client
@pytest.fixture
def mock_tool_manager():
"""Create a mock tool manager for testing"""
mock = Mock()
mock.get_tool_definitions.return_value = [
{
"name": "search_course_content",
"description": "Search course materials",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "What to search for"}
},
"required": ["query"],
},
}
]
mock.execute_tool.return_value = "Mock search result"
mock.get_last_sources.return_value = ["Test Course - Lesson 1"]
mock.get_last_source_links.return_value = ["https://example.com/lesson1"]
return mock
@pytest.fixture
def test_config():
"""Create a test configuration with proper settings"""
return Config(
ANTHROPIC_API_KEY="test-api-key",
ANTHROPIC_MODEL="claude-sonnet-4-20250514",
EMBEDDING_MODEL="all-MiniLM-L6-v2",
CHUNK_SIZE=800,
CHUNK_OVERLAP=100,
MAX_RESULTS=5, # Set to proper value, not 0
MAX_HISTORY=2,
CHROMA_PATH="./test_chroma_db",
)
@pytest.fixture
def broken_config():
"""Create a configuration with the broken MAX_RESULTS=0 setting"""
return Config(
ANTHROPIC_API_KEY="test-api-key",
ANTHROPIC_MODEL="claude-sonnet-4-20250514",
EMBEDDING_MODEL="all-MiniLM-L6-v2",
CHUNK_SIZE=800,
CHUNK_OVERLAP=100,
MAX_RESULTS=0, # This is the broken setting
MAX_HISTORY=2,
CHROMA_PATH="./test_chroma_db",
)
@pytest.fixture
def temp_chroma_db():
"""Create a temporary ChromaDB directory for testing"""
temp_dir = tempfile.mkdtemp()
yield temp_dir
# Cleanup after test
import shutil
shutil.rmtree(temp_dir, ignore_errors=True)
@pytest.fixture
def mock_chroma_collection():
"""Create a mock ChromaDB collection for testing"""
mock = Mock()
mock.query.return_value = {
"documents": [["Sample document"]],
"metadatas": [[{"course_title": "Test Course", "lesson_number": 1}]],
"distances": [[0.1]],
}
mock.get.return_value = {
"ids": ["test_course_1"],
"metadatas": [
{
"title": "Test Course",
"instructor": "Test Instructor",
"course_link": "https://example.com/course",
"lessons_json": '[{"lesson_number": 1, "lesson_title": "Test Lesson", "lesson_link": "https://example.com/lesson1"}]',
"lesson_count": 1,
}
],
}
return mock
# Test data constants
SAMPLE_COURSE_TEXT = """Course Title: Building Towards Computer Use with Anthropic
Course Link: https://www.deeplearning.ai/short-courses/building-toward-computer-use-with-anthropic/
Course Instructor: Colt Steele
Lesson 1: Introduction
Lesson Link: https://learn.deeplearning.ai/courses/building-toward-computer-use-with-anthropic/lesson/1/introduction
Welcome to Building Toward Computer Use with Anthropic. This course covers computer automation.
Lesson 2: Advanced Topics
Lesson Link: https://learn.deeplearning.ai/courses/building-toward-computer-use-with-anthropic/lesson/2/advanced
In this lesson, we'll explore advanced topics including tool calling and agent workflows.
"""
SAMPLE_QUERY_RESPONSES = {
"general": "This is a general knowledge response.",
"course_specific": "Based on the search results, here is information about the course content.",
"tool_use": "I'll search for that information in the course materials.",
}
@pytest.fixture
def mock_rag_system():
"""Create a mock RAG system for API testing"""
mock = Mock()
mock.query.return_value = (
"This is a test response about course content.",
["Building Towards Computer Use with Anthropic - Lesson 1"],
["https://example.com/lesson1"]
)
mock.get_course_analytics.return_value = {
"total_courses": 2,
"course_titles": ["Building Towards Computer Use with Anthropic", "Advanced AI Techniques"]
}
mock.session_manager.create_session.return_value = "test-session-123"
mock.session_manager.clear_session.return_value = None
return mock
@pytest.fixture
def test_app():
"""Create a test FastAPI app with mocked dependencies"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from pydantic import BaseModel
from typing import List, Optional
# Create test app without static file mounting
app = FastAPI(title="Course Materials RAG System Test", root_path="")
# Add middleware
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["*"])
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)
# Pydantic models
class QueryRequest(BaseModel):
query: str
session_id: Optional[str] = None
class QueryResponse(BaseModel):
answer: str
sources: List[str]
source_links: List[Optional[str]]
session_id: str
class CourseStats(BaseModel):
total_courses: int
course_titles: List[str]
class ClearSessionRequest(BaseModel):
session_id: str
# Mock RAG system
mock_rag = Mock()
mock_rag.query.return_value = (
"This is a test response about course content.",
["Building Towards Computer Use with Anthropic - Lesson 1"],
["https://example.com/lesson1"]
)
mock_rag.get_course_analytics.return_value = {
"total_courses": 2,
"course_titles": ["Building Towards Computer Use with Anthropic", "Advanced AI Techniques"]
}
mock_rag.session_manager.create_session.return_value = "test-session-123"
mock_rag.session_manager.clear_session.return_value = None
# API endpoints
@app.post("/api/query", response_model=QueryResponse)
async def query_documents(request: QueryRequest):
session_id = request.session_id or mock_rag.session_manager.create_session()
answer, sources, source_links = mock_rag.query(request.query, session_id)
return QueryResponse(
answer=answer,
sources=sources,
source_links=source_links,
session_id=session_id
)
@app.get("/api/courses", response_model=CourseStats)
async def get_course_stats():
analytics = mock_rag.get_course_analytics()
return CourseStats(
total_courses=analytics["total_courses"],
course_titles=analytics["course_titles"]
)
@app.post("/api/clear-session")
async def clear_session(request: ClearSessionRequest):
mock_rag.session_manager.clear_session(request.session_id)
return {"status": "success", "message": "Session cleared successfully"}
@app.get("/")
async def root():
return {"message": "Course Materials RAG System API"}
return app
@pytest.fixture
def client(test_app):
"""Create a test client for the FastAPI app"""
return TestClient(test_app)
@pytest.fixture
def sample_query_request():
"""Sample query request for testing"""
return {
"query": "What is computer use in AI?",
"session_id": "test-session-123"
}
@pytest.fixture
def sample_clear_session_request():
"""Sample clear session request for testing"""
return {
"session_id": "test-session-123"
}