use super::support::*;
#[test]
fn safe_aggregate_inner_negation_passes_validation() {
let program = Program {
rules: vec![Rule::new(
atom("total_unflagged", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(agg_body_with_inner(
"total",
Aggregator::Sum,
"sal",
vec!["dept"],
vec![
Literal::Positive(atom("salary", vec![var("e"), var("dept"), var("sal")])),
Literal::Negative(atom("flagged", vec![var("e")])),
],
))],
)],
};
assert!(program.validate().is_ok());
}
#[test]
fn unsafe_aggregate_inner_negation_unbound_var_fails_validation() {
let program = Program {
rules: vec![Rule::new(
atom("h", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(agg_body_with_inner(
"total",
Aggregator::Sum,
"sal",
vec!["dept"],
vec![
Literal::Positive(atom("salary", vec![var("e"), var("dept"), var("sal")])),
Literal::Negative(atom("flagged", vec![var("m")])),
],
))],
)],
};
let err = program.validate().unwrap_err();
let ProgramValidationError::UnsafeAggregate { variables, .. } = err else {
panic!("expected UnsafeAggregate, got {err:?}");
};
assert!(
variables.contains(&VariableSymbol::new("m")),
"expected unbound `m` in offending list, got {variables:?}"
);
}
#[test]
fn safe_aggregate_inner_bind_feeds_input_var() {
let program = Program {
rules: vec![Rule::new(
atom("total_bonus", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(agg_body_with_inner(
"total",
Aggregator::Sum,
"bonus",
vec!["dept"],
vec![
Literal::Positive(atom("salary", vec![var("e"), var("dept"), var("sal")])),
Literal::Bind {
var: VariableSymbol::new("bonus"),
expr: BindExpr::Add(
Box::new(BindExpr::Term(var("sal"))),
Box::new(BindExpr::Term(var("sal"))),
),
},
],
))],
)],
};
assert!(program.validate().is_ok());
}
#[test]
fn safe_aggregate_inner_filter_passes_validation() {
let program = Program {
rules: vec![Rule::new(
atom("high_total", vec![var("dept"), var("total")]),
vec![Literal::Aggregate(agg_body_with_inner(
"total",
Aggregator::Sum,
"sal",
vec!["dept"],
vec![
Literal::Positive(atom("salary", vec![var("e"), var("dept"), var("sal")])),
Literal::Filter(BuiltinFilter::Gt(var("sal"), constant("0"))),
],
))],
)],
};
assert!(program.validate().is_ok());
}
#[test]
fn nested_aggregate_in_inner_body_is_rejected() {
let inner_agg = AggregateBody {
result_var: VariableSymbol::new("inner_total"),
aggregator: Aggregator::Sum,
input_var: VariableSymbol::new("sal"),
group_by: vec![VariableSymbol::new("dept")],
inner_body: vec![Literal::Positive(atom(
"salary",
vec![var("e"), var("dept"), var("sal")],
))],
};
let outer_agg = AggregateBody {
result_var: VariableSymbol::new("outer_total"),
aggregator: Aggregator::Sum,
input_var: VariableSymbol::new("inner_total"),
group_by: vec![],
inner_body: vec![Literal::Aggregate(inner_agg)],
};
let program = Program {
rules: vec![Rule::new(
atom("grand_total", vec![var("outer_total")]),
vec![Literal::Aggregate(outer_agg)],
)],
};
let err = program.validate().unwrap_err();
assert_eq!(
err,
ProgramValidationError::NestedAggregateInInnerBody {
rule: rule0(),
literal: lit(0).inner(LiteralIndex::new(0)),
}
);
}
#[test]
fn aggregator_name_renders_canonical_strings() {
assert_eq!(Aggregator::Count.name(), "COUNT");
assert_eq!(Aggregator::Sum.name(), "SUM");
assert_eq!(Aggregator::Min.name(), "MIN");
assert_eq!(Aggregator::Max.name(), "MAX");
assert_eq!(Aggregator::Avg.name(), "AVG");
}
#[test]
fn aggregate_exported_variables_match_outer_scope() {
let agg = agg_body(
"t",
Aggregator::Sum,
"sal", vec!["dept"],
vec![atom("salary", vec![var("e"), var("dept"), var("sal")])],
);
let exported = agg.exported_variables();
let expected: BTreeSet<VariableSymbol> = ["t", "dept"]
.iter()
.copied()
.map(VariableSymbol::new)
.collect();
assert_eq!(exported, expected);
}