fabrilis/crates/storage/src/migrate/tests.rs
loki5512344 93075ab1fd
Some checks failed
ci / check (macos-latest) (push) Has been cancelled
ci / check (ubuntu-latest) (push) Has been cancelled
ci / check (windows-latest) (push) Has been cancelled
Add design persistence, world rendering, metrics overlay and logs
M3: STL/OBJ import with mesh validation, mass/COM/inertia, material database, RON scene with content-addressed blobs and v0->v1 migration. M4: deterministic heightmap terrain, trimesh colliders, wgpu renderer with instancing and depth, winit app with 1 kHz physics, fly camera, on-screen metrics overlay and per-frame CSV/dev logs. Docs: UI, logging, network and headless CLI plans.
2026-11-16 23:34:05 +01:00

94 lines
2.5 KiB
Rust

//! Migration chains: ordering, missing links and downgrades.
#![allow(
clippy::unwrap_used,
reason = "a test aborts on failure, which is the intent"
)]
use std::collections::BTreeMap;
use super::{MigrateError, Migration, migrate_value};
/// Version 0 → 1: adds the field `a`.
fn insert_a(mut value: ron::Value) -> Result<ron::Value, MigrateError> {
let ron::Value::Map(ref mut document) = value else {
return Err(MigrateError::step(0, "document is not a map"));
};
let _previous = document.insert("a", 1);
Ok(value)
}
/// Version 1 → 2: adds the field `b`.
fn insert_b(mut value: ron::Value) -> Result<ron::Value, MigrateError> {
let ron::Value::Map(ref mut document) = value else {
return Err(MigrateError::step(1, "document is not a map"));
};
let _previous = document.insert("b", 2);
Ok(value)
}
fn empty_document() -> ron::Value {
ron::Value::Map(ron::Map::new())
}
#[test]
fn a_two_step_chain_applies_in_order() {
let mut steps = BTreeMap::new();
let _previous = steps.insert(0, insert_a as Migration);
let _previous = steps.insert(1, insert_b as Migration);
let migrated = migrate_value(empty_document(), 0, 2, &steps).unwrap();
let ron::Value::Map(document) = migrated else {
panic!("a migration must return a map");
};
assert_eq!(
document.get(&ron::Value::from("a")),
Some(&ron::Value::from(1))
);
assert_eq!(
document.get(&ron::Value::from("b")),
Some(&ron::Value::from(2))
);
}
#[test]
fn a_missing_step_stops_the_chain() {
let mut steps = BTreeMap::new();
let _previous = steps.insert(0, insert_a as Migration);
let error = migrate_value(empty_document(), 0, 2, &steps).unwrap_err();
assert_eq!(error, MigrateError::MissingStep { version: 1, to: 2 });
}
#[test]
fn a_downgrade_is_rejected_before_any_step_runs() {
let error = migrate_value(empty_document(), 2, 0, &BTreeMap::new()).unwrap_err();
assert_eq!(error, MigrateError::Downgrade { from: 2, to: 0 });
}
#[test]
fn equal_versions_are_a_no_op() {
let value = empty_document();
let migrated = migrate_value(value.clone(), 3, 3, &BTreeMap::new()).unwrap();
assert_eq!(migrated, value);
}
#[test]
fn a_rejecting_step_reports_its_version() {
fn reject(value: ron::Value) -> Result<ron::Value, MigrateError> {
let _ignored = value;
Err(MigrateError::step(0, "malformed payload"))
}
let mut steps = BTreeMap::new();
let _previous = steps.insert(0, reject as Migration);
let error = migrate_value(empty_document(), 0, 1, &steps).unwrap_err();
assert!(matches!(
error,
MigrateError::Step {
version: 0,
ref reason
} if reason == "malformed payload"
));
}