use super::support::*;
#[test]
fn safe_aggregate_passes_validation() {
let program = Program {
rules: vec![Rule::new(
atom("total", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(agg_body(
"total",
Aggregator::Sum,
"sal",
vec!["dept"],
vec![atom("salary", vec![var("e"), var("dept"), var("sal")])],
))],
)],
};
assert!(program.validate().is_ok());
}
#[test]
fn unsafe_aggregate_unbound_input_fails_validation() {
let program = Program {
rules: vec![Rule::new(
atom("total", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(agg_body(
"total",
Aggregator::Sum,
"missing_sal", vec!["dept"],
vec![atom("salary", vec![var("e"), var("dept"), var("sal")])],
))],
)],
};
let err = program.validate().unwrap_err();
let ProgramValidationError::UnsafeAggregate {
rule,
literal,
variables,
} = err
else {
panic!("expected UnsafeAggregate, got {err:?}");
};
assert_eq!(rule, rule0());
assert_eq!(literal, lit(0));
assert!(
variables.contains(&VariableSymbol::new("missing_sal")),
"expected `missing_sal` in offending list, got {variables:?}"
);
}
#[test]
fn unsafe_aggregate_unbound_group_key_fails_validation() {
let program = Program {
rules: vec![Rule::new(
atom("total", vec![var("region"), var("total")]),
vec![Literal::Aggregate(agg_body(
"total",
Aggregator::Sum,
"sal",
vec!["region"], vec![atom("salary", vec![var("e"), var("dept"), var("sal")])],
))],
)],
};
let err = program.validate().unwrap_err();
let ProgramValidationError::UnsafeAggregate { variables, .. } = err else {
panic!("expected UnsafeAggregate, got {err:?}");
};
assert!(
variables.contains(&VariableSymbol::new("region")),
"expected `region` in offending list, got {variables:?}"
);
}
#[test]
fn unsafe_aggregate_result_var_collides_fails_validation() {
let program = Program {
rules: vec![Rule::new(
atom("h", vec![var("dept"), var("total")]),
vec![
Literal::Positive(atom("anchor", vec![var("total")])),
Literal::Aggregate(agg_body(
"total", Aggregator::Sum,
"sal",
vec!["dept"],
vec![atom("salary", vec![var("e"), var("dept"), var("sal")])],
)),
],
)],
};
let err = program.validate().unwrap_err();
let ProgramValidationError::UnsafeAggregate { variables, .. } = err else {
panic!("expected UnsafeAggregate, got {err:?}");
};
assert!(
variables.contains(&VariableSymbol::new("total")),
"expected `total` in offending list, got {variables:?}"
);
}
#[test]
fn aggregate_exports_satisfy_head_safety() {
let program = Program {
rules: vec![Rule::new(
atom("total", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(agg_body(
"total",
Aggregator::Count,
"sal",
vec!["dept"],
vec![atom("salary", vec![var("e"), var("dept"), var("sal")])],
))],
)],
};
assert!(program.validate().is_ok());
}
#[test]
fn aggregate_inner_only_variable_does_not_leak_to_head() {
let program = Program {
rules: vec![Rule::new(
atom("h", vec![var("e"), var("total")]),
vec![Literal::Aggregate(agg_body(
"total",
Aggregator::Count,
"sal",
vec!["dept"],
vec![atom("salary", vec![var("e"), var("dept"), var("sal")])],
))],
)],
};
let err = program.validate().unwrap_err();
let ProgramValidationError::UnsafeRule { variables, .. } = err else {
panic!("expected UnsafeRule, got {err:?}");
};
assert!(
variables.contains(&VariableSymbol::new("e")),
"inner-only `e` must not satisfy head safety; got {variables:?}"
);
}
#[test]
fn recursion_through_aggregate_is_rejected() {
let program = Program {
rules: vec![Rule::new(
atom("r", vec![var("d"), var("t")]),
vec![Literal::Aggregate(agg_body(
"t",
Aggregator::Count,
"sal",
vec!["d"],
vec![atom("r", vec![var("d"), var("sal")])],
))],
)],
};
let err = program.validate().unwrap_err();
let ProgramValidationError::UnstratifiableCycle(cycle) = err else {
panic!("expected UnstratifiableCycle, got {err:?}");
};
let predicates = cycle.predicates;
assert!(
predicates.iter().any(|p| p.as_str() == "r"),
"expected `r` in unstratifiable cycle, got {predicates:?}"
);
}