function test_pdf_session_integration
Integration test function that verifies PDF generation includes session tracking information by creating both response and error PDFs with conversation IDs and exchange numbers.
/tf/active/vicechatdev/e-ink-llm/test_pdf_session.py
11 - 72
moderate
Purpose
This test function validates the integration between SessionManager and PDFGenerator components, ensuring that generated PDFs properly embed session metadata (conversation IDs and exchange numbers). It creates temporary test PDFs for both successful responses and error cases, then verifies the files are created with expected content sizes. This is crucial for maintaining conversation context across multiple document exchanges in a session-based PDF processing system.
Source Code
def test_pdf_session_integration():
"""Test that PDFs include session information"""
print("๐งช Testing PDF Session Integration...")
# Create temporary directory for test files
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Initialize components
session_manager = SessionManager()
pdf_generator = PDFGenerator()
# Start a new conversation
conversation_id = session_manager.create_conversation()
print(f"๐ Started conversation: {conversation_id}")
# Create test response PDF
test_response = "This is a test response to verify session tracking in PDFs."
test_metadata = {
"filename": "test_input.txt",
"processing_time": 1.5,
"tokens_used": 100,
"timestamp": "2025-01-31 22:42:48"
}
response_pdf = temp_path / "test_response.pdf"
pdf_generator.create_response_pdf(
llm_response=test_response,
original_image_b64="", # Empty for text input
metadata=test_metadata,
output_path=str(response_pdf),
conversation_id=conversation_id,
exchange_number=1
)
print(f"โ
Response PDF created: {response_pdf}")
print(f" Size: {response_pdf.stat().st_size} bytes")
# Create test error PDF
error_pdf = temp_path / "test_error.pdf"
pdf_generator.generate_error_pdf(
error_message="This is a test error message",
original_file="test_input.txt",
output_path=str(error_pdf),
conversation_id=conversation_id,
exchange_number=2
)
print(f"โ
Error PDF created: {error_pdf}")
print(f" Size: {error_pdf.stat().st_size} bytes")
# Verify both files exist and have content
assert response_pdf.exists(), "Response PDF should exist"
assert error_pdf.exists(), "Error PDF should exist"
assert response_pdf.stat().st_size > 1000, "Response PDF should have substantial content"
assert error_pdf.stat().st_size > 1000, "Error PDF should have substantial content"
print("๐ All PDF session integration tests passed!")
print(f" Session ID: {conversation_id}")
print(f" Response PDF: {response_pdf.name}")
print(f" Error PDF: {error_pdf.name}")
Return Value
This function does not return any value (implicitly returns None). It performs assertions and prints test progress/results to stdout. If any assertion fails, it will raise an AssertionError.
Dependencies
tempfileospathlib
Required Imports
import tempfile
import os
from pathlib import Path
from pdf_generator import PDFGenerator
from session_manager import SessionManager
Usage Example
# Run the integration test
test_pdf_session_integration()
# Expected output:
# ๐งช Testing PDF Session Integration...
# ๐ Started conversation: <conversation_id>
# โ
Response PDF created: <path>
# Size: <bytes> bytes
# โ
Error PDF created: <path>
# Size: <bytes> bytes
# ๐ All PDF session integration tests passed!
# Session ID: <conversation_id>
# Response PDF: test_response.pdf
# Error PDF: test_error.pdf
Best Practices
- This test function uses tempfile.TemporaryDirectory() context manager to ensure automatic cleanup of test files
- The function performs size assertions (>1000 bytes) to verify PDFs contain substantial content, not just empty files
- Test creates both success and error scenarios to validate complete PDF generation workflow
- Session tracking is validated by passing conversation_id and exchange_number to PDF generation methods
- Should be run as part of a test suite, not in production code
- Requires PDFGenerator and SessionManager classes to be properly implemented and available in the module path
- The test uses hardcoded metadata values suitable for testing; real implementations should use actual data
- Print statements provide verbose output for debugging test failures
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_session_detection 71.1% similar
-
function test_session_manager 68.2% similar
-
function test_document_processing 63.7% similar
-
class SessionDetector 62.3% similar
-
function test_auto_continuation_workflow 59.1% similar