Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Niederb committed Oct 9, 2024
1 parent 6191cb8 commit 833db41
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions testing/async/examples/author_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ async fn main() {
test_submit_and_watch_until_broadcast(&api, transfer_call.clone(), signer_nonce + 3),
test_submit_and_watch_until_in_block(&api, transfer_call.clone(), signer_nonce + 4),
test_submit_and_watch_until_finalized(&api, transfer_call.clone(), signer_nonce + 5),
test_submit_and_watch_until_retracted(&api, transfer_call.clone(), signer_nonce + 6),
// Test some _watch_untils_without_events. We don't need to test all, because it is tested implicitly by `submit_and_watch_extrinsic_until`
// as internal call.
test_submit_and_watch_extrinsic_until_ready_without_events(
&api,
transfer_call.clone(),
signer_nonce + 6
signer_nonce + 7
),
test_submit_and_watch_extrinsic_until_in_block_without_events(
&api,
transfer_call.clone(),
signer_nonce + 7
signer_nonce + 8
)
);
}
Expand Down Expand Up @@ -125,7 +126,7 @@ async fn test_submit_and_watch_until_in_block(
let report = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock).await.unwrap();
assert!(report.block_hash.is_some());
assert!(matches!(report.status, TransactionStatus::InBlock(_)));
assert_associated_events_match_expected(report.events.unwrap());
assert_associated_events_match_expected(&report.events.unwrap());
println!("Success: submit_and_watch_extrinsic_until {:?}", report.status);
}

Expand All @@ -139,7 +140,23 @@ async fn test_submit_and_watch_until_finalized(
let report = api.submit_and_watch_extrinsic_until(xt, XtStatus::Finalized).await.unwrap();
assert!(report.block_hash.is_some());
assert!(matches!(report.status, TransactionStatus::Finalized(_)));
assert_associated_events_match_expected(report.events.unwrap());
assert_associated_events_match_expected(&report.events.unwrap());
println!("Success: submit_and_watch_extrinsic_until {:?}", report.status);
}

async fn test_submit_and_watch_until_retracted(
api: &MyApi,
transfer_call: RuntimeCall,
nonce: Index,
) {
std::thread::sleep(std::time::Duration::from_secs(1));
let xt = api.compose_extrinsic_offline(transfer_call, nonce);
// We wait for `Retracted`` but we cannot simulate this in a test. Therefore we will receive the status after `Retracted`
// which is `Finalized`
let report = api.submit_and_watch_extrinsic_until(xt, XtStatus::Retracted).await.unwrap();
assert!(report.block_hash.is_some());
assert!(matches!(report.status, TransactionStatus::Finalized(_)));
assert_associated_events_match_expected(&report.events.unwrap());
println!("Success: submit_and_watch_extrinsic_until {:?}", report.status);
}

Expand Down Expand Up @@ -178,12 +195,14 @@ async fn test_submit_and_watch_extrinsic_until_in_block_without_events(
// Now we fetch the events separately
let report = api.populate_events(report).await.unwrap();
assert!(report.events.is_some());
let events = report.events.as_ref().unwrap();
assert_associated_events_match_expected(&events);

// Can populate events only once
assert!(api.populate_events(report).await.is_err());
}

fn assert_associated_events_match_expected(events: Vec<RawEventDetails<Hash>>) {
fn assert_associated_events_match_expected(events: &[RawEventDetails<Hash>]) {
// First event
assert_eq!(events[0].pallet_name(), "Balances");
assert_eq!(events[0].variant_name(), "Withdraw");
Expand Down

0 comments on commit 833db41

Please sign in to comment.