Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Merge pull request #22 from Arrowana/program-test
Browse files Browse the repository at this point in the history
implement program test rather than using TestValidator
  • Loading branch information
bartosz-lipinski authored Jun 17, 2021
2 parents f888b80 + 31c43b9 commit b4bd88a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 48 deletions.
38 changes: 1 addition & 37 deletions program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use solana_program::{
solana_program::declare_id!("BpfProgram1111111111111111111111111111111111");

entrypoint!(process_instruction);
fn process_instruction(
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
Expand All @@ -18,39 +18,3 @@ fn process_instruction(
);
Ok(())
}

#[cfg(test)]
mod test {
use {
super::*,
assert_matches::*,
solana_program::instruction::{AccountMeta, Instruction},
solana_program_test::*,
solana_sdk::{signature::Signer, transaction::Transaction},
};

#[tokio::test]
async fn test_transaction() {
let program_id = Pubkey::new_unique();

let (mut banks_client, payer, recent_blockhash) = ProgramTest::new(
"bpf_program_template",
program_id,
processor!(process_instruction),
)
.start()
.await;

let mut transaction = Transaction::new_with_payer(
&[Instruction {
program_id,
accounts: vec![AccountMeta::new(payer.pubkey(), false)],
data: vec![1, 2, 3],
}],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);

assert_matches!(banks_client.process_transaction(transaction).await, Ok(()));
}
}
23 changes: 12 additions & 11 deletions program/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#![cfg(feature = "test-bpf")]

use {
assert_matches::*,
bpf_program_template::process_instruction,
assert_matches::assert_matches,
solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
},
solana_sdk::{signature::Signer, transaction::Transaction},
solana_validator::test_validator::*,
solana_program_test::{processor, tokio, ProgramTest}
};

#[test]
fn test_validator_transaction() {
#[tokio::test]
async fn test_transaction() {
let program_id = Pubkey::new_unique();
let pt = ProgramTest::new(
"bpf_program_template",
program_id,
processor!(process_instruction),
);

let (test_validator, payer) = TestValidatorGenesis::default()
.add_program("bpf_program_template", program_id)
.start();
let (rpc_client, recent_blockhash, _fee_calculator) = test_validator.rpc_client();
let (mut banks_client, payer, recent_blockhash) = pt.start().await;

let mut transaction = Transaction::new_with_payer(
&[Instruction {
Expand All @@ -29,5 +30,5 @@ fn test_validator_transaction() {
);
transaction.sign(&[&payer], recent_blockhash);

assert_matches!(rpc_client.send_and_confirm_transaction(&transaction), Ok(_));
assert_matches!(banks_client.process_transaction(transaction).await, Ok(()));
}

0 comments on commit b4bd88a

Please sign in to comment.