Skip to content

Commit 792c1ce

Browse files
Added tests Docstrings - adapted for devs. Minor refinements.
1 parent 864b6a5 commit 792c1ce

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

rre-dataset-generator/tests/unit/test_vespa_search_engine.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# -----------------------
1818
# Helpers / monkeypatches
1919
# -----------------------
20-
2120
def _monkeypatch_schema_ok(monkeypatch, fields=("title", "description", "id")):
2221
"""Simulate GET /schema/fields returning a payload with field names."""
2322
class _SchemaResp:
@@ -66,7 +65,6 @@ def _post(url, headers=None, json=None, **kwargs):
6665
# ---------------------
6766
# Field Value Normalization
6867
# ---------------------
69-
7068
@pytest.mark.parametrize(
7169
"input_val, expected_val",
7270
[
@@ -90,8 +88,12 @@ def test_normalize_field_value_EXPECTS_correct_conversion(input_val, expected_va
9088
# --------------
9189
# Happy-path generation
9290
# --------------
93-
9491
def test_fetch_for_query_generation_EXPECTS_builds_valid_yql_caps_hits_and_parses_response(monkeypatch):
92+
"""Verify YQL composition, hit capping, and response parsing in the generation flow.
93+
94+
Args:
95+
monkeypatch: PyTest fixture for patching HTTP calls.
96+
"""
9597
_monkeypatch_schema_ok(monkeypatch)
9698

9799
# Simulated Vespa response (one hit with title str, description list[str])
@@ -155,16 +157,21 @@ def test_fetch_for_query_generation_EXPECTS_builds_valid_yql_caps_hits_and_parse
155157
# -------------------
156158
# Happy-path evaluation/keyword
157159
# -------------------
158-
159160
def test_fetch_for_evaluation_EXPECTS_properly_quotes_and_escapes_keyword(monkeypatch):
161+
"""Ensure keyword literals are safely escaped/quoted in evaluation YQL.
162+
163+
Args:
164+
monkeypatch: PyTest fixture for patching HTTP calls.
165+
"""
160166
_monkeypatch_schema_ok(monkeypatch)
161167

162168
vespa_raw = {"root": {"children": []}}
163169
calls = _capture_post(monkeypatch, vespa_raw, status_code=200)
164170

165171
engine = VespaSearchEngine("https://fakehost/base", schema="doc")
166172
template = 'select * from doc where title contains #$query##'
167-
# keyword with quotes, backslash and newline: should be **quoted and escaped**
173+
# keyword with quotes, backslash and newline
174+
## > should be quoted and escaped
168175
kw = 'He said "hi" \\ \n new'
169176

170177
_ = engine.fetch_for_evaluation(
@@ -187,8 +194,12 @@ def test_fetch_for_evaluation_EXPECTS_properly_quotes_and_escapes_keyword(monkey
187194
# -------------------------
188195
# Skips hits without ID
189196
# -------------------------
190-
191197
def test_fetch_for_query_generation_EXPECTS_skip_hits_without_id(monkeypatch):
198+
"""Hits missing an ``id`` must be discarded during query generation.
199+
200+
Args:
201+
monkeypatch: PyTest fixture for patching HTTP calls.
202+
"""
192203
_monkeypatch_schema_ok(monkeypatch)
193204

194205
vespa_raw = {"root": {"children": [{"fields": {"title": "No ID here"}}]}}
@@ -202,8 +213,13 @@ def test_fetch_for_query_generation_EXPECTS_skip_hits_without_id(monkeypatch):
202213
# ----------------------
203214
# Schema warnings
204215
# ----------------------
205-
206216
def test_fetch_for_query_generation_EXPECTS_warn_on_unknown_schema_fields(monkeypatch, caplog):
217+
"""Expect a warning when filters reference fields absent in the Vespa schema.
218+
219+
Args:
220+
monkeypatch: PyTest fixture.
221+
caplog: PyTest log-capture fixture.
222+
"""
207223
# Load a schema with only "title"
208224
_monkeypatch_schema_ok(monkeypatch, fields=("title",))
209225

@@ -224,8 +240,12 @@ def test_fetch_for_query_generation_EXPECTS_warn_on_unknown_schema_fields(monkey
224240
# --------------------
225241
# API loading failure
226242
# --------------------
227-
228243
def test_schema_loading_EXPECTS_continue_on_failure(monkeypatch):
244+
"""Engine should fall back gracefully if the schema endpoint is unreachable.
245+
246+
Args:
247+
monkeypatch: PyTest fixture for patching HTTP calls.
248+
"""
229249
_monkeypatch_schema_fail(monkeypatch)
230250

231251
vespa_raw = {"root": {"children": []}}
@@ -243,6 +263,11 @@ def test_schema_loading_EXPECTS_continue_on_failure(monkeypatch):
243263
# -----------------------
244264

245265
def test_http_requests_EXPECTS_raise_on_negative_responses(monkeypatch):
266+
"""Search/evaluation requests must raise ``HTTPError`` on non-success HTTP status codes.
267+
268+
Args:
269+
monkeypatch: PyTest fixture for patching HTTP calls.
270+
"""
246271
_monkeypatch_schema_ok(monkeypatch)
247272

248273
for code in (400, 401, 402, 403, 500):
@@ -283,9 +308,12 @@ def test_http_requests_EXPECTS_raise_on_negative_responses(monkeypatch):
283308
},
284309
],
285310
)
286-
def test_backwards_compatibility_EXPECTS_work_with_existing_mocks_and_config(monkeypatch, mock_doc):
287-
"""
288-
Tests both generation and evaluation paths using MockResponseVespaSearch.
311+
def test_workflow_with_mocks_and_config_EXPECTS_work_with_existing(monkeypatch, mock_doc):
312+
"""Validate generation & evaluation flows against legacy mocks and YAML config.
313+
314+
Args:
315+
monkeypatch: PyTest fixture.
316+
mock_doc: Single mocked Vespa hit used by ``MockResponseVespaSearch``.
289317
"""
290318
# POST /search with existing mock
291319
monkeypatch.setattr(

0 commit comments

Comments
 (0)