Skip to content

Commit 108f0e6

Browse files
committed
test
1 parent 508be98 commit 108f0e6

3 files changed

Lines changed: 56 additions & 98 deletions

File tree

crates/schema/tests/ensure_same_schema.rs

Lines changed: 39 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,25 @@ fn ensure_same_schema_rust_csharp_benchmarks() {
103103
fn test_case_converted_names() {
104104
let module_def: ModuleDef = get_normalized_schema("module-test");
105105

106-
// println!("Types {:?}", module_def.types().collect::<Vec<_>>());
106+
// println!("Types {:?}", module_def.lookup::<TableDef>::(Identifier::for_test("person")).unwrap().columns().collect::<Vec<_>>());
107107

108108
// println!("Types space {:?}", module_def.typespace());
109109

110110
// Test Tables
111111
let table_names = [
112-
// Accessor is CamelCase in modules.
113-
"test_a_table",
114-
// Uses explicit canonical name
115-
"Person",
112+
// canonical name, accessor name
113+
("test_a", "TestATable"),
116114
];
117-
for name in table_names {
118-
assert!(
119-
TableDef::lookup(&module_def, &Identifier::for_test(name)).is_some(),
120-
"Table '{}' not found",
121-
name
122-
);
115+
for (name, accessor) in table_names {
116+
let def = TableDef::lookup(&module_def, &Identifier::for_test(name));
117+
118+
assert!(def.is_some(), "Table '{}' not found", name);
119+
120+
assert_eq!(&*def.unwrap().accessor_name, &*accessor, "Table '{}' not found", name);
123121
}
124122

125123
// Test Reducers
126-
let reducer_names = [];
124+
let reducer_names = ["list_over_age", "repeating_test"];
127125
for name in reducer_names {
128126
assert!(
129127
ReducerDef::lookup(&module_def, &ReducerName::for_test(name)).is_some(),
@@ -133,7 +131,7 @@ fn test_case_converted_names() {
133131
}
134132

135133
// Test Procedures
136-
let procedure_names = ["get_my_schema_via_http"];
134+
let procedure_names = ["get_my_test_via_http"];
137135
for name in procedure_names {
138136
assert!(
139137
ProcedureDef::lookup(&module_def, &Identifier::for_test(name)).is_some(),
@@ -142,49 +140,34 @@ fn test_case_converted_names() {
142140
);
143141
}
144142

145-
// Test Views
146-
// let view_names = ["my_player"];
147-
// for name in view_names {
148-
// assert!(
149-
// ViewDef::lookup(&module_def, &Identifier::for_test(name)).is_some(),
150-
// "View '{}' not found",
151-
// name
152-
// );
153-
// }
143+
// Test Views
144+
let view_names = ["my_player"];
145+
for name in view_names {
146+
assert!(
147+
ViewDef::lookup(&module_def, &Identifier::for_test(name)).is_some(),
148+
"View '{}' not found",
149+
name
150+
);
151+
}
154152

155153
// Test Types
156-
// let type_names = [
157-
// "PkMultiIdentity",
158-
// "PrivateTable",
159-
// "Baz",
160-
// "TestA",
161-
// "TestFoobar",
162-
// "TestE",
163-
// "RemoveTable",
164-
// "Foobar",
165-
// "Player",
166-
// "RepeatingTestArg",
167-
// "Person",
168-
// "Point",
169-
// "TestB",
170-
// "HasSpecialStuff",
171-
// "TestD",
172-
// "Namespace::TestF",
173-
// "Namespace::TestC",
174-
// ];
175-
// for name in type_names {
176-
// assert!(
177-
// TypeDef::lookup(&module_def, &ScopedTypeName::new(name)).is_some(),
178-
// "Type '{}' not found",
179-
// name
180-
// );
181-
// }
182-
//
154+
let type_names = [
155+
// types are Pascal case
156+
"TestB", "Person",
157+
];
158+
for name in type_names {
159+
assert!(
160+
TypeDef::lookup(&module_def, &ScopedTypeName::new([].into(), Identifier::for_test(name))).is_some(),
161+
"Type '{}' not found",
162+
name
163+
);
164+
}
165+
183166
// Test Indexes (using lookup via stored_in_table_def)
184167
let index_names = [
185-
"Person_age_idx_btree",
186-
"Person_id_idx_btree",
187-
"test_a_table_x_idx_btree",
168+
// index name should be generated from canonical name
169+
"test_a_x_idx_btree",
170+
"person_id_idx_btree",
188171
];
189172
for index_name in index_names {
190173
assert!(
@@ -194,17 +177,8 @@ fn test_case_converted_names() {
194177
);
195178
}
196179

197-
let index_names_and_alias = [("person_age_idx_btree", "P")];
198-
// for (index_name, alias) in index_names {
199-
// assert!(
200-
// &IndexDef::lookup(&module_def, &RawIdentifier::new(index_name)).expect("index exists").accessor_name,
201-
// "Index '{}' not found",
202-
// alias
203-
// );
204-
// }
205-
206180
// Test Constraints
207-
let constraint_names = ["Person_id_key"];
181+
let constraint_names = ["person_id_key"];
208182
for constraint_name in constraint_names {
209183
assert!(
210184
ConstraintDef::lookup(&module_def, &RawIdentifier::new(constraint_name)).is_some(),
@@ -214,7 +188,7 @@ fn test_case_converted_names() {
214188
}
215189

216190
// Test Sequences
217-
let sequence_names = ["Person_id_seq"];
191+
let sequence_names = ["person_id_seq"];
218192
for sequence_name in sequence_names {
219193
assert!(
220194
SequenceDef::lookup(&module_def, &RawIdentifier::new(sequence_name)).is_some(),
@@ -232,7 +206,8 @@ fn test_case_converted_names() {
232206
);
233207

234208
// Test Columns (using composite key: table_name, column_name)
235-
let column_names = [("Person", "id")];
209+
// Id has bigger case in accessor
210+
let column_names = [("person", "id")];
236211
for (table_name, col_name) in column_names {
237212
assert!(
238213
ColumnDef::lookup(
@@ -245,22 +220,4 @@ fn test_case_converted_names() {
245220
col_name
246221
);
247222
}
248-
249-
// Test View Columns
250-
// let view_column_names = [
251-
// ("my_player", "identity"),
252-
// ("my_player", "player_id"),
253-
// ("my_player", "name"),
254-
// ];
255-
// for (view_name, col_name) in view_column_names {
256-
// assert!(
257-
// ViewColumnDef::lookup(
258-
// &module_def,
259-
// (&Identifier::for_test(view_name), &Identifier::for_test(col_name))
260-
// )
261-
// .is_some(),
262-
// "View column '{}.{}' not found",
263-
// view_name,
264-
// col_name
265-
// );
266223
}

modules/module-test-ts/src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type TestAlias = TestA;
2222
// ─────────────────────────────────────────────────────────────────────────────
2323

2424
// Rust: #[derive(SpacetimeType)] pub struct TestB { foo: String }
25-
const testB = t.object('TestB', {
25+
const testB = t.object('testB', {
2626
foo: t.string(),
2727
});
2828
type TestB = Infer<typeof testB>;
@@ -151,7 +151,6 @@ const spacetimedb = schema({
151151
// person (public) with btree index on age
152152
person: table(
153153
{
154-
name: 'Person',
155154
public: true,
156155
indexes: [{ name: 'age', algorithm: 'btree', columns: ['age'] }],
157156
},
@@ -161,6 +160,7 @@ const spacetimedb = schema({
161160
// test_a with index foo on x
162161
testATable: table(
163162
{
163+
name: "test_a",
164164
indexes: [{ name: 'foo', algorithm: 'btree', columns: ['x'] }],
165165
},
166166
testA
@@ -207,7 +207,7 @@ const spacetimedb = schema({
207207
repeatingTestArg: table(
208208
{
209209
name: 'repeating_test_arg',
210-
scheduled: (): any => repeating_test,
210+
scheduled: (): any => repeatingTest
211211
},
212212
repeatingTestArg
213213
),
@@ -229,8 +229,8 @@ export default spacetimedb;
229229
// VIEWS
230230
// ─────────────────────────────────────────────────────────────────────────────
231231

232-
export const my_player = spacetimedb.view(
233-
{ name: 'my_player', public: true },
232+
export const myPlayer = spacetimedb.view(
233+
{ public: true },
234234
playerLikeRow.optional(),
235235
// FIXME: this should not be necessary; change `OptionBuilder` to accept `null|undefined` for `none`
236236
ctx => ctx.db.player.identity.find(ctx.sender) ?? undefined
@@ -250,7 +250,7 @@ export const init = spacetimedb.init(ctx => {
250250
});
251251

252252
// repeating_test
253-
export const repeating_test = spacetimedb.reducer(
253+
export const repeatingTest = spacetimedb.reducer(
254254
{ arg: repeatingTestArg },
255255
(ctx, { arg }) => {
256256
const delta = ctx.timestamp.since(arg.prev_time); // adjust if API differs
@@ -275,7 +275,7 @@ export const say_hello = spacetimedb.reducer(ctx => {
275275
});
276276

277277
// list_over_age(age)
278-
export const list_over_age = spacetimedb.reducer(
278+
export const listOverAge = spacetimedb.reducer(
279279
{ age: t.u8() },
280280
(ctx, { age }) => {
281281
// Prefer an index-based scan if exposed by bindings; otherwise iterate.
@@ -464,7 +464,7 @@ export const assert_caller_identity_is_module_identity = spacetimedb.reducer(
464464
// Hit SpacetimeDB's schema HTTP route and return its result as a string.
465465
//
466466
// This is a silly thing to do, but an effective test of the procedure HTTP API.
467-
export const get_my_schema_via_http = spacetimedb.procedure(t.string(), ctx => {
467+
export const getMyTestViaHttp = spacetimedb.procedure(t.string(), ctx => {
468468
const module_identity = ctx.identity;
469469
try {
470470
const response = ctx.http.fetch(

modules/module-test/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Person {
2727
}
2828

2929
#[cfg(not(feature = "test-add-column"))]
30-
#[spacetimedb::table(accessor = person, name="Person", public, index(accessor = age, btree(columns = [age])))]
30+
#[spacetimedb::table(accessor = person, public, index(accessor = age, btree(columns = [age])))]
3131
pub struct Person {
3232
#[primary_key]
3333
#[auto_inc]
@@ -42,15 +42,16 @@ pub struct RemoveTable {
4242
pub id: u32,
4343
}
4444

45-
#[spacetimedb::table(accessor = TestATable, index(accessor = foo, btree(columns = [x])))]
45+
#[spacetimedb::table(accessor = TestATable, name="test_a", index(accessor = foo, btree(columns = [x])))]
4646
pub struct TestA {
4747
pub x: u32,
4848
pub y: u32,
4949
pub z: String,
5050
}
5151

5252
#[derive(SpacetimeType)]
53-
pub struct TestB {
53+
#[allow(non_camel_case_types)]
54+
pub struct Test_b {
5455
foo: String,
5556
}
5657

@@ -204,7 +205,7 @@ impl Foo<'_> {
204205
// VIEWS
205206
// ─────────────────────────────────────────────────────────────────────────────
206207

207-
#[spacetimedb::view(accessor = myOwnPlayer, public)]
208+
#[spacetimedb::view(accessor = myPlayer, public)]
208209
fn my_player(ctx: &ViewContext) -> Option<Player> {
209210
ctx.db.player().identity().find(ctx.sender())
210211
}
@@ -253,7 +254,7 @@ pub fn say_hello(ctx: &ReducerContext) {
253254
}
254255

255256
#[spacetimedb::reducer]
256-
pub fn list_over_age(ctx: &ReducerContext, age: u8) {
257+
pub fn listOverAge(ctx: &ReducerContext, age: u8) {
257258
for person in ctx.db.person().age().filter(age..) {
258259
log::info!("{} has age {} >= {}", person.name, person.age, age);
259260
}
@@ -265,7 +266,7 @@ fn log_module_identity(ctx: &ReducerContext) {
265266
}
266267

267268
#[spacetimedb::reducer]
268-
pub fn test(ctx: &ReducerContext, arg: TestAlias, arg2: TestB, arg3: TestC, arg4: TestF) -> anyhow::Result<()> {
269+
pub fn test(ctx: &ReducerContext, arg: TestAlias, arg2: Test_b, arg3: TestC, arg4: TestF) -> anyhow::Result<()> {
269270
log::info!("BEGIN");
270271
log::info!("sender: {:?}", ctx.sender());
271272
log::info!("timestamp: {:?}", ctx.timestamp);
@@ -502,8 +503,8 @@ fn with_tx(ctx: &mut ProcedureContext) {
502503
/// Hit SpacetimeDB's schema HTTP route and return its result as a string.
503504
///
504505
/// This is a silly thing to do, but an effective test of the procedure HTTP API.
505-
#[spacetimedb::procedure]
506-
fn get_my_schema_via_http(ctx: &mut ProcedureContext) -> String {
506+
#[spacetimedb::procedure(name = "get_my_test_via_http")]
507+
fn getMyTestViaHttp(ctx: &mut ProcedureContext) -> String {
507508
let module_identity = ctx.identity();
508509
match ctx.http.get(format!(
509510
"http://localhost:3000/v1/database/{module_identity}/schema?version=9"

0 commit comments

Comments
 (0)