- Categorized scripts into tests/unit/, tests/integration/, tests/e2e/, and tests/tools/. - Adopted consistent naming prefixes (test_unit_*, test_int_*, test_e2e_*, tool_*). - Renamed conftest_mock.py to mock_config_helper.py for clarity. - Updated test_int_boot_diagnosis.py with sys.path setup for root-level execution.
45 lines
1.4 KiB
Python
45 lines
1.4 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 == "Duplicate entry 'abc' for key 'id_random'":
|
|
print("✅ Error formatting works.")
|
|
else:
|
|
print("❌ Error formatting FAILED.")
|
|
|
|
def test_null_error_handling():
|
|
print("\n--- Testing Null Error Handling ---")
|
|
if format_db_error(None) == "":
|
|
print("✅ Null error handled correctly.")
|
|
else:
|
|
print("❌ Null error check FAILED.")
|
|
|
|
if __name__ == "__main__":
|
|
test_error_formatting()
|
|
test_null_error_handling()
|