55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
import os
|
|
from datetime import datetime
|
|
|
|
# Add the project root to the Python path
|
|
sys.path.insert(0, '/Users/cakecai/Documents/gitea/hsa/Html')
|
|
|
|
# Test the JSON serializer
|
|
try:
|
|
from apps.utils.json_serializer import json_serialize, json_serialize_dict, JSONEncoder
|
|
import json
|
|
|
|
# Create a test dictionary with datetime object
|
|
test_data = {
|
|
"user_id": "test_user",
|
|
"material_id": "test_course",
|
|
"chapter_name": "test_chapter",
|
|
"lesson_name": "test_lesson",
|
|
"chat_historys": [],
|
|
"scores": [],
|
|
"chapter_chain_now": None,
|
|
"updated_at": datetime.now()
|
|
}
|
|
|
|
print("Testing datetime serialization...")
|
|
print(f"Original data: {test_data}")
|
|
print(f"Type of updated_at: {type(test_data['updated_at'])}")
|
|
|
|
# Test json_serialize function
|
|
json_str = json_serialize(test_data)
|
|
print(f"\nSerialized JSON string: {json_str}")
|
|
|
|
# Test json_serialize_dict function
|
|
serialized_dict = json_serialize_dict(test_data)
|
|
print(f"\nSerialized dictionary: {serialized_dict}")
|
|
print(f"Type of updated_at in serialized dict: {type(serialized_dict['updated_at'])}")
|
|
|
|
# Test direct json.dumps with custom encoder
|
|
json_str2 = json.dumps(test_data, cls=JSONEncoder, ensure_ascii=False)
|
|
print(f"\nDirect json.dumps with custom encoder: {json_str2}")
|
|
|
|
# Verify it can be parsed back
|
|
parsed = json.loads(json_str)
|
|
print(f"\nParsed back from JSON: {parsed}")
|
|
|
|
print("\n✅ All tests passed! datetime objects can be correctly serialized to JSON.")
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ Test failed with error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|