From 9af6edbc2b428309d5294d91ca64fcdc1ecddde3 Mon Sep 17 00:00:00 2001 From: Juho Inkinen <34240031+juhoinkinen@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:21:59 +0300 Subject: [PATCH 1/2] Pass limit parameter to requests --- annif/backend/http.py | 2 ++ tests/test_backend_http.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/annif/backend/http.py b/annif/backend/http.py index 9036ec152..c17b91829 100644 --- a/annif/backend/http.py +++ b/annif/backend/http.py @@ -68,6 +68,8 @@ def _suggest(self, text: str, params: dict[str, Any]) -> list[SubjectSuggestion] data = {"text": text} if "project" in params: data["project"] = params["project"] + if "limit" in params: + data["limit"] = params["limit"] try: req = requests.post(params["endpoint"], data=data, headers=self.headers) diff --git a/tests/test_backend_http.py b/tests/test_backend_http.py index d987a7a4c..39cfa7039 100644 --- a/tests/test_backend_http.py +++ b/tests/test_backend_http.py @@ -84,6 +84,38 @@ def test_http_suggest_with_results(app_project): assert hits[0].score == 1.0 +def test_http_suggest_limit(app_project): + with unittest.mock.patch("requests.post") as mock_request: + # create a mock response whose .json() method returns the list that we + # define here + mock_response = unittest.mock.Mock() + mock_response.json.return_value = [ + {"uri": "http://example.org/dummy", "label": "dummy", "score": 1.0} + ] + mock_request.return_value = mock_response + + http_type = annif.backend.get_backend("http") + http = http_type( + backend_id="http", + config_params={ + "endpoint": "http://api.example.org/analyze", + "project": "dummy", + "limit": "42", + }, + project=app_project, + ) + result = http.suggest(["this is some text"])[0] + assert len(result) == 1 + hits = list(result) + assert hits[0].subject_id is not None + assert hits[0].subject_id == app_project.subjects.by_uri( + "http://example.org/dummy" + ) + assert hits[0].score == 1.0 + assert "limit" in requests.post.call_args.kwargs["data"] + assert requests.post.call_args.kwargs["data"]["limit"] == "42" + + def test_http_suggest_zero_score(project): with unittest.mock.patch("requests.post") as mock_request: # create a mock response whose .json() method returns the list that we From 011e60262e3a1217d4f56d88f761d42292ac05bf Mon Sep 17 00:00:00 2001 From: juhoinkinen <34240031+juhoinkinen@users.noreply.github.com> Date: Thu, 13 Jun 2024 10:48:50 +0300 Subject: [PATCH 2/2] Test all args of request by http backend suggest call --- tests/test_backend_http.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/tests/test_backend_http.py b/tests/test_backend_http.py index 39cfa7039..114bcbe96 100644 --- a/tests/test_backend_http.py +++ b/tests/test_backend_http.py @@ -84,16 +84,8 @@ def test_http_suggest_with_results(app_project): assert hits[0].score == 1.0 -def test_http_suggest_limit(app_project): - with unittest.mock.patch("requests.post") as mock_request: - # create a mock response whose .json() method returns the list that we - # define here - mock_response = unittest.mock.Mock() - mock_response.json.return_value = [ - {"uri": "http://example.org/dummy", "label": "dummy", "score": 1.0} - ] - mock_request.return_value = mock_response - +def test_http_suggest_post_args(app_project): + with unittest.mock.patch("requests.post"): http_type = annif.backend.get_backend("http") http = http_type( backend_id="http", @@ -104,14 +96,13 @@ def test_http_suggest_limit(app_project): }, project=app_project, ) - result = http.suggest(["this is some text"])[0] - assert len(result) == 1 - hits = list(result) - assert hits[0].subject_id is not None - assert hits[0].subject_id == app_project.subjects.by_uri( - "http://example.org/dummy" - ) - assert hits[0].score == 1.0 + http.suggest(["this is some text"]) + + assert requests.post.call_args.args == ("http://api.example.org/analyze",) + assert "text" in requests.post.call_args.kwargs["data"] + assert requests.post.call_args.kwargs["data"]["text"] == "this is some text" + assert "project" in requests.post.call_args.kwargs["data"] + assert requests.post.call_args.kwargs["data"]["project"] == "dummy" assert "limit" in requests.post.call_args.kwargs["data"] assert requests.post.call_args.kwargs["data"]["limit"] == "42"