Skip to content

Commit

Permalink
test(dojo-core): add tests for event upgrades (#2976)
Browse files Browse the repository at this point in the history
core: add tests for event upgrades
  • Loading branch information
remybar authored Jan 30, 2025
1 parent 687334d commit db840b7
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/dojo/core-cairo-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "dojo"
version = "1.1.1"
version = "1.1.2"
dependencies = [
"dojo_plugin",
]
Expand Down
30 changes: 30 additions & 0 deletions crates/dojo/core-cairo-test/src/tests/helpers/event.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ struct FooEventMemberAdded {
pub b: u128,
}

#[derive(Introspect, Copy, Drop, Serde)]
enum MyEnum {
X: u8,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberChanged {
#[key]
pub caller: ContractAddress,
pub a: (MyEnum, u8),
pub b: u128,
}

#[derive(Introspect, Copy, Drop, Serde)]
enum AnotherEnum {
X: u8,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberIllegalChange {
#[key]
pub caller: ContractAddress,
pub a: AnotherEnum,
pub b: u128,
}

pub fn deploy_world_for_event_upgrades() -> IWorldDispatcher {
let namespace_def = NamespaceDef {
namespace: "dojo",
Expand All @@ -74,6 +102,8 @@ pub fn deploy_world_for_event_upgrades() -> IWorldDispatcher {
),
TestResource::Event(e_FooEventMemberAddedButMoved::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Event(e_FooEventMemberAdded::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Event(e_FooEventMemberChanged::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Event(e_FooEventMemberIllegalChange::TEST_CLASS_HASH.try_into().unwrap()),
]
.span(),
};
Expand Down
70 changes: 70 additions & 0 deletions crates/dojo/core-cairo-test/src/tests/world/event.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ pub struct FooEventMemberAdded {
pub c: u256,
}

#[derive(Introspect, Copy, Drop, Serde, PartialEq)]
enum MyEnum {
X: u8,
Y: u16,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberChanged {
#[key]
pub caller: ContractAddress,
pub a: (MyEnum, u8, u32),
pub b: u128,
}

#[derive(Introspect, Copy, Drop, Serde)]
enum AnotherEnum {
X: bool,
}

#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::event]
struct FooEventMemberIllegalChange {
#[key]
pub caller: ContractAddress,
pub a: MyEnum,
pub b: u128,
}

#[test]
fn test_register_event_for_namespace_owner() {
let bob = starknet::contract_address_const::<0xb0b>();
Expand Down Expand Up @@ -169,6 +198,35 @@ fn test_upgrade_event() {
}
}

#[test]
fn test_upgrade_event_with_member_changed() {
let world = deploy_world_for_event_upgrades();

drop_all_events(world.contract_address);

world.upgrade_event("dojo", e_FooEventMemberChanged::TEST_CLASS_HASH.try_into().unwrap());

let event = starknet::testing::pop_log::<world::Event>(world.contract_address);
assert(event.is_some(), 'no event)');

if let world::Event::EventUpgraded(event) = event.unwrap() {
assert(
event.selector == Event::<FooEventMemberChanged>::selector(DOJO_NSH),
'bad event selector',
);
assert(
event.class_hash == e_FooEventMemberChanged::TEST_CLASS_HASH.try_into().unwrap(),
'bad event class_hash',
);
assert(
event.address != core::num::traits::Zero::<ContractAddress>::zero(),
'bad event address',
);
} else {
core::panic_with_felt252('no EventUpgraded event');
}
}

#[test]
#[should_panic(
expected: (
Expand Down Expand Up @@ -220,6 +278,18 @@ fn test_upgrade_event_with_member_moved() {
world.upgrade_event("dojo", e_FooEventMemberAddedButMoved::TEST_CLASS_HASH.try_into().unwrap());
}

#[test]
#[should_panic(
expected: (
"Invalid new schema to upgrade the resource `dojo-FooEventMemberIllegalChange`",
'ENTRYPOINT_FAILED',
),
)]
fn test_upgrade_event_with_member_illegal_change() {
let world = deploy_world_for_event_upgrades();
world.upgrade_event("dojo", e_FooEventMemberIllegalChange::TEST_CLASS_HASH.try_into().unwrap());
}

#[test]
#[should_panic(
expected: (
Expand Down
1 change: 1 addition & 0 deletions crates/dojo/core-cairo-test/src/tests/world/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ fn test_upgrade_model() {
assert!(read.c == 0);
}

#[test]
fn test_upgrade_model_with_member_changed() {
let caller = starknet::contract_address_const::<0xb0b>();
let world = deploy_world_for_model_upgrades();
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/types-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version = "2.9.2"

[[package]]
name = "types_test"
version = "1.1.1"
version = "1.1.2"
dependencies = [
"dojo",
]
2 changes: 1 addition & 1 deletion examples/simple/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "dojo"
version = "1.1.1"
version = "1.1.2"
dependencies = [
"dojo_plugin",
]
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = [

[[package]]
name = "dojo_examples"
version = "1.1.1"
version = "1.1.2"
dependencies = [
"armory",
"bestiary",
Expand Down
Binary file modified spawn-and-move-db.tar.gz
Binary file not shown.
Binary file modified types-test-db.tar.gz
Binary file not shown.

0 comments on commit db840b7

Please sign in to comment.