aiagents4pharma 1.27.2__py3-none-any.whl → 1.29.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aiagents4pharma/talk2scholars/agents/__init__.py +1 -0
- aiagents4pharma/talk2scholars/agents/main_agent.py +35 -209
- aiagents4pharma/talk2scholars/agents/pdf_agent.py +106 -0
- aiagents4pharma/talk2scholars/agents/s2_agent.py +10 -6
- aiagents4pharma/talk2scholars/agents/zotero_agent.py +12 -6
- aiagents4pharma/talk2scholars/configs/agents/talk2scholars/__init__.py +1 -0
- aiagents4pharma/talk2scholars/configs/agents/talk2scholars/main_agent/default.yaml +2 -48
- aiagents4pharma/talk2scholars/configs/agents/talk2scholars/pdf_agent/__init__.py +3 -0
- aiagents4pharma/talk2scholars/configs/agents/talk2scholars/s2_agent/default.yaml +5 -28
- aiagents4pharma/talk2scholars/configs/agents/talk2scholars/zotero_agent/default.yaml +5 -21
- aiagents4pharma/talk2scholars/configs/config.yaml +3 -0
- aiagents4pharma/talk2scholars/configs/tools/__init__.py +2 -0
- aiagents4pharma/talk2scholars/configs/tools/multi_paper_recommendation/default.yaml +1 -1
- aiagents4pharma/talk2scholars/configs/tools/question_and_answer/__init__.py +3 -0
- aiagents4pharma/talk2scholars/configs/tools/search/default.yaml +1 -1
- aiagents4pharma/talk2scholars/configs/tools/single_paper_recommendation/default.yaml +1 -1
- aiagents4pharma/talk2scholars/configs/tools/zotero_read/default.yaml +42 -1
- aiagents4pharma/talk2scholars/configs/tools/zotero_write/__inti__.py +3 -0
- aiagents4pharma/talk2scholars/state/state_talk2scholars.py +1 -0
- aiagents4pharma/talk2scholars/tests/test_main_agent.py +186 -111
- aiagents4pharma/talk2scholars/tests/test_pdf_agent.py +126 -0
- aiagents4pharma/talk2scholars/tests/test_question_and_answer_tool.py +186 -0
- aiagents4pharma/talk2scholars/tests/test_s2_display.py +74 -0
- aiagents4pharma/talk2scholars/tests/test_s2_multi.py +282 -0
- aiagents4pharma/talk2scholars/tests/test_s2_query.py +78 -0
- aiagents4pharma/talk2scholars/tests/test_s2_retrieve.py +65 -0
- aiagents4pharma/talk2scholars/tests/test_s2_search.py +266 -0
- aiagents4pharma/talk2scholars/tests/test_s2_single.py +274 -0
- aiagents4pharma/talk2scholars/tests/test_zotero_path.py +57 -0
- aiagents4pharma/talk2scholars/tests/test_zotero_read.py +412 -0
- aiagents4pharma/talk2scholars/tests/test_zotero_write.py +626 -0
- aiagents4pharma/talk2scholars/tools/__init__.py +1 -0
- aiagents4pharma/talk2scholars/tools/pdf/__init__.py +5 -0
- aiagents4pharma/talk2scholars/tools/pdf/question_and_answer.py +170 -0
- aiagents4pharma/talk2scholars/tools/s2/multi_paper_rec.py +50 -34
- aiagents4pharma/talk2scholars/tools/s2/query_results.py +1 -1
- aiagents4pharma/talk2scholars/tools/s2/retrieve_semantic_scholar_paper_id.py +8 -8
- aiagents4pharma/talk2scholars/tools/s2/search.py +36 -23
- aiagents4pharma/talk2scholars/tools/s2/single_paper_rec.py +44 -38
- aiagents4pharma/talk2scholars/tools/zotero/__init__.py +2 -0
- aiagents4pharma/talk2scholars/tools/zotero/utils/__init__.py +5 -0
- aiagents4pharma/talk2scholars/tools/zotero/utils/zotero_path.py +63 -0
- aiagents4pharma/talk2scholars/tools/zotero/zotero_read.py +64 -19
- aiagents4pharma/talk2scholars/tools/zotero/zotero_write.py +247 -0
- {aiagents4pharma-1.27.2.dist-info → aiagents4pharma-1.29.0.dist-info}/METADATA +6 -5
- {aiagents4pharma-1.27.2.dist-info → aiagents4pharma-1.29.0.dist-info}/RECORD +49 -33
- aiagents4pharma/talk2scholars/tests/test_call_s2.py +0 -100
- aiagents4pharma/talk2scholars/tests/test_call_zotero.py +0 -94
- aiagents4pharma/talk2scholars/tests/test_s2_tools.py +0 -355
- aiagents4pharma/talk2scholars/tests/test_zotero_tool.py +0 -171
- {aiagents4pharma-1.27.2.dist-info → aiagents4pharma-1.29.0.dist-info}/LICENSE +0 -0
- {aiagents4pharma-1.27.2.dist-info → aiagents4pharma-1.29.0.dist-info}/WHEEL +0 -0
- {aiagents4pharma-1.27.2.dist-info → aiagents4pharma-1.29.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,266 @@
|
|
1
|
+
"""
|
2
|
+
Unit tests for S2 tools functionality.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from types import SimpleNamespace
|
6
|
+
import pytest
|
7
|
+
import hydra
|
8
|
+
import requests
|
9
|
+
from langgraph.types import Command
|
10
|
+
from langchain_core.messages import ToolMessage
|
11
|
+
from aiagents4pharma.talk2scholars.tools.s2.search import search_tool
|
12
|
+
|
13
|
+
# --- Dummy Hydra Config Setup ---
|
14
|
+
|
15
|
+
|
16
|
+
class DummyHydraContext:
|
17
|
+
"""hydra.initialize context manager that does nothing."""
|
18
|
+
|
19
|
+
def __enter__(self):
|
20
|
+
return None
|
21
|
+
|
22
|
+
def __exit__(self, exc_type, exc_val, traceback):
|
23
|
+
return None
|
24
|
+
|
25
|
+
|
26
|
+
# Create a dummy configuration that mimics the expected hydra config
|
27
|
+
dummy_config = SimpleNamespace(
|
28
|
+
tools=SimpleNamespace(
|
29
|
+
search=SimpleNamespace(
|
30
|
+
api_endpoint="http://dummy.endpoint",
|
31
|
+
api_fields=["paperId", "title", "authors"],
|
32
|
+
)
|
33
|
+
)
|
34
|
+
)
|
35
|
+
|
36
|
+
# --- Dummy Response Classes and Functions for requests.get ---
|
37
|
+
|
38
|
+
|
39
|
+
class DummyResponse:
|
40
|
+
"""A dummy response class for mocking HTTP responses."""
|
41
|
+
|
42
|
+
def __init__(self, json_data, status_code=200):
|
43
|
+
"""initialize a DummyResponse with the given JSON data and status code."""
|
44
|
+
self._json_data = json_data
|
45
|
+
self.status_code = status_code
|
46
|
+
|
47
|
+
def json(self):
|
48
|
+
"""access the JSON data from the response."""
|
49
|
+
return self._json_data
|
50
|
+
|
51
|
+
def raise_for_status(self):
|
52
|
+
"""Raise an HTTP error for status codes >= 400."""
|
53
|
+
if self.status_code >= 400:
|
54
|
+
raise requests.HTTPError("HTTP Error")
|
55
|
+
|
56
|
+
|
57
|
+
def test_dummy_response_no_error():
|
58
|
+
"""Test that raise_for_status does not raise an exception for a successful response."""
|
59
|
+
# Create a DummyResponse with a successful status code.
|
60
|
+
response = DummyResponse({"data": "success"}, status_code=200)
|
61
|
+
# Calling raise_for_status should not raise an exception and should return None.
|
62
|
+
assert response.raise_for_status() is None
|
63
|
+
|
64
|
+
|
65
|
+
def test_dummy_response_raise_error():
|
66
|
+
"""Test that raise_for_status raises an exception for a failing response."""
|
67
|
+
# Create a DummyResponse with a failing status code.
|
68
|
+
response = DummyResponse({"error": "fail"}, status_code=400)
|
69
|
+
# Calling raise_for_status should raise an HTTPError.
|
70
|
+
with pytest.raises(requests.HTTPError):
|
71
|
+
response.raise_for_status()
|
72
|
+
|
73
|
+
|
74
|
+
def dummy_requests_get_success(url, params, timeout):
|
75
|
+
"""A dummy requests.get function that returns a successful response."""
|
76
|
+
# Record call parameters for assertions
|
77
|
+
dummy_requests_get_success.called_url = url
|
78
|
+
dummy_requests_get_success.called_params = params
|
79
|
+
dummy_requests_get_success.called_timeout = timeout
|
80
|
+
|
81
|
+
# Simulate a valid API response with three papers;
|
82
|
+
# one paper missing authors should be filtered out.
|
83
|
+
dummy_data = {
|
84
|
+
"data": [
|
85
|
+
{
|
86
|
+
"paperId": "1",
|
87
|
+
"title": "Paper 1",
|
88
|
+
"authors": ["Author A"],
|
89
|
+
"year": 2020,
|
90
|
+
"citationCount": 10,
|
91
|
+
"url": "http://paper1",
|
92
|
+
"externalIds": {"ArXiv": "arxiv1"},
|
93
|
+
},
|
94
|
+
{
|
95
|
+
"paperId": "2",
|
96
|
+
"title": "Paper 2",
|
97
|
+
"authors": ["Author B"],
|
98
|
+
"year": 2021,
|
99
|
+
"citationCount": 20,
|
100
|
+
"url": "http://paper2",
|
101
|
+
"externalIds": {},
|
102
|
+
},
|
103
|
+
{
|
104
|
+
"paperId": "3",
|
105
|
+
"title": "Paper 3",
|
106
|
+
"authors": None, # This paper should be filtered out.
|
107
|
+
"year": 2022,
|
108
|
+
"citationCount": 30,
|
109
|
+
"url": "http://paper3",
|
110
|
+
"externalIds": {"ArXiv": "arxiv3"},
|
111
|
+
},
|
112
|
+
]
|
113
|
+
}
|
114
|
+
return DummyResponse(dummy_data)
|
115
|
+
|
116
|
+
|
117
|
+
def dummy_requests_get_no_data(url, params, timeout):
|
118
|
+
"""A dummy requests.get function that returns a response without the expected 'data' key."""
|
119
|
+
dummy_requests_get_no_data.called_url = url
|
120
|
+
dummy_requests_get_no_data.called_params = params
|
121
|
+
dummy_requests_get_no_data.called_timeout = timeout
|
122
|
+
# Simulate a response with an unexpected format (missing "data" key)
|
123
|
+
return DummyResponse({"error": "Invalid format"})
|
124
|
+
|
125
|
+
|
126
|
+
def dummy_requests_get_no_papers(url, params, timeout):
|
127
|
+
"""A dummy requests.get function that returns a response with an empty papers list."""
|
128
|
+
dummy_requests_get_no_papers.called_url = url
|
129
|
+
dummy_requests_get_no_papers.called_params = params
|
130
|
+
dummy_requests_get_no_papers.called_timeout = timeout
|
131
|
+
# Simulate a response with an empty papers list.
|
132
|
+
return DummyResponse({"data": []})
|
133
|
+
|
134
|
+
|
135
|
+
def dummy_requests_get_exception(url, params, timeout):
|
136
|
+
"""A dummy requests.get function that raises an exception."""
|
137
|
+
dummy_requests_get_exception.called_url = url
|
138
|
+
dummy_requests_get_exception.called_params = params
|
139
|
+
dummy_requests_get_exception.called_timeout = timeout
|
140
|
+
# Simulate a network/connection exception.
|
141
|
+
raise requests.exceptions.RequestException("Connection error")
|
142
|
+
|
143
|
+
|
144
|
+
# --- Pytest Fixture to Patch Hydra ---
|
145
|
+
@pytest.fixture(autouse=True)
|
146
|
+
def patch_hydra(monkeypatch):
|
147
|
+
"""hydra patch to mock initialize and compose functions."""
|
148
|
+
# Patch hydra.initialize to return our dummy context manager.
|
149
|
+
monkeypatch.setattr(
|
150
|
+
hydra, "initialize", lambda version_base, config_path: DummyHydraContext()
|
151
|
+
)
|
152
|
+
# Patch hydra.compose to return our dummy config.
|
153
|
+
monkeypatch.setattr(hydra, "compose", lambda config_name, overrides: dummy_config)
|
154
|
+
|
155
|
+
|
156
|
+
# --- Test Cases ---
|
157
|
+
|
158
|
+
|
159
|
+
def test_search_tool_success(monkeypatch):
|
160
|
+
"""
|
161
|
+
Test that search_tool returns a valid Command object when the API response is successful.
|
162
|
+
Also checks that papers without required fields are filtered out.
|
163
|
+
"""
|
164
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_success)
|
165
|
+
|
166
|
+
tool_call_id = "test_tool_call_id"
|
167
|
+
# Invoke using .run() with a dictionary input.
|
168
|
+
result = search_tool.run(
|
169
|
+
{
|
170
|
+
"query": "machine learning",
|
171
|
+
"tool_call_id": tool_call_id,
|
172
|
+
"limit": 3,
|
173
|
+
"year": "2020",
|
174
|
+
}
|
175
|
+
)
|
176
|
+
|
177
|
+
# Check that a Command is returned with the expected update structure.
|
178
|
+
assert isinstance(result, Command)
|
179
|
+
update = result.update
|
180
|
+
assert "papers" in update
|
181
|
+
|
182
|
+
papers = update["papers"]
|
183
|
+
# Papers with valid 'title' and 'authors' should be included.
|
184
|
+
assert "1" in papers
|
185
|
+
assert "2" in papers
|
186
|
+
# Paper "3" is missing authors and should be filtered out.
|
187
|
+
assert "3" not in papers
|
188
|
+
|
189
|
+
# Check that a ToolMessage is included in the messages.
|
190
|
+
messages = update.get("messages", [])
|
191
|
+
assert len(messages) == 1
|
192
|
+
msg = messages[0]
|
193
|
+
assert isinstance(msg, ToolMessage)
|
194
|
+
assert "Number of papers found:" in msg.content
|
195
|
+
|
196
|
+
# Verify that the correct parameters were sent to requests.get.
|
197
|
+
called_params = dummy_requests_get_success.called_params
|
198
|
+
assert called_params["query"] == "machine learning"
|
199
|
+
# The "year" parameter should have been added.
|
200
|
+
assert called_params["year"] == "2020"
|
201
|
+
# The limit is set to min(limit, 100) so it should be 3.
|
202
|
+
assert called_params["limit"] == 3
|
203
|
+
# The fields should be a comma-separated string from the dummy config.
|
204
|
+
assert called_params["fields"] == "paperId,title,authors"
|
205
|
+
|
206
|
+
|
207
|
+
def test_search_tool_unexpected_format(monkeypatch):
|
208
|
+
"""
|
209
|
+
Test that search_tool raises a RuntimeError when the API response
|
210
|
+
does not include the expected 'data' key.
|
211
|
+
"""
|
212
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_no_data)
|
213
|
+
tool_call_id = "test_tool_call_id"
|
214
|
+
with pytest.raises(
|
215
|
+
RuntimeError,
|
216
|
+
match=(
|
217
|
+
"Unexpected response from Semantic Scholar API. The results could not be "
|
218
|
+
"retrieved due to an unexpected format. "
|
219
|
+
"Please modify your search query and try again."
|
220
|
+
),
|
221
|
+
):
|
222
|
+
search_tool.run(
|
223
|
+
{
|
224
|
+
"query": "test",
|
225
|
+
"tool_call_id": tool_call_id,
|
226
|
+
}
|
227
|
+
)
|
228
|
+
|
229
|
+
|
230
|
+
def test_search_tool_no_papers(monkeypatch):
|
231
|
+
"""
|
232
|
+
Test that search_tool raises a RuntimeError when the API returns no papers.
|
233
|
+
"""
|
234
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_no_papers)
|
235
|
+
tool_call_id = "test_tool_call_id"
|
236
|
+
with pytest.raises(
|
237
|
+
RuntimeError,
|
238
|
+
match=(
|
239
|
+
"No papers were found for your query. Consider refining your search "
|
240
|
+
"by using more specific keywords or different terms."
|
241
|
+
),
|
242
|
+
):
|
243
|
+
search_tool.run(
|
244
|
+
{
|
245
|
+
"query": "test",
|
246
|
+
"tool_call_id": tool_call_id,
|
247
|
+
}
|
248
|
+
)
|
249
|
+
|
250
|
+
|
251
|
+
def test_search_tool_requests_exception(monkeypatch):
|
252
|
+
"""
|
253
|
+
Test that search_tool raises a RuntimeError when requests.get throws an exception.
|
254
|
+
"""
|
255
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_exception)
|
256
|
+
tool_call_id = "test_tool_call_id"
|
257
|
+
with pytest.raises(
|
258
|
+
RuntimeError,
|
259
|
+
match="Failed to connect to Semantic Scholar API. Please retry the same query.",
|
260
|
+
):
|
261
|
+
search_tool.run(
|
262
|
+
{
|
263
|
+
"query": "test",
|
264
|
+
"tool_call_id": tool_call_id,
|
265
|
+
}
|
266
|
+
)
|
@@ -0,0 +1,274 @@
|
|
1
|
+
"""
|
2
|
+
Unit tests for S2 tools functionality.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from types import SimpleNamespace
|
6
|
+
import pytest
|
7
|
+
import requests
|
8
|
+
import hydra
|
9
|
+
from langgraph.types import Command
|
10
|
+
from langchain_core.messages import ToolMessage
|
11
|
+
from aiagents4pharma.talk2scholars.tools.s2.single_paper_rec import (
|
12
|
+
get_single_paper_recommendations,
|
13
|
+
)
|
14
|
+
|
15
|
+
# --- Dummy Hydra Config Setup ---
|
16
|
+
|
17
|
+
|
18
|
+
class DummyHydraContext:
|
19
|
+
"""
|
20
|
+
A dummy context manager for mocking Hydra's initialize and compose functions.
|
21
|
+
"""
|
22
|
+
|
23
|
+
def __enter__(self):
|
24
|
+
return None
|
25
|
+
|
26
|
+
def __exit__(self, exc_val, exc_type, traceback):
|
27
|
+
pass
|
28
|
+
|
29
|
+
|
30
|
+
# Create a dummy configuration that mimics the expected Hydra config
|
31
|
+
dummy_config = SimpleNamespace(
|
32
|
+
tools=SimpleNamespace(
|
33
|
+
single_paper_recommendation=SimpleNamespace(
|
34
|
+
api_endpoint="http://dummy.endpoint",
|
35
|
+
api_fields=["paperId", "title", "authors"],
|
36
|
+
recommendation_params=SimpleNamespace(from_pool="default_pool"),
|
37
|
+
request_timeout=10,
|
38
|
+
)
|
39
|
+
)
|
40
|
+
)
|
41
|
+
|
42
|
+
# --- Dummy Response Classes and Functions for requests.get ---
|
43
|
+
|
44
|
+
|
45
|
+
class DummyResponse:
|
46
|
+
"""
|
47
|
+
A dummy response class for mocking HTTP responses.
|
48
|
+
"""
|
49
|
+
|
50
|
+
def __init__(self, json_data, status_code=200):
|
51
|
+
"""Initialize a DummyResponse with the given JSON data and status code."""
|
52
|
+
self._json_data = json_data
|
53
|
+
self.status_code = status_code
|
54
|
+
|
55
|
+
def json(self):
|
56
|
+
"""Return the JSON data from the response."""
|
57
|
+
return self._json_data
|
58
|
+
|
59
|
+
def raise_for_status(self):
|
60
|
+
"""Raise an HTTP error for status codes >= 400."""
|
61
|
+
if self.status_code >= 400:
|
62
|
+
raise requests.HTTPError("HTTP Error")
|
63
|
+
|
64
|
+
|
65
|
+
def test_dummy_response_no_error():
|
66
|
+
"""Test DummyResponse does not raise an error for 200 status code."""
|
67
|
+
response = DummyResponse({"data": "success"}, status_code=200)
|
68
|
+
# Calling raise_for_status should not raise an exception and should return None.
|
69
|
+
assert response.raise_for_status() is None
|
70
|
+
|
71
|
+
|
72
|
+
def test_dummy_response_raise_error():
|
73
|
+
"""Test DummyResponse raises an HTTPError for status codes >= 400."""
|
74
|
+
response = DummyResponse({"error": "fail"}, status_code=400)
|
75
|
+
# Calling raise_for_status should raise an HTTPError.
|
76
|
+
with pytest.raises(requests.HTTPError):
|
77
|
+
response.raise_for_status()
|
78
|
+
|
79
|
+
|
80
|
+
def dummy_requests_get_success(url, params, timeout):
|
81
|
+
"""
|
82
|
+
Dummy function to simulate a successful API request returning recommended papers.
|
83
|
+
"""
|
84
|
+
dummy_requests_get_success.called_url = url
|
85
|
+
dummy_requests_get_success.called_params = params
|
86
|
+
dummy_requests_get_success.called_timeout = timeout
|
87
|
+
|
88
|
+
# Simulate a valid API response with three recommended papers;
|
89
|
+
# one paper missing authors should be filtered out.
|
90
|
+
dummy_data = {
|
91
|
+
"recommendedPapers": [
|
92
|
+
{
|
93
|
+
"paperId": "paper1",
|
94
|
+
"title": "Recommended Paper 1",
|
95
|
+
"authors": ["Author A"],
|
96
|
+
"year": 2020,
|
97
|
+
"citationCount": 15,
|
98
|
+
"url": "http://paper1",
|
99
|
+
"externalIds": {"ArXiv": "arxiv1"},
|
100
|
+
},
|
101
|
+
{
|
102
|
+
"paperId": "paper2",
|
103
|
+
"title": "Recommended Paper 2",
|
104
|
+
"authors": ["Author B"],
|
105
|
+
"year": 2021,
|
106
|
+
"citationCount": 25,
|
107
|
+
"url": "http://paper2",
|
108
|
+
"externalIds": {},
|
109
|
+
},
|
110
|
+
{
|
111
|
+
"paperId": "paper3",
|
112
|
+
"title": "Recommended Paper 3",
|
113
|
+
"authors": None, # This paper should be filtered out.
|
114
|
+
"year": 2022,
|
115
|
+
"citationCount": 35,
|
116
|
+
"url": "http://paper3",
|
117
|
+
"externalIds": {"ArXiv": "arxiv3"},
|
118
|
+
},
|
119
|
+
]
|
120
|
+
}
|
121
|
+
return DummyResponse(dummy_data)
|
122
|
+
|
123
|
+
|
124
|
+
def dummy_requests_get_unexpected(url, params, timeout):
|
125
|
+
"""
|
126
|
+
Dummy function to simulate an API response with an unexpected format.
|
127
|
+
"""
|
128
|
+
dummy_requests_get_unexpected.called_url = url
|
129
|
+
dummy_requests_get_unexpected.called_params = params
|
130
|
+
dummy_requests_get_unexpected.called_timeout = timeout
|
131
|
+
return DummyResponse({"error": "Invalid format"})
|
132
|
+
|
133
|
+
|
134
|
+
def dummy_requests_get_no_recs(url, params, timeout):
|
135
|
+
"""
|
136
|
+
Dummy function to simulate an API response returning no recommendations.
|
137
|
+
"""
|
138
|
+
dummy_requests_get_no_recs.called_url = url
|
139
|
+
dummy_requests_get_no_recs.called_params = params
|
140
|
+
dummy_requests_get_no_recs.called_timeout = timeout
|
141
|
+
return DummyResponse({"recommendedPapers": []})
|
142
|
+
|
143
|
+
|
144
|
+
def dummy_requests_get_exception(url, params, timeout):
|
145
|
+
"""
|
146
|
+
Dummy function to simulate a request exception (e.g., network failure).
|
147
|
+
"""
|
148
|
+
dummy_requests_get_exception.called_url = url
|
149
|
+
dummy_requests_get_exception.called_params = params
|
150
|
+
dummy_requests_get_exception.called_timeout = timeout
|
151
|
+
raise requests.exceptions.RequestException("Connection error")
|
152
|
+
|
153
|
+
|
154
|
+
# --- Pytest Fixture to Patch Hydra ---
|
155
|
+
@pytest.fixture(autouse=True)
|
156
|
+
def patch_hydra(monkeypatch):
|
157
|
+
"""Patch Hydra's initialize and compose functions with dummy implementations."""
|
158
|
+
monkeypatch.setattr(
|
159
|
+
hydra, "initialize", lambda version_base, config_path: DummyHydraContext()
|
160
|
+
)
|
161
|
+
# Patch hydra.compose to return our dummy config.
|
162
|
+
monkeypatch.setattr(hydra, "compose", lambda config_name, overrides: dummy_config)
|
163
|
+
|
164
|
+
|
165
|
+
# --- Test Cases ---
|
166
|
+
|
167
|
+
|
168
|
+
def test_single_paper_rec_success(monkeypatch):
|
169
|
+
"""
|
170
|
+
Test that get_single_paper_recommendations returns a valid Command object
|
171
|
+
when the API response is successful. Also, ensure that recommendations missing
|
172
|
+
required fields (like authors) are filtered out.
|
173
|
+
"""
|
174
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_success)
|
175
|
+
|
176
|
+
tool_call_id = "test_tool_call_id"
|
177
|
+
input_data = {
|
178
|
+
"paper_id": "12345",
|
179
|
+
"tool_call_id": tool_call_id,
|
180
|
+
"limit": 3,
|
181
|
+
"year": "2020",
|
182
|
+
}
|
183
|
+
# Invoke the tool using .run() with a single dictionary as input.
|
184
|
+
result = get_single_paper_recommendations.run(input_data)
|
185
|
+
|
186
|
+
# Validate that the result is a Command with the expected structure.
|
187
|
+
assert isinstance(result, Command)
|
188
|
+
update = result.update
|
189
|
+
assert "papers" in update
|
190
|
+
|
191
|
+
papers = update["papers"]
|
192
|
+
# Papers with valid 'title' and 'authors' should be included.
|
193
|
+
assert "paper1" in papers
|
194
|
+
assert "paper2" in papers
|
195
|
+
# Paper "paper3" is missing authors and should be filtered out.
|
196
|
+
assert "paper3" not in papers
|
197
|
+
|
198
|
+
# Check that a ToolMessage is included in the messages.
|
199
|
+
messages = update.get("messages", [])
|
200
|
+
assert len(messages) == 1
|
201
|
+
msg = messages[0]
|
202
|
+
assert isinstance(msg, ToolMessage)
|
203
|
+
assert "Recommendations based on the single paper were successful" in msg.content
|
204
|
+
|
205
|
+
# Verify that the correct parameters were sent to requests.get.
|
206
|
+
called_params = dummy_requests_get_success.called_params
|
207
|
+
assert called_params["limit"] == 3 # limited to min(limit, 500)
|
208
|
+
# "fields" should be a comma-separated string from the dummy config.
|
209
|
+
assert called_params["fields"] == "paperId,title,authors"
|
210
|
+
# Check that the "from" parameter is set from our dummy config.
|
211
|
+
assert called_params["from"] == "default_pool"
|
212
|
+
# The year parameter should be present.
|
213
|
+
assert called_params["year"] == "2020"
|
214
|
+
|
215
|
+
|
216
|
+
def test_single_paper_rec_unexpected_format(monkeypatch):
|
217
|
+
"""
|
218
|
+
Test that get_single_paper_recommendations raises a RuntimeError when the API
|
219
|
+
response does not include the expected 'recommendedPapers' key.
|
220
|
+
"""
|
221
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_unexpected)
|
222
|
+
tool_call_id = "test_tool_call_id"
|
223
|
+
input_data = {
|
224
|
+
"paper_id": "12345",
|
225
|
+
"tool_call_id": tool_call_id,
|
226
|
+
}
|
227
|
+
with pytest.raises(
|
228
|
+
RuntimeError,
|
229
|
+
match=(
|
230
|
+
"Unexpected response from Semantic Scholar API. The results could not be "
|
231
|
+
"retrieved due to an unexpected format. "
|
232
|
+
"Please modify your search query and try again."
|
233
|
+
),
|
234
|
+
):
|
235
|
+
get_single_paper_recommendations.run(input_data)
|
236
|
+
|
237
|
+
|
238
|
+
def test_single_paper_rec_no_recommendations(monkeypatch):
|
239
|
+
"""
|
240
|
+
Test that get_single_paper_recommendations raises a RuntimeError when the API
|
241
|
+
returns no recommendations.
|
242
|
+
"""
|
243
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_no_recs)
|
244
|
+
tool_call_id = "test_tool_call_id"
|
245
|
+
input_data = {
|
246
|
+
"paper_id": "12345",
|
247
|
+
"tool_call_id": tool_call_id,
|
248
|
+
}
|
249
|
+
with pytest.raises(
|
250
|
+
RuntimeError,
|
251
|
+
match=(
|
252
|
+
"No recommendations were found for your query. Consider refining your search "
|
253
|
+
"by using more specific keywords or different terms."
|
254
|
+
),
|
255
|
+
):
|
256
|
+
get_single_paper_recommendations.run(input_data)
|
257
|
+
|
258
|
+
|
259
|
+
def test_single_paper_rec_requests_exception(monkeypatch):
|
260
|
+
"""
|
261
|
+
Test that get_single_paper_recommendations raises a RuntimeError when requests.get
|
262
|
+
throws an exception.
|
263
|
+
"""
|
264
|
+
monkeypatch.setattr(requests, "get", dummy_requests_get_exception)
|
265
|
+
tool_call_id = "test_tool_call_id"
|
266
|
+
input_data = {
|
267
|
+
"paper_id": "12345",
|
268
|
+
"tool_call_id": tool_call_id,
|
269
|
+
}
|
270
|
+
with pytest.raises(
|
271
|
+
RuntimeError,
|
272
|
+
match="Failed to connect to Semantic Scholar API. Please retry the same query.",
|
273
|
+
):
|
274
|
+
get_single_paper_recommendations.run(input_data)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
"""
|
2
|
+
Unit tests for Zotero path utility in zotero_path.py.
|
3
|
+
"""
|
4
|
+
|
5
|
+
import unittest
|
6
|
+
from unittest.mock import MagicMock
|
7
|
+
from aiagents4pharma.talk2scholars.tools.zotero.utils.zotero_path import (
|
8
|
+
get_item_collections,
|
9
|
+
)
|
10
|
+
|
11
|
+
|
12
|
+
class TestGetItemCollections(unittest.TestCase):
|
13
|
+
"""Unit tests for the get_item_collections function."""
|
14
|
+
|
15
|
+
def test_basic_collections_mapping(self):
|
16
|
+
"""test_basic_collections_mapping"""
|
17
|
+
# Define fake collections with one parent-child relationship and one independent collection.
|
18
|
+
fake_collections = [
|
19
|
+
{"key": "A", "data": {"name": "Parent", "parentCollection": None}},
|
20
|
+
{"key": "B", "data": {"name": "Child", "parentCollection": "A"}},
|
21
|
+
{"key": "C", "data": {"name": "Independent", "parentCollection": None}},
|
22
|
+
]
|
23
|
+
# Define fake collection items for each collection:
|
24
|
+
# - Collection A returns one item with key "item1"
|
25
|
+
# - Collection B returns one item with key "item2"
|
26
|
+
# - Collection C returns two items: one duplicate ("item1") and one new ("item3")
|
27
|
+
fake_collection_items = {
|
28
|
+
"A": [{"data": {"key": "item1"}}],
|
29
|
+
"B": [{"data": {"key": "item2"}}],
|
30
|
+
"C": [{"data": {"key": "item1"}}, {"data": {"key": "item3"}}],
|
31
|
+
}
|
32
|
+
fake_zot = MagicMock()
|
33
|
+
fake_zot.collections.return_value = fake_collections
|
34
|
+
|
35
|
+
# When collection_items is called, return the appropriate list based on collection key.
|
36
|
+
def fake_collection_items_func(collection_key):
|
37
|
+
return fake_collection_items.get(collection_key, [])
|
38
|
+
|
39
|
+
fake_zot.collection_items.side_effect = fake_collection_items_func
|
40
|
+
|
41
|
+
# Expected full collection paths:
|
42
|
+
# - Collection A: "/Parent"
|
43
|
+
# - Collection B: "/Parent/Child" (child of A)
|
44
|
+
# - Collection C: "/Independent"
|
45
|
+
#
|
46
|
+
# Expected mapping for items:
|
47
|
+
# - "item1" appears in collections A and C → ["/Parent", "/Independent"]
|
48
|
+
# - "item2" appears in B → ["/Parent/Child"]
|
49
|
+
# - "item3" appears in C → ["/Independent"]
|
50
|
+
expected_mapping = {
|
51
|
+
"item1": ["/Parent", "/Independent"],
|
52
|
+
"item2": ["/Parent/Child"],
|
53
|
+
"item3": ["/Independent"],
|
54
|
+
}
|
55
|
+
|
56
|
+
result = get_item_collections(fake_zot)
|
57
|
+
self.assertEqual(result, expected_mapping)
|