From 96064cc0cd842e4d791208d15938539b075eacac Mon Sep 17 00:00:00 2001 From: manlikeHB Date: Mon, 30 Sep 2024 11:35:27 +0100 Subject: [PATCH 1/3] add test --- .../components/registry/registry_test.cairo | 56 +++++++++++++++++-- contracts/src/tests/constants.cairo | 5 ++ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/contracts/src/components/registry/registry_test.cairo b/contracts/src/components/registry/registry_test.cairo index 916868f..a56da17 100644 --- a/contracts/src/components/registry/registry_test.cairo +++ b/contracts/src/components/registry/registry_test.cairo @@ -53,19 +53,63 @@ fn test_register() { ) } -// #[test] +#[test] fn test_register_twice_same_offchain_id() { - panic!("Not implemented yet"); + let mut state = setup(); + let mut _spy = spy_events(); + let contract_address = test_address(); + + // setup caller + start_cheat_caller_address(contract_address, constants::CALLER()); + + // first registeration + state.register(offchain_id: constants::REVOLUT_ID()); + + // second registeration + state.register(offchain_id: constants::REVOLUT_ID()); + + // assert state after + assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID())); + //TODO: check on emitted events } -// #[test] +#[test] fn test_register_two_different_offchain_id() { - panic!("Not implemented yet"); + let mut state = setup(); + let mut _spy = spy_events(); + let contract_address = test_address(); + + // setup caller + start_cheat_caller_address(contract_address, constants::CALLER()); + + // register + state.register(offchain_id: constants::REVOLUT_ID()); + state.register(offchain_id: constants::REVOLUT_ID_TWO()); + + // assert state after + assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID())); + assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID_TWO())); + //TODO: check on emitted events } -// #[test] +#[test] fn test_register_same_offchain_id_from_two_different_callers() { - panic!("Not implemented yet"); + let mut state = setup(); + let mut _spy = spy_events(); + let contract_address = test_address(); + + // setup caller one and register + start_cheat_caller_address(contract_address, constants::CALLER()); + state.register(offchain_id: constants::REVOLUT_ID()); + + // setup caller two and register + start_cheat_caller_address(contract_address, constants::SPENDER()); + state.register(offchain_id: constants::REVOLUT_ID()); + + // assert state after + assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID())); + assert!(state.is_registered(constants::SPENDER(), constants::REVOLUT_ID())); + //TODO: check on emitted events } // diff --git a/contracts/src/tests/constants.cairo b/contracts/src/tests/constants.cairo index e7e9335..ff4a6f4 100644 --- a/contracts/src/tests/constants.cairo +++ b/contracts/src/tests/constants.cairo @@ -2,11 +2,16 @@ use core::starknet::{ContractAddress, contract_address_const}; use zkramp::components::registry::interface::OffchainId; const REVTAG: felt252 = 'just a random revtag hash'; +const REVTAG_TWO: felt252 = 'just another random revtag hash'; pub fn REVOLUT_ID() -> OffchainId { OffchainId::Revolut(REVTAG) } +pub fn REVOLUT_ID_TWO() -> OffchainId { + OffchainId::Revolut(REVTAG_TWO) +} + pub fn CALLER() -> ContractAddress { contract_address_const::<'caller'>() } From 7548da6c1cb6ffda795e07a0e43a9a7b46e24970 Mon Sep 17 00:00:00 2001 From: 0xChqrles Date: Wed, 2 Oct 2024 17:02:17 +0200 Subject: [PATCH 2/3] improve tests --- .../components/registry/registry_test.cairo | 145 ++++++++++++++---- 1 file changed, 114 insertions(+), 31 deletions(-) diff --git a/contracts/src/components/registry/registry_test.cairo b/contracts/src/components/registry/registry_test.cairo index a56da17..a060d45 100644 --- a/contracts/src/components/registry/registry_test.cairo +++ b/contracts/src/components/registry/registry_test.cairo @@ -29,15 +29,17 @@ fn test_register() { let mut state = setup(); let mut spy = spy_events(); let contract_address = test_address(); + let caller = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); // setup caller start_cheat_caller_address(contract_address, constants::CALLER()); // register - state.register(offchain_id: constants::REVOLUT_ID()); + state.register(:offchain_id); // assert state after - assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID())); + assert!(state.is_registered(contract_address: caller, :offchain_id)); // check on emitted events spy @@ -46,77 +48,158 @@ fn test_register() { ( contract_address, Event::RegistrationEvent( - RegistrationEvent { caller: constants::CALLER(), offchain_id: constants::REVOLUT_ID() } + RegistrationEvent { caller, offchain_id } ) ) ] - ) + ); } #[test] fn test_register_twice_same_offchain_id() { let mut state = setup(); - let mut _spy = spy_events(); + let mut spy = spy_events(); let contract_address = test_address(); + let caller = constants::CALLER(); + let offchain_id = constants::REVOLUT_ID(); // setup caller - start_cheat_caller_address(contract_address, constants::CALLER()); + start_cheat_caller_address(contract_address, caller); - // first registeration - state.register(offchain_id: constants::REVOLUT_ID()); - - // second registeration - state.register(offchain_id: constants::REVOLUT_ID()); + // double registeration + state.register(:offchain_id); + state.register(:offchain_id); // assert state after - assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID())); - //TODO: check on emitted events + assert!(state.is_registered(contract_address: caller, :offchain_id)); + + // check on emitted events + spy + .assert_emitted( + @array![ + ( + contract_address, + Event::RegistrationEvent( + RegistrationEvent { caller, offchain_id } + ) + ), + ( + contract_address, + Event::RegistrationEvent( + RegistrationEvent { caller, offchain_id } + ) + ) + ] + ); } #[test] fn test_register_two_different_offchain_id() { let mut state = setup(); - let mut _spy = spy_events(); + let mut spy = spy_events(); let contract_address = test_address(); + let caller = constants::CALLER(); + let offchain_id1 = constants::REVOLUT_ID(); + let offchain_id2 = constants::REVOLUT_ID2(); // setup caller - start_cheat_caller_address(contract_address, constants::CALLER()); + start_cheat_caller_address(contract_address, caller); - // register - state.register(offchain_id: constants::REVOLUT_ID()); - state.register(offchain_id: constants::REVOLUT_ID_TWO()); + // registerations + state.register(offchain_id: offchain_id1); + state.register(offchain_id: offchain_id2); // assert state after - assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID())); - assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID_TWO())); - //TODO: check on emitted events + assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id1)); + assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id2)); + + // check on emitted events + spy + .assert_emitted( + @array![ + ( + contract_address, + Event::RegistrationEvent( + RegistrationEvent { caller, offchain_id: offchain_id1 } + ) + ), + ( + contract_address, + Event::RegistrationEvent( + RegistrationEvent { caller, offchain_id: offchain_id2 } + ) + ) + ] + ); } #[test] fn test_register_same_offchain_id_from_two_different_callers() { let mut state = setup(); - let mut _spy = spy_events(); + let mut spy = spy_events(); let contract_address = test_address(); + let caller1 = constants::CALLER(); + let caller2 = constants::OTHER(); + let offchain_id = constants::REVOLUT_ID(); // setup caller one and register - start_cheat_caller_address(contract_address, constants::CALLER()); - state.register(offchain_id: constants::REVOLUT_ID()); + start_cheat_caller_address(contract_address, caller1); + state.register(:offchain_id); // setup caller two and register - start_cheat_caller_address(contract_address, constants::SPENDER()); - state.register(offchain_id: constants::REVOLUT_ID()); + start_cheat_caller_address(contract_address, caller2); + state.register(:offchain_id); // assert state after - assert!(state.is_registered(constants::CALLER(), constants::REVOLUT_ID())); - assert!(state.is_registered(constants::SPENDER(), constants::REVOLUT_ID())); - //TODO: check on emitted events + assert!(state.is_registered(contract_address: caller1, :offchain_id)); + assert!(state.is_registered(contract_address: caller2, :offchain_id)); + + // check on emitted events + spy + .assert_emitted( + @array![ + ( + contract_address, + Event::RegistrationEvent( + RegistrationEvent { caller: caller1, offchain_id } + ) + ), + ( + contract_address, + Event::RegistrationEvent( + RegistrationEvent { caller: caller2, offchain_id } + ) + ) + ] + ); } // // is_registered // -// #[test] +#[test] fn test_is_registered() { - panic!("Not implemented yet"); + let mut state = setup(); + let contract_address = test_address(); + let caller = constants::CALLER(); + let offchain_id1 = constants::REVOLUT_ID(); + let offchain_id2 = constants::REVOLUT_ID2(); + + assert!(!state.is_registered(contract_address: caller, offchain_id: offchain_id1)); + assert!(!state.is_registered(contract_address: caller, offchain_id: offchain_id2)); + + // register + start_cheat_caller_address(contract_address, constants::CALLER()); + state.register(offchain_id: offchain_id1); + + assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id1)); + assert!(!state.is_registered(contract_address: caller, offchain_id: offchain_id2)); + + // register another offchain ID + start_cheat_caller_address(contract_address, constants::CALLER()); + state.register(offchain_id: offchain_id2); + + assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id1)); + assert!(state.is_registered(contract_address: caller, offchain_id: offchain_id2)); } From f98a3ff120e0d9b746d8e1d81376207d55fb7028 Mon Sep 17 00:00:00 2001 From: 0xChqrles Date: Wed, 2 Oct 2024 17:07:25 +0200 Subject: [PATCH 3/3] fmt --- .../components/registry/registry_test.cairo | 51 +++---------------- 1 file changed, 7 insertions(+), 44 deletions(-) diff --git a/contracts/src/components/registry/registry_test.cairo b/contracts/src/components/registry/registry_test.cairo index a060d45..b1aa6b9 100644 --- a/contracts/src/components/registry/registry_test.cairo +++ b/contracts/src/components/registry/registry_test.cairo @@ -44,14 +44,7 @@ fn test_register() { // check on emitted events spy .assert_emitted( - @array![ - ( - contract_address, - Event::RegistrationEvent( - RegistrationEvent { caller, offchain_id } - ) - ) - ] + @array![(contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id }))] ); } @@ -77,18 +70,8 @@ fn test_register_twice_same_offchain_id() { spy .assert_emitted( @array![ - ( - contract_address, - Event::RegistrationEvent( - RegistrationEvent { caller, offchain_id } - ) - ), - ( - contract_address, - Event::RegistrationEvent( - RegistrationEvent { caller, offchain_id } - ) - ) + (contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id })), + (contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id })) ] ); } @@ -117,18 +100,8 @@ fn test_register_two_different_offchain_id() { spy .assert_emitted( @array![ - ( - contract_address, - Event::RegistrationEvent( - RegistrationEvent { caller, offchain_id: offchain_id1 } - ) - ), - ( - contract_address, - Event::RegistrationEvent( - RegistrationEvent { caller, offchain_id: offchain_id2 } - ) - ) + (contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id: offchain_id1 })), + (contract_address, Event::RegistrationEvent(RegistrationEvent { caller, offchain_id: offchain_id2 })) ] ); } @@ -158,18 +131,8 @@ fn test_register_same_offchain_id_from_two_different_callers() { spy .assert_emitted( @array![ - ( - contract_address, - Event::RegistrationEvent( - RegistrationEvent { caller: caller1, offchain_id } - ) - ), - ( - contract_address, - Event::RegistrationEvent( - RegistrationEvent { caller: caller2, offchain_id } - ) - ) + (contract_address, Event::RegistrationEvent(RegistrationEvent { caller: caller1, offchain_id })), + (contract_address, Event::RegistrationEvent(RegistrationEvent { caller: caller2, offchain_id })) ] ); }