134 lines
5.3 KiB
Python
134 lines
5.3 KiB
Python
import json
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from services.shared.kb_search import normalize_kb_text, search_kb_rows
|
|
|
|
|
|
def _u(value: str) -> str:
|
|
return value.encode("ascii").decode("unicode_escape")
|
|
|
|
|
|
def _row(*, row_id: int, title: str, body: str, tags: list[str]):
|
|
return SimpleNamespace(
|
|
id=row_id,
|
|
title=title,
|
|
body=body,
|
|
tags_json=json.dumps(tags, ensure_ascii=False),
|
|
)
|
|
|
|
|
|
def test_normalize_kb_text_collapses_punctuation_and_yo():
|
|
value = _u(r"\u0401\u0436,\u0438\u043a!!! \u0433\u0440\u0430\u0444\u0438\u043a?")
|
|
assert normalize_kb_text(value) == _u(r"\u0435\u0436 \u0438\u043a \u0433\u0440\u0430\u0444\u0438\u043a")
|
|
|
|
|
|
def test_search_kb_rows_matches_long_phrase_to_graphik_article():
|
|
rows = [
|
|
_row(
|
|
row_id=1,
|
|
title=_u(r"\u0413\u0440\u0430\u0444\u0438\u043a \u0440\u0430\u0431\u043e\u0442\u044b"),
|
|
body=_u(r"\u0423\u043a\u0430\u0436\u0438\u0442\u0435 \u0433\u043e\u0440\u043e\u0434 \u0438\u043b\u0438 \u0444\u0438\u043b\u0438\u0430\u043b."),
|
|
tags=[_u(r"\u0440\u0435\u0436\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u044b")],
|
|
),
|
|
_row(
|
|
row_id=2,
|
|
title=_u(r"\u0422\u0430\u0440\u0438\u0444\u044b"),
|
|
body=_u(r"\u0423\u0442\u043e\u0447\u043d\u0438\u0442\u0435 \u0443\u0441\u043b\u0443\u0433\u0443 \u0438\u043b\u0438 \u0442\u0430\u0440\u0438\u0444."),
|
|
tags=[_u(r"\u0446\u0435\u043d\u0430")],
|
|
),
|
|
]
|
|
|
|
results = search_kb_rows(
|
|
rows,
|
|
_u(r"\u0445\u043e\u0447\u0443 \u0443\u0437\u043d\u0430\u0442\u044c \u0433\u0440\u0430\u0444\u0438\u043a \u0440\u0430\u0431\u043e\u0442\u044b"),
|
|
limit=5,
|
|
)
|
|
|
|
assert results[0].title == rows[0].title
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("query", "expected_title"),
|
|
[
|
|
(
|
|
_u(r"\u0430\u0434\u0440\u0435\u0441\u0430"),
|
|
_u(r"\u0410\u0434\u0440\u0435\u0441 \u0438 \u0444\u0438\u043b\u0438\u0430\u043b"),
|
|
),
|
|
(
|
|
_u(r"\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443"),
|
|
_u(r"\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c"),
|
|
),
|
|
(
|
|
_u(r"\u0437\u0430\u044f\u0432\u043a\u0438"),
|
|
_u(r"\u0421\u0442\u0430\u0442\u0443\u0441 \u0437\u0430\u044f\u0432\u043a\u0438"),
|
|
),
|
|
],
|
|
)
|
|
def test_search_kb_rows_matches_close_word_forms(query: str, expected_title: str):
|
|
rows = [
|
|
_row(
|
|
row_id=1,
|
|
title=_u(r"\u0410\u0434\u0440\u0435\u0441 \u0438 \u0444\u0438\u043b\u0438\u0430\u043b"),
|
|
body=_u(r"\u041d\u0430\u0437\u043e\u0432\u0438\u0442\u0435 \u0433\u043e\u0440\u043e\u0434."),
|
|
tags=[_u(r"\u0430\u0434\u0440\u0435\u0441")],
|
|
),
|
|
_row(
|
|
row_id=2,
|
|
title=_u(r"\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c"),
|
|
body=_u(r"\u0421\u043a\u0430\u0436\u0438\u0442\u0435, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u0447\u0435\u043b\u043e\u0432\u0435\u043a."),
|
|
tags=[_u(r"\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440")],
|
|
),
|
|
_row(
|
|
row_id=3,
|
|
title=_u(r"\u0421\u0442\u0430\u0442\u0443\u0441 \u0437\u0430\u044f\u0432\u043a\u0438"),
|
|
body=_u(r"\u041d\u0443\u0436\u0435\u043d \u043d\u043e\u043c\u0435\u0440 \u0437\u0430\u044f\u0432\u043a\u0438."),
|
|
tags=[_u(r"\u0437\u0430\u044f\u0432\u043a\u0430")],
|
|
),
|
|
]
|
|
|
|
results = search_kb_rows(rows, query, limit=5)
|
|
|
|
assert results[0].title == expected_title
|
|
|
|
|
|
def test_search_kb_rows_prefers_title_and_tags_over_body_only_mentions():
|
|
rows = [
|
|
_row(
|
|
row_id=1,
|
|
title=_u(r"\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c"),
|
|
body=_u(r"\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u0441\u043a\u0430\u0436\u0438\u0442\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c."),
|
|
tags=[_u(r"\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440")],
|
|
),
|
|
_row(
|
|
row_id=2,
|
|
title=_u(r"\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u0443\u0441\u043b\u0443\u0433\u043e\u0439"),
|
|
body=_u(r"\u0412 \u0441\u043b\u043e\u0436\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u0432\u043e\u043d\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443."),
|
|
tags=[_u(r"\u043e\u0448\u0438\u0431\u043a\u0430")],
|
|
),
|
|
]
|
|
|
|
results = search_kb_rows(rows, _u(r"\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440"), limit=5)
|
|
|
|
assert results[0].title == rows[0].title
|
|
|
|
|
|
def test_search_kb_rows_breaks_ties_by_newer_id():
|
|
older = _row(
|
|
row_id=10,
|
|
title="KB Freshness",
|
|
body="Freshness article body",
|
|
tags=["freshkb"],
|
|
)
|
|
newer = _row(
|
|
row_id=11,
|
|
title="KB Freshness",
|
|
body="Freshness article body",
|
|
tags=["freshkb"],
|
|
)
|
|
|
|
results = search_kb_rows([older, newer], "freshkb", limit=5)
|
|
|
|
assert [item.id for item in results[:2]] == [11, 10]
|