Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChqrles committed Oct 2, 2024
1 parent affc146 commit 7548da6
Showing 1 changed file with 114 additions and 31 deletions.
145 changes: 114 additions & 31 deletions contracts/src/components/registry/registry_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}

0 comments on commit 7548da6

Please sign in to comment.