"""
测试配置和工具
"""
import pytest
import asyncio
from fastapi.testclient import TestClient
from unittest.mock import AsyncMock, MagicMock
from pubchem_mcp.main import app
from pubchem_mcp.services.cache_service import CacheService
from pubchem_mcp.services.pubchem_client import PubChemClient
@pytest.fixture
def client():
"""测试客户端"""
return TestClient(app)
@pytest.fixture
def mock_cache_service():
"""模拟缓存服务"""
cache = CacheService()
cache.get = AsyncMock(return_value=None)
cache.set = AsyncMock()
cache.get_compound_info = AsyncMock(return_value=None)
cache.set_compound_info = AsyncMock()
cache.get_safety_info = AsyncMock(return_value=None)
cache.set_safety_info = AsyncMock()
cache.get_toxicity_data = AsyncMock(return_value=None)
cache.set_toxicity_data = AsyncMock()
return cache
@pytest.fixture
def mock_pubchem_client():
"""模拟PubChem客户端"""
client = MagicMock()
client.__aenter__ = AsyncMock(return_value=client)
client.__aexit__ = AsyncMock(return_value=None)
client.get_compound_cid = AsyncMock(return_value=11294)
client.get_compound_by_name = AsyncMock(return_value={
"PropertyTable": {
"Properties": [{
"MolecularFormula": "C23H25ClN2",
"MolecularWeight": 364.91,
"IUPACName": "4-[(4-dimethylaminophenyl)phenylmethylidene]-N,N-dimethylaniline",
"IsomericSMILES": "CN(C)c1ccc(cc1)C(=C2C=CC(=[N+](C)C)C=C2)c3ccc(cc3)N(C)C.[Cl-]",
"InChIKey": "BXXJDRTZQYFIBD-UHFFFAOYSA-M"
}]
}
})
client.get_safety_info = AsyncMock(return_value={
"signal_word": "Danger",
"ghs_pictograms": ["Corrosive", "Health Hazard"],
"ghs_hazard_statements": ["H302: Harmful if swallowed"],
"precautionary_statements": ["P280: Wear protective gloves"]
})
client.get_toxicity_data = AsyncMock(return_value={
"acute_toxicity": {"oral_rat_LD50": "275 mg/kg"},
"ecotoxicity": {"fish_LC50": "0.1 mg/L (96h)"},
"carcinogenicity": "Suspected carcinogen",
"reproductive_toxicity": None
})
return client
@pytest.fixture
def event_loop():
"""事件循环fixture"""
loop = asyncio.new_event_loop()
yield loop
loop.close()