Files
OSIT-AE-API-FastAPI/tests/unit/test_unit_hosted_file_logic.py

71 lines
2.1 KiB
Python

import sys
import os
from unittest.mock import MagicMock
# Add project root to path
sys.path.append(os.getcwd())
# Mock dependencies to allow importing the model without side effects
mock_config = MagicMock()
mock_config.settings = MagicMock()
sys.modules["app.config"] = mock_config
sys.modules["redis"] = MagicMock()
sys.modules["app.log"] = MagicMock()
sys.modules["app.lib_general"] = MagicMock()
# Mock app.db_sql
mock_db_sql = MagicMock()
mock_db_sql.get_id_random.return_value = "random_str_abc"
sys.modules["app.db_sql"] = mock_db_sql
from app.models.hosted_file_models import Hosted_File_Base
def test_hosted_file_model_id_mapping():
print("--- Testing Hosted_File_Base ID Mapping ---")
# 1. Test simulation of database record (integers)
db_record = {
"id": 123,
"id_random": "random_str_abc",
"account_id": 456,
"account_id_random": "acc_rand_xyz",
"filename": "test.txt",
"extension": "txt"
}
model = Hosted_File_Base(**db_record)
result = model.dict(by_alias=True)
print(f"Model Data (by_alias=True): {result}")
# Verify that the frontend sees string IDs
assert result.get("id") == "random_str_abc"
assert result.get("hosted_file_id") == "random_str_abc"
assert result.get("account_id") == "acc_rand_xyz"
# 2. Test simulation of manual creation with mixed IDs
manual_data = {
"account_id": 456, # already resolved integer
"account_id_random": "acc_rand_xyz",
"id_random": "new_file_id"
}
model2 = Hosted_File_Base(**manual_data)
result2 = model2.dict(by_alias=True)
print(f"Model Data 2 (by_alias=True): {result2}")
assert result2.get("account_id") == "acc_rand_xyz"
assert result2.get("id") == "new_file_id"
print("✅ Hosted_File_Base ID mapping verified.")
if __name__ == "__main__":
try:
test_hosted_file_model_id_mapping()
print("\n🎉 MODEL LOGIC TEST PASSED!")
except Exception as e:
print(f"\n❌ TEST FAILED: {e}")
import traceback
traceback.print_exc()
sys.exit(1)