Skip to content

Commit a070f4b

Browse files
committed
Port smoketests to new api
1 parent 5800ed7 commit a070f4b

File tree

21 files changed

+200
-304
lines changed

21 files changed

+200
-304
lines changed

crates/bindings-macro/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,6 @@ fn table_impl(mut args: TableArgs, mut item: MutItem<syn::DeriveInput>) -> syn::
875875
let field_names = fields.iter().map(|f| f.ident.unwrap()).collect::<Vec<_>>();
876876
let field_types = fields.iter().map(|f| f.ty).collect::<Vec<_>>();
877877

878-
let table_name = &sats_ty.name;
879878
let tabletype_impl = quote! {
880879
impl spacetimedb::Table for #tablehandle_ident<'_> {
881880
type Row = #row_type;

crates/bindings/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,14 @@ macro_rules! __volatile_nonatomic_schedule_immediate_impl {
198198
([$($cur:tt)*] [$next:tt $($rest:tt)*]) => {
199199
$crate::__volatile_nonatomic_schedule_immediate_impl!([$($cur)* $next] [$($rest)*])
200200
};
201-
(@process_args $repeater:path, (_$(, $args:expr)* $(,)?)) => {
202-
$crate::__volatile_nonatomic_schedule_immediate_impl!(@call $repeater, ($crate::ReducerContext::__dummy(), $($args),*))
203-
};
204201
(@process_args $repeater:path, ($($args:expr),* $(,)?)) => {
205202
$crate::__volatile_nonatomic_schedule_immediate_impl!(@call $repeater, ($($args),*))
206203
};
207204
(@call $repeater:path, ($($args:expr),*)) => {
208205
if false {
209-
let _ = $repeater($($args,)*);
206+
let _ = $repeater(&$crate::ReducerContext::__dummy(), $($args,)*);
210207
} else {
211-
$crate::rt::volatile_nonatomic_schedule_immediate::<_, _, $repeater, _>($repeater, ($($args,)*))
208+
$crate::rt::volatile_nonatomic_schedule_immediate::<_, _, $repeater>($repeater, ($($args,)*))
212209
}
213210
};
214211
}

crates/bindings/src/rt.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ pub fn invoke_reducer<'a, A: Args<'a>>(
3636
/// A trait for types representing the *execution logic* of a reducer.
3737
pub trait Reducer<'de, A: Args<'de>> {
3838
fn invoke(&self, ctx: &ReducerContext, args: A) -> ReducerResult;
39-
40-
type ArgsWithContext;
41-
fn extract_args(args: Self::ArgsWithContext) -> A;
4239
}
4340

4441
/// A trait for types that can *describe* a reducer.
@@ -192,13 +189,6 @@ macro_rules! impl_reducer {
192189
let ($($T,)*) = args;
193190
self(ctx, $($T),*).into_result()
194191
}
195-
196-
type ArgsWithContext = (ReducerContext, $($T,)*);
197-
#[allow(non_snake_case, clippy::unused_unit)]
198-
fn extract_args(args: Self::ArgsWithContext) -> ($($T,)*) {
199-
let (_ctx, $($T,)*) = args;
200-
($($T,)*)
201-
}
202192
}
203193

204194
};
@@ -554,11 +544,10 @@ macro_rules! __make_register_reftype {
554544

555545
#[cfg(feature = "unstable_abi")]
556546
#[doc(hidden)]
557-
pub fn volatile_nonatomic_schedule_immediate<'de, A: Args<'de>, R: Reducer<'de, A, T>, R2: ReducerInfo, T>(
547+
pub fn volatile_nonatomic_schedule_immediate<'de, A: Args<'de>, R: Reducer<'de, A>, R2: ReducerInfo>(
558548
_reducer: R,
559-
args: R::ArgsWithContext,
549+
args: A,
560550
) {
561-
let args = R::extract_args(args);
562551
let arg_bytes = bsatn::to_vec(&SerDeArgs(args)).unwrap();
563552

564553
// Schedule the reducer.

crates/cli/src/subcommands/project/rust/lib._rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
use spacetimedb::ReducerContext;
1+
use spacetimedb::{ReducerContext, Table};
22

3-
#[spacetimedb::table(name = people)]
3+
#[spacetimedb::table(name = person)]
44
pub struct Person {
55
name: String
66
}
77

88
#[spacetimedb::reducer(init)]
9-
pub fn init() {
9+
pub fn init(_ctx: &ReducerContext) {
1010
// Called when the module is initially published
1111
}
1212

1313
#[spacetimedb::reducer(client_connected)]
14-
pub fn identity_connected(_ctx: ReducerContext) {
14+
pub fn identity_connected(_ctx: &ReducerContext) {
1515
// Called everytime a new client connects
1616
}
1717

1818
#[spacetimedb::reducer(client_disconnected)]
19-
pub fn identity_disconnected(_ctx: ReducerContext) {
19+
pub fn identity_disconnected(_ctx: &ReducerContext) {
2020
// Called everytime a client disconnects
2121
}
2222

2323
#[spacetimedb::reducer]
24-
pub fn add(name: String) {
25-
Person::insert(Person { name });
24+
pub fn add(ctx: &ReducerContext, name: String) {
25+
ctx.db.person().insert(Person { name });
2626
}
2727

2828
#[spacetimedb::reducer]
29-
pub fn say_hello() {
30-
for person in Person::iter() {
29+
pub fn say_hello(ctx: &ReducerContext) {
30+
for person in ctx.db.person().iter() {
3131
log::info!("Hello, {}!", person.name);
3232
}
3333
log::info!("Hello, World!");

modules/rust-wasm-test/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use spacetimedb::{duration, table, Address, Deserialize, Identity, ReducerContex
55

66
#[spacetimedb::table(name = test_a, index(name = foo, btree(columns = [x])))]
77
pub struct TestA {
8-
#[index(btree)]
98
pub x: u32,
109
pub y: u32,
1110
pub z: String,

smoketests/tests/add_remove_index.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,44 @@ class AddRemoveIndex(Smoketest):
44
AUTOPUBLISH = False
55

66
MODULE_CODE = """
7+
use spacetimedb::{ReducerContext, Table};
8+
79
#[spacetimedb::table(name = t1)]
810
pub struct T1 { id: u64 }
911
1012
#[spacetimedb::table(name = t2)]
1113
pub struct T2 { id: u64 }
1214
1315
#[spacetimedb::reducer(init)]
14-
pub fn init() {
16+
pub fn init(ctx: &ReducerContext) {
1517
for id in 0..1_000 {
16-
T1::insert(T1 { id });
17-
T2::insert(T2 { id });
18+
ctx.db.t1().insert(T1 { id });
19+
ctx.db.t2().insert(T2 { id });
1820
}
1921
}
2022
"""
2123
MODULE_CODE_INDEXED = """
24+
use spacetimedb::{ReducerContext, Table};
25+
2226
#[spacetimedb::table(name = t1)]
2327
pub struct T1 { #[index(btree)] id: u64 }
2428
2529
#[spacetimedb::table(name = t2)]
2630
pub struct T2 { #[index(btree)] id: u64 }
2731
2832
#[spacetimedb::reducer(init)]
29-
pub fn init() {
33+
pub fn init(ctx: &ReducerContext) {
3034
for id in 0..1_000 {
31-
T1::insert(T1 { id });
32-
T2::insert(T2 { id });
35+
ctx.db.t1().insert(T1 { id });
36+
ctx.db.t2().insert(T2 { id });
3337
}
3438
}
3539
3640
#[spacetimedb::reducer]
37-
pub fn add() {
41+
pub fn add(ctx: &ReducerContext) {
3842
let id = 1_001;
39-
T1::insert(T1 { id });
40-
T2::insert(T2 { id });
43+
ctx.db.t1().insert(T1 { id });
44+
ctx.db.t2().insert(T2 { id });
4145
}
4246
"""
4347

smoketests/tests/add_table_pseudomigration.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66
class AddTablePseudomigration(Smoketest):
77
MODULE_CODE = """
8-
use spacetimedb::println;
8+
use spacetimedb::{println, ReducerContext, Table};
99
1010
#[spacetimedb::table(name = person)]
1111
pub struct Person {
1212
name: String,
1313
}
1414
1515
#[spacetimedb::reducer]
16-
pub fn add_person(name: String) {
17-
Person::insert(Person { name });
16+
pub fn add_person(ctx: &ReducerContext, name: String) {
17+
ctx.db.person().insert(Person { name });
1818
}
1919
2020
#[spacetimedb::reducer]
21-
pub fn print_persons(prefix: String) {
22-
for person in Person::iter() {
21+
pub fn print_persons(ctx: &ReducerContext, prefix: String) {
22+
for person in ctx.db.person().iter() {
2323
println!("{}: {}", prefix, person.name);
2424
}
2525
}
@@ -34,13 +34,13 @@ class AddTablePseudomigration(Smoketest):
3434
}
3535
3636
#[spacetimedb::reducer]
37-
pub fn add_book(isbn: String) {
38-
Book::insert(Book { isbn });
37+
pub fn add_book(ctx: &ReducerContext, isbn: String) {
38+
ctx.db.book().insert(Book { isbn });
3939
}
4040
4141
#[spacetimedb::reducer]
42-
pub fn print_books(prefix: String) {
43-
for book in Book::iter() {
42+
pub fn print_books(ctx: &ReducerContext, prefix: String) {
43+
for book in ctx.db.book().iter() {
4444
println!("{}: {}", prefix, book.isbn);
4545
}
4646
}
@@ -87,28 +87,28 @@ def test_add_table_pseudomigration(self):
8787

8888
class RejectTableChanges(Smoketest):
8989
MODULE_CODE = """
90-
use spacetimedb::println;
90+
use spacetimedb::{println, ReducerContext, Table};
9191
9292
#[spacetimedb::table(name = person)]
9393
pub struct Person {
9494
name: String,
9595
}
9696
9797
#[spacetimedb::reducer]
98-
pub fn add_person(name: String) {
99-
Person::insert(Person { name });
98+
pub fn add_person(ctx: &ReducerContext, name: String) {
99+
ctx.db.person().insert(Person { name });
100100
}
101101
102102
#[spacetimedb::reducer]
103-
pub fn print_persons(prefix: String) {
104-
for person in Person::iter() {
103+
pub fn print_persons(ctx: &ReducerContext, prefix: String) {
104+
for person in ctx.db.person().iter() {
105105
println!("{}: {}", prefix, person.name);
106106
}
107107
}
108108
"""
109109

110110
MODULE_CODE_UPDATED = """
111-
use spacetimedb::println;
111+
use spacetimedb::{println, ReducerContext, Table};
112112
113113
#[spacetimedb::table(name = person)]
114114
pub struct Person {
@@ -117,13 +117,13 @@ class RejectTableChanges(Smoketest):
117117
}
118118
119119
#[spacetimedb::reducer]
120-
pub fn add_person(name: String) {
121-
Person::insert(Person { name, age: 70 });
120+
pub fn add_person(ctx: &ReducerContext, name: String) {
121+
ctx.db.person().insert(Person { name, age: 70 });
122122
}
123123
124124
#[spacetimedb::reducer]
125-
pub fn print_persons(prefix: String) {
126-
for person in Person::iter() {
125+
pub fn print_persons(ctx: &ReducerContext, prefix: String) {
126+
for person in ctx.db.person().iter() {
127127
println!("{}: {}", prefix, person.name);
128128
}
129129
}

smoketests/tests/auto_inc.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class IntTests:
2222
}
2323
2424
#[spacetimedb::reducer]
25-
pub fn add_$KEY_TY(name: String, expected_value: $KEY_TY) {
26-
let value = Person_$KEY_TY::insert(Person_$KEY_TY { key_col: 0, name });
25+
pub fn add_$KEY_TY(ctx: &ReducerContext, name: String, expected_value: $KEY_TY) {
26+
let value = ctx.db.person_$KEY_TY().insert(Person_$KEY_TY { key_col: 0, name });
2727
assert_eq!(value.key_col, expected_value);
2828
}
2929
3030
#[spacetimedb::reducer]
31-
pub fn say_hello_$KEY_TY() {
32-
for person in Person_$KEY_TY::iter() {
31+
pub fn say_hello_$KEY_TY(ctx: &ReducerContext) {
32+
for person in ctx.db.person_$KEY_TY().iter() {
3333
println!("Hello, {}:{}!", person.key_col, person.name);
3434
}
3535
println!("Hello, World!");
@@ -43,7 +43,7 @@ class AutoincBasic(IntTests, Smoketest):
4343

4444
MODULE_CODE = f"""
4545
#![allow(non_camel_case_types)]
46-
use spacetimedb::println;
46+
use spacetimedb::{{println, ReducerContext, Table}};
4747
{"".join(autoinc1_template.substitute(KEY_TY=int_ty) for int_ty in ints)}
4848
"""
4949

@@ -71,21 +71,21 @@ def do_test_autoinc(self, int_ty):
7171
}
7272
7373
#[spacetimedb::reducer]
74-
pub fn add_new_$KEY_TY(name: String) -> Result<(), Box<dyn Error>> {
75-
let value = Person_$KEY_TY::insert(Person_$KEY_TY { key_col: 0, name })?;
74+
pub fn add_new_$KEY_TY(ctx: &ReducerContext, name: String) -> Result<(), Box<dyn Error>> {
75+
let value = ctx.db.person_$KEY_TY().insert(Person_$KEY_TY { key_col: 0, name });
7676
println!("Assigned Value: {} -> {}", value.key_col, value.name);
7777
Ok(())
7878
}
7979
8080
#[spacetimedb::reducer]
81-
pub fn update_$KEY_TY(name: String, new_id: $KEY_TY) {
82-
Person_$KEY_TY::delete_by_name(&name);
83-
let _value = Person_$KEY_TY::insert(Person_$KEY_TY { key_col: new_id, name });
81+
pub fn update_$KEY_TY(ctx: &ReducerContext, name: String, new_id: $KEY_TY) {
82+
ctx.db.person_$KEY_TY().name().delete(&name);
83+
let _value = ctx.db.person_$KEY_TY().insert(Person_$KEY_TY { key_col: new_id, name });
8484
}
8585
8686
#[spacetimedb::reducer]
87-
pub fn say_hello_$KEY_TY() {
88-
for person in Person_$KEY_TY::iter() {
87+
pub fn say_hello_$KEY_TY(ctx: &ReducerContext) {
88+
for person in ctx.db.person_$KEY_TY().iter() {
8989
println!("Hello, {}:{}!", person.key_col, person.name);
9090
}
9191
println!("Hello, World!");
@@ -99,7 +99,7 @@ class AutoincUnique(IntTests, Smoketest):
9999
MODULE_CODE = f"""
100100
#![allow(non_camel_case_types)]
101101
use std::error::Error;
102-
use spacetimedb::println;
102+
use spacetimedb::{{println, ReducerContext, Table}};
103103
{"".join(autoinc2_template.substitute(KEY_TY=int_ty) for int_ty in ints)}
104104
"""
105105

smoketests/tests/connect_disconnect_from_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ class ConnDisconnFromCli(Smoketest):
66
use spacetimedb::{println, ReducerContext};
77
88
#[spacetimedb::reducer(client_connected)]
9-
pub fn connected(_ctx: ReducerContext) {
9+
pub fn connected(_ctx: &ReducerContext) {
1010
println!("_connect called");
1111
panic!("Panic on connect");
1212
}
1313
1414
#[spacetimedb::reducer(client_disconnected)]
15-
pub fn disconnected(_ctx: ReducerContext) {
15+
pub fn disconnected(_ctx: &ReducerContext) {
1616
println!("disconnect called");
1717
panic!("Panic on disconnect");
1818
}
1919
2020
#[spacetimedb::reducer]
21-
pub fn say_hello() {
21+
pub fn say_hello(ctx: &ReducerContext) {
2222
println!("Hello, World!");
2323
}
2424
"""

smoketests/tests/describe.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
class ModuleDescription(Smoketest):
44
MODULE_CODE = """
5-
use spacetimedb::println;
5+
use spacetimedb::{println, ReducerContext, Table};
66
77
#[spacetimedb::table(name = person)]
88
pub struct Person {
99
name: String,
1010
}
1111
1212
#[spacetimedb::reducer]
13-
pub fn add(name: String) {
14-
Person::insert(Person { name });
13+
pub fn add(ctx: &ReducerContext, name: String) {
14+
ctx.db.person().insert(Person { name });
1515
}
1616
1717
#[spacetimedb::reducer]
18-
pub fn say_hello() {
19-
for person in Person::iter() {
18+
pub fn say_hello(ctx: &ReducerContext) {
19+
for person in ctx.db.person().iter() {
2020
println!("Hello, {}!", person.name);
2121
}
2222
println!("Hello, World!");

0 commit comments

Comments
 (0)