function test_session_manager
A comprehensive test function that validates the SessionManager class functionality including conversation creation, exchange tracking, filename generation, and context retrieval.
/tf/active/vicechatdev/e-ink-llm/test_improvements.py
63 - 134
moderate
Purpose
This test function serves as both a validation suite and usage example for the SessionManager class. It tests core functionality including: creating conversations, adding exchanges with metadata, generating session filenames, retrieving conversation data, generating conversation context, and listing active conversations. The function creates a temporary test database, performs all operations, and cleans up afterward.
Source Code
def test_session_manager():
"""Test the session manager"""
print("\nšļø Testing Session Manager")
print("=" * 50)
# Clean up any existing test database
test_db = Path("test_sessions.db")
if test_db.exists():
test_db.unlink()
session_manager = SessionManager(str(test_db))
# Create a new conversation
conv_id = session_manager.create_conversation(user_id="test_user")
print(f"Created conversation: {conv_id}")
# Add some exchanges
exchange1_id = session_manager.add_exchange(
conversation_id=conv_id,
input_file="math_problem.pdf",
input_type=".pdf",
response_text="šÆ Quadratic equation\nš x² + 5x + 6 = 0 ā (x+3)(x+2) = 0\nā
x = -3, x = -2",
processing_time=2.5,
tokens_used=150,
metadata={"compact_mode": True}
)
exchange2_id = session_manager.add_exchange(
conversation_id=conv_id,
input_file="followup_question.pdf",
input_type=".pdf",
response_text="šÆ Graph analysis\nš Parabola opens upward\nš Vertex: (-2.5, -0.25)",
processing_time=1.8,
tokens_used=120,
metadata={"compact_mode": True}
)
print(f"Added exchange 1: {exchange1_id}")
print(f"Added exchange 2: {exchange2_id}")
# Test filename generation
filename1 = session_manager.generate_session_filename(conv_id, 1, "math_problem.pdf")
filename2 = session_manager.generate_session_filename(conv_id, 2, "followup_question.pdf")
filename_error = session_manager.generate_session_filename(conv_id, 3, "error_test.pdf", is_error=True)
print(f"\nGenerated filenames:")
print(f" Exchange 1: {filename1}")
print(f" Exchange 2: {filename2}")
print(f" Error file: {filename_error}")
# Test conversation retrieval
conversation = session_manager.get_conversation(conv_id)
print(f"\nConversation summary:")
print(f" ID: {conversation.conversation_id}")
print(f" User: {conversation.user_id}")
print(f" Exchanges: {conversation.total_exchanges}")
print(f" Status: {conversation.status}")
# Test context generation
context = session_manager.get_conversation_context(conv_id)
print(f"\nConversation context:")
print(context)
# List conversations
conversations = session_manager.list_active_conversations()
print(f"\nActive conversations: {len(conversations)}")
for conv in conversations:
print(f" {conv['conversation_id']} - {conv['total_exchanges']} exchanges")
# Clean up
test_db.unlink()
print("\nā
Session manager tests completed")
Return Value
This function does not return any value (implicitly returns None). It performs side effects by printing test results to stdout and creating/deleting a test database file.
Dependencies
pathlibasynciocompact_formattersession_manager
Required Imports
from pathlib import Path
from session_manager import SessionManager
Usage Example
# Direct execution
from pathlib import Path
from session_manager import SessionManager
def test_session_manager():
# Function implementation here
pass
# Run the test
test_session_manager()
# Or as part of a test suite
if __name__ == '__main__':
test_session_manager()
Best Practices
- Always clean up test database files before and after running tests to avoid state pollution
- Use descriptive test data that clearly demonstrates the functionality being tested
- Print clear progress indicators and results for manual test verification
- Test both normal operations and edge cases (like error filename generation)
- Ensure test database uses a distinct name (test_sessions.db) to avoid conflicts with production data
- Verify cleanup occurs even if tests fail by considering try-finally blocks in production test code
- This function is designed for manual testing and demonstration; for automated testing, consider using pytest or unittest framework with assertions instead of print statements
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_session_detection 72.1% similar
-
class SessionManager_v1 71.9% similar
-
function test_pdf_session_integration 68.2% similar
-
function test_data_analysis_service 63.6% similar
-
function test_auto_continuation_workflow 59.7% similar