Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

fix whitespace issue for author suggestions #14

Merged
merged 4 commits into from
Dec 5, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- Fix whitespace issue in author suggestion

## [0.0.7] - 30.10.2024

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/scholarag/app/routers/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ async def author_suggestion(
# Match case insensitive. Sadly aggregate queries don't support the CASE_INSENSITIVE flag
# The /i flag is not supported either in regex queries.
start = time.time()

regex_pattern = "".join(
[f"[{char.lower()}{char.upper()}]" for char in request.name]
)

pattern = re.compile(re.escape(request.name), re.IGNORECASE)

# Regex query to partially match author name with a keyword field.
query = {"regexp": {"authors": f".*{regex_pattern}.*"}}
query = {"regexp": {"authors.keyword": f".*{regex_pattern}.*"}}
kwargs = {"_source": ["authors"]}
# The size here is a bit arbitrary, but in theory it should ensure that
# There is enough authors to fill the user's request. If not, people should
Expand Down
60 changes: 60 additions & 0 deletions tests/app/test_suggestions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for the suggestions endpoints."""

from unittest.mock import AsyncMock

import pytest
from httpx import ASGITransport, AsyncClient
from scholarag.app.config import Settings
Expand Down Expand Up @@ -684,3 +686,61 @@ async def test_journal_duplicates(get_testing_async_ds_client):
assert set(d.keys()) == expected_keys
assert d["eissn"] == expected_result["eissn"]
assert d["print_issn"] == expected_result["print_issn"]


@pytest.mark.asyncio
async def test_author_suggestion_with_spaces():
# Override the get_settings dependency
test_settings = Settings(
db={
"db_type": "elasticsearch",
"index_paragraphs": "pmc_paragraphs2",
"host": "localhost",
"port": 9200,
"user": "test_user",
"password": "test_password",
}
)
app.dependency_overrides[get_settings] = lambda: test_settings

# Mock the get_ds_client dependency
mock_ds_client = AsyncMock()
mock_ds_client.search.return_value = {
"hits": {
"total": {"value": 1, "relation": "eq"},
"hits": [{"_source": {"authors": ["Jing Yuan"]}}],
}
}
app.dependency_overrides[get_ds_client] = lambda: mock_ds_client

async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
# Test with a name containing spaces
response_with_spaces = await client.get(
"/suggestions/author", params={"name": "jing yuan", "limit": 100}
)
assert (
response_with_spaces.status_code != 500
), "Request with spaces should not return 500"

# Test with a name without spaces
response_without_spaces = await client.get(
"/suggestions/author", params={"name": "jing", "limit": 100}
)
assert (
response_without_spaces.status_code == 200
), "Request without spaces should return 200"

# Optionally, check the response content
response_with_spaces_json = response_with_spaces.json()
response_without_spaces_json = response_without_spaces.json()

# Ensure the responses are as expected
assert isinstance(response_with_spaces_json, list), "Response should be a list"
assert isinstance(
response_without_spaces_json, list
), "Response should be a list"

# Clean up dependency overrides
app.dependency_overrides.clear()
Loading