Skip to content

Commit

Permalink
Added tests for text highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
murdo-moj committed Feb 20, 2024
1 parent c5d8b81 commit 536be0c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
47 changes: 25 additions & 22 deletions home/service/search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any

from copy import deepcopy
from data_platform_catalogue.search_types import MultiSelectFilter, SortOption
from django.core.paginator import Paginator

Expand All @@ -14,6 +14,7 @@ def __init__(self, form: SearchForm, page: str, items_per_page: int = 20):
self.page = page
self.client = self._get_catalogue_client()
self.results = self._get_search_results(page, items_per_page)
self.highlighted_results = self._highlight_results()
self.paginator = self._get_paginator(items_per_page)
self.context = self._get_context()

Expand Down Expand Up @@ -42,13 +43,8 @@ def _get_search_results(self, page: str, items_per_page: int):
sort=sort_option,
count=items_per_page,
)
highlighted_results = (
self._highlight_results(results, query)
if query != ""
else results
)

return highlighted_results
return results

def _get_paginator(self, items_per_page: int) -> Paginator:
pages_list = list(range(self.results.total_results))
Expand Down Expand Up @@ -85,22 +81,29 @@ def _get_context(self) -> dict[str, Any]:

return context

def _highlight_results(self, results, string_to_highlight):
"Take a SearchResponse and add bold markdown where a string appears"
for result in results.page_results:
metadata = getattr(result, "metadata")
highlighted_search_summary = metadata.get("search_summary", "").replace(
string_to_highlight, f"**{string_to_highlight}**"
)
setattr(result, "metadata", metadata)

name = getattr(result, "description")
highlighted_description = name.replace(
string_to_highlight, f"**{string_to_highlight}**"
)
setattr(result, "description", highlighted_description)
def _highlight_results(self):
"Take a SearchResponse and add bold markdown where the query appears"
string_to_highlight = self.form.cleaned_data.get("query") if self.form.is_valid() else ""
highlighted_results = deepcopy(self.results)

return results
if string_to_highlight == "":
return highlighted_results

else:
for result in highlighted_results.page_results:
metadata = getattr(result, "metadata")
metadata["search_summary"] = metadata.get("search_summary", "").replace(
string_to_highlight, f"**{string_to_highlight}**"
)
setattr(result, "metadata", metadata)

name = getattr(result, "description")
highlighted_description = name.replace(
string_to_highlight, f"**{string_to_highlight}**"
)
setattr(result, "description", highlighted_description)

return highlighted_results

def _get_match_reason_display_names(self):
return {
Expand Down
13 changes: 8 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def generate_page(page_size=20):
result_type=choice(
(ResultType.DATA_PRODUCT, ResultType.TABLE)),
name=fake.name(),
description=fake.paragraphs(),
description=fake.paragraph(),
metadata={"search_summary":"a"},
)
)
return results
Expand Down Expand Up @@ -105,10 +106,12 @@ def valid_form():


@pytest.fixture
def search_context(valid_form):
search_service = SearchService(form=valid_form, page="1")
context = search_service._get_context()
return context
def search_service(valid_form):
return SearchService(form=valid_form, page="1")

@pytest.fixture
def search_context(search_service):
return search_service.context


@pytest.fixture
Expand Down
38 changes: 38 additions & 0 deletions tests/test_services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from types import GeneratorType
from data_platform_catalogue.search_types import ResultType
from home.service.search import SearchService


class TestSearchService:
Expand All @@ -24,6 +25,43 @@ def test_get_context_label_clear_href(self, search_context):
"HMCTS": "?query=test&sort=ascending&clear_filter=False&clear_label=False"
}

def test_highlight_results_no_query(self, search_service):
search_service.form.cleaned_data = {"query": ""}
search_service._highlight_results()
assert (
normal.description == highlighted.description
& normal.metadata == highlighted.metadata
for normal, highlighted in zip(
search_service.results.page_results,
search_service.highlighted_results.page_results,
)
)

def test_highlight_results_with_query(self, search_service):
search_service.form.cleaned_data = {"query": "a"}
search_service._highlight_results()

assert (
"**a**" in highlighted.description
& "**" not in normal.description
for normal, highlighted
in zip(
search_service.results.page_results,
search_service.highlighted_results.page_results,
)
if "a" in normal.description
)
assert (
"**a**" in highlighted.metadata["search_summary"]
& "**" not in normal.metadata["search_summary"]
for normal, highlighted
in zip(
search_service.results.page_results,
search_service.highlighted_results.page_results,
)
if "a" in normal.metadata["search_summary"]
)


class TestDetailsService:
def test_get_context(self, detail_context, mock_catalogue):
Expand Down

0 comments on commit 536be0c

Please sign in to comment.