Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix limit parameter not passed to requests by HTTP backend #792

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 23 additions & 0 deletions tests/test_backend_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ def test_http_suggest_with_results(app_project):
assert hits[0].score == 1.0


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",
config_params={
"endpoint": "http://api.example.org/analyze",
"project": "dummy",
"limit": "42",
},
project=app_project,
)
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"


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
Loading