use super::*;
fn s(x: &str) -> Value {
Value::String(x.into())
}
fn int(x: i64) -> Value {
Value::Int(x)
}
fn ty_err(msg: &str) -> String {
Error::ty(msg).to_string()
}
fn int_list(xs: &[i64]) -> Value {
Value::List(xs.iter().map(|&x| int(x)).collect::<Vec<_>>().into())
}
#[test]
fn integer_division_overflow_is_rejected_and_modulo_wraps() {
assert_eq!(
div(int(i64::MIN), int(-1)).unwrap_err().to_string(),
ty_err("integer out of range")
);
assert_eq!(mod_op(int(i64::MIN), int(-1)).unwrap(), int(0));
}
const MB: &str = "aƱšbƧ";
fn mb_slice(start: Option<Value>, end: Option<Value>) -> Value {
slice(&s(MB), start, end).unwrap()
}
#[test]
fn string_slice_indexes_by_character() {
assert_eq!(mb_slice(Some(int(1)), Some(int(3))), s("Ʊš"));
assert_eq!(mb_slice(Some(int(2)), None), s("šbƧ"));
assert_eq!(mb_slice(None, Some(int(2))), s("aƱ"));
assert_eq!(mb_slice(None, None), s(MB));
assert_eq!(mb_slice(Some(int(4)), Some(int(5))), s("Ƨ"));
assert_eq!(
mb_slice(Some(Value::Float(1.0)), Some(Value::Float(2.0))),
s("Ʊ")
);
}
#[test]
fn string_slice_negative_bounds_count_from_the_end() {
assert_eq!(mb_slice(Some(int(-2)), None), s("bƧ"));
assert_eq!(mb_slice(Some(int(-4)), Some(int(-1))), s("Ʊšb"));
assert_eq!(mb_slice(None, Some(int(-3))), s("aƱ"));
}
#[test]
fn string_slice_out_of_range_bounds_clamp() {
assert_eq!(mb_slice(Some(int(-10)), Some(int(10))), s(MB));
assert_eq!(mb_slice(Some(int(3)), Some(int(99))), s("bƧ"));
assert_eq!(mb_slice(Some(int(-99)), Some(int(2))), s("aƱ"));
}
#[test]
fn string_slice_empty_range_is_the_empty_string() {
assert_eq!(mb_slice(Some(int(2)), Some(int(2))), s(""));
assert_eq!(mb_slice(Some(int(4)), Some(int(1))), s(""));
assert_eq!(mb_slice(Some(int(7)), None), s(""));
assert_eq!(mb_slice(None, Some(int(-9))), s(""));
assert_eq!(slice(&s(""), Some(int(0)), Some(int(1))).unwrap(), s(""));
}
#[test]
fn string_slice_null_and_non_integer_bounds() {
assert_eq!(mb_slice(Some(Value::Null), Some(int(2))), Value::Null);
assert_eq!(mb_slice(Some(int(1)), Some(Value::Null)), Value::Null);
assert_eq!(mb_slice(Some(Value::Null), None), Value::Null);
assert_eq!(mb_slice(Some(s("x")), Some(Value::Null)), Value::Null);
let err = |start, end| slice(&s(MB), start, end).unwrap_err().to_string();
assert_eq!(err(Some(s("x")), None), ty_err("slice start"));
assert_eq!(err(None, Some(Value::Bool(true))), ty_err("slice end"));
assert_eq!(err(Some(s("x")), Some(s("y"))), ty_err("slice start"));
}
#[test]
fn list_slice_follows_the_same_bounds_rules() {
let xs = int_list(&[0, 1, 2, 3]);
let sl = |start, end| slice(&xs, start, end);
assert_eq!(sl(Some(int(1)), Some(int(3))).unwrap(), int_list(&[1, 2]));
assert_eq!(sl(Some(int(-3)), Some(int(-1))).unwrap(), int_list(&[1, 2]));
assert_eq!(
sl(Some(int(-9)), Some(int(9))).unwrap(),
int_list(&[0, 1, 2, 3])
);
assert_eq!(sl(Some(int(3)), Some(int(1))).unwrap(), int_list(&[]));
assert_eq!(sl(None, Some(Value::Null)).unwrap(), Value::Null);
assert_eq!(
sl(Some(Value::Bool(true)), None).unwrap_err().to_string(),
ty_err("slice start")
);
assert_eq!(
sl(None, Some(s("x"))).unwrap_err().to_string(),
ty_err("slice end")
);
}
#[test]
fn list_slice_bounds_clamp_at_and_beyond_the_int32_range() {
let xs = int_list(&[1, 2, 3, 4, 5]);
let sl = |start, end| slice(&xs, start, end).unwrap();
let i32_max_plus_1 = i64::from(i32::MAX) + 1;
assert_eq!(sl(None, Some(int(i32_max_plus_1))), xs);
assert_eq!(
sl(Some(int(0)), Some(int(i32_max_plus_1))),
int_list(&[1, 2, 3, 4, 5])
);
assert_eq!(
sl(Some(int(i32_max_plus_1)), Some(int(i32_max_plus_1 + 1))),
int_list(&[])
);
assert_eq!(sl(None, Some(int(i64::from(i32::MIN) - 1))), int_list(&[]));
assert_eq!(sl(Some(int(i64::from(i32::MIN) - 1)), None), xs);
assert_eq!(sl(None, Some(int(i64::MAX))), xs);
assert_eq!(sl(Some(int(i64::MAX)), None), int_list(&[]));
assert_eq!(sl(None, Some(int(i64::MIN))), int_list(&[]));
assert_eq!(sl(Some(int(i64::MIN)), None), xs);
}
#[test]
fn float_slice_bounds_saturate_into_the_int32_range() {
let xs = int_list(&[1, 2, 3]);
let sl = |start, end| slice(&xs, start, end).unwrap();
assert_eq!(
sl(Some(Value::Float(f64::INFINITY)), Some(int(2))),
int_list(&[])
);
assert_eq!(sl(Some(Value::Float(2147483648.0)), None), int_list(&[]));
assert_eq!(sl(Some(Value::Float(9.3e18)), None), int_list(&[]));
assert_eq!(sl(None, Some(Value::Float(f64::INFINITY))), xs);
assert_eq!(sl(None, Some(Value::Float(9.3e18))), xs);
assert_eq!(sl(Some(Value::Float(f64::NEG_INFINITY)), None), xs);
assert_eq!(
sl(None, Some(Value::Float(f64::NEG_INFINITY))),
int_list(&[])
);
assert_eq!(
sl(Some(Value::Float(-9.3e18)), Some(int(2))),
int_list(&[1, 2])
);
assert_eq!(mb_slice(Some(Value::Float(f64::INFINITY)), None), s(""));
assert_eq!(mb_slice(None, Some(Value::Float(f64::NEG_INFINITY))), s(""));
assert_eq!(mb_slice(Some(Value::Float(f64::NEG_INFINITY)), None), s(MB));
}
#[test]
fn add_concatenates_a_string_with_temporal_and_point_scalars() {
let g = crate::graph::Graph::default();
let graph = ExecGraph::read(&g);
let point = Value::Point(relate_core::PointValue::cartesian_2d(1.0, 0.0));
let point_text = "point({x: 1.0, y: 0.0, crs: 'cartesian'})";
let cases: Vec<(Value, Value, String)> = vec![
(s("hi"), duration(0, 1), "hiP1D".to_string()),
(duration(0, 1), s("hi"), "P1Dhi".to_string()),
(s("hi"), point.clone(), format!("hi{point_text}")),
(point, s("hi"), format!("{point_text}hi")),
(s("hi"), int(2), "hi2".to_string()),
(int(2), s("hi"), "2hi".to_string()),
];
for (l, r, expected) in cases {
assert_eq!(add(&graph, l, r).unwrap(), s(&expected), "{expected}");
}
}
#[test]
fn add_rejects_a_uuid_operand_beside_a_string() {
let g = crate::graph::Graph::default();
let graph = ExecGraph::read(&g);
for (l, r) in [(s("id="), Value::Uuid(1)), (Value::Uuid(1), s("!"))] {
assert_eq!(
add(&graph, l, r).unwrap_err().to_string(),
ty_err("+ type mismatch")
);
}
}
#[test]
fn slice_of_null_or_a_non_sequence() {
assert_eq!(
slice(&Value::Null, Some(s("x")), None).unwrap(),
Value::Null
);
assert_eq!(
slice(&int(5), Some(s("x")), None).unwrap_err().to_string(),
ty_err("slice needs a list")
);
}
#[test]
fn regex_rejections_carry_the_neo4j_classes() {
let g = crate::graph::Graph::default();
let graph = ExecGraph::read(&g);
let run = |l: Value, r: Value| binary(&graph, BinaryOp::Regex, l, r);
assert!(run(s("a"), int(42)).is_err());
assert_eq!(
run(s("a"), s("[")).unwrap_err(),
Error::argument_error("invalid regular expression")
);
assert_eq!(run(Value::Null, int(42)).unwrap(), Value::Null);
}
#[test]
fn regex_anchors_at_both_ends() {
assert!(!regex_is_match("21_val", "1_.*").unwrap());
assert!(regex_is_match("1_val", "1_.*").unwrap());
assert!(!regex_is_match("ab", "a|b").unwrap());
assert!(regex_is_match("a", "a|b").unwrap());
assert!(regex_is_match("", "x*").unwrap());
assert!(regex_is_match("x1_val", ".*1_.*").unwrap());
assert!(regex_is_match("abc", "abc").unwrap());
assert!(regex_is_match("abc", "b").is_ok_and(|m| !m));
assert!(regex_is_match("abc", "[").is_err());
}
fn duration(months: i64, days: i64) -> Value {
Value::Temporal(relate_core::Temporal::Duration {
months,
days,
nanos: 0,
})
}
#[test]
fn case_when_rejects_a_runtime_non_boolean_condition() {
use crate::exec::test_support::{exec_autocommit, try_exec_cypher};
use crate::graph::Graph;
let graph = Graph::default();
exec_autocommit(&graph, "CREATE (:Person {age: 38, name: 'Alice'})").unwrap();
for predicate in ["n.age", "n.name"] {
let query =
format!("MATCH (n:Person) RETURN CASE WHEN {predicate} THEN 'yes' ELSE 'no' END AS a");
let error = try_exec_cypher(&graph, &query)
.err()
.unwrap_or_else(|| panic!("{predicate}: a non-boolean WHEN must be rejected"));
assert!(
error.to_string().contains("requires a boolean"),
"{predicate}: {error}"
);
}
}
#[test]
fn plain_case_evaluates_only_the_taken_branch() {
use crate::exec::test_support::try_exec_cypher;
use crate::graph::Graph;
let result = try_exec_cypher(
&Graph::default(),
"RETURN CASE 1 WHEN 1 THEN 1 ELSE 1/0 END AS x",
)
.expect("the unselected ELSE arm must not evaluate");
assert_eq!(result.rows, vec![vec![Value::Int(1)]]);
}
#[test]
fn mixed_list_and_string_charges_the_list_buffer() {
let g = crate::graph::Graph::default();
let graph = ExecGraph::read(&g);
let list = Value::List(vec![int(1), int(2), int(3)].into());
let out = add(&graph, list, s("x")).unwrap();
assert!(matches!(out, Value::List(_)), "{out:?}");
assert_eq!(
graph.expression_built_bytes(),
crate::workspace::buffer_bytes(4, std::mem::size_of::<Value>() as u64,)
);
}
#[test]
fn mixed_list_and_string_is_refused_when_the_buffer_exceeds_the_cap() {
let mut g = crate::graph::Graph::default();
g.configure_expression_memory_limit(Some(1));
let graph = ExecGraph::read(&g);
let list = Value::List(vec![int(1), int(2), int(3)].into());
let error = add(&graph, list, s("x")).unwrap_err();
assert!(
matches!(
error,
Error::Capacity {
limit: 1,
kind: relate_core::CapacityKind::Expression,
..
}
),
"{error}"
);
}
fn cmp(op: BinaryOp, l: Value, r: Value) -> Value {
let g = crate::graph::Graph::default();
let graph = ExecGraph::read(&g);
binary(&graph, op, l, r).unwrap()
}
#[test]
fn equal_durations_satisfy_the_equality_tolerant_operators() {
let d = duration(0, 1);
assert_eq!(cmp(BinaryOp::Lt, d.clone(), d.clone()), Value::Null);
assert_eq!(cmp(BinaryOp::Gt, d.clone(), d.clone()), Value::Null);
assert_eq!(cmp(BinaryOp::Lte, d.clone(), d.clone()), Value::Bool(true));
assert_eq!(cmp(BinaryOp::Gte, d.clone(), d.clone()), Value::Bool(true));
let other = duration(0, 2);
for op in [BinaryOp::Lt, BinaryOp::Gt, BinaryOp::Lte, BinaryOp::Gte] {
assert_eq!(cmp(op, d.clone(), other.clone()), Value::Null);
}
}
#[test]
fn subtracting_two_temporals_is_a_type_mismatch() {
let date = Value::Temporal(temporal::parse_iso("date", "2020-01-01").unwrap());
let error = sub(date.clone(), date.clone()).unwrap_err();
assert_eq!(error.to_string(), ty_err("- type mismatch"));
assert_eq!(
sub(date, duration(0, 1)).unwrap(),
Value::Temporal(temporal::parse_iso("date", "2019-12-31").unwrap())
);
}
#[test]
fn equal_uuids_satisfy_the_equality_tolerant_operators() {
let a = Value::Uuid(1);
let b = Value::Uuid(2);
assert_eq!(cmp(BinaryOp::Gt, a.clone(), b.clone()), Value::Null);
assert_eq!(cmp(BinaryOp::Lt, b.clone(), a.clone()), Value::Null);
assert_eq!(cmp(BinaryOp::Gte, a.clone(), a.clone()), Value::Bool(true));
assert_eq!(cmp(BinaryOp::Lte, a.clone(), a.clone()), Value::Bool(true));
}
#[test]
fn a_scalar_in_right_operand_answers_through_equality() {
use crate::exec::test_support::try_exec_cypher_params;
let g = crate::graph::Graph::default();
let run = |params: std::collections::BTreeMap<&str, Value>, query: &str| {
try_exec_cypher_params(
&g,
query,
¶ms
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect(),
)
.unwrap()
.rows[0][0]
.clone()
};
let query = "RETURN 'a' IN $tag AS r";
let tag = |v: Value| std::collections::BTreeMap::from([("tag", v)]);
assert_eq!(
run(tag(Value::String("a".into())), query),
Value::Bool(true)
);
assert_eq!(
run(tag(Value::String("b".into())), query),
Value::Bool(false),
"a miss answers false, never null"
);
assert_eq!(run(tag(Value::Null), query), Value::Null);
}
#[test]
fn a_container_in_right_operand_stays_the_typed_rejection() {
use crate::exec::test_support::{error_of, try_exec_cypher};
let g = crate::graph::Graph::default();
for query in [
"WITH {a: 1} AS m RETURN 2 IN m",
"RETURN 5 IN vector([1, 2, 3], 3, INTEGER)",
] {
let err = error_of(try_exec_cypher(&g, query));
assert!(
err.contains("IN requires a list on the right-hand side"),
"{query}: {err}"
);
}
}