Skip to content

Commit aed23fb

Browse files
committed
add rust test for favorites example
1 parent f3e8e8d commit aed23fb

File tree

6 files changed

+99
-4
lines changed

6 files changed

+99
-4
lines changed

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/favorites/native/program/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ solana-system-interface.workspace = true
1313
crate-type = ["cdylib", "lib"]
1414

1515
[features]
16-
anchor-debug = []
1716
custom-heap = []
1817
custom-panic = []
1918

2019
[lints.rust]
2120
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
21+
22+
[dev-dependencies]
23+
litesvm = "0.8.1"
24+
solana-instruction = "3.0.0"
25+
solana-keypair = "3.0.1"
26+
solana-native-token = "3.0.0"
27+
solana-pubkey = "3.0.0"
28+
solana-transaction = "3.0.1"

basics/favorites/native/program/src/processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};
22

33
use crate::instructions::{create_pda::*, get_pda::*};
4-
use crate::state::Favorites;
4+
pub use crate::state::Favorites;
55
use borsh::{BorshDeserialize, BorshSerialize};
66

77
#[derive(BorshDeserialize, BorshSerialize)]

basics/favorites/native/program/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use borsh::{BorshDeserialize, BorshSerialize};
22

3-
#[derive(BorshDeserialize, BorshSerialize, Debug)]
3+
#[derive(BorshDeserialize, BorshSerialize, Debug, Clone)]
44
pub struct Favorites {
55
pub number: u64,
66
pub color: String,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use borsh::BorshDeserialize;
2+
use favorites_native::processor::{Favorites, FavoritesInstruction};
3+
use litesvm::LiteSVM;
4+
use solana_instruction::{AccountMeta, Instruction};
5+
use solana_keypair::{Keypair, Signer};
6+
use solana_native_token::LAMPORTS_PER_SOL;
7+
use solana_pubkey::Pubkey;
8+
use solana_transaction::Transaction;
9+
10+
#[test]
11+
fn test_favorites() {
12+
let program_id = Pubkey::new_unique();
13+
let program_bytes = include_bytes!("../../../../../target/deploy/favorites_native.so");
14+
15+
let mut svm = LiteSVM::new();
16+
svm.add_program(program_id, program_bytes).unwrap();
17+
18+
let payer = Keypair::new();
19+
20+
svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
21+
22+
let favorites_pda =
23+
Pubkey::find_program_address(&[b"favorite", payer.pubkey().as_ref()], &program_id).0;
24+
25+
let favorites = Favorites {
26+
number: 42,
27+
color: "blue".to_string(),
28+
hobbies: vec![
29+
"coding".to_string(),
30+
"reading".to_string(),
31+
"travelling".to_string(),
32+
],
33+
};
34+
35+
let data = borsh::to_vec(&FavoritesInstruction::CreatePda(favorites.clone())).unwrap();
36+
37+
let ix = Instruction {
38+
program_id,
39+
accounts: vec![
40+
AccountMeta::new(payer.pubkey(), true),
41+
AccountMeta::new(favorites_pda, false),
42+
AccountMeta::new(solana_system_interface::program::ID, false),
43+
],
44+
data,
45+
};
46+
47+
let tx = Transaction::new_signed_with_payer(
48+
&[ix],
49+
Some(&payer.pubkey()),
50+
&[&payer],
51+
svm.latest_blockhash(),
52+
);
53+
54+
let _ = svm.send_transaction(tx).is_ok();
55+
56+
let favorites_account_data = svm.get_account(&favorites_pda).unwrap().data;
57+
58+
let deserialized_data = Favorites::try_from_slice(&favorites_account_data).unwrap();
59+
60+
assert_eq!(deserialized_data.number, favorites.number);
61+
assert_eq!(deserialized_data.color, favorites.color);
62+
assert_eq!(deserialized_data.hobbies, favorites.hobbies);
63+
64+
let data = borsh::to_vec(&FavoritesInstruction::GetPda).unwrap();
65+
66+
let ix = Instruction {
67+
program_id,
68+
accounts: vec![
69+
AccountMeta::new(payer.pubkey(), true),
70+
AccountMeta::new(favorites_pda, false),
71+
],
72+
data,
73+
};
74+
75+
let tx = Transaction::new_signed_with_payer(
76+
&[ix],
77+
Some(&payer.pubkey()),
78+
&[&payer],
79+
svm.latest_blockhash(),
80+
);
81+
82+
let _ = svm.send_transaction(tx).is_ok();
83+
}

basics/hello-solana/native/program/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ solana-program.workspace = true
1010
crate-type = ["cdylib", "lib"]
1111

1212
[features]
13-
anchor-debug = []
1413
custom-heap = []
1514
custom-panic = []
1615

0 commit comments

Comments
 (0)