Skip to content

Commit

Permalink
Pass limit parameter to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed Jun 12, 2024
1 parent 3b5f7a1 commit 9af6edb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions annif/backend/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_backend_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9af6edb

Please sign in to comment.