Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add tests for event upgrades #2976

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
Comment on lines +80 to +92
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Type mismatch in AnotherEnum between files.

The AnotherEnum::X variant is u8 here but bool in the test file. This inconsistency will cause upgrade failures.

Apply this diff to match the test file:

 enum AnotherEnum {
-    X: u8,
+    X: bool,
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[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,
}
#[derive(Introspect, Copy, Drop, Serde)]
enum AnotherEnum {
X: bool,
}
#[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,
}
Comment on lines +48 to +61
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Tuple type mismatch between event implementations.

The FooEventMemberChanged.a tuple includes an additional u32 compared to the helper version:

  • Helper: (MyEnum, u8)
  • Test: (MyEnum, u8, u32)

Update the helper version to match:

 struct FooEventMemberChanged {
     #[key]
     pub caller: ContractAddress,
-    pub a: (MyEnum, u8),
+    pub a: (MyEnum, u8, u32),
     pub b: u128,
 }

Committable suggestion skipped: line range outside the PR's diff.


#[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.
Loading