#![allow(clippy::unwrap_used, clippy::panic)]
use super::*;
#[test]
fn two_reads_of_one_request_charge_one_running_total() {
let budget = CostBudget::new(Some(100)).unwrap();
budget.charge(60).expect("the first read fits");
assert_eq!(budget.used(), 60);
let err = budget.charge(60).expect_err("the two together do not");
match err {
FraiseQLError::CostExceeded {
cost,
limit,
retry_after_secs,
..
} => {
assert_eq!(cost, 120, "the figure reported is the total that crossed the line");
assert_eq!(limit, 100);
assert_eq!(retry_after_secs, None, "a per-request ceiling is permanent, not a window");
},
other => panic!("expected CostExceeded, got {other:?}"),
}
}
#[test]
fn one_read_over_the_ceiling_is_still_refused_on_its_own() {
let budget = CostBudget::new(Some(100)).unwrap();
let err = budget.charge(101).expect_err("one read may exceed the ceiling by itself");
assert!(
matches!(
err,
FraiseQLError::CostExceeded {
cost: 101,
limit: 100,
..
}
),
"{err:?}"
);
}
#[test]
fn a_total_equal_to_the_ceiling_is_served() {
let budget = CostBudget::new(Some(100)).unwrap();
budget.charge(40).unwrap();
budget.charge(60).expect("100 is not over 100");
}
#[test]
fn no_declared_ceiling_is_no_budget() {
assert!(CostBudget::new(None).is_none());
}
#[test]
fn a_request_holds_its_two_ceilings_unswapped() {
let budget = RequestBudget::new(Some(4096), Some(100));
assert_eq!(budget.bytes().expect("a bytes ceiling was declared").limit(), 4096);
assert_eq!(budget.cost().expect("a cost ceiling was declared").limit(), 100);
}
#[test]
fn either_ceiling_may_be_absent_on_its_own() {
let bytes_only = RequestBudget::new(Some(4096), None);
assert!(bytes_only.bytes().is_some());
assert!(bytes_only.cost().is_none(), "no cost ceiling was declared");
let cost_only = RequestBudget::new(None, Some(100));
assert!(cost_only.bytes().is_none(), "no bytes ceiling was declared");
assert!(cost_only.cost().is_some());
let neither = RequestBudget::new(None, None);
assert!(neither.bytes().is_none());
assert!(neither.cost().is_none());
}