Skip to content

Testing

Robin Kooyman edited this page Oct 29, 2026 · 4 revisions

Testing

Test a plugin in two steps:

  1. Unit tests with cargo test. They need no Beacon server and take seconds.
  2. A test in Beacon, with the packed archive. Do this before a release.

A fault in native code stops the Beacon server. So test the plugin well before you install it on a server that other people use.

Run the unit tests

cargo test --manifest-path native/Cargo.toml

The template has 15 unit tests: 10 in native/src/lib.rs and 5 in native/src/tsv.rs. The tests use tokio from [dev-dependencies].

Test a function

Register the functions in a DataFusion SessionContext. Then run SQL:

async fn run(sql: &str) -> Vec<Option<f64>> {
    let ctx = SessionContext::new();
    for udf in functions() {
        ctx.register_udf(udf);
    }
    let batches = ctx.sql(sql).await.unwrap().collect().await.unwrap();
    batches
        .iter()
        .flat_map(|b| as_float64_array(b.column(0)).unwrap().iter().collect::<Vec<_>>())
        .collect()
}

#[tokio::test]
async fn add_one_works_on_a_column() {
    let sql = "SELECT example_add_one(column1) FROM (VALUES (1.0), (2.0))";
    assert_eq!(run(sql).await, [Some(2.0), Some(3.0)]);
}

Test these cases for each function:

  • A constant argument, such as example_add_one(1.5).
  • A column argument, with VALUES.
  • A null argument, such as CAST(NULL AS DOUBLE).
  • A wrong argument. Check the error text.

In these tests, DataFusion calls the function directly, not through the plugin boundary. So a test in Beacon is still necessary. See Test in Beacon.

Test a planner hook

hooks_of(&udf) gives the hooks. See Planner hooks. Interval arithmetic rounds float bounds outward. So compare intervals with a tolerance:

fn assert_near(actual: &Interval, low: f64, high: f64) {
    let (ScalarValue::Float64(Some(a)), ScalarValue::Float64(Some(b))) =
        (actual.lower(), actual.upper())
    else {
        panic!("expected finite Float64 bounds: {actual:?}");
    };
    assert!(*a <= low && low - a < 1e-9 && *b >= high && b - high < 1e-9, "{actual:?}");
}

Test a file format

Put a file in an InMemory store. Then call the reader:

use beacon_plugin_sdk::object_store::store::memory::InMemory;
use beacon_plugin_sdk::object_store::store::path::Path;

const STATIONS: &str = include_str!("../testdata/stations.etsv");

async fn stored() -> (Arc<dyn ObjectStore>, ObjectMeta) {
    let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
    let path = Path::from("stations.etsv");
    store.put(&path, STATIONS.as_bytes().to_vec().into()).await.unwrap();
    let object = store.head(&path).await.unwrap();
    (store, object)
}

Test these cases:

  • infer_schema gives the expected names and types.
  • open gives only the wanted columns, in batches of batch_size.
  • open skips a wanted column that the file does not have.
  • open with a type that is wider than the file type, such as Float64 for an Int64 column.
  • Each option of create, and a missing option.
  • A bad file gives an error, not a panic.

Put sample files in native/testdata/. Keep them small.

Test in Beacon

The unit tests do not test the plugin boundary, the install or the Beacon scan plan. So test the archive in a Beacon server before a release.

  1. Build and pack the plugin for the platform of the server. See Packaging and releasing.

  2. Make the archive folder available to the server, such as a Docker volume on /plugins.

  3. Install the archive:

    INSTALL example FROM 'file:///plugins/example-0.1.0-df54-x86_64-unknown-linux-gnu.tar.gz'
  4. Check the plugin:

    SELECT name, version, status, error, functions FROM beacon.system.plugins
  5. Call each function. Read a sample file with each format:

    SELECT example_add_one(1.5), example_add_two(1.5), example_round(3.14159, 2);
    SELECT * FROM read_example_tsv(['stations.etsv'], {null_value: '-'});
  6. Remove the plugin:

    UNINSTALL example

A library stays in the memory of the server after UNINSTALL. Restart a test server after many installs.

Before a release

  • cargo test passes.
  • cargo clippy --manifest-path native/Cargo.toml gives no warnings.
  • The archive installs in a Beacon server of each version in plugin.beacon.
  • Each function and each format works in Beacon.

Clone this wiki locally