Skip to content

Commit 69e881c

Browse files
CentrilRReverser
andauthored
bindings-csharp: pass address_1 (#1621)
Signed-off-by: Mazdak Farrokhzad <twingoow@gmail.com> Co-authored-by: Ingvar Stepanyan <me@rreverser.com>
1 parent dd65d6d commit 69e881c

File tree

5 files changed

+103
-17
lines changed

5 files changed

+103
-17
lines changed

crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-csharp/Codegen/Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ SpacetimeDB.Internal.BytesSink error
463463
sender_2,
464464
sender_3,
465465
address_0,
466-
address_0,
466+
address_1,
467467
timestamp,
468468
args,
469469
error

crates/sdk/tests/test-client/src/main.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use module_bindings::*;
1717
use test_counter::TestCounter;
1818

1919
mod simple_test_table;
20-
use simple_test_table::insert_one;
20+
use simple_test_table::{insert_one, on_insert_one};
2121

2222
mod pk_test_table;
2323
use pk_test_table::insert_update_delete_one;
@@ -64,10 +64,12 @@ fn main() {
6464
"update_primitive" => exec_update_primitive(),
6565

6666
"insert_identity" => exec_insert_identity(),
67+
"insert_caller_identity" => exec_insert_caller_identity(),
6768
"delete_identity" => exec_delete_identity(),
6869
"update_identity" => exec_update_identity(),
6970

7071
"insert_address" => exec_insert_address(),
72+
"insert_caller_address" => exec_insert_caller_address(),
7173
"delete_address" => exec_delete_address(),
7274
"update_address" => exec_update_address(),
7375

@@ -493,6 +495,36 @@ fn exec_insert_identity() {
493495
test_counter.wait_for_all();
494496
}
495497

498+
/// This tests that we can retrieve and use the caller's `Identity` from the reducer context.
499+
fn exec_insert_caller_identity() {
500+
let test_counter = TestCounter::new();
501+
let name = db_name_or_panic();
502+
503+
let conn_result = test_counter.add_test("connect");
504+
505+
let sub_result = test_counter.add_test("subscribe");
506+
507+
let sub_applied_nothing_result = test_counter.add_test("on_subscription_applied_nothing");
508+
509+
{
510+
let test_counter = test_counter.clone();
511+
once_on_subscription_applied(move || {
512+
on_insert_one::<OneIdentity>(&test_counter, identity().unwrap(), |event| {
513+
matches!(event, ReducerEvent::InsertCallerOneIdentity(_))
514+
});
515+
insert_caller_one_identity();
516+
517+
sub_applied_nothing_result(assert_all_tables_empty());
518+
});
519+
}
520+
521+
once_on_connect(move |_, _| sub_result(subscribe(SUBSCRIBE_ALL)));
522+
523+
conn_result(connect(LOCALHOST, &name, None));
524+
525+
test_counter.wait_for_all();
526+
}
527+
496528
/// This test doesn't add much alongside `exec_insert_identity` and `exec_delete_primitive`,
497529
/// but it's here for symmetry.
498530
fn exec_delete_identity() {
@@ -580,6 +612,36 @@ fn exec_insert_address() {
580612
test_counter.wait_for_all();
581613
}
582614

615+
/// This tests that we can serialize and deserialize `Address` in various contexts.
616+
fn exec_insert_caller_address() {
617+
let test_counter = TestCounter::new();
618+
let name = db_name_or_panic();
619+
620+
let conn_result = test_counter.add_test("connect");
621+
622+
let sub_result = test_counter.add_test("subscribe");
623+
624+
let sub_applied_nothing_result = test_counter.add_test("on_subscription_applied_nothing");
625+
626+
{
627+
let test_counter = test_counter.clone();
628+
once_on_subscription_applied(move || {
629+
on_insert_one::<OneAddress>(&test_counter, address().unwrap(), |event| {
630+
matches!(event, ReducerEvent::InsertCallerOneAddress(_))
631+
});
632+
insert_caller_one_address();
633+
634+
sub_applied_nothing_result(assert_all_tables_empty());
635+
});
636+
}
637+
638+
once_on_connect(move |_, _| sub_result(subscribe(SUBSCRIBE_ALL)));
639+
640+
conn_result(connect(LOCALHOST, &name, None));
641+
642+
test_counter.wait_for_all();
643+
}
644+
583645
/// This test doesn't add much alongside `exec_insert_address` and `exec_delete_primitive`,
584646
/// but it's here for symmetry.
585647
fn exec_delete_address() {

crates/sdk/tests/test-client/src/simple_test_table.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::module_bindings::*;
2-
use anyhow::anyhow;
2+
use anyhow::Context;
33
use spacetimedb_sdk::{
44
identity::Identity,
55
sats::{i256, u256},
@@ -391,25 +391,39 @@ impl_simple_test_table! {
391391
}
392392
}
393393

394-
pub fn insert_one<T: SimpleTestTable>(test_counter: &Arc<TestCounter>, value: T::Contents) {
395-
let mut result = Some(test_counter.add_test(format!("insert-{}", T::TABLE_NAME)));
396-
let value_dup = value.clone();
394+
pub fn on_insert_one<T: SimpleTestTable>(
395+
test_counter: &Arc<TestCounter>,
396+
value: T::Contents,
397+
is_expected_variant: impl Fn(&T::ReducerEvent) -> bool + Send + 'static,
398+
) where
399+
T::ReducerEvent: std::fmt::Debug,
400+
{
401+
let mut set_result = Some(test_counter.add_test(format!("insert-{}", T::TABLE_NAME)));
402+
397403
T::on_insert(move |row, reducer_event| {
398-
if result.is_some() {
404+
if let Some(set_result) = set_result.take() {
399405
let run_checks = || {
400-
if row.as_contents() != &value_dup {
401-
anyhow::bail!("Unexpected row value. Expected {:?} but found {:?}", value_dup, row);
402-
}
403-
reducer_event
404-
.ok_or(anyhow!("Expected a reducer event, but found None."))
405-
.map(T::is_insert_reducer_event)
406-
.and_then(|is_good| is_good.then_some(()).ok_or(anyhow!("Unexpected ReducerEvent variant.")))?;
407-
406+
anyhow::ensure!(
407+
*row.as_contents() == value,
408+
"Unexpected row value. Expected {value:?} but found {row:?}"
409+
);
410+
let reducer_event = reducer_event.context("Expected a reducer event, but found None")?;
411+
anyhow::ensure!(
412+
is_expected_variant(reducer_event),
413+
"Unexpected ReducerEvent variant {reducer_event:?}."
414+
);
408415
Ok(())
409416
};
410-
(result.take().unwrap())(run_checks());
417+
418+
set_result(run_checks())
411419
}
412420
});
421+
}
413422

423+
pub fn insert_one<T: SimpleTestTable>(test_counter: &Arc<TestCounter>, value: T::Contents)
424+
where
425+
T::ReducerEvent: std::fmt::Debug,
426+
{
427+
on_insert_one::<T>(test_counter, value.clone(), T::is_insert_reducer_event);
414428
T::insert(value);
415429
}

crates/sdk/tests/test.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ macro_rules! declare_tests_with_suffix {
3838
make_test("insert_identity").run();
3939
}
4040

41+
#[test]
42+
fn insert_caller_identity() {
43+
make_test("insert_caller_identity").run();
44+
}
45+
4146
#[test]
4247
fn delete_identity() {
4348
make_test("delete_identity").run();
@@ -53,6 +58,11 @@ macro_rules! declare_tests_with_suffix {
5358
make_test("insert_address").run();
5459
}
5560

61+
#[test]
62+
fn insert_caller_address() {
63+
make_test("insert_caller_address").run();
64+
}
65+
5666
#[test]
5767
fn delete_address() {
5868
make_test("delete_address").run();

0 commit comments

Comments
 (0)