139 lines
4.5 KiB
Python
139 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test that BaiduFanyiView uses enum classes for code and message
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import django
|
|
from django.conf import settings
|
|
|
|
# Add the project directory to Python path
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'chunyu_project'))
|
|
|
|
# Set up Django environment
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chunyu_project.settings')
|
|
django.setup()
|
|
|
|
def test_baidu_enum_import():
|
|
"""Test if Baidu response codes can be imported"""
|
|
try:
|
|
from api.views.baidu_response_codes import (
|
|
BaiduResponseCode,
|
|
BaiduResponseMessage,
|
|
create_baidu_standardized_response,
|
|
create_baidu_error_response
|
|
)
|
|
print("OK Baidu response codes imported successfully")
|
|
|
|
# Test enum values
|
|
assert BaiduResponseCode.SUCCESS == 10000
|
|
assert BaiduResponseCode.PARAMETER_ERROR == 20001
|
|
assert BaiduResponseCode.TEXT_TOO_LONG == 20002
|
|
assert BaiduResponseCode.LANGUAGE_NOT_SUPPORTED == 20004
|
|
assert BaiduResponseCode.SERVICE_UNAVAILABLE == 20007
|
|
|
|
print("OK Baidu response codes enumeration works correctly")
|
|
return True
|
|
except Exception as e:
|
|
print(f"FAIL Error importing Baidu enums: {e}")
|
|
return False
|
|
|
|
def test_baidu_view_import():
|
|
"""Test if Baidu views can be imported with enum usage"""
|
|
try:
|
|
from api.views.BaiduFanyiView import (
|
|
BaiduFanyiView,
|
|
RecognizeLangTypeViews,
|
|
PictureRecognizeViews,
|
|
SpeechRecognitionView
|
|
)
|
|
|
|
print("OK Baidu views imported successfully")
|
|
|
|
# Check if views are properly defined
|
|
assert hasattr(BaiduFanyiView, 'post'), "BaiduFanyiView should have post method"
|
|
assert hasattr(RecognizeLangTypeViews, 'post'), "RecognizeLangTypeViews should have post method"
|
|
|
|
print("OK All Baidu view classes are defined")
|
|
return True
|
|
except Exception as e:
|
|
print(f"FAIL Error importing Baidu views: {e}")
|
|
return False
|
|
|
|
def test_standardized_responses():
|
|
"""Test standardized response creation"""
|
|
try:
|
|
from api.views.baidu_response_codes import (
|
|
BaiduResponseCode,
|
|
create_baidu_standardized_response,
|
|
create_baidu_error_response
|
|
)
|
|
|
|
# Test success response
|
|
success_resp = create_baidu_standardized_response(
|
|
data={"trans_result": [{"src": "Hello", "dst": "你好"}]},
|
|
code=BaiduResponseCode.TRANSLATION_SUCCESS
|
|
)
|
|
assert success_resp["code"] == "10001"
|
|
assert success_resp["message"] == "Translation completed successfully"
|
|
assert success_resp["data"]["trans_result"][0]["src"] == "Hello"
|
|
|
|
# Test error response
|
|
error_resp = create_baidu_error_response(
|
|
message="Text too long",
|
|
code=BaiduResponseCode.TEXT_TOO_LONG
|
|
)
|
|
assert error_resp["code"] == "20002"
|
|
assert error_resp["message"] == "Text too long"
|
|
assert error_resp["data"] == {}
|
|
|
|
print("OK Standardized Baidu responses work correctly")
|
|
return True
|
|
except Exception as e:
|
|
print(f"FAIL Error testing Baidu responses: {e}")
|
|
return False
|
|
|
|
def main():
|
|
"""Main test function"""
|
|
print("Testing BaiduFanyiView Enum Usage")
|
|
print("=" * 50)
|
|
|
|
tests = [
|
|
("Baidu Enum Import", test_baidu_enum_import),
|
|
("Baidu View Import", test_baidu_view_import),
|
|
("Standardized Responses", test_standardized_responses),
|
|
]
|
|
|
|
passed = 0
|
|
total = len(tests)
|
|
|
|
for test_name, test_func in tests:
|
|
print(f"\n{test_name}:")
|
|
print("-" * len(test_name))
|
|
|
|
if test_func():
|
|
passed += 1
|
|
print(f"PASS {test_name}")
|
|
else:
|
|
print(f"FAIL {test_name}")
|
|
|
|
print("\n" + "=" * 50)
|
|
print(f"Results: {passed}/{total} tests passed")
|
|
|
|
if passed == total:
|
|
print("\n🎉 BaiduFanyiView enum usage test successful!")
|
|
print("\n✅ Your Baidu translation API now:")
|
|
print(" - Uses standardized enum-based response codes")
|
|
print(" - Returns consistent format across all endpoints")
|
|
print(" - Has comprehensive error handling with enum codes")
|
|
print(" - Maintains async performance benefits")
|
|
print("\n🚀 Ready to test your Baidu API endpoints!")
|
|
return True
|
|
else:
|
|
print("\n⚠️ Some tests failed.")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
success = main()
|
|
sys.exit(0 if success else 1) |