Skip to main content

Module mock_registrar

Module mock_registrar 

Source
Expand description

MockRegistrar — a Registrar implementation for testing.

MockRegistrar records which functions were registered without calling any DuckDB C API. Use it to unit-test your registration logic — verifying that the right functions are registered with the right names — without a live DuckDB instance.

It refuses, with the same error, a builder the real registration refuses before its first DuckDB call: a missing return type or callback, a function set with no overloads, a copy function that implements neither direction, a config option without a type or default, a composite or literal TypeId in any slot, an ANY return type. Checks that need DuckDB are not run: a name or signature collision, a type the running DuckDB lacks (TIME_NS before 1.5.0), a default that does not convert to its config option’s type.

§Limitation: builders with LogicalType fields

Builders that contain LogicalType values (e.g., created with .returns_logical(...) or .param_logical(...)) cannot be used with MockRegistrar in loadable-extension test mode. LogicalType’s Drop implementation calls duckdb_destroy_logical_type, which panics when the DuckDB dispatch table is uninitialized.

Stick to TypeId-based parameter and return types when building functions for use with MockRegistrar.

§Example

use quack_rs::connection::Registrar;
use quack_rs::testing::MockRegistrar;
use quack_rs::scalar::ScalarFunctionBuilder;
use quack_rs::aggregate::AggregateFunctionBuilder;
use quack_rs::types::TypeId;
use quack_rs::error::ExtensionError;
use libduckdb_sys::{duckdb_data_chunk, duckdb_function_info, duckdb_vector};

unsafe extern "C" fn word_count(
    _info: duckdb_function_info,
    _input: duckdb_data_chunk,
    _output: duckdb_vector,
) {
    // ...
}

fn register_all(reg: &impl Registrar) -> Result<(), ExtensionError> {
    let scalar = ScalarFunctionBuilder::new("word_count")
        .param(TypeId::Varchar)
        .returns(TypeId::BigInt)
        .function(word_count);
    unsafe { reg.register_scalar(scalar) }
}

let mock = MockRegistrar::new();
register_all(&mock).unwrap();
assert!(mock.has_scalar("word_count"));
assert_eq!(mock.total_registrations(), 1);

Structs§

CastRecord
A record of a single cast function registration.
MockRegistrar
An in-memory mock implementation of Registrar for unit testing.