136 lines
4.4 KiB
Python
136 lines
4.4 KiB
Python
import os
|
|
import uuid
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.models.db import UUID_COLUMN_TYPE, Base, SQLiteUUID
|
|
|
|
# --- Test SQLiteUUID Type ---
|
|
|
|
|
|
def test_sqliteuuid_process_bind_param_none():
|
|
"""Test SQLiteUUID.process_bind_param with None returns None"""
|
|
uuid_type = SQLiteUUID()
|
|
assert uuid_type.process_bind_param(None, None) is None
|
|
|
|
|
|
def test_sqliteuuid_process_bind_param_valid_uuid():
|
|
"""Test SQLiteUUID.process_bind_param with valid UUID returns string"""
|
|
uuid_type = SQLiteUUID()
|
|
test_uuid = uuid.uuid4()
|
|
assert uuid_type.process_bind_param(test_uuid, None) == str(test_uuid)
|
|
|
|
|
|
def test_sqliteuuid_process_bind_param_valid_string():
|
|
"""Test SQLiteUUID.process_bind_param with valid UUID string returns string"""
|
|
uuid_type = SQLiteUUID()
|
|
test_uuid_str = "550e8400-e29b-41d4-a716-446655440000"
|
|
assert uuid_type.process_bind_param(test_uuid_str, None) == test_uuid_str
|
|
|
|
|
|
def test_sqliteuuid_process_bind_param_invalid_string():
|
|
"""Test SQLiteUUID.process_bind_param raises ValueError for invalid UUID"""
|
|
uuid_type = SQLiteUUID()
|
|
with pytest.raises(ValueError, match="Invalid UUID string format"):
|
|
uuid_type.process_bind_param("invalid-uuid", None)
|
|
|
|
|
|
def test_sqliteuuid_process_result_value_none():
|
|
"""Test SQLiteUUID.process_result_value with None returns None"""
|
|
uuid_type = SQLiteUUID()
|
|
assert uuid_type.process_result_value(None, None) is None
|
|
|
|
|
|
def test_sqliteuuid_process_result_value_valid_string():
|
|
"""Test SQLiteUUID.process_result_value converts string to UUID"""
|
|
uuid_type = SQLiteUUID()
|
|
test_uuid = uuid.uuid4()
|
|
result = uuid_type.process_result_value(str(test_uuid), None)
|
|
assert isinstance(result, uuid.UUID)
|
|
assert result == test_uuid
|
|
|
|
|
|
def test_sqliteuuid_compare_values_none():
|
|
"""Test SQLiteUUID.compare_values handles None values"""
|
|
uuid_type = SQLiteUUID()
|
|
assert uuid_type.compare_values(None, None) is True
|
|
assert uuid_type.compare_values(None, uuid.uuid4()) is False
|
|
assert uuid_type.compare_values(uuid.uuid4(), None) is False
|
|
|
|
|
|
def test_sqliteuuid_compare_values_uuid():
|
|
"""Test SQLiteUUID.compare_values compares UUIDs as strings"""
|
|
uuid_type = SQLiteUUID()
|
|
test_uuid = uuid.uuid4()
|
|
assert uuid_type.compare_values(test_uuid, test_uuid) is True
|
|
assert uuid_type.compare_values(test_uuid, uuid.uuid4()) is False
|
|
|
|
|
|
def test_sqlite_uuid_comparison():
|
|
"""Test SQLiteUUID comparison functionality (moved from db_mocks.py)"""
|
|
uuid_type = SQLiteUUID()
|
|
|
|
# Test equal UUIDs
|
|
uuid1 = uuid.uuid4()
|
|
uuid2 = uuid.UUID(str(uuid1))
|
|
assert uuid_type.compare_values(uuid1, uuid2) is True
|
|
|
|
# Test UUID vs string
|
|
assert uuid_type.compare_values(uuid1, str(uuid1)) is True
|
|
|
|
# Test None comparisons
|
|
assert uuid_type.compare_values(None, None) is True
|
|
assert uuid_type.compare_values(uuid1, None) is False
|
|
assert uuid_type.compare_values(None, uuid1) is False
|
|
|
|
# Test different UUIDs
|
|
uuid3 = uuid.uuid4()
|
|
assert uuid_type.compare_values(uuid1, uuid3) is False
|
|
|
|
|
|
def test_sqlite_uuid_binding():
|
|
"""Test SQLiteUUID binding parameter handling (moved from db_mocks.py)"""
|
|
uuid_type = SQLiteUUID()
|
|
|
|
# Test UUID object binding
|
|
uuid_obj = uuid.uuid4()
|
|
assert uuid_type.process_bind_param(uuid_obj, None) == str(uuid_obj)
|
|
|
|
# Test valid UUID string binding
|
|
uuid_str = str(uuid.uuid4())
|
|
assert uuid_type.process_bind_param(uuid_str, None) == uuid_str
|
|
|
|
# Test None handling
|
|
assert uuid_type.process_bind_param(None, None) is None
|
|
|
|
# Test invalid UUID string
|
|
with pytest.raises(ValueError):
|
|
uuid_type.process_bind_param("invalid-uuid", None)
|
|
|
|
|
|
# --- Test UUID Column Type Configuration ---
|
|
|
|
|
|
def test_uuid_column_type_default():
|
|
"""Test UUID_COLUMN_TYPE uses SQLiteUUID in test environment"""
|
|
assert isinstance(UUID_COLUMN_TYPE, SQLiteUUID)
|
|
|
|
|
|
@patch.dict(os.environ, {"MOCK_AUTH": "false"})
|
|
def test_uuid_column_type_postgres():
|
|
"""Test UUID_COLUMN_TYPE uses Postgres UUID when MOCK_AUTH=false"""
|
|
# Need to re-import to get the patched environment
|
|
from importlib import reload
|
|
|
|
from app import models
|
|
|
|
reload(models.db)
|
|
from sqlalchemy.dialects.postgresql import UUID as PostgresUUID
|
|
|
|
from app.models.db import UUID_COLUMN_TYPE
|
|
|
|
assert isinstance(UUID_COLUMN_TYPE, PostgresUUID)
|