|
1 | 1 | mod module_bindings; |
2 | 2 |
|
3 | | -use module_bindings::*; |
4 | | - |
5 | 3 | use anyhow::Context; |
6 | | -use spacetimedb_sdk::DbConnectionBuilder; |
| 4 | +use module_bindings::*; |
| 5 | +use spacetimedb_sdk::{DbConnectionBuilder, DbContext, Table}; |
7 | 6 | use test_counter::TestCounter; |
8 | 7 |
|
9 | 8 | const LOCALHOST: &str = "http://localhost:3000"; |
@@ -39,10 +38,33 @@ fn main() { |
39 | 38 | match &*test { |
40 | 39 | "procedure-return-values" => exec_procedure_return_values(), |
41 | 40 | "procedure-observe-panic" => exec_procedure_panic(), |
| 41 | + "insert-with-tx-commit" => exec_insert_with_tx_commit(), |
| 42 | + "insert-with-tx-rollback" => exec_insert_with_tx_rollback(), |
42 | 43 | _ => panic!("Unknown test: {test}"), |
43 | 44 | } |
44 | 45 | } |
45 | 46 |
|
| 47 | +fn assert_table_empty<T: Table>(tbl: T) -> anyhow::Result<()> { |
| 48 | + let count = tbl.count(); |
| 49 | + if count != 0 { |
| 50 | + anyhow::bail!( |
| 51 | + "Expected table {} to be empty, but found {} rows resident", |
| 52 | + std::any::type_name::<T::Row>(), |
| 53 | + count, |
| 54 | + ) |
| 55 | + } |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | + |
| 59 | +/// Each subscribing test runs against a fresh DB, |
| 60 | +/// so all tables should be empty until we call an insert reducer. |
| 61 | +/// |
| 62 | +/// We'll call this function within our initial `on_subscription_applied` callback to verify that. |
| 63 | +fn assert_all_tables_empty(ctx: &impl RemoteDbContext) -> anyhow::Result<()> { |
| 64 | + assert_table_empty(ctx.db().my_table())?; |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
46 | 68 | fn connect_with_then( |
47 | 69 | test_counter: &std::sync::Arc<TestCounter>, |
48 | 70 | on_connect_suffix: &str, |
@@ -71,6 +93,24 @@ fn connect_then( |
71 | 93 | connect_with_then(test_counter, "", |x| x, callback) |
72 | 94 | } |
73 | 95 |
|
| 96 | +/// A query that subscribes to all rows from all tables. |
| 97 | +const SUBSCRIBE_ALL: &[&str] = &["SELECT * FROM my_table;"]; |
| 98 | + |
| 99 | +fn subscribe_all_then(ctx: &impl RemoteDbContext, callback: impl FnOnce(&SubscriptionEventContext) + Send + 'static) { |
| 100 | + subscribe_these_then(ctx, SUBSCRIBE_ALL, callback) |
| 101 | +} |
| 102 | + |
| 103 | +fn subscribe_these_then( |
| 104 | + ctx: &impl RemoteDbContext, |
| 105 | + queries: &[&str], |
| 106 | + callback: impl FnOnce(&SubscriptionEventContext) + Send + 'static, |
| 107 | +) { |
| 108 | + ctx.subscription_builder() |
| 109 | + .on_applied(callback) |
| 110 | + .on_error(|_ctx, error| panic!("Subscription errored: {error:?}")) |
| 111 | + .subscribe(queries); |
| 112 | +} |
| 113 | + |
74 | 114 | fn exec_procedure_return_values() { |
75 | 115 | let test_counter = TestCounter::new(); |
76 | 116 |
|
@@ -142,3 +182,65 @@ fn exec_procedure_panic() { |
142 | 182 |
|
143 | 183 | test_counter.wait_for_all(); |
144 | 184 | } |
| 185 | + |
| 186 | +fn exec_insert_with_tx_commit() { |
| 187 | + fn expected() -> ReturnStruct { |
| 188 | + ReturnStruct { |
| 189 | + a: 42, |
| 190 | + b: "magic".into(), |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + let test_counter = TestCounter::new(); |
| 195 | + let sub_applied_nothing_result = test_counter.add_test("on_subscription_applied_nothing"); |
| 196 | + let inspect_result = test_counter.add_test("insert_with_tx_commit_values"); |
| 197 | + let mut callback_result = Some(test_counter.add_test("insert_with_tx_commit_callback")); |
| 198 | + |
| 199 | + connect_then(&test_counter, { |
| 200 | + move |ctx| { |
| 201 | + ctx.db().my_table().on_insert(move |_, row| { |
| 202 | + assert_eq!(row.field, expected()); |
| 203 | + (callback_result.take().unwrap())(Ok(())); |
| 204 | + }); |
| 205 | + |
| 206 | + subscribe_all_then(ctx, move |ctx| { |
| 207 | + sub_applied_nothing_result(assert_all_tables_empty(ctx)); |
| 208 | + |
| 209 | + ctx.procedures.insert_with_tx_commit_then(move |ctx, res| { |
| 210 | + assert!(res.is_ok()); |
| 211 | + let row = ctx.db().my_table().iter().next().unwrap(); |
| 212 | + assert_eq!(row.field, expected()); |
| 213 | + inspect_result(Ok(())); |
| 214 | + }); |
| 215 | + }); |
| 216 | + } |
| 217 | + }); |
| 218 | + |
| 219 | + test_counter.wait_for_all(); |
| 220 | +} |
| 221 | + |
| 222 | +fn exec_insert_with_tx_rollback() { |
| 223 | + let test_counter = TestCounter::new(); |
| 224 | + let sub_applied_nothing_result = test_counter.add_test("on_subscription_applied_nothing"); |
| 225 | + let inspect_result = test_counter.add_test("insert_with_tx_rollback_values"); |
| 226 | + |
| 227 | + connect_then(&test_counter, { |
| 228 | + move |ctx| { |
| 229 | + ctx.db() |
| 230 | + .my_table() |
| 231 | + .on_insert(|_, _| unreachable!("should not have inserted a row")); |
| 232 | + |
| 233 | + subscribe_all_then(ctx, move |ctx| { |
| 234 | + sub_applied_nothing_result(assert_all_tables_empty(ctx)); |
| 235 | + |
| 236 | + ctx.procedures.insert_with_tx_rollback_then(move |ctx, res| { |
| 237 | + assert!(res.is_ok()); |
| 238 | + assert_eq!(ctx.db().my_table().iter().next(), None); |
| 239 | + inspect_result(Ok(())); |
| 240 | + }); |
| 241 | + }); |
| 242 | + } |
| 243 | + }); |
| 244 | + |
| 245 | + test_counter.wait_for_all(); |
| 246 | +} |
0 commit comments