2023-08-02 00:49:11 +08:00
|
|
|
# Copyright 2023 Memgraph Ltd.
|
2021-10-26 14:53:56 +08:00
|
|
|
#
|
|
|
|
# Use of this software is governed by the Business Source License
|
|
|
|
# included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
|
|
|
|
# License, and you may not use this file except in compliance with the Business Source License.
|
|
|
|
#
|
|
|
|
# As of the Change Date specified in that file, in accordance with
|
|
|
|
# the Business Source License, use of this software will be governed
|
|
|
|
# by the Apache License, Version 2.0, included in the file
|
|
|
|
# licenses/APL.txt.
|
|
|
|
|
2023-08-02 00:49:11 +08:00
|
|
|
import sys
|
2021-09-22 14:49:29 +08:00
|
|
|
import typing
|
2023-08-02 00:49:11 +08:00
|
|
|
|
2021-09-22 14:49:29 +08:00
|
|
|
import mgclient
|
|
|
|
import pytest
|
2023-08-02 00:49:11 +08:00
|
|
|
from common import execute_and_fetch_all, has_n_result_row, has_one_result_row
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def multi_db(request, connection):
|
|
|
|
cursor = connection.cursor()
|
|
|
|
if request.param:
|
|
|
|
execute_and_fetch_all(cursor, "CREATE DATABASE clean")
|
|
|
|
execute_and_fetch_all(cursor, "USE DATABASE clean")
|
|
|
|
execute_and_fetch_all(cursor, "MATCH (n) DETACH DELETE n")
|
|
|
|
pass
|
|
|
|
yield connection
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
|
2023-08-02 00:49:11 +08:00
|
|
|
@pytest.mark.parametrize("multi_db", [False, True], indirect=True)
|
|
|
|
def test_is_write(multi_db):
|
2021-09-22 14:49:29 +08:00
|
|
|
is_write = 2
|
|
|
|
result_order = "name, signature, is_write"
|
2023-08-02 00:49:11 +08:00
|
|
|
cursor = multi_db.cursor()
|
2021-09-22 14:49:29 +08:00
|
|
|
for proc in execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor,
|
|
|
|
"CALL mg.procedures() YIELD * WITH name, signature, "
|
|
|
|
"is_write WHERE name STARTS WITH 'write' "
|
|
|
|
f"RETURN {result_order}",
|
|
|
|
):
|
2021-09-22 14:49:29 +08:00
|
|
|
assert proc[is_write] is True
|
|
|
|
|
|
|
|
for proc in execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor,
|
|
|
|
"CALL mg.procedures() YIELD * WITH name, signature, "
|
|
|
|
"is_write WHERE NOT name STARTS WITH 'write' "
|
|
|
|
f"RETURN {result_order}",
|
|
|
|
):
|
2021-09-22 14:49:29 +08:00
|
|
|
assert proc[is_write] is False
|
|
|
|
|
|
|
|
assert cursor.description[0].name == "name"
|
|
|
|
assert cursor.description[1].name == "signature"
|
|
|
|
assert cursor.description[2].name == "is_write"
|
|
|
|
|
|
|
|
|
2023-08-02 00:49:11 +08:00
|
|
|
@pytest.mark.parametrize("multi_db", [False, True], indirect=True)
|
|
|
|
def test_single_vertex(multi_db):
|
|
|
|
cursor = multi_db.cursor()
|
2021-09-22 14:49:29 +08:00
|
|
|
assert has_n_result_row(cursor, "MATCH (n) RETURN n", 0)
|
2022-08-22 19:47:52 +08:00
|
|
|
result = execute_and_fetch_all(cursor, "CALL write.create_vertex() YIELD v RETURN v")
|
2021-09-22 14:49:29 +08:00
|
|
|
vertex = result[0][0]
|
|
|
|
assert isinstance(vertex, mgclient.Node)
|
|
|
|
assert has_one_result_row(cursor, "MATCH (n) RETURN n")
|
|
|
|
assert vertex.labels == set()
|
|
|
|
assert vertex.properties == {}
|
|
|
|
|
|
|
|
def add_label(label: str):
|
2022-08-22 19:47:52 +08:00
|
|
|
execute_and_fetch_all(cursor, f"MATCH (n) CALL write.add_label(n, '{label}') " "YIELD * RETURN *")
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
def remove_label(label: str):
|
2022-08-22 19:47:52 +08:00
|
|
|
execute_and_fetch_all(cursor, f"MATCH (n) CALL write.remove_label(n, '{label}') " "YIELD * RETURN *")
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
def get_vertex() -> mgclient.Node:
|
|
|
|
return execute_and_fetch_all(cursor, "MATCH (n) RETURN n")[0][0]
|
|
|
|
|
|
|
|
def set_property(property_name: str, property: typing.Any):
|
|
|
|
nonlocal cursor
|
|
|
|
execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor,
|
|
|
|
f"MATCH (n) CALL write.set_property(n, '{property_name}', " "$property) YIELD * RETURN *",
|
|
|
|
{"property": property},
|
|
|
|
)
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
label_1 = "LABEL1"
|
|
|
|
label_2 = "LABEL2"
|
|
|
|
|
|
|
|
add_label(label_1)
|
|
|
|
assert get_vertex().labels == {label_1}
|
|
|
|
add_label(label_1)
|
|
|
|
assert get_vertex().labels == {label_1}
|
|
|
|
add_label(label_2)
|
|
|
|
assert get_vertex().labels == {label_1, label_2}
|
|
|
|
remove_label(label_1)
|
|
|
|
assert get_vertex().labels == {label_2}
|
|
|
|
property_name = "prop"
|
|
|
|
property_value_1 = 1
|
|
|
|
property_value_2 = [42, 24]
|
|
|
|
set_property(property_name, property_value_1)
|
|
|
|
assert get_vertex().properties == {property_name: property_value_1}
|
|
|
|
set_property(property_name, property_value_2)
|
|
|
|
assert get_vertex().properties == {property_name: property_value_2}
|
|
|
|
set_property(property_name, None)
|
|
|
|
assert get_vertex().properties == {}
|
|
|
|
|
2022-08-22 19:47:52 +08:00
|
|
|
execute_and_fetch_all(cursor, "MATCH (n) CALL write.delete_vertex(n) YIELD * RETURN 1")
|
2021-09-22 14:49:29 +08:00
|
|
|
assert has_n_result_row(cursor, "MATCH (n) RETURN n", 0)
|
|
|
|
|
|
|
|
|
2023-08-02 00:49:11 +08:00
|
|
|
@pytest.mark.parametrize("multi_db", [False, True], indirect=True)
|
|
|
|
def test_single_edge(multi_db):
|
|
|
|
cursor = multi_db.cursor()
|
2021-09-22 14:49:29 +08:00
|
|
|
assert has_n_result_row(cursor, "MATCH (n) RETURN n", 0)
|
2022-08-22 19:47:52 +08:00
|
|
|
v1_id = execute_and_fetch_all(cursor, "CALL write.create_vertex() YIELD v RETURN v")[0][0].id
|
|
|
|
v2_id = execute_and_fetch_all(cursor, "CALL write.create_vertex() YIELD v RETURN v")[0][0].id
|
2021-09-22 14:49:29 +08:00
|
|
|
edge_type = "EDGE"
|
|
|
|
edge = execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor,
|
|
|
|
f"MATCH (n) WHERE id(n) = {v1_id} "
|
|
|
|
f"MATCH (m) WHERE id(m) = {v2_id} "
|
|
|
|
f"CALL write.create_edge(n, m, '{edge_type}') "
|
|
|
|
"YIELD e RETURN e",
|
|
|
|
)[0][0]
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
assert edge.type == edge_type
|
|
|
|
assert edge.properties == {}
|
|
|
|
property_name = "very_looong_prooooperty_naaame"
|
|
|
|
property_value_1 = {"a": 1, "b": 3.4, "c": [666]}
|
|
|
|
property_value_2 = 64
|
|
|
|
|
|
|
|
def get_edge() -> mgclient.Node:
|
|
|
|
return execute_and_fetch_all(cursor, "MATCH ()-[e]->() RETURN e")[0][0]
|
|
|
|
|
|
|
|
def set_property(property_name: str, property: typing.Any):
|
|
|
|
nonlocal cursor
|
|
|
|
execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor,
|
|
|
|
"MATCH ()-[e]->() " f"CALL write.set_property(e, '{property_name}', " "$property) YIELD * RETURN *",
|
|
|
|
{"property": property},
|
|
|
|
)
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
set_property(property_name, property_value_1)
|
|
|
|
assert get_edge().properties == {property_name: property_value_1}
|
|
|
|
set_property(property_name, property_value_2)
|
|
|
|
assert get_edge().properties == {property_name: property_value_2}
|
|
|
|
set_property(property_name, None)
|
|
|
|
assert get_edge().properties == {}
|
2022-08-22 19:47:52 +08:00
|
|
|
execute_and_fetch_all(cursor, "MATCH ()-[e]->() CALL write.delete_edge(e) YIELD * RETURN 1")
|
2021-09-22 14:49:29 +08:00
|
|
|
assert has_n_result_row(cursor, "MATCH ()-[e]->() RETURN e", 0)
|
|
|
|
|
|
|
|
|
2023-08-02 00:49:11 +08:00
|
|
|
@pytest.mark.parametrize("multi_db", [False, True], indirect=True)
|
|
|
|
def test_detach_delete_vertex(multi_db):
|
|
|
|
cursor = multi_db.cursor()
|
2021-09-22 14:49:29 +08:00
|
|
|
assert has_n_result_row(cursor, "MATCH (n) RETURN n", 0)
|
2022-08-22 19:47:52 +08:00
|
|
|
v1_id = execute_and_fetch_all(cursor, "CALL write.create_vertex() YIELD v RETURN v")[0][0].id
|
|
|
|
v2_id = execute_and_fetch_all(cursor, "CALL write.create_vertex() YIELD v RETURN v")[0][0].id
|
2021-09-22 14:49:29 +08:00
|
|
|
execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor,
|
|
|
|
f"MATCH (n) WHERE id(n) = {v1_id} "
|
2021-09-22 14:49:29 +08:00
|
|
|
f"MATCH (m) WHERE id(m) = {v2_id} "
|
|
|
|
f"CALL write.create_edge(n, m, 'EDGE') "
|
2022-08-22 19:47:52 +08:00
|
|
|
"YIELD e RETURN e",
|
|
|
|
)
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
assert has_one_result_row(cursor, "MATCH (n)-[e]->(m) RETURN n, e, m")
|
|
|
|
execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor, f"MATCH (n) WHERE id(n) = {v1_id} " "CALL write.detach_delete_vertex(n) YIELD * RETURN 1"
|
|
|
|
)
|
2021-09-22 14:49:29 +08:00
|
|
|
assert has_n_result_row(cursor, "MATCH (n)-[e]->(m) RETURN n, e, m", 0)
|
|
|
|
assert has_n_result_row(cursor, "MATCH ()-[e]->() RETURN e", 0)
|
2022-08-22 19:47:52 +08:00
|
|
|
assert has_one_result_row(cursor, f"MATCH (n) WHERE id(n) = {v2_id} RETURN n")
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
|
2023-08-02 00:49:11 +08:00
|
|
|
@pytest.mark.parametrize("multi_db", [False, True], indirect=True)
|
|
|
|
def test_graph_mutability(multi_db):
|
|
|
|
cursor = multi_db.cursor()
|
2021-09-22 14:49:29 +08:00
|
|
|
assert has_n_result_row(cursor, "MATCH (n) RETURN n", 0)
|
2022-08-22 19:47:52 +08:00
|
|
|
v1_id = execute_and_fetch_all(cursor, "CALL write.create_vertex() YIELD v RETURN v")[0][0].id
|
|
|
|
v2_id = execute_and_fetch_all(cursor, "CALL write.create_vertex() YIELD v RETURN v")[0][0].id
|
2021-09-22 14:49:29 +08:00
|
|
|
execute_and_fetch_all(
|
2022-08-22 19:47:52 +08:00
|
|
|
cursor,
|
|
|
|
f"MATCH (n) WHERE id(n) = {v1_id} "
|
2021-09-22 14:49:29 +08:00
|
|
|
f"MATCH (m) WHERE id(m) = {v2_id} "
|
|
|
|
f"CALL write.create_edge(n, m, 'EDGE') "
|
2022-08-22 19:47:52 +08:00
|
|
|
"YIELD e RETURN e",
|
|
|
|
)
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
def test_mutability(is_write: bool):
|
|
|
|
module = "write" if is_write else "read"
|
2022-08-22 19:47:52 +08:00
|
|
|
assert (
|
|
|
|
execute_and_fetch_all(cursor, f"CALL {module}.graph_is_mutable() " "YIELD mutable RETURN mutable")[0][0]
|
|
|
|
is is_write
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
execute_and_fetch_all(
|
|
|
|
cursor, "MATCH (n) " f"CALL {module}.underlying_graph_is_mutable(n) " "YIELD mutable RETURN mutable"
|
|
|
|
)[0][0]
|
|
|
|
is is_write
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
execute_and_fetch_all(
|
|
|
|
cursor,
|
|
|
|
"MATCH (n)-[e]->(m) " f"CALL {module}.underlying_graph_is_mutable(e) " "YIELD mutable RETURN mutable",
|
|
|
|
)[0][0]
|
|
|
|
is is_write
|
|
|
|
)
|
2021-09-22 14:49:29 +08:00
|
|
|
|
|
|
|
test_mutability(True)
|
|
|
|
test_mutability(False)
|
|
|
|
|
|
|
|
|
2023-08-02 00:49:11 +08:00
|
|
|
@pytest.mark.parametrize("multi_db", [False, True], indirect=True)
|
|
|
|
def test_log_message(multi_db):
|
|
|
|
cursor = multi_db.cursor()
|
2022-08-22 19:47:52 +08:00
|
|
|
success = execute_and_fetch_all(cursor, f"CALL read.log_message('message') YIELD success RETURN success")[0][0]
|
|
|
|
assert (success) is True
|
|
|
|
|
|
|
|
|
2021-09-22 14:49:29 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(pytest.main([__file__, "-rA"]))
|