File size: 12,714 Bytes
4e2ccbf d55440b 05ffeb4 d55440b 4e2ccbf 05ffeb4 d55440b 4e2ccbf d55440b 05ffeb4 d55440b 1bc9785 d55440b 1bc9785 d55440b 1bc9785 d55440b 4e2ccbf d55440b 4e2ccbf d55440b 4e2ccbf 05ffeb4 d55440b fa696e8 d55440b fa696e8 d55440b fa696e8 d55440b fa696e8 d55440b 4e2ccbf d55440b 4e2ccbf d55440b fa696e8 4e2ccbf d55440b fa696e8 d55440b 4e2ccbf 05ffeb4 d55440b 4e2ccbf d55440b 4e2ccbf d55440b 4e2ccbf d55440b 1bc9785 d55440b 05ffeb4 d55440b 4e2ccbf d55440b 4e2ccbf de1af88 d55440b 4e2ccbf d55440b 4e2ccbf 05ffeb4 89f1173 4e2ccbf 89f1173 4e2ccbf d55440b 4e2ccbf d55440b de1af88 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
"""Unit tests for ClinicalTrials.gov tool."""
from unittest.mock import MagicMock, patch
import pytest
from src.tools.clinicaltrials import ClinicalTrialsTool
from src.utils.models import Evidence
@pytest.mark.unit
class TestClinicalTrialsTool:
"""Tests for ClinicalTrialsTool."""
@pytest.fixture
def tool(self) -> ClinicalTrialsTool:
return ClinicalTrialsTool()
def test_tool_name(self, tool: ClinicalTrialsTool) -> None:
assert tool.name == "clinicaltrials"
@pytest.mark.asyncio
async def test_search_uses_filters(self, tool: ClinicalTrialsTool) -> None:
"""Test that search applies status and type filters."""
mock_response = MagicMock()
mock_response.json.return_value = {"studies": []}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response) as mock_get:
await tool.search("test query", max_results=5)
# Verify filters were applied
call_args = mock_get.call_args
params = call_args.kwargs.get("params", call_args[1].get("params", {}))
# Should filter for active/completed studies
assert "filter.overallStatus" in params
assert "COMPLETED" in params["filter.overallStatus"]
assert "RECRUITING" in params["filter.overallStatus"]
# Should filter for interventional studies via query term
assert "AREA[StudyType]INTERVENTIONAL" in params["query.term"]
assert "filter.studyType" not in params
@pytest.mark.asyncio
async def test_search_returns_evidence(self, tool: ClinicalTrialsTool) -> None:
"""Test that search returns Evidence objects."""
mock_study = {
"protocolSection": {
"identificationModule": {
"nctId": "NCT12345678",
"briefTitle": "Testosterone for HSDD Treatment",
},
"statusModule": {
"overallStatus": "COMPLETED",
"startDateStruct": {"date": "2023-01-01"},
},
"descriptionModule": {
"briefSummary": "A study examining testosterone for HSDD symptoms.",
},
"designModule": {
"phases": ["PHASE2", "PHASE3"],
},
"conditionsModule": {
"conditions": ["HSDD", "Hypoactive Sexual Desire"],
},
"armsInterventionsModule": {
"interventions": [{"name": "Testosterone"}],
},
}
}
mock_response = MagicMock()
mock_response.json.return_value = {"studies": [mock_study]}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response):
results = await tool.search("testosterone hsdd", max_results=5)
assert len(results) == 1
assert isinstance(results[0], Evidence)
assert "Testosterone" in results[0].citation.title
assert "PHASE2" in results[0].content or "Phase" in results[0].content
@pytest.mark.asyncio
async def test_search_includes_phase_info(self, tool: ClinicalTrialsTool) -> None:
"""Test that phase information is included in content."""
mock_study = {
"protocolSection": {
"identificationModule": {
"nctId": "NCT12345678",
"briefTitle": "Test Study",
},
"statusModule": {
"overallStatus": "RECRUITING",
"startDateStruct": {"date": "2024-01-01"},
},
"descriptionModule": {
"briefSummary": "Test summary.",
},
"designModule": {
"phases": ["PHASE3"],
},
"conditionsModule": {"conditions": ["Test"]},
"armsInterventionsModule": {"interventions": []},
}
}
mock_response = MagicMock()
mock_response.json.return_value = {"studies": [mock_study]}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response):
results = await tool.search("test", max_results=5)
# Phase should be in content
assert "PHASE3" in results[0].content or "Phase 3" in results[0].content
@pytest.mark.asyncio
async def test_search_empty_results(self, tool: ClinicalTrialsTool) -> None:
"""Test handling of empty results."""
mock_response = MagicMock()
mock_response.json.return_value = {"studies": []}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response):
results = await tool.search("nonexistent xyz 12345", max_results=5)
assert results == []
@pytest.mark.unit
class TestClinicalTrialsOutcomes:
"""Tests for outcome measure extraction."""
@pytest.fixture
def tool(self) -> ClinicalTrialsTool:
return ClinicalTrialsTool()
@pytest.mark.asyncio
async def test_extracts_primary_outcome(self, tool: ClinicalTrialsTool) -> None:
"""Test that primary outcome is extracted from response."""
mock_study = {
"protocolSection": {
"identificationModule": {"nctId": "NCT12345678", "briefTitle": "Test"},
"statusModule": {"overallStatus": "COMPLETED", "startDateStruct": {"date": "2023"}},
"descriptionModule": {"briefSummary": "Summary"},
"designModule": {"phases": ["PHASE3"]},
"conditionsModule": {"conditions": ["ED"]},
"armsInterventionsModule": {"interventions": []},
"outcomesModule": {
"primaryOutcomes": [
{"measure": "Change in IIEF-EF score", "timeFrame": "Week 12"}
]
},
},
"hasResults": True,
}
mock_response = MagicMock()
mock_response.json.return_value = {"studies": [mock_study]}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response):
results = await tool.search("test", max_results=1)
assert len(results) == 1
assert "Primary Outcome" in results[0].content
assert "IIEF-EF" in results[0].content
assert "Week 12" in results[0].content
@pytest.mark.asyncio
async def test_includes_results_status(self, tool: ClinicalTrialsTool) -> None:
"""Test that results availability is shown."""
mock_study = {
"protocolSection": {
"identificationModule": {"nctId": "NCT12345678", "briefTitle": "Test"},
"statusModule": {
"overallStatus": "COMPLETED",
"startDateStruct": {"date": "2023"},
# Note: resultsFirstPostDateStruct, not resultsFirstSubmitDate
"resultsFirstPostDateStruct": {"date": "2024-06-15"},
},
"descriptionModule": {"briefSummary": "Summary"},
"designModule": {"phases": ["PHASE3"]},
"conditionsModule": {"conditions": ["ED"]},
"armsInterventionsModule": {"interventions": []},
"outcomesModule": {},
},
"hasResults": True, # Note: hasResults is TOP-LEVEL
}
mock_response = MagicMock()
mock_response.json.return_value = {"studies": [mock_study]}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response):
results = await tool.search("test", max_results=1)
assert "Results Available: Yes" in results[0].content
assert "2024-06-15" in results[0].content
@pytest.mark.asyncio
async def test_shows_no_results_when_missing(self, tool: ClinicalTrialsTool) -> None:
"""Test that missing results are indicated."""
mock_study = {
"protocolSection": {
"identificationModule": {
"nctId": "NCT99999999",
"briefTitle": "Test Study",
},
"statusModule": {
"overallStatus": "RECRUITING",
"startDateStruct": {"date": "2024"},
},
"descriptionModule": {"briefSummary": "Summary"},
"designModule": {"phases": ["PHASE2"]},
"conditionsModule": {"conditions": ["ED"]},
"armsInterventionsModule": {"interventions": []},
"outcomesModule": {},
},
"hasResults": False,
}
mock_response = MagicMock()
mock_response.json.return_value = {"studies": [mock_study]}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response):
results = await tool.search("test", max_results=1)
assert "Results Available: Not yet posted" in results[0].content
@pytest.mark.asyncio
async def test_boosts_relevance_for_results(self, tool: ClinicalTrialsTool) -> None:
"""Trials with results should have higher relevance score."""
with_results = {
"protocolSection": {
"identificationModule": {"nctId": "NCT11111111", "briefTitle": "With Results"},
"statusModule": {"overallStatus": "COMPLETED", "startDateStruct": {"date": "2023"}},
"descriptionModule": {"briefSummary": "Summary"},
"designModule": {"phases": []},
"conditionsModule": {"conditions": []},
"armsInterventionsModule": {"interventions": []},
"outcomesModule": {},
},
"hasResults": True,
}
without_results = {
"protocolSection": {
"identificationModule": {"nctId": "NCT22222222", "briefTitle": "No Results"},
"statusModule": {
"overallStatus": "RECRUITING",
"startDateStruct": {"date": "2024"},
},
"descriptionModule": {"briefSummary": "Summary"},
"designModule": {"phases": []},
"conditionsModule": {"conditions": []},
"armsInterventionsModule": {"interventions": []},
"outcomesModule": {},
},
"hasResults": False,
}
mock_response = MagicMock()
mock_response.json.return_value = {"studies": [with_results, without_results]}
mock_response.raise_for_status = MagicMock()
with patch("requests.get", return_value=mock_response):
results = await tool.search("test", max_results=2)
assert results[0].relevance == 0.90 # With results
assert results[1].relevance == 0.85 # Without results
@pytest.mark.integration
class TestClinicalTrialsIntegration:
"""Integration tests with real API."""
@pytest.mark.asyncio
async def test_real_api_returns_interventional(self) -> None:
"""Test that real API returns interventional studies for sexual health query."""
tool = ClinicalTrialsTool()
results = await tool.search("testosterone HSDD", max_results=3)
# Should get results
assert len(results) > 0
# Results should mention interventions or treatments
all_content = " ".join([r.content.lower() for r in results])
has_intervention = (
"intervention" in all_content
or "treatment" in all_content
or "drug" in all_content
or "phase" in all_content
)
assert has_intervention
@pytest.mark.asyncio
async def test_real_completed_trial_has_outcome(self) -> None:
"""Real completed Phase 3 trials should have outcome measures."""
tool = ClinicalTrialsTool()
# Search for completed Phase 3 ED trials (likely to have outcomes)
results = await tool.search(
"sildenafil erectile dysfunction Phase 3 COMPLETED", max_results=3
)
# Skip if API returns no results (external dependency)
if not results:
pytest.skip("API returned no results for this query")
# At least one should have primary outcome
has_outcome = any("Primary Outcome" in r.content for r in results)
assert has_outcome, "No completed trials with outcome measures found"
|