Repository navigation
Testing
Test a plugin in two steps:
- Unit tests with
cargo test. They need no Beacon server and take seconds. - 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.
cargo test --manifest-path native/Cargo.tomlThe 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].
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.
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:?}");
}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_schemagives the expected names and types. -
opengives only the wanted columns, in batches ofbatch_size. -
openskips a wanted column that the file does not have. -
openwith a type that is wider than the file type, such asFloat64for anInt64column. - Each option of
create, and a missing option. - A bad file gives an error, not a panic.
-
writegives the expected file text and row count. Read the file back from the store. -
writerefuses a value that the format cannot hold, and writes nothing.
Put sample files in native/testdata/. Keep them small.
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 INTOand thenSELECTgive the rows back.
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.
-
Build and pack the plugin for the platform of the server. See Packaging and releasing.
-
Make the archive folder available to the server, such as a Docker volume on
/plugins. -
Install the archive:
INSTALL example FROM 'file:///plugins/example-0.1.0-df54-x86_64-unknown-linux-gnu.tar.gz'
-
Check the plugin:
SELECT name, version, status, error, functions FROM beacon.system.plugins
-
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;
-
Remove the plugin:
UNINSTALL example
A library stays in the memory of the server after UNINSTALL. Restart a test server after many installs.
-
cargo testpasses. -
cargo clippy --manifest-path native/Cargo.tomlgives no warnings. - The archive installs in a Beacon server of each version in
plugin.beacon. - Each function, format and table type works in Beacon.
For Beacon v2.1.0-rc4 (SDK ABI 7, DataFusion 55). Template repository: maris-development/beacon-plugin-template.