Add execute_with to TestExternalities (#3793)

This function executes the given closure in a context where the test
externalities are set. This makes the srml tests easier to write, as the
test externalities need to be created anyway.
This commit is contained in:
Bastian Köcher
2019-10-10 15:01:30 +02:00
committed by GitHub
parent 34c7338211
commit 4dbc9265ee
44 changed files with 2369 additions and 2645 deletions
+35 -38
View File
@@ -976,10 +976,7 @@ mod tests {
traits::Contains
};
use primitives::H256;
use sr_primitives::{
set_and_run_with_externalities, traits::{BlakeTwo256, IdentityLookup, Bounded},
testing::Header, Perbill,
};
use sr_primitives::{traits::{BlakeTwo256, IdentityLookup, Bounded}, testing::Header, Perbill};
use balances::BalanceLock;
use system::EnsureSignedBy;
@@ -1101,7 +1098,7 @@ mod tests {
#[test]
fn params_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
assert_eq!(Democracy::referendum_count(), 0);
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::total_issuance(), 210);
@@ -1133,7 +1130,7 @@ mod tests {
#[test]
fn external_and_public_interleaving_works() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
@@ -1246,7 +1243,7 @@ mod tests {
#[test]
fn emergency_cancel_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let r = Democracy::inject_referendum(
2,
@@ -1275,7 +1272,7 @@ mod tests {
#[test]
fn veto_external_works() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(Democracy::external_propose(
Origin::signed(2),
@@ -1335,7 +1332,7 @@ mod tests {
#[test]
fn external_referendum_works() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_noop!(Democracy::external_propose(
Origin::signed(1),
@@ -1364,7 +1361,7 @@ mod tests {
#[test]
fn external_majority_referendum_works() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_noop!(Democracy::external_propose_majority(
Origin::signed(1),
@@ -1389,7 +1386,7 @@ mod tests {
#[test]
fn external_default_referendum_works() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_noop!(Democracy::external_propose_default(
Origin::signed(3),
@@ -1414,7 +1411,7 @@ mod tests {
#[test]
fn fast_track_referendum_works() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let h = BlakeTwo256::hash_of(&set_balance_proposal(2));
assert_noop!(Democracy::fast_track(Origin::signed(5), h, 3, 2), "no proposal made");
@@ -1438,7 +1435,7 @@ mod tests {
#[test]
fn fast_track_referendum_fails_when_no_simple_majority() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let h = BlakeTwo256::hash_of(&set_balance_proposal(2));
assert_ok!(Democracy::external_propose(
@@ -1454,7 +1451,7 @@ mod tests {
#[test]
fn locked_for_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance(1, 2, 2));
assert_ok!(propose_set_balance(1, 4, 4));
@@ -1467,7 +1464,7 @@ mod tests {
#[test]
fn single_proposal_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
assert!(Democracy::referendum_info(0).is_none());
@@ -1514,7 +1511,7 @@ mod tests {
#[test]
fn cancel_queued_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1538,7 +1535,7 @@ mod tests {
#[test]
fn proxy_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
assert_eq!(Democracy::proxy(10), None);
assert_ok!(Democracy::set_proxy(Origin::signed(1), 10));
assert_eq!(Democracy::proxy(10), Some(1));
@@ -1568,7 +1565,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_proxy() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1588,7 +1585,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_delegation() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1613,7 +1610,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_cyclic_delegation() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1640,7 +1637,7 @@ mod tests {
#[test]
/// If transactor already voted, delegated vote is overwriten.
fn single_proposal_should_work_with_vote_and_delegation() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1666,7 +1663,7 @@ mod tests {
#[test]
fn single_proposal_should_work_with_undelegation() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1695,7 +1692,7 @@ mod tests {
#[test]
/// If transactor voted, delegated vote is overwriten.
fn single_proposal_should_work_with_delegation_and_vote() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 1));
@@ -1726,7 +1723,7 @@ mod tests {
#[test]
fn deposit_for_proposals_should_be_taken() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
@@ -1741,7 +1738,7 @@ mod tests {
#[test]
fn deposit_for_proposals_should_be_returned() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
@@ -1757,7 +1754,7 @@ mod tests {
#[test]
fn proposal_with_deposit_below_minimum_should_not_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_noop!(propose_set_balance(1, 2, 0), "value too low");
});
@@ -1765,7 +1762,7 @@ mod tests {
#[test]
fn poor_proposer_should_not_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_noop!(propose_set_balance(1, 2, 11), "proposer\'s balance too low");
});
@@ -1773,7 +1770,7 @@ mod tests {
#[test]
fn poor_seconder_should_not_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_ok!(propose_set_balance(2, 2, 11));
assert_noop!(Democracy::second(Origin::signed(1), 0), "seconder\'s balance too low");
@@ -1782,7 +1779,7 @@ mod tests {
#[test]
fn runners_up_should_come_after() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
assert_ok!(propose_set_balance(1, 2, 2));
assert_ok!(propose_set_balance(1, 4, 4));
@@ -1798,7 +1795,7 @@ mod tests {
#[test]
fn simple_passing_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1821,7 +1818,7 @@ mod tests {
#[test]
fn cancel_referendum_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1841,7 +1838,7 @@ mod tests {
#[test]
fn simple_failing_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1864,7 +1861,7 @@ mod tests {
#[test]
fn controversial_voting_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1890,7 +1887,7 @@ mod tests {
#[test]
fn delayed_enactment_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1918,7 +1915,7 @@ mod tests {
#[test]
fn controversial_low_turnout_voting_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,
@@ -1940,7 +1937,7 @@ mod tests {
#[test]
fn passing_low_turnout_voting_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(&42), 0);
assert_eq!(Balances::total_issuance(), 210);
@@ -1966,7 +1963,7 @@ mod tests {
#[test]
fn lock_voting_should_work() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(0);
let r = Democracy::inject_referendum(
1,
@@ -2026,7 +2023,7 @@ mod tests {
#[test]
fn lock_voting_should_work_with_delegation() {
set_and_run_with_externalities(&mut new_test_ext(), || {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let r = Democracy::inject_referendum(
1,