@@ -53,6 +53,30 @@ def test_init_fail_wo_api_key(self, monkeypatch):
5353 with pytest .raises (ValueError ):
5454 JinaRanker ()
5555
56+ def test_init_fails_with_invalid_top_k (self ):
57+ with pytest .raises (ValueError , match = "top_k must be > 0, but got 0" ):
58+ JinaRanker (api_key = Secret .from_token ("fake-api-key" ), top_k = 0 )
59+
60+ def test_from_dict (self , monkeypatch ):
61+ monkeypatch .setenv ("JINA_API_KEY" , "fake-api-key" )
62+ data = {
63+ "type" : "haystack_integrations.components.rankers.jina.ranker.JinaRanker" ,
64+ "init_parameters" : {
65+ "api_key" : {"env_vars" : ["JINA_API_KEY" ], "strict" : True , "type" : "env_var" },
66+ "model" : "model" ,
67+ "base_url" : "https://my.custom.url/v1/rerank" ,
68+ "top_k" : 5 ,
69+ "score_threshold" : 0.3 ,
70+ },
71+ }
72+ ranker = JinaRanker .from_dict (data )
73+
74+ assert ranker .api_key == Secret .from_env_var ("JINA_API_KEY" )
75+ assert ranker .model == "model"
76+ assert ranker .base_url == "https://my.custom.url/v1/rerank"
77+ assert ranker .top_k == 5
78+ assert ranker .score_threshold == 0.3
79+
5680 def test_to_dict (self , monkeypatch ):
5781 monkeypatch .setenv ("JINA_API_KEY" , "fake-api-key" )
5882 component = JinaRanker ()
@@ -190,6 +214,34 @@ def test_run_on_empty_docs(self):
190214 assert result ["documents" ] is not None
191215 assert not result ["documents" ] # empty list
192216
217+ def test_run_raises_runtime_error_on_api_error (self ):
218+ mock_response = httpx .Response (400 , json = {"detail" : "Bad request" })
219+ with patch ("httpx.Client.post" , return_value = mock_response ):
220+ ranker = JinaRanker (api_key = Secret .from_token ("fake-api-key" ))
221+ with pytest .raises (RuntimeError , match = "Bad request" ):
222+ ranker .run (query = "q" , documents = [Document (content = "doc" )])
223+
224+ def test_run_with_score_threshold_filters_results (self ):
225+ docs = [Document (content = f"doc { i } " ) for i in range (4 )]
226+
227+ with patch ("httpx.Client.post" , side_effect = mock_httpx_post_response ):
228+ ranker = JinaRanker (api_key = Secret .from_token ("fake-api-key" ), score_threshold = 2.5 )
229+ result = ranker .run (query = "q" , documents = docs )
230+
231+ # mock assigns scores len(docs)-i, so for 4 docs scores are 4, 3, 2, 1 - only first two pass
232+ ranked = result ["documents" ]
233+ assert len (ranked ) == 2
234+ assert all (doc .score >= 2.5 for doc in ranked )
235+
236+ def test_run_with_top_k_truncates_results (self ):
237+ docs = [Document (content = f"doc { i } " ) for i in range (5 )]
238+
239+ with patch ("httpx.Client.post" , side_effect = mock_httpx_post_response ):
240+ ranker = JinaRanker (api_key = Secret .from_token ("fake-api-key" ))
241+ result = ranker .run (query = "q" , documents = docs , top_k = 2 )
242+
243+ assert len (result ["documents" ]) == 2
244+
193245 @pytest .mark .skipif (not os .environ .get ("JINA_API_KEY" , None ), reason = "JINA_API_KEY env var not set" )
194246 @pytest .mark .integration
195247 def test_run_integration (self ):
0 commit comments