use relate_core::{Error, Value};
use crate::exec::test_support::{exec_write, exec_write_uncommitted, write_scratch, Builder};
use crate::graph::Graph;
#[test]
fn runtime_dynamic_property_key_rejects_empty_and_nul_names() {
for bad_key in ["", "a\0b"] {
let builder = Builder::new();
builder.prop_node(
"holder",
&["Holder"],
&[("k", Value::String(bad_key.into()))],
);
let graph = builder.done();
let scratch = write_scratch(&graph);
let error = match exec_write(
&graph,
&scratch,
"MATCH (n:Holder) WITH n, n.k AS prop_key SET n[prop_key] = 1",
) {
Err(error) => error,
Ok(_) => panic!("an empty/NUL property key must be rejected at runtime"),
};
assert!(
error.to_string().contains(
"invalid name. A property key name cannot be empty or contain any null-bytes"
),
"unexpected error: {error}"
);
}
}
#[test]
fn runtime_computed_property_key_still_writes_the_property() {
let builder = Builder::new();
builder.prop_node("holder", &["Holder"], &[("k", Value::String("x".into()))]);
let graph = builder.done();
let result = exec_write_uncommitted(
&graph,
"MATCH (n:Holder) WITH n, n.k AS prop_key SET n[prop_key] = 1 RETURN n.x AS v",
);
assert_eq!(result.rows, vec![vec![Value::Int(1)]]);
}
#[test]
fn set_property_rejects_a_list_holding_null() {
let builder = Builder::new();
builder.anon(&["A"]);
let graph = builder.done();
let error = match exec_write(
&graph,
&write_scratch(&graph),
"MATCH (n:A) SET n.p = [1, null]",
) {
Err(error) => error,
Ok(_) => panic!("a list holding null must not be stored as a property"),
};
assert!(error.to_string().contains("InvalidPropertyType"), "{error}");
}
#[test]
fn non_entity_write_targets_are_classed_by_the_target_shape() {
let graph = Graph::default();
let cases: [(&str, bool); 7] = [
("SET (CASE WHEN true THEN 42 END).name = 'neo4j'", true),
("UNWIND [42] AS x SET x.p = 1", false),
("UNWIND [42] AS x REMOVE x.p", false),
(
"UNWIND [{a: 1}] AS n UNWIND ['a'] AS x UNWIND ['b'] AS y SET n[x][y] = 1",
false,
),
("UNWIND [42] AS x SET x:Label", true),
("UNWIND [42] AS x REMOVE x:Label", true),
("SET (1).p = 2", true),
];
for (query, is_static) in cases {
let error = match exec_write(&graph, &write_scratch(&graph), query) {
Err(error) => error,
Ok(_) => panic!("{query}: a non-entity write target must be rejected"),
};
let expected = if is_static {
"the static SyntaxError class"
} else {
"the runtime type-error class"
};
assert_eq!(
matches!(error, Error::Syntax(_)),
is_static,
"{query}: expected {expected}, got: {error}"
);
}
}
#[test]
fn runtime_dynamic_labels_reject_wrong_types_and_bad_names() {
let cases: [(Value, &str); 4] = [
(Value::Int(1), "Expected to be STRING or LIST<STRING>."),
(
Value::List(vec![Value::String("A".into()), Value::Int(1)].into()),
"Expected to be STRING or LIST<STRING>.",
),
(
Value::String("".into()),
"invalid name. A label name cannot be empty or contain any null-bytes",
),
(
Value::String("a\0b".into()),
"invalid name. A label name cannot be empty or contain any null-bytes",
),
];
for (value, expected) in cases {
let builder = Builder::new();
builder.prop_node("holder", &["Holder"], &[("label", value)]);
let graph = builder.done();
let scratch = write_scratch(&graph);
let error = match exec_write(
&graph,
&scratch,
"MATCH (n:Holder) WITH n, n.label AS lbl SET n:$(lbl)",
) {
Err(error) => error,
Ok(_) => panic!("a bad runtime dynamic label must be rejected"),
};
assert!(
error.to_string().contains(expected),
"expected {expected:?} in {error}"
);
}
}
#[test]
fn delete_list_of_nodes_and_paths_keeps_the_row_list() {
let builder = Builder::new();
let a = builder.anon(&["A"]);
let b = builder.anon(&["B"]);
builder.rel(a, b, "R", &[]);
builder.anon(&["C"]);
let g = builder.done();
let r = exec_write_uncommitted(
&g,
"MATCH p = (:A)-->(:B), (c:C) WITH [c] + [[p], null] AS xs \
DELETE xs RETURN size(xs) AS n",
);
assert_eq!(r.rows, vec![vec![Value::Int(3)]]);
assert_eq!(r.stats.nodes_deleted, 3);
assert_eq!(r.stats.relationships_deleted, 1);
}
#[test]
fn delete_path_fails_like_delete_n_on_outside_relationships() {
fn graph_a_r_b_outside_c() -> Graph {
let builder = Builder::new();
let a = builder.anon(&["A"]);
let b = builder.anon(&["B"]);
let c = builder.anon(&["C"]);
builder.rel(a, b, "R", &[]);
builder.rel(b, c, "OUTSIDE", &[]);
builder.done()
}
let g = graph_a_r_b_outside_c();
let path_error = match exec_write(&g, &write_scratch(&g), "MATCH p = (:A)-[:R]->(:B) DELETE p")
{
Err(error) => error,
Ok(_) => panic!("DELETE p must fail on a node with outside relationships"),
};
let g = graph_a_r_b_outside_c();
let node_error = match exec_write(&g, &write_scratch(&g), "MATCH (b:B) DELETE b") {
Err(error) => error,
Ok(_) => panic!("DELETE b on the same graph state must fail"),
};
assert_eq!(path_error.to_string(), node_error.to_string());
assert!(
path_error.to_string().contains(
"ConstraintVerificationFailed: DeleteConnectedNode: Cannot delete node with \
relationships unless DETACH"
),
"unexpected error: {path_error}"
);
}
fn two_user_cycle() -> Graph {
let builder = Builder::new();
let a = builder.anon(&["User"]);
let b = builder.anon(&["User"]);
builder.rel(a, b, "R", &[]);
builder.rel(b, a, "R", &[]);
builder.done()
}
#[test]
fn delete_paths_covering_all_relationships_delete_every_node() {
let r = exec_write_uncommitted(
&two_user_cycle(),
"MATCH p = (:User)-[r]->(:User) \
WITH {key: collect(p)} AS pathColls \
DELETE pathColls.key[0], pathColls.key[1]",
);
assert_eq!(r.stats.nodes_deleted, 2);
assert_eq!(r.stats.relationships_deleted, 2);
}
#[test]
fn delete_path_set_spans_all_rows_of_the_clause() {
let r = exec_write_uncommitted(
&two_user_cycle(),
"MATCH p = (:User)-[:R]->(:User) DELETE p",
);
assert_eq!(r.stats.nodes_deleted, 2);
assert_eq!(r.stats.relationships_deleted, 2);
}
#[test]
fn deleted_relationship_all_properties_projection_raises_deleted_access() {
let b = Builder::new();
let a = b.anon(&["A"]);
let b_node = b.anon(&["B"]);
b.rel(a, b_node, "R", &[("since", Value::Int(2020))]);
let g = b.done();
let error = match exec_write(
&g,
&write_scratch(&g),
"MATCH (:A)-[r:R {since: 2020}]->(:B) DELETE r RETURN r {.*} AS p",
) {
Err(error) => error,
Ok(_) => panic!("a deleted relationship projection must not project an empty map"),
};
assert!(error.to_string().contains("DeletedEntityAccess"), "{error}");
}