Add tests for regex search and aggregations

This commit is contained in:
Ante Pušić 2024-02-26 07:46:14 +01:00
parent 12af33ac0a
commit 57ec774c86
3 changed files with 62 additions and 16 deletions

View File

@ -50,7 +50,10 @@ def memgraph_with_text_indexed_data(**kwargs) -> Memgraph:
memgraph = Memgraph()
memgraph.execute(
"""CREATE (:Document {title: "Rules2023", version: 1, fulltext: "Rules2024", date: date("2023-11-14")});"""
"""CREATE (:Document {title: "Rules2024", version: 1, fulltext: "random works", date: date("2023-11-14")});"""
)
memgraph.execute(
"""CREATE (:Document {title: "Rules2023", version: 9, fulltext: "text Rules2024", date: date("2023-11-14")});"""
)
memgraph.execute(
"""CREATE (:Document:Revision {title: "Rules2024", version: 2, fulltext: "random words", date: date("2023-12-15")});"""

View File

@ -9,6 +9,8 @@
# by the Apache License, Version 2.0, included in the file
# licenses/APL.txt.
import json
import re
import sys
import gqlalchemy
@ -60,31 +62,48 @@ def test_text_search_given_property(memgraph_with_text_indexed_data):
def test_text_search_all_properties(memgraph_with_text_indexed_data):
result = list(
memgraph_with_text_indexed_data.execute_and_fetch(
"""CALL text_search.search_all("complianceDocuments", "Rules2024") YIELD node
SEARCH_QUERY = "Rules2024"
ALL_PROPERTIES_QUERY = f"""CALL text_search.search_all("complianceDocuments", "{SEARCH_QUERY}") YIELD node
RETURN node
ORDER BY node.version ASC, node.title ASC;"""
)
result = list(memgraph_with_text_indexed_data.execute_and_fetch(ALL_PROPERTIES_QUERY))
result_nodes = [record["node"] for record in result]
assert len(result) == 3 and (
result_nodes[0].title == SEARCH_QUERY
and result_nodes[1].title == SEARCH_QUERY
and SEARCH_QUERY in result_nodes[2].fulltext
)
print(result)
assert True
def test_text_search_regex(memgraph_with_text_indexed_data):
result = list(
memgraph_with_text_indexed_data.execute_and_fetch(
"""CALL text_search.regex_search("complianceDocuments", "Rules*") YIELD node
def test_regex_text_search(memgraph_with_text_indexed_data):
REGEX_QUERY = """CALL text_search.regex_search("complianceDocuments", "wor.*s") YIELD node
RETURN node
ORDER BY node.version ASC, node.title ASC;"""
)
result = list(memgraph_with_text_indexed_data.execute_and_fetch(REGEX_QUERY))
assert (
len(result) == 2
and re.search("wor.*s", result[0]["node"].fulltext)
and re.search("wor.*s", result[1]["node"].fulltext)
# In this test, all values matching the regex string are found in the .node property only ^
)
print(result)
assert True
def test_text_search_aggregate(memgraph_with_text_indexed_data):
input_aggregation = json.dumps({"count": {"value_count": {"field": "metadata.gid"}}}, separators=(",", ":"))
expected_aggregation = json.dumps({"count": {"value": 2.0}}, separators=(",", ":"))
AGGREGATION_QUERY = f"""CALL text_search.aggregate("complianceDocuments", "data.title:Rules2024", '{input_aggregation}')
YIELD aggregation
RETURN aggregation;"""
result = list(memgraph_with_text_indexed_data.execute_and_fetch(AGGREGATION_QUERY))
assert len(result) == 1 and result[0]["aggregation"] == expected_aggregation
def test_text_search_query_boolean(memgraph_with_text_indexed_data):

View File

@ -38,5 +38,29 @@ def test_text_search_given_property(memgraph):
)
def test_text_search_all_properties(memgraph):
with pytest.raises(gqlalchemy.exceptions.GQLAlchemyDatabaseError, match=TEXT_SEARCH_DISABLED_ERROR) as _:
memgraph.execute(
"""CALL text_search.search_all("complianceDocuments", "Rules2024") YIELD node
RETURN node;"""
)
def test_regex_text_search(memgraph):
with pytest.raises(gqlalchemy.exceptions.GQLAlchemyDatabaseError, match=TEXT_SEARCH_DISABLED_ERROR) as _:
memgraph.execute(
"""CALL text_search.regex_search("complianceDocuments", "wor.*s") YIELD node
RETURN node;"""
)
def test_text_search_aggregate(memgraph):
with pytest.raises(gqlalchemy.exceptions.GQLAlchemyDatabaseError, match=TEXT_SEARCH_DISABLED_ERROR) as _:
memgraph.execute(
"""CALL text_search.aggregate("complianceDocuments", "wor.*s", "dummyAggregation") YIELD aggregation
RETURN aggregation;"""
)
if __name__ == "__main__":
sys.exit(pytest.main([__file__, "-rA"]))