Skip to content

Commit fc41730

Browse files
authored
neo4j: Fix test for order-insensitive comparison and floating-point precision issues (#24338)
**Description:** This PR addresses two main issues in the `test_neo4jvector.py`: 1. **Order-insensitive Comparison:** Modified the `test_retrieval_dictionary` to ensure that it passes regardless of the order of returned values by parsing `page_content` into a structured format (dictionary) before comparison. 2. **Floating-point Precision:** Updated `test_neo4jvector_relevance_score` to handle minor floating-point precision differences by using the `isclose` function for comparing relevance scores with a relative tolerance. Errors addressed: - **test_neo4jvector_relevance_score:** ``` AssertionError: assert [(Document(page_content='foo', metadata={'page': '0'}), 1.0000014305114746), (Document(page_content='bar', metadata={'page': '1'}), 0.9998371005058289), (Document(page_content='baz', metadata={'page': '2'}), 0.9993508458137512)] == [(Document(page_content='foo', metadata={'page': '0'}), 1.0), (Document(page_content='bar', metadata={'page': '1'}), 0.9998376369476318), (Document(page_content='baz', metadata={'page': '2'}), 0.9993523359298706)] At index 0 diff: (Document(page_content='foo', metadata={'page': '0'}), 1.0000014305114746) != (Document(page_content='foo', metadata={'page': '0'}), 1.0) Full diff: - [(Document(page_content='foo', metadata={'page': '0'}), 1.0), + [(Document(page_content='foo', metadata={'page': '0'}), 1.0000014305114746), ? +++++++++++++++ - (Document(page_content='bar', metadata={'page': '1'}), 0.9998376369476318), ? ^^^ ------ + (Document(page_content='bar', metadata={'page': '1'}), 0.9998371005058289), ? ^^^^^^^^^ - (Document(page_content='baz', metadata={'page': '2'}), 0.9993523359298706), ? ---------- + (Document(page_content='baz', metadata={'page': '2'}), 0.9993508458137512), ? ++++++++++ ] ``` - **test_retrieval_dictionary:** ``` AssertionError: assert [Document(page_content='skills:\n- Python\n- Data Analysis\n- Machine Learning\nname: John\nage: 30\n')] == [Document(page_content='skills:\n- Python\n- Data Analysis\n- Machine Learning\nage: 30\nname: John\n')] At index 0 diff: Document(page_content='skills:\n- Python\n- Data Analysis\n- Machine Learning\nname: John\nage: 30\n') != Document(page_content='skills:\n- Python\n- Data Analysis\n- Machine Learning\nage: 30\nname: John\n') Full diff: - [Document(page_content='skills:\n- Python\n- Data Analysis\n- Machine Learning\nage: 30\nname: John\n')] ? --------- + [Document(page_content='skills:\n- Python\n- Data Analysis\n- Machine Learning\nage: John\nage: 30\n')] ? +++++++++ ```
1 parent 47ed7f7 commit fc41730

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

libs/community/tests/integration_tests/vectorstores/test_neo4jvector.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Test Neo4jVector functionality."""
22

33
import os
4+
from math import isclose
45
from typing import Any, Dict, List, cast
56

67
from langchain_core.documents import Document
8+
from yaml import safe_load
79

810
from langchain_community.graphs import Neo4jGraph
911
from langchain_community.vectorstores.neo4j_vector import (
@@ -217,12 +219,21 @@ def test_neo4jvector_relevance_score() -> None:
217219
)
218220

219221
output = docsearch.similarity_search_with_relevance_scores("foo", k=3)
220-
assert output == [
222+
expected_output = [
221223
(Document(page_content="foo", metadata={"page": "0"}), 1.0),
222224
(Document(page_content="bar", metadata={"page": "1"}), 0.9998376369476318),
223225
(Document(page_content="baz", metadata={"page": "2"}), 0.9993523359298706),
224226
]
225227

228+
# Check if the length of the outputs matches
229+
assert len(output) == len(expected_output)
230+
231+
# Check if each document and its relevance score is close to the expected value
232+
for (doc, score), (expected_doc, expected_score) in zip(output, expected_output):
233+
assert doc.page_content == expected_doc.page_content
234+
assert doc.metadata == expected_doc.metadata
235+
assert isclose(score, expected_score, rel_tol=1e-5)
236+
226237
drop_vector_indexes(docsearch)
227238

228239

@@ -779,8 +790,16 @@ def test_retrieval_dictionary() -> None:
779790
)
780791
)
781792
]
793+
782794
output = docsearch.similarity_search("Foo", k=1)
783-
assert output == expected_output
795+
796+
def parse_document(doc: Document) -> Any:
797+
return safe_load(doc.page_content)
798+
799+
parsed_expected = [parse_document(doc) for doc in expected_output]
800+
parsed_output = [parse_document(doc) for doc in output]
801+
802+
assert parsed_output == parsed_expected
784803
drop_vector_indexes(docsearch)
785804

786805

0 commit comments

Comments
 (0)