Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test extrinsic submitter #12

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions dhatu/src/tx/extrinsics/extrinsics_submitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,76 @@ impl ExtrinsicSubmitter {
Ok(result)
}
}

#[cfg(test)]
mod extrinsic_submitter_tests{
use std::str::FromStr;
pub(crate) use subxt::OnlineClient;
use crate::registrar::signer::TxBuilder;
use crate::tx::extrinsics::extrinsics_submitter::ExtrinsicSubmitter;
use crate::types::MandalaConfig;
use crate::registrar::key_manager::prelude::PublicAddress;
use crate::types::MandalaExtrinsics;
use sp_core::sr25519::Pair;
use sp_core::crypto::Pair as CryptoPair;


fn mock_pair() -> sp_core::sr25519::Pair {
sp_keyring::Sr25519Keyring::Alice.pair()
}
async fn mock_client() -> crate::types::NodeClient {
OnlineClient::<MandalaConfig>::new().await.unwrap()
}
#[tokio::test]
async fn submit_successful_tests() {
let address= "5DJk1gegyQJk6BNs7LceZ1akt5e9fpm4gUYGzcfpKaLG9Mmb";
let newAddress = PublicAddress::from_str(address).unwrap();
let pair = mock_pair();
let node_client = mock_client().await;

let value = rand::random();
// Create the payload using the `construct` function from `BalanceTransfer`
let payload = crate::tx::extrinsics::prelude::transfer_balance::constructor::BalanceTransfer::construct(newAddress, value);
let extrinsic = TxBuilder::signed(&node_client, pair, payload)
.await
.unwrap().0;

// Create a mock MandalaExtrinsics object
let tx = MandalaExtrinsics::new(extrinsic);
let result = ExtrinsicSubmitter::submit(tx).await;

assert!(result.is_ok());


}
#[tokio::test]
#[should_panic]
async fn submit_failure_tests() {
let address = "5DJk1gegyQJk6BNs7LceZ1akt5e9fpm4gUYGzcfpKaLG9Mmb";
let newAddress = PublicAddress::from_str(address).unwrap();
let pair = mock_pair();
let node_client = mock_client().await;

// Assign a invalid value
let value: u128 = 0;

// Create the payload using the `construct` function from `BalanceTransfer`
let payload = crate::tx::extrinsics::prelude::transfer_balance::constructor::BalanceTransfer::construct(newAddress, value);

// Introduce a failure by using an invalid client for signing
let extrinsic = TxBuilder::signed(&node_client, pair, payload)
.await
.unwrap().0;

// Create a mock MandalaExtrinsics object
let tx = MandalaExtrinsics::new(extrinsic);
let result = ExtrinsicSubmitter::submit(tx).await;

// Assert that the result is an error indicating the failure
assert!(result.is_err());
// Add additional assertions or error handling as needed
}

}