Skip to main content

AggregateTestHarness

Struct AggregateTestHarness 

Source
pub struct AggregateTestHarness<S: AggregateState> { /* private fields */ }
Expand description

A test harness that simulates the DuckDB aggregate function lifecycle in pure Rust, without requiring a DuckDB instance.

§Usage

use quack_rs::testing::AggregateTestHarness;
use quack_rs::aggregate::AggregateState;

#[derive(Default, Debug, PartialEq)]
struct Counter { n: u64 }
impl AggregateState for Counter {}

let mut harness = AggregateTestHarness::<Counter>::new();
harness.update(|c| c.n += 1);
harness.update(|c| c.n += 1);
harness.update(|c| c.n += 1);

let state = harness.finalize();
assert_eq!(state.n, 3);

Implementations§

Source§

impl<S: AggregateState> AggregateTestHarness<S>

Source

pub fn new() -> Self

Creates a new harness with a fresh S::default() state.

§Example
use quack_rs::testing::AggregateTestHarness;
use quack_rs::aggregate::AggregateState;

#[derive(Default)]
struct MyState { count: u64 }
impl AggregateState for MyState {}

let harness = AggregateTestHarness::<MyState>::new();
Source

pub const fn with_state(state: S) -> Self

Creates a harness with an explicit initial state.

Useful for testing combine with a pre-populated state.

Source

pub fn update<F>(&mut self, f: F)
where F: FnOnce(&mut S),

Applies an update to the current state.

Simulates one row being processed by the aggregate’s update callback.

§Example
use quack_rs::testing::AggregateTestHarness;
use quack_rs::aggregate::AggregateState;

#[derive(Default)]
struct Sum { total: i64 }
impl AggregateState for Sum {}

let mut h = AggregateTestHarness::<Sum>::new();
h.update(|s| s.total += 5);
h.update(|s| s.total += 3);
Source

pub fn combine<F>(&mut self, source: &Self, combine_fn: F)
where F: FnOnce(&S, &mut S),

Simulates a combine operation: merges the source harness’s state into this harness’s state using the provided combine function.

This tests the correctness of your combine implementation, including the critical requirement that all configuration fields are propagated from source to target (Pitfall L1 / Problem 4).

§Example
use quack_rs::testing::AggregateTestHarness;
use quack_rs::aggregate::AggregateState;

#[derive(Default, Clone)]
struct Sum { total: i64 }
impl AggregateState for Sum {}

let mut h1 = AggregateTestHarness::<Sum>::new();
h1.update(|s| s.total += 10);

let mut h2 = AggregateTestHarness::<Sum>::new();
h2.update(|s| s.total += 20);

// Combine h1 into h2
h2.combine(&h1, |source, target| target.total += source.total);

let result = h2.finalize();
assert_eq!(result.total, 30);
Source

pub fn finalize(self) -> S

Consumes the harness and returns the final state.

Simulates DuckDB calling the finalize callback.

§Example
use quack_rs::testing::AggregateTestHarness;
use quack_rs::aggregate::AggregateState;

#[derive(Default, Debug, PartialEq)]
struct Sum { total: i64 }
impl AggregateState for Sum {}

let mut h = AggregateTestHarness::<Sum>::new();
h.update(|s| s.total = 42);
assert_eq!(h.finalize().total, 42);
Source

pub const fn state(&self) -> &S

Borrows the current state for inspection without consuming the harness.

Useful for asserting intermediate state during a sequence of updates.

§Example
use quack_rs::testing::AggregateTestHarness;
use quack_rs::aggregate::AggregateState;

#[derive(Default)]
struct Sum { total: i64 }
impl AggregateState for Sum {}

let mut h = AggregateTestHarness::<Sum>::new();
h.update(|s| s.total += 5);
assert_eq!(h.state().total, 5);
h.update(|s| s.total += 3);
assert_eq!(h.state().total, 8);
Source

pub fn reset(&mut self)

Resets the state to S::default().

Useful for running multiple scenarios with a single harness.

Source

pub fn aggregate<I, T, F>(inputs: I, update_fn: F) -> S
where I: IntoIterator<Item = T>, F: Fn(&mut S, T),

Runs a sequence of updates from an iterator and returns the final state.

This is a convenience method for testing aggregate functions over a collection of input values.

§Example
use quack_rs::testing::AggregateTestHarness;
use quack_rs::aggregate::AggregateState;

#[derive(Default)]
struct Sum { total: i64 }
impl AggregateState for Sum {}

let result = AggregateTestHarness::<Sum>::aggregate(
    [1_i64, 2, 3, 4, 5],
    |s, v| s.total += v,
);
assert_eq!(result.total, 15);

Trait Implementations§

Source§

impl<S: AggregateState> Debug for AggregateTestHarness<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: AggregateState> Default for AggregateTestHarness<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.