Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 56 additions & 52 deletions crates/schema/tests/ensure_same_schema.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
use serial_test::serial;
use spacetimedb_schema::auto_migrate::{ponder_auto_migrate, AutoMigrateStep};
use spacetimedb_schema::def::ModuleDef;
use spacetimedb_testing::modules::{CompilationMode, CompiledModule};
// Wrap these tests in a `mod` whose name contains `csharp`
// so that we can run tests with `--skip csharp` in environments without dotnet installed.
mod ensure_same_schema_rust_csharp {
use serial_test::serial;
use spacetimedb_schema::auto_migrate::{ponder_auto_migrate, AutoMigrateStep};
use spacetimedb_schema::def::ModuleDef;
use spacetimedb_testing::modules::{CompilationMode, CompiledModule};

fn get_normalized_schema(module_name: &str) -> ModuleDef {
let module = CompiledModule::compile(module_name, CompilationMode::Debug);
module.extract_schema_blocking()
}
fn get_normalized_schema(module_name: &str) -> ModuleDef {
let module = CompiledModule::compile(module_name, CompilationMode::Debug);
module.extract_schema_blocking()
}

fn assert_identical_modules(module_name_prefix: &str) {
let rs = get_normalized_schema(module_name_prefix);
let cs = get_normalized_schema(&format!("{module_name_prefix}-cs"));
let mut diff = ponder_auto_migrate(&cs, &rs)
.expect("could not compute a diff between Rust and C#")
.steps;
fn assert_identical_modules(module_name_prefix: &str) {
let rs = get_normalized_schema(module_name_prefix);
let cs = get_normalized_schema(&format!("{module_name_prefix}-cs"));
let mut diff = ponder_auto_migrate(&cs, &rs)
.expect("could not compute a diff between Rust and C#")
.steps;

// In any migration plan, all `RowLevelSecurityDef`s are ALWAYS removed and
// re-added to ensure the core engine reinintializes the policies.
// This is slightly silly (and arguably should be hidden inside `core`),
// but for now, we just ignore these steps and manually compare the `RowLevelSecurityDef`s.
diff.retain(|step| {
!matches!(
step,
AutoMigrateStep::AddRowLevelSecurity(_) | AutoMigrateStep::RemoveRowLevelSecurity(_)
)
});
// In any migration plan, all `RowLevelSecurityDef`s are ALWAYS removed and
// re-added to ensure the core engine reinintializes the policies.
// This is slightly silly (and arguably should be hidden inside `core`),
// but for now, we just ignore these steps and manually compare the `RowLevelSecurityDef`s.
diff.retain(|step| {
!matches!(
step,
AutoMigrateStep::AddRowLevelSecurity(_) | AutoMigrateStep::RemoveRowLevelSecurity(_)
)
});

assert!(
diff.is_empty(),
"Rust and C# modules are not identical. Here are the steps to migrate from C# to Rust: {diff:#?}"
);
assert!(
diff.is_empty(),
"Rust and C# modules are not identical. Here are the steps to migrate from C# to Rust: {diff:#?}"
);

let mut rls_rs = rs.row_level_security().collect::<Vec<_>>();
rls_rs.sort();
let mut rls_cs = cs.row_level_security().collect::<Vec<_>>();
rls_cs.sort();
assert_eq!(
rls_rs, rls_cs,
"Rust and C# modules are not identical: different row level security policies"
)
}
let mut rls_rs = rs.row_level_security().collect::<Vec<_>>();
rls_rs.sort();
let mut rls_cs = cs.row_level_security().collect::<Vec<_>>();
rls_cs.sort();
assert_eq!(
rls_rs, rls_cs,
"Rust and C# modules are not identical: different row level security policies"
)
}

macro_rules! declare_tests {
($($name:ident => $path:literal,)*) => {
$(
#[test]
#[serial]
fn $name() {
assert_identical_modules($path);
}
)*
macro_rules! declare_tests {
($($name:ident => $path:literal,)*) => {
$(
#[test]
#[serial]
fn $name() {
assert_identical_modules($path);
}
)*
}
}
}

declare_tests! {
module_test => "module-test",
sdk_test_connect_disconnect => "sdk-test-connect-disconnect",
sdk_test => "sdk-test",
benchmarks => "benchmarks",
declare_tests! {
module_test => "module-test",
sdk_test_connect_disconnect => "sdk-test-connect-disconnect",
sdk_test => "sdk-test",
benchmarks => "benchmarks",
}
}
Loading