From 57ec774c86c219cdc2491b500eff588e058464a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Pu=C5=A1i=C4=87?= Date: Mon, 26 Feb 2024 07:46:14 +0100 Subject: [PATCH] Add tests for regex search and aggregations --- tests/e2e/text_search/common.py | 5 +- tests/e2e/text_search/test_text_search.py | 49 +++++++++++++------ .../text_search/test_text_search_disabled.py | 24 +++++++++ 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/tests/e2e/text_search/common.py b/tests/e2e/text_search/common.py index 4090ce839..0f28351d3 100644 --- a/tests/e2e/text_search/common.py +++ b/tests/e2e/text_search/common.py @@ -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")});""" diff --git a/tests/e2e/text_search/test_text_search.py b/tests/e2e/text_search/test_text_search.py index 477ee5c30..96c068dcc 100644 --- a/tests/e2e/text_search/test_text_search.py +++ b/tests/e2e/text_search/test_text_search.py @@ -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): diff --git a/tests/e2e/text_search/test_text_search_disabled.py b/tests/e2e/text_search/test_text_search_disabled.py index fd30b78ce..9125feead 100644 --- a/tests/e2e/text_search/test_text_search_disabled.py +++ b/tests/e2e/text_search/test_text_search_disabled.py @@ -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"]))