Skip to main content

Module mock_vector

Module mock_vector 

Source
Expand description

In-memory mock types for DuckDB vectors.

MockVectorWriter and MockVectorReader let you test callback logic — the code that reads input rows and writes output values — without a live DuckDB instance.

§Why these exist

DuckDB loadable extensions use libduckdb-sys with features = ["loadable-extension"], which routes every C API call through a lazy dispatch table. That table is only initialized when DuckDB calls duckdb_rs_extension_api_init at extension load time. In cargo test, no DuckDB process loads the extension, so the dispatch table is never initialized and any call to VectorReader::new or VectorWriter::new panics with DuckDB API not initialized.

These mock types provide the same write/read interface but store data in a plain Vec, with no DuckDB dependency at all.

§What they are for

Keep the per-row computation in plain Rust functions and test those directly. The mocks are for the row loop around it. They mirror VectorReader and VectorWriter without matching them method for method — MockVectorWriter shares most write_* names, while MockVectorReader offers try_get_* getters that return None for NULL where the real reader has is_valid plus read_* — and they are separate types, so a function written against them cannot be handed the real reader and writer. To run a real callback against real vectors, use InMemoryDb (bundled-test / bundled-test-prebuilt features).

MockVectorWriter reproduces a real output vector’s NULL and capacity behaviour rather than being more forgiving than it; see its type docs. MockVectorReader likewise panics on a row past its end, where a real reader would read out of bounds.

use quack_rs::testing::{MockVectorWriter, MockVectorReader, MockDuckValue};

/// A row loop prototyped against the mocks.
fn compute_double(reader: &MockVectorReader, writer: &mut MockVectorWriter) {
    for i in 0..reader.row_count() {
        if reader.is_valid(i) {
            let v = reader.try_get_i64(i).unwrap_or(0);
            writer.write_i64(i, v * 2);
        } else {
            writer.set_null(i);
        }
    }
}

let reader = MockVectorReader::from_i64s([Some(1), Some(5), None, Some(-3)]);
let mut writer = MockVectorWriter::new(4);
compute_double(&reader, &mut writer);

assert_eq!(writer.try_get_i64(0), Some(2));
assert_eq!(writer.try_get_i64(1), Some(10));
assert!(writer.is_null(2));
assert_eq!(writer.try_get_i64(3), Some(-6));

Structs§

MockVectorReader
An in-memory mock input vector for testing update and scan callbacks.
MockVectorWriter
An in-memory mock output vector for testing finalize and scan callbacks.

Enums§

MockDuckValue
A DuckDB-compatible value variant for testing.