Skip to content

Testing

Robin Kooyman edited this page Dec 2, 2026 · 4 revisions

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 20 unit tests: 10 in native/src/lib.rs, 7 in native/src/tsv.rs and 3 in native/src/memory.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.tsv");

async fn stored() -> (Arc<dyn ObjectStore>, ObjectMeta) {
    let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
    let path = Path::from("stations.tsv");
    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.
  • write gives the expected file text and row count. Read the file back from the store.
  • write refuses a value that the format cannot hold, and writes nothing.

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

Test a table type

Put the factory in a SessionContext under its upper-case name. Then run CREATE EXTERNAL TABLE, INSERT INTO and SELECT. See Table types and the tests in native/src/memory.rs.

Test these cases:

  • A table with each option, and without options.
  • A wrong option value gives a clear error.
  • INSERT INTO and then SELECT give the rows back.

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-df55-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 and write a sample file with each format. Use each table type:

    SELECT example_add_one(1.5), example_add_two(1.5), example_round(3.14159, 2);
    SELECT * FROM read_example_tsv(['stations.tsv'], {null_value: '-'});
    COPY (SELECT * FROM read_example_tsv(['stations.tsv'])) TO 'copy.tsv' STORED AS example_tsv;
    CREATE EXTERNAL TABLE notes (id BIGINT) STORED AS example_memory LOCATION 'memory';
    INSERT INTO notes VALUES (1);
    SELECT * FROM notes;
  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, format and table type works in Beacon.

Clone this wiki locally