Skip to content

Commit

Permalink
Add regression test for context Identity & Address
Browse files Browse the repository at this point in the history
These reducers were there in the SDK tests already, but were never called or used.
  • Loading branch information
RReverser authored and Centril committed Sep 4, 2024
1 parent e75d06e commit b7322d1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 15 deletions.
64 changes: 63 additions & 1 deletion crates/sdk/tests/test-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use module_bindings::*;
use test_counter::TestCounter;

mod simple_test_table;
use simple_test_table::insert_one;
use simple_test_table::{insert_one, on_insert_one};

mod pk_test_table;
use pk_test_table::insert_update_delete_one;
Expand Down Expand Up @@ -64,10 +64,12 @@ fn main() {
"update_primitive" => exec_update_primitive(),

"insert_identity" => exec_insert_identity(),
"insert_caller_identity" => exec_insert_caller_identity(),
"delete_identity" => exec_delete_identity(),
"update_identity" => exec_update_identity(),

"insert_address" => exec_insert_address(),
"insert_caller_address" => exec_insert_caller_address(),
"delete_address" => exec_delete_address(),
"update_address" => exec_update_address(),

Expand Down Expand Up @@ -493,6 +495,36 @@ fn exec_insert_identity() {
test_counter.wait_for_all();
}

/// This tests that we can retrieve and use the caller's `Identity` from the reducer context.
fn exec_insert_caller_identity() {
let test_counter = TestCounter::new();
let name = db_name_or_panic();

let conn_result = test_counter.add_test("connect");

let sub_result = test_counter.add_test("subscribe");

let sub_applied_nothing_result = test_counter.add_test("on_subscription_applied_nothing");

{
let test_counter = test_counter.clone();
once_on_subscription_applied(move || {
on_insert_one::<OneIdentity>(&test_counter, identity().unwrap(), |event| {
matches!(event, ReducerEvent::InsertCallerOneIdentity(_))
});
insert_caller_one_identity();

sub_applied_nothing_result(assert_all_tables_empty());
});
}

once_on_connect(move |_, _| sub_result(subscribe(SUBSCRIBE_ALL)));

conn_result(connect(LOCALHOST, &name, None));

test_counter.wait_for_all();
}

/// This test doesn't add much alongside `exec_insert_identity` and `exec_delete_primitive`,
/// but it's here for symmetry.
fn exec_delete_identity() {
Expand Down Expand Up @@ -580,6 +612,36 @@ fn exec_insert_address() {
test_counter.wait_for_all();
}

/// This tests that we can serialize and deserialize `Address` in various contexts.
fn exec_insert_caller_address() {
let test_counter = TestCounter::new();
let name = db_name_or_panic();

let conn_result = test_counter.add_test("connect");

let sub_result = test_counter.add_test("subscribe");

let sub_applied_nothing_result = test_counter.add_test("on_subscription_applied_nothing");

{
let test_counter = test_counter.clone();
once_on_subscription_applied(move || {
on_insert_one::<OneAddress>(&test_counter, address().unwrap(), |event| {
matches!(event, ReducerEvent::InsertCallerOneAddress(_))
});
insert_caller_one_address();

sub_applied_nothing_result(assert_all_tables_empty());
});
}

once_on_connect(move |_, _| sub_result(subscribe(SUBSCRIBE_ALL)));

conn_result(connect(LOCALHOST, &name, None));

test_counter.wait_for_all();
}

/// This test doesn't add much alongside `exec_insert_address` and `exec_delete_primitive`,
/// but it's here for symmetry.
fn exec_delete_address() {
Expand Down
42 changes: 28 additions & 14 deletions crates/sdk/tests/test-client/src/simple_test_table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::module_bindings::*;
use anyhow::anyhow;
use anyhow::Context;
use spacetimedb_sdk::{
identity::Identity,
sats::{i256, u256},
Expand Down Expand Up @@ -391,25 +391,39 @@ impl_simple_test_table! {
}
}

pub fn insert_one<T: SimpleTestTable>(test_counter: &Arc<TestCounter>, value: T::Contents) {
let mut result = Some(test_counter.add_test(format!("insert-{}", T::TABLE_NAME)));
let value_dup = value.clone();
pub fn on_insert_one<T: SimpleTestTable>(
test_counter: &Arc<TestCounter>,
value: T::Contents,
is_expected_variant: impl Fn(&T::ReducerEvent) -> bool + Send + 'static,
) where
T::ReducerEvent: std::fmt::Debug,
{
let mut set_result = Some(test_counter.add_test(format!("insert-{}", T::TABLE_NAME)));

T::on_insert(move |row, reducer_event| {
if result.is_some() {
if let Some(set_result) = set_result.take() {
let run_checks = || {
if row.as_contents() != &value_dup {
anyhow::bail!("Unexpected row value. Expected {:?} but found {:?}", value_dup, row);
}
reducer_event
.ok_or(anyhow!("Expected a reducer event, but found None."))
.map(T::is_insert_reducer_event)
.and_then(|is_good| is_good.then_some(()).ok_or(anyhow!("Unexpected ReducerEvent variant.")))?;

anyhow::ensure!(
*row.as_contents() == value,
"Unexpected row value. Expected {value:?} but found {row:?}"
);
let reducer_event = reducer_event.context("Expected a reducer event, but found None")?;
anyhow::ensure!(
is_expected_variant(reducer_event),
"Unexpected ReducerEvent variant {reducer_event:?}."
);
Ok(())
};
(result.take().unwrap())(run_checks());

set_result(run_checks())
}
});
}

pub fn insert_one<T: SimpleTestTable>(test_counter: &Arc<TestCounter>, value: T::Contents)
where
T::ReducerEvent: std::fmt::Debug,
{
on_insert_one::<T>(test_counter, value.clone(), T::is_insert_reducer_event);
T::insert(value);
}
10 changes: 10 additions & 0 deletions crates/sdk/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ macro_rules! declare_tests_with_suffix {
make_test("insert_identity").run();
}

#[test]
fn insert_caller_identity() {
make_test("insert_caller_identity").run();
}

#[test]
fn delete_identity() {
make_test("delete_identity").run();
Expand All @@ -53,6 +58,11 @@ macro_rules! declare_tests_with_suffix {
make_test("insert_address").run();
}

#[test]
fn insert_caller_address() {
make_test("insert_caller_address").run();
}

#[test]
fn delete_address() {
make_test("delete_address").run();
Expand Down

0 comments on commit b7322d1

Please sign in to comment.