Skip to content

Commit d831170

Browse files
fix docstring backticks and add unit tests
1 parent 8b73068 commit d831170

2 files changed

Lines changed: 94 additions & 10 deletions

File tree

integrations/falkordb/src/haystack_integrations/document_stores/falkordb/document_store.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def update_by_filter(self, filters: dict[str, Any], meta: dict[str, Any]) -> int
481481
Update metadata fields on all documents that match the provided filters.
482482
483483
:param filters: Haystack filter dict selecting which documents to update.
484-
:param meta: Metadata fields to set. Keys may include or omit the ``meta.`` prefix.
484+
:param meta: Metadata fields to set. Keys may include or omit the `meta.` prefix.
485485
:returns: Number of documents updated.
486486
"""
487487
self._ensure_connected()
@@ -516,8 +516,8 @@ def count_unique_metadata_by_filter(self, filters: dict[str, Any], metadata_fiel
516516
Return the number of unique values for each metadata field among matching documents.
517517
518518
:param filters: Haystack filter dict. Pass an empty dict to count across all documents.
519-
:param metadata_fields: List of metadata field names. May include or omit the ``meta.`` prefix.
520-
:returns: Dict mapping each field name (without ``meta.`` prefix) to its unique value count.
519+
:param metadata_fields: List of metadata field names. May include or omit the `meta.` prefix.
520+
:returns: Dict mapping each field name (without `meta.` prefix) to its unique value count.
521521
"""
522522
self._ensure_connected()
523523
if filters:
@@ -542,8 +542,8 @@ def get_metadata_fields_info(self) -> dict[str, dict[str, str]]:
542542
"""
543543
Return type information for each metadata field present on document nodes.
544544
545-
:returns: Dict mapping field names to a ``{"type": <typename>}`` dict.
546-
Type names are ``"str"``, ``"int"``, ``"float"``, or ``"bool"``.
545+
:returns: Dict mapping field names to a `{"type": <typename>}` dict.
546+
Type names are `"str"`, `"int"`, `"float"`, or `"bool"`.
547547
"""
548548
self._ensure_connected()
549549
standard_fields = {"id", "content", "embedding", "score", "sparse_embedding"}
@@ -574,8 +574,8 @@ def get_metadata_field_min_max(self, metadata_field: str) -> dict[str, Any]:
574574
"""
575575
Return the minimum and maximum values for the given metadata field.
576576
577-
:param metadata_field: Metadata field name. May include or omit the ``meta.`` prefix.
578-
:returns: Dict with keys ``"min"`` and ``"max"``. Values are ``None`` when no documents
577+
:param metadata_field: Metadata field name. May include or omit the `meta.` prefix.
578+
:returns: Dict with keys `"min"` and `"max"`. Values are `None` when no documents
579579
have a non-null value for the field.
580580
"""
581581
self._ensure_connected()
@@ -598,11 +598,11 @@ def get_metadata_field_unique_values(
598598
"""
599599
Return distinct values for the given metadata field with optional filtering and pagination.
600600
601-
:param metadata_field: Metadata field name. May include or omit the ``meta.`` prefix.
601+
:param metadata_field: Metadata field name. May include or omit the `meta.` prefix.
602602
:param search_term: Optional substring filter applied to string field values.
603603
:param size: Maximum number of values to return per page. Defaults to 10 000.
604-
:param after: Pagination cursor returned by a previous call. Pass ``None`` for the first page.
605-
:returns: Tuple of ``(values, next_cursor)``. ``next_cursor`` is ``None`` on the last page.
604+
:param after: Pagination cursor returned by a previous call. Pass `None` for the first page.
605+
:returns: Tuple of `(values, next_cursor)`. `next_cursor` is `None` on the last page.
606606
"""
607607
self._ensure_connected()
608608
field = metadata_field[5:] if metadata_field.startswith("meta.") else metadata_field

integrations/falkordb/tests/test_document_store.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,90 @@ def test_write_documents_wraps_errors(self, mock_falkordb):
277277
with pytest.raises(DocumentStoreError, match="Failed to write documents"):
278278
FalkorDBDocumentStore().write_documents([Document(id="a", content="x")], policy=DuplicatePolicy.OVERWRITE)
279279

280+
def test_delete_all_documents(self, mock_falkordb):
281+
_, _, graph = mock_falkordb
282+
graph.query.side_effect = [_result([]), _result([]), _result([])]
283+
FalkorDBDocumentStore().delete_all_documents()
284+
assert "DETACH DELETE" in graph.query.call_args_list[-1].args[0]
285+
286+
def test_delete_by_filter_returns_count(self, mock_falkordb):
287+
_, _, graph = mock_falkordb
288+
graph.query.side_effect = [_result([]), _result([]), _result([[3]]), _result([])]
289+
count = FalkorDBDocumentStore().delete_by_filter({"field": "year", "operator": "==", "value": 2024})
290+
assert count == 3
291+
assert "DETACH DELETE" in graph.query.call_args_list[-1].args[0]
292+
293+
def test_delete_by_filter_empty_result(self, mock_falkordb):
294+
_, _, graph = mock_falkordb
295+
graph.query.side_effect = [_result([]), _result([]), _result([]), _result([])]
296+
assert FalkorDBDocumentStore().delete_by_filter({"field": "year", "operator": "==", "value": 2024}) == 0
297+
298+
def test_update_by_filter_returns_count(self, mock_falkordb):
299+
_, _, graph = mock_falkordb
300+
graph.query.side_effect = [_result([]), _result([]), _result([[2]])]
301+
count = FalkorDBDocumentStore().update_by_filter(
302+
{"field": "year", "operator": "==", "value": 2024}, {"status": "published"}
303+
)
304+
assert count == 2
305+
assert "SET d +=" in graph.query.call_args_list[-1].args[0]
306+
307+
def test_update_by_filter_strips_meta_prefix(self, mock_falkordb):
308+
_, _, graph = mock_falkordb
309+
graph.query.side_effect = [_result([]), _result([]), _result([[1]])]
310+
FalkorDBDocumentStore().update_by_filter(
311+
{"field": "year", "operator": "==", "value": 2024}, {"meta.status": "published"}
312+
)
313+
assert graph.query.call_args_list[-1].args[1]["meta_update"] == {"status": "published"}
314+
315+
@pytest.mark.parametrize("rows, expected", [([[5]], 5), ([], 0)])
316+
def test_count_documents_by_filter(self, mock_falkordb, rows, expected):
317+
_, _, graph = mock_falkordb
318+
graph.query.side_effect = [_result([]), _result([]), _result(rows)]
319+
count = FalkorDBDocumentStore().count_documents_by_filter({"field": "year", "operator": "==", "value": 2024})
320+
assert count == expected
321+
322+
def test_count_unique_metadata_by_filter(self, mock_falkordb):
323+
_, _, graph = mock_falkordb
324+
graph.query.side_effect = [_result([]), _result([]), _result([[3]]), _result([[2]])]
325+
result = FalkorDBDocumentStore().count_unique_metadata_by_filter({}, ["category", "status"])
326+
assert result == {"category": 3, "status": 2}
327+
328+
def test_get_metadata_fields_info(self, mock_falkordb):
329+
_, _, graph = mock_falkordb
330+
graph.query.side_effect = [
331+
_result([]),
332+
_result([]),
333+
_result([[["category", "year"]]]),
334+
_result([["A"]]),
335+
_result([[2024]]),
336+
]
337+
info = FalkorDBDocumentStore().get_metadata_fields_info()
338+
assert info["category"] == {"type": "str"}
339+
assert info["year"] == {"type": "int"}
340+
341+
@pytest.mark.parametrize(
342+
"rows, expected",
343+
[([[2020, 2024]], {"min": 2020, "max": 2024}), ([], {"min": None, "max": None})],
344+
)
345+
def test_get_metadata_field_min_max(self, mock_falkordb, rows, expected):
346+
_, _, graph = mock_falkordb
347+
graph.query.side_effect = [_result([]), _result([]), _result(rows)]
348+
assert FalkorDBDocumentStore().get_metadata_field_min_max("year") == expected
349+
350+
def test_get_metadata_field_unique_values(self, mock_falkordb):
351+
_, _, graph = mock_falkordb
352+
graph.query.side_effect = [_result([]), _result([]), _result([["A"], ["B"], ["C"]])]
353+
values, cursor = FalkorDBDocumentStore().get_metadata_field_unique_values("category", size=10)
354+
assert values == ["A", "B", "C"]
355+
assert cursor is None
356+
357+
def test_get_metadata_field_unique_values_pagination(self, mock_falkordb):
358+
_, _, graph = mock_falkordb
359+
graph.query.side_effect = [_result([]), _result([]), _result([["A"], ["B"], ["C"]])]
360+
values, cursor = FalkorDBDocumentStore().get_metadata_field_unique_values("category", size=2)
361+
assert values == ["A", "B"]
362+
assert cursor == {"offset": 2}
363+
280364

281365
@pytest.mark.integration
282366
class TestDocumentStore(

0 commit comments

Comments
 (0)