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

feat: add rust integration tests #3108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/tutorial/basic-0/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ wallet = "~/.config/solana/id.json"
basic_0 = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[scripts]
test = "yarn run mocha -t 1000000 tests/"
test = "yarn run mocha -t 1000000 tests/ && cargo test"
10 changes: 10 additions & 0 deletions examples/tutorial/basic-0/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "tests"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-client = { path = "../../../../client" }
basic-0 = { version = "0.1.0", path = "../programs/basic-0" }
2 changes: 2 additions & 0 deletions examples/tutorial/basic-0/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod test_ixs;
36 changes: 36 additions & 0 deletions examples/tutorial/basic-0/tests/src/test_ixs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use anchor_client::{
solana_sdk::{
commitment_config::CommitmentConfig,
signature::{read_keypair_file, Keypair},
},
Client, Cluster, Program,
};
use basic_0::{accounts, instruction};
use std::sync::Arc;

fn setup_program() -> (Client<Arc<Keypair>>, Program<Arc<Keypair>>, Keypair) {
let anchor_wallet = std::env::var("ANCHOR_WALLET").unwrap();
let payer = Arc::new(read_keypair_file(&anchor_wallet).unwrap());
let client = Client::new_with_options(
Cluster::Localnet,
Arc::clone(&payer),
CommitmentConfig::confirmed(),
);
let program = client.program(basic_0::id()).unwrap();

(client, program, payer.insecure_clone())
}

#[test]
fn test_initialize() {
let (_client, program, _authority) = setup_program();

let tx = program
.request()
.accounts(accounts::Initialize {})
.args(instruction::Initialize {})
.send()
.expect("Failed to send initialize account transaction");

println!("Initialize transaction signature: {}", tx);
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-1/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ wallet = "~/.config/solana/id.json"
basic_1 = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[scripts]
test = "yarn run mocha -t 1000000 tests/"
test = "yarn run mocha -t 1000000 tests/ && cargo test"
10 changes: 10 additions & 0 deletions examples/tutorial/basic-1/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "tests"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-client = { path = "../../../../client" }
basic-1 = { version = "0.1.0", path = "../programs/basic-1" }
2 changes: 2 additions & 0 deletions examples/tutorial/basic-1/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod test_ixs;
82 changes: 82 additions & 0 deletions examples/tutorial/basic-1/tests/src/test_ixs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use anchor_client::{
solana_sdk::{
commitment_config::CommitmentConfig,
signature::{read_keypair_file, Keypair, Signer},
system_program,
},
Client, Cluster, Program,
};
use basic_1::{accounts, instruction, MyAccount};
use std::sync::Arc;

fn setup_program() -> (Client<Arc<Keypair>>, Program<Arc<Keypair>>, Keypair) {
let anchor_wallet = std::env::var("ANCHOR_WALLET").unwrap();
let payer = Arc::new(read_keypair_file(&anchor_wallet).unwrap());
let client = Client::new_with_options(
Cluster::Localnet,
Arc::clone(&payer),
CommitmentConfig::confirmed(),
);
let program = client.program(basic_1::id()).unwrap();

(client, program, payer.insecure_clone())
}

#[test]
fn test_create_and_initialize_account() {
let (_client, program, authority) = setup_program();

let my_account = Keypair::new();

let _tx = program
.request()
.accounts(accounts::Initialize {
my_account: my_account.pubkey(),
user: authority.pubkey(),
system_program: system_program::id(),
})
.args(instruction::Initialize { data: 1234 })
.signer(&my_account)
.send()
.expect("Failed to send initialize account transaction");

let account: MyAccount = program
.account(my_account.pubkey())
.expect("Failed to fetch account");

assert_eq!(account.data, 1234);
}

#[test]
fn test_update_account() {
let (_client, program, authority) = setup_program();

let my_account = Keypair::new();

let _tx = program
.request()
.accounts(accounts::Initialize {
my_account: my_account.pubkey(),
user: authority.pubkey(),
system_program: system_program::id(),
})
.args(instruction::Initialize { data: 1234 })
.signer(&my_account)
.send()
.expect("Failed to send initialize account transaction");

let _tx = program
.request()
.accounts(accounts::Update {
my_account: my_account.pubkey(),
})
.args(instruction::Update { data: 4321 })
.send()
.expect("Failed to send update account transaction");

let account: MyAccount = program
.account(my_account.pubkey())
.expect("Failed to fetch account");

assert_eq!(account.data, 4321);
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-2/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ wallet = "~/.config/solana/id.json"
basic_2 = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[scripts]
test = "yarn run mocha -t 1000000 tests/"
test = "yarn run mocha -t 1000000 tests/ && cargo test"
10 changes: 10 additions & 0 deletions examples/tutorial/basic-2/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "tests"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-client = { path = "../../../../client" }
basic-2 = { version = "0.1.0", path = "../programs/basic-2" }
2 changes: 2 additions & 0 deletions examples/tutorial/basic-2/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod test_ixs;
89 changes: 89 additions & 0 deletions examples/tutorial/basic-2/tests/src/test_ixs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use anchor_client::{
solana_sdk::{
commitment_config::CommitmentConfig,
signature::{read_keypair_file, Keypair, Signer},
system_program,
},
Client, Cluster, Program,
};
use basic_2::{accounts, instruction, Counter};
use std::sync::Arc;

fn setup_program() -> (Client<Arc<Keypair>>, Program<Arc<Keypair>>, Keypair) {
let anchor_wallet = std::env::var("ANCHOR_WALLET").unwrap();
let payer = Arc::new(read_keypair_file(&anchor_wallet).unwrap());
let client = Client::new_with_options(
Cluster::Localnet,
Arc::clone(&payer),
CommitmentConfig::confirmed(),
);
let program = client.program(basic_2::id()).unwrap();

(client, program, payer.insecure_clone())
}

#[test]
fn test_create_counter() {
let (_client, program, authority) = setup_program();

let counter = Keypair::new();

let _tx = program
.request()
.accounts(accounts::Create {
counter: counter.pubkey(),
user: authority.pubkey(),
system_program: system_program::id(),
})
.args(instruction::Create {
authority: authority.pubkey(),
})
.signer(&counter)
.send()
.expect("Failed to send create counter transaction");

let counter_account: Counter = program
.account(counter.pubkey())
.expect("Failed to fetch counter");

assert_eq!(counter_account.authority, authority.pubkey());
assert_eq!(counter_account.count, 0);
}

#[test]
fn test_update_counter() {
let (_client, program, authority) = setup_program();

let counter = Keypair::new();

let _tx = program
.request()
.accounts(accounts::Create {
counter: counter.pubkey(),
user: authority.pubkey(),
system_program: system_program::id(),
})
.args(instruction::Create {
authority: authority.pubkey(),
})
.signer(&counter)
.send()
.expect("Failed to send create counter transaction");

let _tx = program
.request()
.accounts(accounts::Increment {
counter: counter.pubkey(),
authority: authority.pubkey(),
})
.args(instruction::Increment {})
.send()
.expect("Failed to send increment counter transaction");

let counter_account: Counter = program
.account(counter.pubkey())
.expect("Failed to fetch counter");

assert_eq!(counter_account.authority, authority.pubkey());
assert_eq!(counter_account.count, 1);
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-3/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ puppet = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
puppet_master = "HmbTLCmaGvZhKnn1Zfa1JVnp7vkMV4DYVxPLWBVoN65L"

[scripts]
test = "yarn run mocha -t 1000000 tests/"
test = "yarn run mocha -t 1000000 tests/ && cargo test"
11 changes: 11 additions & 0 deletions examples/tutorial/basic-3/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "tests"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-client = { path = "../../../../client" }
puppet = { version = "0.1.0", path = "../programs/puppet" }
puppet-master = { version = "0.1.0", path = "../programs/puppet-master" }
2 changes: 2 additions & 0 deletions examples/tutorial/basic-3/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod test_ixs;
69 changes: 69 additions & 0 deletions examples/tutorial/basic-3/tests/src/test_ixs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use anchor_client::{
solana_sdk::{
commitment_config::CommitmentConfig,
signature::{read_keypair_file, Keypair, Signer},
system_program,
},
Client, Cluster, Program,
};
use std::sync::Arc;

fn setup_programs() -> (
Client<Arc<Keypair>>,
Program<Arc<Keypair>>,
Program<Arc<Keypair>>,
Keypair,
) {
let anchor_wallet = std::env::var("ANCHOR_WALLET").unwrap();
let payer = Arc::new(read_keypair_file(&anchor_wallet).unwrap());
let client = Client::new_with_options(
Cluster::Localnet,
Arc::clone(&payer),
CommitmentConfig::confirmed(),
);
let puppet_master_program = client.program(puppet_master::id()).unwrap();
let puppet_program = client.program(puppet::id()).unwrap();

(
client,
puppet_master_program,
puppet_program,
payer.insecure_clone(),
)
}

#[test]
fn test_perform_cpi() {
let (_client, puppet_master_program, puppet_program, authority) = setup_programs();

let new_puppet_account = Keypair::new();

let _tx = puppet_program
.request()
.accounts(puppet::accounts::Initialize {
puppet: new_puppet_account.pubkey(),
user: authority.pubkey(),
system_program: system_program::id(),
})
.args(puppet::instruction::Initialize {})
.signer(&authority)
.signer(&new_puppet_account)
.send()
.expect("Failed to send initialize puppet account transaction");

let _tx = puppet_master_program
.request()
.accounts(puppet_master::accounts::PullStrings {
puppet: new_puppet_account.pubkey(),
puppet_program: puppet_program.id(),
})
.args(puppet_master::instruction::PullStrings { data: 111 })
.send()
.expect("Failed to send pull strings transaction");

let puppet_account: puppet::Data = puppet_program
.account(new_puppet_account.pubkey())
.expect("Failed to fetch puppet account");

assert_eq!(puppet_account.data, 111);
}
2 changes: 1 addition & 1 deletion examples/tutorial/basic-4/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ wallet = "~/.config/solana/id.json"
basic_4 = "CwrqeMj2U8tFr1Rhkgwc84tpAsqbt9pTt2a4taoTADPr"

[scripts]
test = "yarn run mocha -t 1000000 tests/"
test = "yarn run mocha -t 1000000 tests/ && cargo test"
10 changes: 10 additions & 0 deletions examples/tutorial/basic-4/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "tests"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-client = { path = "../../../../client" }
basic-4 = { version = "0.1.0", path = "../programs/basic-4" }
2 changes: 2 additions & 0 deletions examples/tutorial/basic-4/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod test_ixs;
Loading