1- import pytest
1+ """Unit tests for api/tools/tools.py."""
22from unittest .mock import patch , MagicMock
33from api .tools .tools import (
44 search_plugin_docs ,
99
1010
1111class TestSearchStackoverflowThreads :
12+ """Tests for search_stackoverflow_threads function."""
13+
1214 def test_returns_nothing_relevant_for_valid_query (self ):
15+ """Test that a valid query returns nothing relevant."""
1316 result = search_stackoverflow_threads ("how to fix jenkins pipeline" )
1417 assert result == "Nothing relevant"
1518
1619 def test_returns_nothing_relevant_for_empty_query (self ):
20+ """Test that an empty query returns nothing relevant."""
1721 result = search_stackoverflow_threads ("" )
1822 assert result == "Nothing relevant"
1923
2024
2125class TestSearchPluginDocs :
26+ """Tests for search_plugin_docs function."""
27+
2228 @patch ("api.tools.tools.extract_top_chunks" )
2329 @patch ("api.tools.tools.retrieve_documents" )
2430 def test_returns_result_without_plugin_name (self , mock_retrieve , mock_extract ):
31+ """Test result is returned when no plugin name is provided."""
2532 mock_retrieve .return_value = (["doc1" ], [0.9 ], ["doc2" ], [0.8 ])
2633 mock_extract .return_value = "plugin result"
2734 logger = MagicMock ()
28-
2935 result = search_plugin_docs ("query" , "keywords" , logger )
30-
3136 assert result == "plugin result"
3237 mock_retrieve .assert_called_once ()
3338 mock_extract .assert_called_once ()
@@ -39,14 +44,13 @@ def test_returns_result_without_plugin_name(self, mock_retrieve, mock_extract):
3944 def test_filters_data_when_valid_plugin_name (
4045 self , mock_retrieve , mock_is_valid , mock_filter , mock_extract
4146 ):
47+ """Test that data is filtered when a valid plugin name is given."""
4248 mock_retrieve .return_value = (["doc1" ], [0.9 ], ["doc2" ], [0.8 ])
4349 mock_is_valid .return_value = True
4450 mock_filter .return_value = (["filtered1" ], ["filtered2" ])
4551 mock_extract .return_value = "filtered result"
4652 logger = MagicMock ()
47-
4853 result = search_plugin_docs ("query" , "keywords" , logger , plugin_name = "git" )
49-
5054 assert result == "filtered result"
5155 mock_filter .assert_called_once ()
5256
@@ -56,42 +60,63 @@ def test_filters_data_when_valid_plugin_name(
5660 def test_skips_filter_when_invalid_plugin_name (
5761 self , mock_retrieve , mock_is_valid , mock_extract
5862 ):
63+ """Test that filter is skipped when plugin name is invalid."""
5964 mock_retrieve .return_value = (["doc1" ], [0.9 ], ["doc2" ], [0.8 ])
6065 mock_is_valid .return_value = False
6166 mock_extract .return_value = "unfiltered result"
6267 logger = MagicMock ()
63-
6468 result = search_plugin_docs ("query" , "keywords" , logger , plugin_name = "invalid" )
65-
6669 assert result == "unfiltered result"
6770
6871
6972class TestSearchJenkinsDocs :
73+ """Tests for search_jenkins_docs function."""
74+
7075 @patch ("api.tools.tools.extract_top_chunks" )
7176 @patch ("api.tools.tools.retrieve_documents" )
7277 def test_returns_result (self , mock_retrieve , mock_extract ):
78+ """Test that jenkins docs search returns a result."""
7379 mock_retrieve .return_value = (["doc1" ], [0.9 ], ["doc2" ], [0.8 ])
7480 mock_extract .return_value = "jenkins docs result"
7581 logger = MagicMock ()
76-
7782 result = search_jenkins_docs ("query" , "keywords" , logger )
78-
7983 assert result == "jenkins docs result"
8084 mock_retrieve .assert_called_once ()
8185 mock_extract .assert_called_once ()
8286
87+ @patch ("api.tools.tools.extract_top_chunks" )
88+ @patch ("api.tools.tools.retrieve_documents" )
89+ def test_returns_result_for_empty_query (self , mock_retrieve , mock_extract ):
90+ """Test that jenkins docs search handles empty query."""
91+ mock_retrieve .return_value = ([], [], [], [])
92+ mock_extract .return_value = ""
93+ logger = MagicMock ()
94+ result = search_jenkins_docs ("" , "" , logger )
95+ assert result == ""
96+
8397
8498class TestSearchCommunityThreads :
99+ """Tests for search_community_threads function."""
100+
85101 @patch ("api.tools.tools.extract_top_chunks" )
86102 @patch ("api.tools.tools.retrieve_documents" )
87103 def test_returns_result_with_semantic_weight (self , mock_retrieve , mock_extract ):
104+ """Test that community threads search uses correct semantic weight."""
88105 mock_retrieve .return_value = (["doc1" ], [0.9 ], ["doc2" ], [0.8 ])
89106 mock_extract .return_value = "community result"
90107 logger = MagicMock ()
91-
92108 result = search_community_threads ("query" , "keywords" , logger )
93-
94109 assert result == "community result"
95110 mock_extract .assert_called_once ()
96111 call_kwargs = mock_extract .call_args .kwargs
97- assert call_kwargs .get ("semantic_weight" ) == 0.7
112+ assert call_kwargs .get ("semantic_weight" ) == 0.7
113+
114+ @patch ("api.tools.tools.extract_top_chunks" )
115+ @patch ("api.tools.tools.retrieve_documents" )
116+ def test_returns_result_for_empty_query (self , mock_retrieve , mock_extract ):
117+ """Test that community threads search handles empty query."""
118+ mock_retrieve .return_value = ([], [], [], [])
119+ mock_extract .return_value = ""
120+ logger = MagicMock ()
121+ result = search_community_threads ("" , "" , logger )
122+ assert result == ""
0 commit comments