Skip to content
Snippets Groups Projects
Commit bb318736 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

refactor(tests): use assert_has_event

parent 386e4d84
No related branches found
No related tags found
1 merge request!104refactor(tests): use assert_has_event
...@@ -18,7 +18,6 @@ use crate::mock::Event as RuntimeEvent; ...@@ -18,7 +18,6 @@ use crate::mock::Event as RuntimeEvent;
use crate::mock::*; use crate::mock::*;
use crate::{Error, Event}; use crate::{Error, Event};
use frame_support::assert_ok; use frame_support::assert_ok;
//use frame_system::{EventRecord, Phase};
use maplit::btreemap; use maplit::btreemap;
use sp_std::collections::btree_map::BTreeMap; use sp_std::collections::btree_map::BTreeMap;
......
...@@ -19,7 +19,6 @@ use crate::mock::{Identity, System}; ...@@ -19,7 +19,6 @@ use crate::mock::{Identity, System};
use codec::Encode; use codec::Encode;
use frame_support::instances::Instance1; use frame_support::instances::Instance1;
use frame_support::{assert_noop, assert_ok}; use frame_support::{assert_noop, assert_ok};
use frame_system::{EventRecord, Phase};
use pallet_identity::{ use pallet_identity::{
IdtyName, IdtyStatus, NewOwnerKeyPayload, RevocationPayload, NEW_OWNER_KEY_PAYLOAD_PREFIX, IdtyName, IdtyStatus, NewOwnerKeyPayload, RevocationPayload, NEW_OWNER_KEY_PAYLOAD_PREFIX,
REVOCATION_PAYLOAD_PREFIX, REVOCATION_PAYLOAD_PREFIX,
...@@ -151,32 +150,17 @@ fn test_create_idty_ok() { ...@@ -151,32 +150,17 @@ fn test_create_idty_ok() {
// Alice should be able to create an identity at block #2 // Alice should be able to create an identity at block #2
assert_ok!(Identity::create_identity(Origin::signed(1), 6)); assert_ok!(Identity::create_identity(Origin::signed(1), 6));
// 2 events should have occurred: IdtyCreated and NewCert // 2 events should have occurred: IdtyCreated and NewCert
let events = System::events(); System::assert_has_event(Event::Identity(pallet_identity::Event::IdtyCreated {
assert_eq!(events.len(), 2); idty_index: 6,
assert_eq!( owner_key: 6,
events[0], }));
EventRecord { System::assert_has_event(Event::Cert(pallet_certification::Event::NewCert {
phase: Phase::Initialization, issuer: 1,
event: Event::Identity(pallet_identity::Event::IdtyCreated { issuer_issued_count: 5,
idty_index: 6, receiver: 6,
owner_key: 6, receiver_received_count: 1,
}), }));
topics: vec![],
}
);
assert_eq!(
events[1],
EventRecord {
phase: Phase::Initialization,
event: Event::Cert(pallet_certification::Event::NewCert {
issuer: 1,
issuer_issued_count: 5,
receiver: 6,
receiver_received_count: 1
}),
topics: vec![],
}
);
assert_eq!(Identity::identity(6).unwrap().status, IdtyStatus::Created); assert_eq!(Identity::identity(6).unwrap().status, IdtyStatus::Created);
assert_eq!(Identity::identity(6).unwrap().removable_on, 4); assert_eq!(Identity::identity(6).unwrap().removable_on, 4);
}); });
...@@ -247,30 +231,14 @@ fn test_confirm_idty_ok() { ...@@ -247,30 +231,14 @@ fn test_confirm_idty_ok() {
Origin::signed(6), Origin::signed(6),
IdtyName::from("Ferdie"), IdtyName::from("Ferdie"),
)); ));
let events = System::events(); System::assert_has_event(Event::Membership(
// 2 events should have occurred: MembershipRequested and IdtyConfirmed pallet_membership::Event::MembershipRequested(6),
assert_eq!(events.len(), 2); ));
//println!("{:?}", events[0]); System::assert_has_event(Event::Identity(pallet_identity::Event::IdtyConfirmed {
assert_eq!( idty_index: 6,
events[0], owner_key: 6,
EventRecord { name: IdtyName::from("Ferdie"),
phase: Phase::Initialization, }));
event: Event::Membership(pallet_membership::Event::MembershipRequested(6)),
topics: vec![],
}
);
assert_eq!(
events[1],
EventRecord {
phase: Phase::Initialization,
event: Event::Identity(pallet_identity::Event::IdtyConfirmed {
idty_index: 6,
owner_key: 6,
name: IdtyName::from("Ferdie"),
}),
topics: vec![],
}
);
}); });
} }
...@@ -287,25 +255,13 @@ fn test_idty_membership_expire_them_requested() { ...@@ -287,25 +255,13 @@ fn test_idty_membership_expire_them_requested() {
// Charlie's membership should expire at block #8 // Charlie's membership should expire at block #8
run_to_block(8); run_to_block(8);
assert!(Membership::membership(3).is_none()); assert!(Membership::membership(3).is_none());
let events = System::events();
println!("{:?}", events); System::assert_has_event(Event::Membership(
assert_eq!(events.len(), 2); pallet_membership::Event::MembershipExpired(3),
assert_eq!( ));
events[0], System::assert_has_event(Event::Identity(pallet_identity::Event::IdtyRemoved {
EventRecord { idty_index: 3,
phase: Phase::Initialization, }));
event: Event::Membership(pallet_membership::Event::MembershipExpired(3)),
topics: vec![],
}
);
assert_eq!(
events[1],
EventRecord {
phase: Phase::Initialization,
event: Event::Identity(pallet_identity::Event::IdtyRemoved { idty_index: 3 }),
topics: vec![],
}
);
// Charlie's identity should be removed at block #8 // Charlie's identity should be removed at block #8
assert!(Identity::identity(3).is_none()); assert!(Identity::identity(3).is_none());
......
...@@ -21,7 +21,6 @@ use crate::{ ...@@ -21,7 +21,6 @@ use crate::{
}; };
use codec::Encode; use codec::Encode;
use frame_support::{assert_noop, assert_ok}; use frame_support::{assert_noop, assert_ok};
use frame_system::{EventRecord, Phase};
use sp_runtime::testing::TestSignature; use sp_runtime::testing::TestSignature;
type IdtyVal = IdtyValue<u64, u64, ()>; type IdtyVal = IdtyValue<u64, u64, ()>;
...@@ -77,19 +76,11 @@ fn test_create_identity_ok() { ...@@ -77,19 +76,11 @@ fn test_create_identity_ok() {
// Alice should be able to create an identity // Alice should be able to create an identity
assert_ok!(Identity::create_identity(Origin::signed(1), 2)); assert_ok!(Identity::create_identity(Origin::signed(1), 2));
let events = System::events();
assert_eq!(events.len(), 1); System::assert_has_event(Event::Identity(crate::Event::IdtyCreated {
assert_eq!( idty_index: 2,
events[0], owner_key: 2,
EventRecord { }));
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyCreated {
idty_index: 2,
owner_key: 2,
}),
topics: vec![],
}
);
}); });
} }
...@@ -107,33 +98,17 @@ fn test_create_identity_but_not_confirm_it() { ...@@ -107,33 +98,17 @@ fn test_create_identity_but_not_confirm_it() {
// The identity shoud expire in blocs #3 // The identity shoud expire in blocs #3
run_to_block(3); run_to_block(3);
let events = System::events();
assert_eq!(events.len(), 1); System::assert_has_event(Event::Identity(crate::Event::IdtyRemoved { idty_index: 2 }));
assert_eq!(
events[0],
EventRecord {
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyRemoved { idty_index: 2 }),
topics: vec![],
}
);
// We shoud be able to recreate the identity // We shoud be able to recreate the identity
run_to_block(4); run_to_block(4);
assert_ok!(Identity::create_identity(Origin::signed(1), 2)); assert_ok!(Identity::create_identity(Origin::signed(1), 2));
let events = System::events();
assert_eq!(events.len(), 1); System::assert_has_event(Event::Identity(crate::Event::IdtyCreated {
assert_eq!( idty_index: 3,
events[0], owner_key: 2,
EventRecord { }));
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyCreated {
idty_index: 3,
owner_key: 2,
}),
topics: vec![],
}
);
}); });
} }
...@@ -148,19 +123,12 @@ fn test_idty_creation_period() { ...@@ -148,19 +123,12 @@ fn test_idty_creation_period() {
// Alice should be able to create an identity // Alice should be able to create an identity
assert_ok!(Identity::create_identity(Origin::signed(1), 2)); assert_ok!(Identity::create_identity(Origin::signed(1), 2));
let events = System::events();
assert_eq!(events.len(), 1); System::assert_has_event(Event::Identity(crate::Event::IdtyCreated {
assert_eq!( idty_index: 2,
events[0], owner_key: 2,
EventRecord { }));
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyCreated {
idty_index: 2,
owner_key: 2,
}),
topics: vec![],
}
);
assert_eq!(Identity::identity(1).unwrap().next_creatable_identity_on, 4); assert_eq!(Identity::identity(1).unwrap().next_creatable_identity_on, 4);
// Alice cannot create a new identity before block #4 // Alice cannot create a new identity before block #4
...@@ -173,19 +141,11 @@ fn test_idty_creation_period() { ...@@ -173,19 +141,11 @@ fn test_idty_creation_period() {
// Alice should be able to create a second identity after block #4 // Alice should be able to create a second identity after block #4
run_to_block(4); run_to_block(4);
assert_ok!(Identity::create_identity(Origin::signed(1), 3)); assert_ok!(Identity::create_identity(Origin::signed(1), 3));
let events = System::events();
assert_eq!(events.len(), 1); System::assert_has_event(Event::Identity(crate::Event::IdtyCreated {
assert_eq!( idty_index: 3,
events[0], owner_key: 3,
EventRecord { }));
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyCreated {
idty_index: 3,
owner_key: 3,
}),
topics: vec![],
}
);
}); });
} }
...@@ -462,24 +422,10 @@ fn test_idty_revocation() { ...@@ -462,24 +422,10 @@ fn test_idty_revocation() {
TestSignature(1, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode()) TestSignature(1, (REVOCATION_PAYLOAD_PREFIX, revocation_payload).encode())
)); ));
let events = System::events(); System::assert_has_event(Event::System(frame_system::Event::KilledAccount {
assert_eq!(events.len(), 2); account: 1,
assert_eq!( }));
events[0], System::assert_has_event(Event::Identity(crate::Event::IdtyRemoved { idty_index: 1 }));
EventRecord {
phase: Phase::Initialization,
event: Event::System(frame_system::Event::KilledAccount { account: 1 }),
topics: vec![],
}
);
assert_eq!(
events[1],
EventRecord {
phase: Phase::Initialization,
event: Event::Identity(crate::Event::IdtyRemoved { idty_index: 1 }),
topics: vec![],
}
);
run_to_block(2); run_to_block(2);
......
...@@ -91,22 +91,16 @@ fn test_remove_identity() { ...@@ -91,22 +91,16 @@ fn test_remove_identity() {
4, 4,
None None
)); ));
let events = System::events();
assert_eq!(events.len(), 3); System::assert_has_event(Event::Membership(
assert_eq!( pallet_membership::Event::MembershipRevoked(4),
System::events()[0].event, ));
Event::Membership(pallet_membership::Event::MembershipRevoked(4)) System::assert_has_event(Event::System(frame_system::Event::KilledAccount {
); account: AccountKeyring::Dave.to_account_id(),
assert_eq!( }));
System::events()[1].event, System::assert_has_event(Event::Identity(pallet_identity::Event::IdtyRemoved {
Event::System(frame_system::Event::KilledAccount { idty_index: 4,
account: AccountKeyring::Dave.to_account_id() }));
})
);
assert_eq!(
System::events()[2].event,
Event::Identity(pallet_identity::Event::IdtyRemoved { idty_index: 4 })
);
}); });
} }
#[test] #[test]
...@@ -122,39 +116,27 @@ fn test_remove_identity_after_one_ud() { ...@@ -122,39 +116,27 @@ fn test_remove_identity_after_one_ud() {
)); ));
// Verify events // Verify events
let events = System::events(); System::assert_has_event(Event::Membership(
//println!("{:?}", events); pallet_membership::Event::MembershipRevoked(4),
assert_eq!(events.len(), 5); ));
assert_eq!( System::assert_has_event(Event::Balances(pallet_balances::Event::Deposit {
System::events()[0].event, who: AccountKeyring::Dave.to_account_id(),
Event::Membership(pallet_membership::Event::MembershipRevoked(4)) amount: 1_000,
); }));
assert_eq!( System::assert_has_event(Event::Balances(pallet_balances::Event::Endowed {
System::events()[1].event, account: AccountKeyring::Dave.to_account_id(),
Event::Balances(pallet_balances::Event::Deposit { free_balance: 1_000,
who: AccountKeyring::Dave.to_account_id(), }));
amount: 1_000 System::assert_has_event(Event::UniversalDividend(
}) pallet_universal_dividend::Event::UdsAutoPaidAtRemoval {
);
assert_eq!(
System::events()[2].event,
Event::Balances(pallet_balances::Event::Endowed {
account: AccountKeyring::Dave.to_account_id(),
free_balance: 1_000
})
);
assert_eq!(
System::events()[3].event,
Event::UniversalDividend(pallet_universal_dividend::Event::UdsAutoPaidAtRemoval {
count: 1, count: 1,
total: 1_000, total: 1_000,
who: AccountKeyring::Dave.to_account_id(), who: AccountKeyring::Dave.to_account_id(),
}) },
); ));
assert_eq!( System::assert_has_event(Event::Identity(pallet_identity::Event::IdtyRemoved {
System::events()[4].event, idty_index: 4,
Event::Identity(pallet_identity::Event::IdtyRemoved { idty_index: 4 }) }));
);
// Verify state // Verify state
assert!(Identity::identity(4).is_none()); assert!(Identity::identity(4).is_none());
...@@ -175,25 +157,19 @@ fn test_remove_smith_identity() { ...@@ -175,25 +157,19 @@ fn test_remove_smith_identity() {
3, 3,
None None
)); ));
let events = System::events(); // Verify events
assert_eq!(events.len(), 4); System::assert_has_event(Event::SmithsMembership(
assert_eq!( pallet_membership::Event::MembershipRevoked(3),
System::events()[0].event, ));
Event::SmithsMembership(pallet_membership::Event::MembershipRevoked(3)) System::assert_has_event(Event::AuthorityMembers(
); pallet_authority_members::Event::MemberRemoved(3),
assert_eq!( ));
System::events()[1].event, System::assert_has_event(Event::Membership(
Event::AuthorityMembers(pallet_authority_members::Event::MemberRemoved(3)) pallet_membership::Event::MembershipRevoked(3),
); ));
assert_eq!( System::assert_has_event(Event::Identity(pallet_identity::Event::IdtyRemoved {
System::events()[2].event, idty_index: 3,
Event::Membership(pallet_membership::Event::MembershipRevoked(3)) }));
);
assert_eq!(
System::events()[3].event,
Event::Identity(pallet_identity::Event::IdtyRemoved { idty_index: 3 })
);
//println!("{:#?}", events);
}); });
} }
...@@ -211,55 +187,38 @@ fn test_create_new_account_with_insufficient_balance() { ...@@ -211,55 +187,38 @@ fn test_create_new_account_with_insufficient_balance() {
MultiAddress::Id(AccountKeyring::Eve.to_account_id()), MultiAddress::Id(AccountKeyring::Eve.to_account_id()),
400 400
)); ));
let events = System::events();
//println!("{:#?}", events); System::assert_has_event(Event::System(frame_system::Event::NewAccount {
assert_eq!(events.len(), 3); account: AccountKeyring::Eve.to_account_id(),
assert_eq!( }));
System::events()[0].event, System::assert_has_event(Event::Balances(pallet_balances::Event::Endowed {
Event::System(frame_system::Event::NewAccount { account: AccountKeyring::Eve.to_account_id(),
account: AccountKeyring::Eve.to_account_id(), free_balance: 400,
}) }));
); System::assert_has_event(Event::Balances(pallet_balances::Event::Transfer {
assert_eq!( from: AccountKeyring::Alice.to_account_id(),
System::events()[1].event, to: AccountKeyring::Eve.to_account_id(),
Event::Balances(pallet_balances::Event::Endowed { amount: 400,
account: AccountKeyring::Eve.to_account_id(), }));
free_balance: 400,
})
);
assert_eq!(
System::events()[2].event,
Event::Balances(pallet_balances::Event::Transfer {
from: AccountKeyring::Alice.to_account_id(),
to: AccountKeyring::Eve.to_account_id(),
amount: 400,
})
);
// At next block, the new account must be reaped because its balance is not sufficient // At next block, the new account must be reaped because its balance is not sufficient
// to pay the "new account tax" // to pay the "new account tax"
run_to_block(3); run_to_block(3);
let events = System::events();
//println!("{:#?}", events); System::assert_has_event(Event::Account(
assert_eq!(events.len(), 3); pallet_duniter_account::Event::ForceDestroy {
assert_eq!(
System::events()[0].event,
Event::Account(pallet_duniter_account::Event::ForceDestroy {
who: AccountKeyring::Eve.to_account_id(), who: AccountKeyring::Eve.to_account_id(),
balance: 400, balance: 400,
}) },
); ));
assert_eq!( System::assert_has_event(Event::Balances(pallet_balances::Event::Deposit {
System::events()[1].event, who: Treasury::account_id(),
Event::Balances(pallet_balances::Event::Deposit { amount: 400,
who: Treasury::account_id(), }));
amount: 400, System::assert_has_event(Event::Treasury(pallet_treasury::Event::Deposit {
}) value: 400,
); }));
assert_eq!(
System::events()[2].event,
Event::Treasury(pallet_treasury::Event::Deposit { value: 400 })
);
assert_eq!( assert_eq!(
Balances::free_balance(AccountKeyring::Eve.to_account_id()), Balances::free_balance(AccountKeyring::Eve.to_account_id()),
0 0
...@@ -299,27 +258,19 @@ fn test_create_new_account() { ...@@ -299,27 +258,19 @@ fn test_create_new_account() {
// At next block, the new account must be created, // At next block, the new account must be created,
// and new account tax should be collected and deposited in the treasury // and new account tax should be collected and deposited in the treasury
run_to_block(3); run_to_block(3);
let events = System::events();
println!("{:#?}", events); System::assert_has_event(Event::Balances(pallet_balances::Event::Withdraw {
assert_eq!(events.len(), 3); who: AccountKeyring::Eve.to_account_id(),
assert_eq!( amount: 300,
System::events()[0].event, }));
Event::Balances(pallet_balances::Event::Withdraw { System::assert_has_event(Event::Balances(pallet_balances::Event::Deposit {
who: AccountKeyring::Eve.to_account_id(), who: Treasury::account_id(),
amount: 300, amount: 300,
}) }));
); System::assert_has_event(Event::Treasury(pallet_treasury::Event::Deposit {
assert_eq!( value: 300,
System::events()[1].event, }));
Event::Balances(pallet_balances::Event::Deposit {
who: Treasury::account_id(),
amount: 300,
})
);
assert_eq!(
System::events()[2].event,
Event::Treasury(pallet_treasury::Event::Deposit { value: 300 })
);
assert_eq!( assert_eq!(
Balances::free_balance(AccountKeyring::Eve.to_account_id()), Balances::free_balance(AccountKeyring::Eve.to_account_id()),
200 200
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment