Spaces:
Running
Running
Commit
·
ebc6429
1
Parent(s):
a6398e4
fix: mock get_model in tests to avoid API key requirement in CI
Browse files
tests/unit/agents/test_hypothesis_agent.py
CHANGED
|
@@ -48,19 +48,21 @@ async def test_hypothesis_agent_generates_hypotheses(sample_evidence, mock_asses
|
|
| 48 |
"""HypothesisAgent should generate mechanistic hypotheses."""
|
| 49 |
store = {"current": sample_evidence, "hypotheses": []}
|
| 50 |
|
| 51 |
-
with patch("src.agents.hypothesis_agent.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.asyncio
|
|
@@ -68,12 +70,12 @@ async def test_hypothesis_agent_no_evidence():
|
|
| 68 |
"""HypothesisAgent should handle empty evidence gracefully."""
|
| 69 |
store = {"current": [], "hypotheses": []}
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
|
| 79 |
@pytest.mark.asyncio
|
|
@@ -82,20 +84,22 @@ async def test_hypothesis_agent_uses_embeddings(sample_evidence, mock_assessment
|
|
| 82 |
store = {"current": sample_evidence, "hypotheses": []}
|
| 83 |
mock_embeddings = MagicMock()
|
| 84 |
|
| 85 |
-
with patch("src.agents.hypothesis_agent.
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
| 48 |
"""HypothesisAgent should generate mechanistic hypotheses."""
|
| 49 |
store = {"current": sample_evidence, "hypotheses": []}
|
| 50 |
|
| 51 |
+
with patch("src.agents.hypothesis_agent.get_model") as mock_get_model:
|
| 52 |
+
with patch("src.agents.hypothesis_agent.Agent") as mock_agent_class:
|
| 53 |
+
mock_get_model.return_value = MagicMock() # Mock model
|
| 54 |
+
mock_result = MagicMock()
|
| 55 |
+
mock_result.output = mock_assessment
|
| 56 |
+
# pydantic-ai Agent returns an object with .output for structured output
|
| 57 |
+
mock_agent_class.return_value.run = AsyncMock(return_value=mock_result)
|
| 58 |
|
| 59 |
+
agent = HypothesisAgent(store)
|
| 60 |
+
response = await agent.run("metformin cancer")
|
| 61 |
|
| 62 |
+
assert isinstance(response, AgentRunResponse)
|
| 63 |
+
assert "AMPK" in response.messages[0].text
|
| 64 |
+
assert len(store["hypotheses"]) == 1
|
| 65 |
+
assert store["hypotheses"][0].drug == "Metformin"
|
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.asyncio
|
|
|
|
| 70 |
"""HypothesisAgent should handle empty evidence gracefully."""
|
| 71 |
store = {"current": [], "hypotheses": []}
|
| 72 |
|
| 73 |
+
# No need to mock Agent/get_model - empty evidence returns early
|
| 74 |
+
agent = HypothesisAgent(store)
|
| 75 |
+
response = await agent.run("test query")
|
| 76 |
|
| 77 |
+
assert "No evidence" in response.messages[0].text
|
| 78 |
+
assert len(store["hypotheses"]) == 0
|
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.asyncio
|
|
|
|
| 84 |
store = {"current": sample_evidence, "hypotheses": []}
|
| 85 |
mock_embeddings = MagicMock()
|
| 86 |
|
| 87 |
+
with patch("src.agents.hypothesis_agent.get_model") as mock_get_model:
|
| 88 |
+
with patch("src.agents.hypothesis_agent.Agent") as mock_agent_class:
|
| 89 |
+
# Mock format_hypothesis_prompt to check if embeddings were passed
|
| 90 |
+
with patch("src.agents.hypothesis_agent.format_hypothesis_prompt") as mock_format:
|
| 91 |
+
mock_get_model.return_value = MagicMock() # Mock model
|
| 92 |
+
mock_format.return_value = "Prompt"
|
| 93 |
+
|
| 94 |
+
mock_result = MagicMock()
|
| 95 |
+
mock_result.output = mock_assessment
|
| 96 |
+
mock_agent_class.return_value.run = AsyncMock(return_value=mock_result)
|
| 97 |
+
|
| 98 |
+
agent = HypothesisAgent(store, embedding_service=mock_embeddings)
|
| 99 |
+
await agent.run("query")
|
| 100 |
+
|
| 101 |
+
mock_format.assert_called_once()
|
| 102 |
+
_args, kwargs = mock_format.call_args
|
| 103 |
+
assert kwargs["embeddings"] == mock_embeddings
|
| 104 |
+
assert _args[0] == "query" # First positional arg is query
|
| 105 |
+
assert _args[1] == sample_evidence # Second positional arg is evidence
|