[9d3784]: / aiagents4pharma / talk2scholars / tests / test_s2_query.py

Download this file

79 lines (64 with data), 2.4 kB

 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
"""
Unit tests for S2 tools functionality.
"""
# pylint: disable=redefined-outer-name
from unittest.mock import patch
from unittest.mock import MagicMock
import pytest
from ..tools.s2.query_results import query_results, NoPapersFoundError
@pytest.fixture
def initial_state():
"""Provides an empty initial state for tests."""
return {"papers": {}, "multi_papers": {}}
# Fixed test data for deterministic results
MOCK_SEARCH_RESPONSE = {
"data": [
{
"paperId": "123",
"title": "Machine Learning Basics",
"abstract": "An introduction to ML",
"year": 2023,
"citationCount": 100,
"url": "https://example.com/paper1",
"authors": [{"name": "Test Author"}],
}
]
}
MOCK_STATE_PAPER = {
"123": {
"Title": "Machine Learning Basics",
"Abstract": "An introduction to ML",
"Year": 2023,
"Citation Count": 100,
"URL": "https://example.com/paper1",
}
}
class TestS2Tools:
"""Unit tests for individual S2 tools"""
def test_query_results_empty_state(self, initial_state):
"""Tests query_results tool behavior when no papers are found."""
with pytest.raises(
NoPapersFoundError,
match="No papers found. A search needs to be performed first.",
):
query_results.invoke(
{"question": "List all papers", "state": initial_state}
)
@patch(
"aiagents4pharma.talk2scholars.tools.s2.query_results.create_pandas_dataframe_agent"
)
def test_query_results_with_papers(self, mock_create_agent, initial_state):
"""Tests querying papers when data is available."""
state = initial_state.copy()
state["last_displayed_papers"] = "papers"
state["papers"] = MOCK_STATE_PAPER
# Mock the dataframe agent instead of the LLM
mock_agent = MagicMock()
mock_agent.invoke.return_value = {"output": "Mocked response"}
mock_create_agent.return_value = (
mock_agent # Mock the function returning the agent
)
# Ensure that the output of query_results is correctly structured
result = query_results.invoke({"question": "List all papers", "state": state})
assert isinstance(result, str) # Ensure output is a string
assert result == "Mocked response" # Validate the expected response