Skip to content

Commit

Permalink
bindings-csharp: pass address_1 (#1621)
Browse files Browse the repository at this point in the history
Signed-off-by: Mazdak Farrokhzad <[email protected]>
Co-authored-by: Ingvar Stepanyan <[email protected]>
  • Loading branch information
Centril and RReverser authored Sep 4, 2024
1 parent dd65d6d commit 69e881c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 17 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ SpacetimeDB.Internal.BytesSink error
sender_2,
sender_3,
address_0,
address_0,
address_1,
timestamp,
args,
error
Expand Down
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

2 comments on commit 69e881c

@github-actions
Copy link

@github-actions github-actions bot commented on 69e881c Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

@github-actions github-actions bot commented on 69e881c Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

Please sign in to comment.