use relate_front::parse_and_lower;
use relate_ir::{Clause, Expr};
use crate::binding::Env;
use crate::validate::validate_part_with_env;
fn rejection(cypher: &str) -> String {
let query = parse_and_lower(cypher).unwrap_or_else(|error| panic!("{cypher}: {error}"));
let [part] = query.parts.as_slice() else {
panic!("{cypher}: expected one part");
};
validate_part_with_env(part, super::Scope::stage(Env::new()), &mut |_| false)
.expect_err("the composition rule rejects this statement")
.to_string()
}
#[test]
fn use_after_a_preceding_clause_is_rejected() {
let mut query = parse_and_lower("MATCH (n) RETURN n").expect("the statement lowers");
let [part] = query.parts.as_mut_slice() else {
panic!("expected one part");
};
part.clauses.insert(
1,
Clause::Use {
graph: Expr::String("neo4j".to_string()),
},
);
let error = validate_part_with_env(part, super::Scope::stage(Env::new()), &mut |_| false)
.expect_err("the placement rule rejects a USE after another clause")
.to_string();
assert!(
error.contains("InvalidPlacementOfUseClause: USE must be the first clause"),
"unexpected error: {error}"
);
}
#[test]
fn write_before_call_in_transactions_is_rejected() {
let error = rejection(
"CREATE (n:Boxed) \
CALL { CREATE (m:Other) } IN TRANSACTIONS OF 1 ROWS",
);
assert!(
error.contains("WriteBeforeCallInTransactions"),
"unexpected error: {error}"
);
}
#[test]
fn a_grammar_ending_ddl_command_is_standalone() {
for cypher in [
"CREATE CONSTRAINT c FOR (n:L) REQUIRE n.p IS UNIQUE WITH 1 AS x",
"CREATE CONSTRAINT c FOR (n:L) REQUIRE n.p IS UNIQUE RETURN 1",
"CREATE CONSTRAINT c FOR (n:L) REQUIRE n.p IS UNIQUE FINISH",
"CREATE INDEX i FOR (n:L) ON (n.p) WITH 1 AS x",
] {
let err = rejection(cypher);
assert!(
err.contains(
"InvalidClauseComposition: schema and database-alteration commands must be standalone clauses"
),
"{cypher}: {err}"
);
}
}