- Overhauled README.md to serve as a unified system index and WIP tracker. - Standardized documentation filenames (ARCH__, GUIDE__, PLAN__) for better discoverability. - Archived completed project plans and scopes. - Fixed regressions in unit tests (errors, models, email) caused by V3 architectural updates. - Ensured unit tests remain non-destructive and environment-independent via mocking.
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
import sys
|
|
import os
|
|
import asyncio
|
|
from unittest.mock import MagicMock, AsyncMock
|
|
|
|
# --- Environment Setup ---
|
|
sys.modules['redis'] = MagicMock()
|
|
sys.modules['sqlalchemy'] = MagicMock()
|
|
sys.modules['sqlalchemy.text'] = MagicMock()
|
|
sys.modules['app.config'] = MagicMock()
|
|
sys.modules['app.log'] = MagicMock()
|
|
sys.modules['app.lib_general'] = MagicMock()
|
|
|
|
# Mock app.db_sql
|
|
mock_db_sql = MagicMock()
|
|
mock_db_sql.get_last_sql_error.return_value = '(pymysql.err.IntegrityError) (1062, "Duplicate entry \'test-id\' for key \'id_random\'" )'
|
|
sys.modules['app.db_sql'] = mock_db_sql
|
|
|
|
# Add project root to path
|
|
sys.path.append(os.getcwd())
|
|
|
|
from app.lib_api_crud_v3 import format_db_error
|
|
|
|
def test_error_formatting():
|
|
print("\n--- Testing Error Formatting ---")
|
|
raw = '(pymysql.err.IntegrityError) (1062, "Duplicate entry \'abc\' for key \'id_random\'" )'
|
|
formatted = format_db_error(raw)
|
|
print(f"Raw: {raw}")
|
|
print(f"Formatted: {formatted}")
|
|
|
|
if formatted.category == "database_duplicate" and formatted.code == 1062:
|
|
print("✅ Error formatting works.")
|
|
else:
|
|
print("❌ Error formatting FAILED.")
|
|
|
|
def test_null_error_handling():
|
|
print("\n--- Testing Null Error Handling ---")
|
|
formatted = format_db_error(None)
|
|
if formatted.category == "unknown":
|
|
print("✅ Null error handled correctly.")
|
|
else:
|
|
print("❌ Null error check FAILED.")
|
|
|
|
if __name__ == "__main__":
|
|
test_error_formatting()
|
|
test_null_error_handling()
|