Skip to content

Commit

Permalink
test: update tests to use TestRpcProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ifiokjr committed Oct 8, 2024
1 parent 806642f commit 644d864
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions programs/example_client/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Keypair;
use test_utils::SECRET_KEY_WALLET;
use test_utils_solana::ProgramTest;
use test_utils_solana::ProgramTestContext;
use test_utils_solana::TestRpcProvider;
use test_utils_solana::anchor_processor;
use test_utils_solana::prelude::*;
use wallet_standard_wallets::MemoryWallet;
use wasm_client_solana::LOCALNET;
use wasm_client_solana::SolanaRpcClient;

#[test_log::test(tokio::test)]
async fn initialize() -> Result<()> {
let keypair = get_wallet_keypair();
let pubkey = keypair.pubkey();
let (mut ctx, rpc) = create_program_test().await;
let (_, rpc) = create_program_test().await;
let mut wallet = MemoryWallet::new(rpc.clone(), &[keypair]);

wallet.connect().await?;
Expand All @@ -35,10 +34,12 @@ async fn initialize() -> Result<()> {
.initialize()
.accounts(example_program::accounts::Initialize { unchecked: pubkey })
.build()
.simulate_banks_client_transaction(&mut ctx.banks_client)
.simulate_transaction()
.await?;

check!(simulation.result.unwrap().is_ok());
log::info!("simulation: {simulation:#?}");

check!(simulation.value.err.is_none());

Ok(())
}
Expand All @@ -49,7 +50,7 @@ async fn composition() -> Result<()> {
let signer_keypair = Keypair::new();
let signer = signer_keypair.pubkey();
let keypair = get_wallet_keypair();
let (mut ctx, rpc) = create_program_test().await;
let (_, rpc) = create_program_test().await;
let mut wallet = MemoryWallet::new(rpc.clone(), &[keypair]);

wallet.connect().await?;
Expand All @@ -71,36 +72,34 @@ async fn composition() -> Result<()> {
.accounts(example_program::accounts::Initialize { unchecked })
.build();

let simulation = request
.simulate_banks_client_transaction(&mut ctx.banks_client)
.await?;
let simulation = request.simulate_transaction().await?;

check!(simulation.result.unwrap().is_ok());
check!(simulation.value.err.is_none());

request
.sign_and_process_banks_client_transaction(&mut ctx.banks_client)
.await?;
request.sign_transaction().await?;

Ok(())
}

async fn create_program_test() -> (ProgramTestContext, SolanaRpcClient) {
async fn create_program_test() -> (TestRpcProvider, SolanaRpcClient) {
let pubkey = get_wallet_keypair().pubkey();
let mut program_test = ProgramTest::new(
"example_program",
example_program::ID_CONST,
anchor_processor!(example_program),
);
let rpc = SolanaRpcClient::new_with_commitment(LOCALNET, CommitmentConfig::finalized());

program_test.add_account(pubkey, Account {
lamports: sol_to_lamports(1.0),
..Account::default()
});

let ctx = program_test.start_with_context().await;
let test_rpc_provider = TestRpcProvider::new(ctx);
let rpc =
SolanaRpcClient::new_with_provider(test_rpc_provider.arc(), CommitmentConfig::finalized());

(ctx, rpc)
(test_rpc_provider, rpc)
}

pub fn get_wallet_keypair() -> Keypair {
Expand Down

0 comments on commit 644d864

Please sign in to comment.