test_search.py
1 """Tests for paper search functionality.""" 2 3 import pytest 4 import json 5 from unittest.mock import patch 6 from arxiv_mcp_server.tools import handle_search 7 8 9 @pytest.mark.asyncio 10 async def test_basic_search(mock_client): 11 """Test basic paper search functionality.""" 12 with patch("arxiv.Client", return_value=mock_client): 13 result = await handle_search({"query": "test query", "max_results": 1}) 14 15 assert len(result) == 1 16 content = json.loads(result[0].text) 17 assert content["total_results"] == 1 18 paper = content["papers"][0] 19 assert paper["id"] == "2103.12345" 20 assert paper["title"] == "Test Paper" 21 assert "resource_uri" in paper 22 23 24 @pytest.mark.asyncio 25 async def test_search_with_categories(mock_client): 26 """Test paper search with category filtering.""" 27 with patch("arxiv.Client", return_value=mock_client): 28 result = await handle_search( 29 {"query": "test query", "categories": ["cs.AI", "cs.LG"], "max_results": 1} 30 ) 31 32 content = json.loads(result[0].text) 33 assert content["papers"][0]["categories"] == ["cs.AI", "cs.LG"] 34 35 36 @pytest.mark.asyncio 37 async def test_search_with_dates(mock_client): 38 """Test paper search with date filtering.""" 39 with patch("arxiv.Client", return_value=mock_client): 40 result = await handle_search( 41 { 42 "query": "test query", 43 "date_from": "2022-01-01", 44 "date_to": "2024-01-01", 45 "max_results": 1, 46 } 47 ) 48 49 content = json.loads(result[0].text) 50 assert content["total_results"] == 1 51 assert len(content["papers"]) == 1 52 53 54 @pytest.mark.asyncio 55 async def test_search_with_invalid_dates(mock_client): 56 """Test search with invalid date formats.""" 57 with patch("arxiv.Client", return_value=mock_client): 58 result = await handle_search( 59 {"query": "test query", "date_from": "invalid-date", "max_results": 1} 60 ) 61 62 assert result[0].text.startswith("Error: Invalid date format") 63 64 65 @pytest.mark.asyncio 66 async def test_search_query_field_specifier_fix(mock_client): 67 """Test that plain queries get field specifiers for better relevance (issue #33).""" 68 with patch("arxiv.Client", return_value=mock_client): 69 with patch("arxiv.Search") as search_mock: 70 # Test multi-word query 71 await handle_search({"query": "quantum computing", "max_results": 1}) 72 search_mock.assert_called() 73 assert search_mock.call_args[1]["query"] == "all:quantum AND all:computing" 74 75 # Test single word query 76 search_mock.reset_mock() 77 await handle_search({"query": "transformer", "max_results": 1}) 78 assert search_mock.call_args[1]["query"] == "all:transformer" 79 80 # Test query with existing field specifier (should not be modified) 81 search_mock.reset_mock() 82 await handle_search({"query": "ti:neural networks", "max_results": 1}) 83 assert search_mock.call_args[1]["query"] == "ti:neural networks"