test_database.pyβ’5.87 kB
"""λ°μ΄ν°λ² μ΄μ€ μ°κ²° λ° κ΄λ¦¬ ν
μ€νΈ"""
import pytest
import asyncio
from unittest.mock import Mock, patch, AsyncMock
from src.utils.database import DatabaseManager
from src.exceptions import DatabaseConnectionError
class TestDatabaseManager:
"""λ°μ΄ν°λ² μ΄μ€ λ§€λμ ν
μ€νΈ"""
@pytest.fixture
def db_config(self):
"""ν
μ€νΈμ© λ°μ΄ν°λ² μ΄μ€ μ€μ """
return {
"host": "localhost",
"port": 5432,
"name": "test_db",
"user": "test_user",
"password": "test_pass"
}
@pytest.fixture
def db_manager(self, db_config):
"""ν
μ€νΈμ© λ°μ΄ν°λ² μ΄μ€ λ§€λμ """
return DatabaseManager(db_config)
def test_database_manager_initialization(self, db_manager, db_config):
"""λ°μ΄ν°λ² μ΄μ€ λ§€λμ μ΄κΈ°ν ν
μ€νΈ"""
assert db_manager.config == db_config
assert db_manager.pool is None
assert not db_manager.is_connected
def test_connection_url_generation(self, db_manager):
"""μ°κ²° URL μμ± ν
μ€νΈ"""
expected_url = "postgresql://test_user:test_pass@localhost:5432/test_db"
assert db_manager.connection_url == expected_url
@pytest.mark.asyncio
async def test_successful_connection(self, db_manager):
"""μ±κ³΅μ μΈ λ°μ΄ν°λ² μ΄μ€ μ°κ²° ν
μ€νΈ"""
mock_pool = AsyncMock()
mock_pool._closed = False
with patch('src.utils.database.asyncpg.create_pool', new_callable=AsyncMock) as mock_create_pool:
mock_create_pool.return_value = mock_pool
await db_manager.connect()
assert db_manager.pool == mock_pool
assert db_manager.is_connected
mock_create_pool.assert_called_once()
@pytest.mark.asyncio
async def test_connection_failure(self, db_manager):
"""λ°μ΄ν°λ² μ΄μ€ μ°κ²° μ€ν¨ ν
μ€νΈ"""
with patch('src.utils.database.asyncpg.create_pool', new_callable=AsyncMock) as mock_create_pool:
mock_create_pool.side_effect = Exception("Connection failed")
with pytest.raises(DatabaseConnectionError):
await db_manager.connect()
assert db_manager.pool is None
assert not db_manager.is_connected
@pytest.mark.asyncio
async def test_disconnect(self, db_manager):
"""λ°μ΄ν°λ² μ΄μ€ μ°κ²° ν΄μ ν
μ€νΈ"""
# λ¨Όμ μ°κ²°
mock_pool = AsyncMock()
mock_pool._closed = False
with patch('src.utils.database.asyncpg.create_pool', new_callable=AsyncMock) as mock_create_pool:
mock_create_pool.return_value = mock_pool
await db_manager.connect()
# μ°κ²° ν΄μ
await db_manager.disconnect()
mock_pool.close.assert_called_once()
assert db_manager.pool is None
assert not db_manager.is_connected
@pytest.mark.asyncio
async def test_execute_query(self, db_manager):
"""쿼리 μ€ν ν
μ€νΈ"""
with patch('src.utils.database.asyncpg.create_pool', new_callable=AsyncMock) as mock_create_pool:
mock_pool = AsyncMock()
mock_pool._closed = False
mock_connection = AsyncMock()
mock_pool.acquire.return_value.__aenter__.return_value = mock_connection
# Mock record objects that behave like dicts
mock_record = Mock()
mock_record.__iter__ = Mock(return_value=iter([("id", 1), ("name", "test")]))
mock_connection.fetch.return_value = [mock_record]
mock_create_pool.return_value = mock_pool
await db_manager.connect()
result = await db_manager.fetch_all("SELECT * FROM test")
assert result == [{"id": 1, "name": "test"}]
mock_connection.fetch.assert_called_once_with("SELECT * FROM test")
@pytest.mark.asyncio
async def test_execute_query_without_connection(self, db_manager):
"""μ°κ²° μμ΄ μΏΌλ¦¬ μ€ν μ μμΈ ν
μ€νΈ"""
with pytest.raises(DatabaseConnectionError):
await db_manager.fetch_all("SELECT * FROM test")
@pytest.mark.asyncio
async def test_health_check_success(self, db_manager):
"""ν¬μ€μ²΄ν¬ μ±κ³΅ ν
μ€νΈ"""
with patch('src.utils.database.asyncpg.create_pool', new_callable=AsyncMock) as mock_create_pool:
mock_pool = AsyncMock()
mock_pool._closed = False
mock_connection = AsyncMock()
mock_pool.acquire.return_value.__aenter__.return_value = mock_connection
mock_connection.fetchval.return_value = 1
mock_create_pool.return_value = mock_pool
await db_manager.connect()
is_healthy = await db_manager.health_check()
assert is_healthy is True
mock_connection.fetchval.assert_called_once_with("SELECT 1")
@pytest.mark.asyncio
async def test_health_check_failure(self, db_manager):
"""ν¬μ€μ²΄ν¬ μ€ν¨ ν
μ€νΈ"""
with patch('src.utils.database.asyncpg.create_pool', new_callable=AsyncMock) as mock_create_pool:
mock_pool = AsyncMock()
mock_pool._closed = False
mock_connection = AsyncMock()
mock_pool.acquire.return_value.__aenter__.return_value = mock_connection
mock_connection.fetchval.side_effect = Exception("DB error")
mock_create_pool.return_value = mock_pool
await db_manager.connect()
is_healthy = await db_manager.health_check()
assert is_healthy is False