Testing and benchmarks
Testing a panel
Test a panel over HTTP, in memory: mount builds the router against a test database, and
TestClient sends it requests. The client carries cookies, a tenant and a CSRF token; sign_in
signs it in as a user, log_in goes through the login form, and submit and submit_multipart
post a form with its CSRF pair. The testing module also builds form bodies and reads responses
back: the table’s rows and row_keys, why a field was refused (field_error), the flash
notification, why a table is empty (empty_table), and whether a button asks first
(confirms_first). An app reaches it as tablo::testing through the facade’s testing feature;
enable it for tests only:
[dev-dependencies]
tablo = { version = "0.11.1", features = ["sqlite", "testing"] }
#![allow(unused)]
fn main() {
use tablo::{auth::create_admin, testing::{TestClient, mount_with_handle}};
#[tokio::test]
async fn books_cannot_be_deleted() {
let mut db = seeded_db().await; // your fixture: an in-memory database with rows
let book = first_book(&db).await;
// `mount_with_handle` builds the router an app does, beside its own pages and routes, and
// keeps the mounted panel, which the client reads its URLs from.
let (router, panel) =
mount_with_handle(db.clone(), Panel::new("admin").resource::<BookResource>())
.expect("mount the panel");
let admin = create_admin(&mut db, "admin@example.com", "secret", "Admin")
.await
.expect("seed the admin");
// `sign_in` records a session for `admin`, as a successful login does.
let client = TestClient::new(&router)
.sign_in(&db, "admin", &admin)
.await
.panel(&panel);
let books = client.resource::<BookResource>();
let list = books.list().await;
assert_eq!(list.status(), 200);
assert_eq!(list.rows().len(), 1);
// `delete` posts the confirmed delete to the record's route, with the CSRF pair.
let reply = books.delete(&book).submit().await;
assert_eq!(reply.status(), 403); // the policy does not allow `DeleteAny`
let books = Book::all().exec(&mut db).await.expect("list books");
assert_eq!(books.len(), 1); // nothing was deleted
}
}
client.resource::<R>() sends typed requests to one resource: it loads its pages, builds its
form posts from the form’s derived field enum (.set(BookFormField::Title, "Emma")), and posts
its deletes and actions. Each URL comes from the panel, so a test follows a renamed slug, fails to
compile on a renamed field or action, and panics naming the resource when the panel serves no
such page instead of asserting on a 404. A submission answers with a Reply that reads its
status, redirect, notification, rows and field errors, and follow() loads the page a redirect
names. ResourceClient,
Submission and Reply list the methods.
The free functions remain for what the typed client does not cover: a raw body, a request to a route the panel does not mount, a header to assert.
- Cover every ability your policy decides with a request it allows and one it refuses, and assert on the database as well as the status code.
- Cover
query()scoping by seeding a row the scope excludes and asserting that the list, the detail page and a delete all miss it. - Tenancy.
client.tenant(id)scopes a request to a tenant, the way a signed-in user’s tenant would. - Signed-in requests.
client.sign_in(&db, "admin", &user)signs the client in asuserwith no login form and no password hash (TestClient::sign_in). - Declaration mistakes. A panel that refuses to mount returns a
MountErrorinside the router builder’s error.refusallists its mistakes; match on each one’sDeclarationErrorKindrather than on its message:
#![allow(unused)]
fn main() {
#[tokio::test]
async fn the_panel_refuses_a_db_without_the_auth_models() {
let db = Db::builder()
.models(toasty::models!(Book))
.connect("sqlite::memory:")
.await
.expect("connect");
// Auth is on, and the `Db` does not register its models.
let mistakes = refusal(mount(db, Panel::new("admin").resource::<BookResource>()));
assert!(matches!(
mistakes[0].kind,
tablo::DeclarationErrorKind::MissingAuthModels { .. }
));
}
}
examples/showcase/tests/ is a complete suite covering lists, forms, deletes, filters, export,
tenancy, uploads and authentication; its common module holds the fixtures above.
The browser scripts
tablo-ui’s searchable-select script is a plain browser script with no build step. Its unit
tests run on Node’s built-in runner:
node --test crates/tablo-ui/assets/selects.test.js
Benchmarks
The benchmark renders a 50-row list with two included relations, under tenancy and policy, through the same path as the panel’s list page:
cargo run --manifest-path benchmarks/tablo/Cargo.toml -- --bench
./benchmarks/scripts/bench.sh # adds an HTTP run with `oha`
The target is under 40 ms at the median on local SQLite. It is a reference, not a pass/fail gate:
the harness prints the target beside the measurement. Results are written to
benchmarks/results/, which is not committed. benchmarks/README.md covers the setup, the
optional PostgreSQL run and the method.