#![cfg(all(test, feature = "persistence"))]
use super::having::{DEFAULT_MAX_GROUPS, SERVER_MAX_GROUPS_CEILING};
use super::*;
use crate::velesql::{AggregateArg, AggregateFunction, AggregateType, OrderByExpr, SelectOrderBy};
#[test]
fn test_sort_aggregation_results_order_by_count_desc_sorts_rows() {
let mut rows = vec![
serde_json::json!({"category": "science", "count": 2}),
serde_json::json!({"category": "tech", "count": 5}),
serde_json::json!({"category": "history", "count": 3}),
];
let order_by = vec![SelectOrderBy {
expr: OrderByExpr::Aggregate(AggregateFunction {
function_type: AggregateType::Count,
argument: AggregateArg::Wildcard,
alias: None,
}),
descending: true,
}];
Collection::sort_aggregation_results(&mut rows, &order_by);
let ordered_categories: Vec<&str> = rows
.iter()
.map(|row| {
row.get("category")
.and_then(serde_json::Value::as_str)
.expect("category should be a string")
})
.collect();
assert_eq!(ordered_categories, vec!["tech", "history", "science"]);
}
#[test]
fn test_max_groups_clamped_to_server_ceiling() {
use crate::velesql::{WithClause, WithValue};
let with = WithClause::new().with_option("max_groups", WithValue::Integer(i64::MAX));
let resolved = Collection::extract_max_groups_limit(Some(&with));
assert_eq!(
resolved, SERVER_MAX_GROUPS_CEILING,
"untrusted query must not raise the group ceiling above the server cap"
);
}
#[test]
fn test_max_groups_below_ceiling_is_honored() {
use crate::velesql::{WithClause, WithValue};
let with = WithClause::new().with_option("group_limit", WithValue::Integer(42));
let resolved = Collection::extract_max_groups_limit(Some(&with));
assert_eq!(resolved, 42);
}
#[test]
fn test_max_groups_default_when_unspecified() {
let resolved = Collection::extract_max_groups_limit(None);
assert_eq!(resolved, DEFAULT_MAX_GROUPS);
assert!(resolved < SERVER_MAX_GROUPS_CEILING);
}