Files
OSIT-AE-API-FastAPI/tests/unit/test_unit_models.py
Scott Idem b2384f2869 Tests: Reorganize test suite into functional subdirectories
- 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.
2026-01-16 10:46:19 -05:00

77 lines
2.8 KiB
Python

import sys
import os
from typing import ClassVar
from unittest.mock import MagicMock
# --- Environment Setup ---
# Mocking heavy dependencies to allow running in restricted environments
sys.modules['redis'] = MagicMock()
sys.modules['sqlalchemy'] = MagicMock()
sys.modules['sqlalchemy.exc'] = MagicMock()
sys.modules['sqlalchemy.pool'] = MagicMock()
sys.modules['fastapi'] = MagicMock()
sys.modules['app.config'] = MagicMock()
sys.modules['html2text'] = MagicMock()
sys.modules['app.lib_email'] = MagicMock()
sys.modules['app.lib_export'] = MagicMock()
sys.modules['app.lib_jwt'] = MagicMock()
sys.modules['app.lib_hash'] = MagicMock()
sys.modules['app.log'] = MagicMock()
# Mock app.lib_general (needed for log/logging)
mock_lib_general = MagicMock()
mock_lib_general.log = MagicMock()
mock_lib_general.logging = MagicMock()
sys.modules['app.lib_general'] = mock_lib_general
sys.modules['app.log'] = MagicMock() # Ensure app.log is also mocked if needed separately
mock_db_sql = MagicMock()
mock_db_sql.redis_lookup_id_random.return_value = 1
mock_db_sql.get_id_random.return_value = "mock_id"
sys.modules['app.db_sql'] = mock_db_sql
# Add project root to path
sys.path.append(os.getcwd())
# --- Imports ---
try:
from app.models.person_models import Person_Base
print("✅ Person_Base model imported.")
except Exception as e:
print(f"❌ Failed to import models: {e}")
sys.exit(1)
# --- Tests ---
def test_person_null_given_name():
"""Test that given_name=None is converted to empty string."""
try:
# construct() bypasses validation, so we use the constructor
# We provide dummy values for other likely required fields
p = Person_Base.construct(given_name=None)
# Note: In Pydantic V1 validators run on __init__.
# Since we mocked the environment, we'll test the validator function directly if init fails.
from app.models.person_models import Person_Base
val = Person_Base.given_name_validator(None)
if val == "":
print("✅ given_name validator: None -> '' (Success)")
else:
print(f"❌ given_name validator: Expected '', got {val!r}")
except Exception as e:
print(f"❌ test_person_null_given_name failed: {e}")
def test_person_null_allow_auth_key():
"""Test that allow_auth_key=None is converted to True."""
try:
from app.models.person_models import Person_Base
val = Person_Base.allow_auth_key_validator(None)
if val is True:
print("✅ allow_auth_key validator: None -> True (Success)")
else:
print(f"❌ allow_auth_key validator: Expected True, got {val!r}")
except Exception as e:
print(f"❌ test_person_null_allow_auth_key failed: {e}")
if __name__ == "__main__":
test_person_null_given_name()
test_person_null_allow_auth_key()