use relate_core::{Error, Value};
use crate::graph::Graph;
use crate::redo::RedoOp;
use crate::SlotPosition;
use super::{create_node, create_rel, seeded, seeded_constrained, tuple, unique, unique_rel};
fn assert_conflict_then_distinct_admits(build: impl Fn(i64, Value) -> Vec<RedoOp>) {
let graph = Graph::default();
graph
.apply_redo(
vec![
create_node(1, "Person", &[("email", Value::Int(1))]),
unique("c", "Person", &["email"]),
],
1,
)
.unwrap();
let mut admission = graph.begin_constraint_admission();
let err = admission.admit(&build(2, Value::Int(1))).unwrap_err();
assert!(matches!(err, Error::Conflict));
admission.admit(&build(3, Value::Int(2))).unwrap();
}
#[test]
fn admission_rejects_a_duplicate_and_accepts_distinct_values() {
let graph = Graph::default();
graph
.apply_redo(vec![unique("c", "P", &["email"])], 1)
.unwrap();
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[create_node(0, "P", &[("email", Value::Int(1))])])
.unwrap();
let err = admission
.admit(&[create_node(1, "P", &[("email", Value::Int(1))])])
.unwrap_err();
assert!(matches!(err, Error::Conflict));
admission
.admit(&[create_node(1, "P", &[("email", Value::Int(2))])])
.unwrap();
}
#[test]
fn admission_create_then_label_in_slot_conflicts() {
assert_conflict_then_distinct_admits(|id, value| {
vec![
create_node(id, "Temp", &[("email", value)]),
RedoOp::AddLabels {
id,
labels: vec!["Person".into()],
},
]
});
}
#[test]
fn admission_create_then_label_then_set_in_slot_conflicts() {
assert_conflict_then_distinct_admits(|id, value| {
vec![
create_node(id, "Temp", &[]),
RedoOp::AddLabels {
id,
labels: vec!["Person".into()],
},
RedoOp::SetNodeProp {
id,
key: "email".into(),
value,
},
]
});
}
#[test]
fn admission_create_then_set_then_label_in_slot_conflicts() {
assert_conflict_then_distinct_admits(|id, value| {
vec![
create_node(id, "Temp", &[]),
RedoOp::SetNodeProp {
id,
key: "email".into(),
value,
},
RedoOp::AddLabels {
id,
labels: vec!["Person".into()],
},
]
});
}
#[test]
fn admission_uninterned_rel_create_never_panics_or_false_conflicts() {
let graph = Graph::default();
graph
.apply_redo(
vec![
create_node(0, "N", &[]),
create_node(1, "N", &[]),
create_rel(7, 0, 1, "R", &[("k", Value::Int(1))]),
unique_rel("r", "R", &["k"]),
],
1,
)
.unwrap();
assert_eq!(
graph.constraint_key_holder_count("r", &tuple([Value::Int(1)])),
1
);
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[
create_rel(8, 0, 1, "Unknown", &[]),
RedoOp::SetRelProp {
id: 8,
key: "k".into(),
value: Value::Int(1),
},
])
.unwrap();
let err = admission
.admit(&[
create_rel(9, 0, 1, "R", &[]),
RedoOp::SetRelProp {
id: 9,
key: "k".into(),
value: Value::Int(1),
},
])
.unwrap_err();
assert!(matches!(err, Error::Conflict));
}
#[test]
fn admission_allows_an_earlier_free_in_the_same_group() {
let graph = seeded_constrained();
let mut admission = graph.begin_constraint_admission();
admission.admit(&[RedoOp::DeleteNode { id: 0 }]).unwrap();
admission
.admit(&[create_node(3, "P", &[("email", Value::Int(1))])])
.unwrap();
}
#[test]
fn final_slot_delete_only_records_no_overlay_free() {
let graph = seeded_constrained();
let mut admission = graph.begin_constraint_admission();
let pending = admission
.check_with_earlier(
&[RedoOp::DeleteNode { id: 0 }],
&mut std::iter::empty(),
SlotPosition::Final,
)
.unwrap()
.0;
admission.accept(pending);
let err = admission
.check_with_earlier(
&[create_node(3, "P", &[("email", Value::Int(1))])],
&mut std::iter::empty(),
SlotPosition::Followed,
)
.unwrap_err();
assert!(matches!(err, Error::Conflict));
}
#[test]
fn non_final_slot_delete_only_records_its_free() {
let graph = seeded_constrained();
let mut admission = graph.begin_constraint_admission();
let pending = admission
.check_with_earlier(
&[RedoOp::DeleteNode { id: 0 }],
&mut std::iter::empty(),
SlotPosition::Followed,
)
.unwrap()
.0;
admission.accept(pending);
admission
.check_with_earlier(
&[create_node(3, "P", &[("email", Value::Int(1))])],
&mut std::iter::empty(),
SlotPosition::Followed,
)
.unwrap();
}
#[test]
fn admission_allows_a_free_earlier_in_the_same_slot() {
let graph = seeded_constrained();
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[
RedoOp::DeleteNode { id: 0 },
create_node(3, "P", &[("email", Value::Int(1))]),
])
.unwrap();
}
#[test]
fn admission_does_not_flag_a_reassert_of_the_same_value() {
let graph = seeded_constrained();
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[RedoOp::SetNodeProp {
id: 0,
key: "email".into(),
value: Value::Int(1),
}])
.unwrap();
}
#[test]
fn admission_checks_the_full_composite_tuple() {
let graph = Graph::default();
graph
.apply_redo(vec![unique("c", "P", &["email", "tenant"])], 1)
.unwrap();
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[create_node(
0,
"P",
&[("email", Value::Int(1)), ("tenant", Value::Int(7))],
)])
.unwrap();
admission
.admit(&[create_node(
1,
"P",
&[("email", Value::Int(1)), ("tenant", Value::Int(8))],
)])
.unwrap();
let err = admission
.admit(&[create_node(
2,
"P",
&[("email", Value::Int(1)), ("tenant", Value::Int(7))],
)])
.unwrap_err();
assert!(matches!(err, Error::Conflict));
}
#[test]
fn admission_rejects_a_constraint_install_over_an_accepted_introduction() {
let graph = Graph::default();
let mut admission = graph.begin_constraint_admission();
let first_slot = [create_node(0, "P", &[("email", Value::Int(1))])];
admission.admit(&first_slot).unwrap();
let mut earlier = std::iter::once(first_slot.as_slice());
let err = admission
.check_with_earlier(
&[unique("c", "P", &["email"])],
&mut earlier,
SlotPosition::Followed,
)
.unwrap_err();
assert!(matches!(err, Error::Conflict));
}
#[test]
fn admission_rejects_an_introduction_into_a_constraint_installed_in_the_group() {
let graph = Graph::default();
let mut admission = graph.begin_constraint_admission();
admission.admit(&[unique("c", "P", &["email"])]).unwrap();
let err = admission
.admit(&[create_node(0, "P", &[("email", Value::Int(1))])])
.unwrap_err();
assert!(matches!(err, Error::Conflict));
}
#[test]
fn a_rejected_slot_leaves_no_tuple_hold() {
let graph = Graph::default();
graph
.apply_redo(vec![unique("c", "P", &["email"])], 1)
.unwrap();
let mut admission = graph.begin_constraint_admission();
let pending = admission
.check(&[create_node(0, "P", &[("email", Value::Int(1))])])
.unwrap();
drop(pending);
admission
.admit(&[create_node(1, "P", &[("email", Value::Int(1))])])
.unwrap();
}
#[test]
fn a_rejected_drop_constraint_does_not_free() {
let graph = seeded_constrained();
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[create_node(3, "P", &[("email", Value::Int(9))])])
.unwrap();
let pending = admission
.check(&[RedoOp::DropConstraint { name: "c".into() }])
.unwrap();
drop(pending);
let err = admission
.admit(&[create_node(4, "P", &[("email", Value::Int(9))])])
.unwrap_err();
assert!(matches!(err, Error::Conflict));
}
#[test]
fn admission_allows_a_value_after_an_earlier_constraint_drop() {
let graph = seeded_constrained();
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[RedoOp::DropConstraint { name: "c".into() }])
.unwrap();
admission
.admit(&[create_node(3, "P", &[("email", Value::Int(1))])])
.unwrap();
}
#[test]
fn two_constraints_in_one_group_keep_distinct_overlay_ids() {
let graph = Graph::default();
graph
.apply_redo(
vec![unique("c1", "P", &["email"]), unique("c2", "P", &["ssn"])],
1,
)
.unwrap();
let mut admission = graph.begin_constraint_admission();
admission
.admit(&[create_node(0, "P", &[("email", Value::Int(1))])])
.unwrap();
admission
.admit(&[create_node(1, "P", &[("ssn", Value::Int(1))])])
.unwrap();
admission
.admit(&[create_node(
2,
"P",
&[("ssn", Value::Int(2)), ("email", Value::Int(2))],
)])
.unwrap();
assert!(admission
.admit(&[create_node(3, "P", &[("email", Value::Int(1))])])
.is_err());
}
#[test]
fn corrupt_derived_entity_type_is_unpriceable_before_preflight_and_rejected_by_it() {
let corrupt = RedoOp::CatalogDerivedConstraint {
constraint: relate_model::Constraint {
name: "c".into(),
label: "P".into(),
properties: vec!["email".into()],
constraint_type: relate_model::ConstraintType::Unique,
property_type: None,
entity_type: "FOO".into(),
enforced_label: None,
},
};
let graph = seeded();
let classifiable = RedoOp::CatalogDerivedConstraint {
constraint: relate_model::Constraint {
name: "c".into(),
label: "P".into(),
properties: vec!["email".into()],
constraint_type: relate_model::ConstraintType::Unique,
property_type: None,
entity_type: "NODE".into(),
enforced_label: None,
},
};
assert!(graph.estimate_redo_delta(&[classifiable]).total > 0);
assert_eq!(
graph
.estimate_redo_delta(std::slice::from_ref(&corrupt))
.total,
0
);
assert!(graph.constraint_key_spec("c").is_none());
let mut admission = graph.begin_constraint_admission();
admission.admit(std::slice::from_ref(&corrupt)).unwrap();
let record = [corrupt];
let err = graph
.validate_redo_group_applicability([(&record[..], 1)])
.unwrap_err();
assert!(matches!(err, Error::Storage(_)));
}