#![allow(clippy::unwrap_used, clippy::panic)]
use super::*;
#[test]
fn a_flat_document_is_charged_its_keys_values_and_punctuation() {
let v = serde_json::json!({"id": "1", "name": "Alice"});
assert_eq!(json_bytes(&v), 26);
}
#[test]
fn an_empty_container_is_charged_only_its_brackets() {
assert_eq!(json_bytes(&serde_json::json!({})), 2);
assert_eq!(json_bytes(&serde_json::json!([])), 2);
}
#[test]
fn nesting_is_charged_all_the_way_down() {
let flat = serde_json::json!({"a": "x"});
let nested = serde_json::json!({"a": {"b": {"c": "x"}}});
assert!(
json_bytes(&nested) > json_bytes(&flat),
"a nested document must cost more than a flat one"
);
}
#[test]
fn a_column_row_is_charged_its_payload() {
assert_eq!(column_bytes(&ColumnValue::Text("Alice".into())), 5);
assert_eq!(column_bytes(&ColumnValue::Null), 0);
assert_eq!(column_bytes(&ColumnValue::Boolean(true)), 1);
}
#[test]
fn no_declared_ceiling_yields_no_budget() {
assert!(ResponseBudget::new(None).is_none());
}
#[test]
fn charges_accumulate_and_the_error_carries_the_running_total() {
let budget = ResponseBudget::new(Some(10)).expect("a ceiling was declared");
budget.charge(4).expect("under");
budget.charge(4).expect("still under");
assert_eq!(budget.used(), 8);
let err = budget.charge(4).unwrap_err();
match err {
FraiseQLError::ResponseTooLarge { bytes, limit } => {
assert_eq!(bytes, 12, "4 + 4 + 4, not the 4 that crossed it");
assert_eq!(limit, 10);
},
other => panic!("expected ResponseTooLarge, got {other:?}"),
}
}
#[test]
fn two_shared_handles_charge_one_running_total() {
let budget = ResponseBudget::new(Some(10)).expect("a ceiling was declared");
let first: &ResponseBudget = &budget;
let second: &ResponseBudget = &budget;
first.charge(6).expect("under on its own");
let err = second.charge(6).unwrap_err();
match err {
FraiseQLError::ResponseTooLarge { bytes, limit } => {
assert_eq!(bytes, 12, "both handles charged the same total");
assert_eq!(limit, 10);
},
other => panic!("expected ResponseTooLarge, got {other:?}"),
}
assert_eq!(budget.used(), 12, "and the total is visible through the owner");
}
#[test]
fn exactly_at_the_ceiling_is_allowed() {
let budget = ResponseBudget::new(Some(10)).expect("a ceiling was declared");
budget.charge(10).expect("10 is not over 10");
}
#[test]
fn an_overflowing_charge_saturates_rather_than_wrapping() {
let budget = ResponseBudget::new(Some(u64::MAX - 1)).expect("declared");
budget.charge(u64::MAX).expect_err("saturates to MAX, which is over MAX - 1");
}