Skip to main content
Glama

MCP Market Statistics Server

by whdghk1907
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

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/whdghk1907/mcp-market-statistics'

If you have feedback or need assistance with the MCP directory API, please join our Discord server