gateway/test_pydantic_compat.py

100 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
Test script for Pydantic compatibility module.
This script tests the version-aware functionality for both Pydantic v1 and v2.
"""
import sys
import os
# Add the modules directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'modules'))
def test_compatibility_module():
"""Test the Pydantic compatibility module"""
try:
from shared.pydanticCompat import (
PYDANTIC_VERSION,
create_private_field,
create_model_config,
model_to_dict,
model_from_dict,
get_version_info
)
print(f"✅ Successfully imported Pydantic compatibility module")
print(f"📊 Pydantic version detected: {PYDANTIC_VERSION}")
# Test version info
version_info = get_version_info()
print(f"🔍 Version info: {version_info}")
# Test field creation
private_field = create_private_field(default="test")
print(f"✅ Private field created: {type(private_field)}")
# Test model config
config = create_model_config(validate_assignment=True)
print(f"✅ Model config created: {type(config)}")
return True
except Exception as e:
print(f"❌ Error testing compatibility module: {e}")
return False
def test_chat_document_model():
"""Test the ChatDocument model with compatibility"""
try:
from interfaces.interfaceChatModel import ChatDocument
print(f"✅ Successfully imported ChatDocument model")
# Test creating a document
doc = ChatDocument(fileId="test-file-123")
print(f"✅ ChatDocument created: {doc.id}")
# Test setting component interface
doc.setComponentInterface("mock_interface")
print(f"✅ Component interface set")
# Test serialization
doc_dict = doc.to_dict()
print(f"✅ Document serialized: {doc_dict}")
# Test validation
is_valid = doc.validate_component_interface()
print(f"✅ Component interface validation: {is_valid}")
return True
except Exception as e:
print(f"❌ Error testing ChatDocument model: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Main test function"""
print("🧪 Testing Pydantic Compatibility Module")
print("=" * 50)
# Test compatibility module
compat_ok = test_compatibility_module()
print()
# Test ChatDocument model
model_ok = test_chat_document_model()
print()
# Summary
print("=" * 50)
if compat_ok and model_ok:
print("🎉 All tests passed! Pydantic compatibility is working correctly.")
return 0
else:
print("💥 Some tests failed. Check the errors above.")
return 1
if __name__ == "__main__":
sys.exit(main())