소스 검색

add rust test to pda example (#448)

* add rust test to pda example

* Update test.rs
Perelyn 3 일 전
부모
커밋
7288bd5f3f
3개의 변경된 파일114개의 추가작업 그리고 1개의 파일을 삭제
  1. 7 0
      Cargo.lock
  2. 9 1
      basics/program-derived-addresses/native/program/Cargo.toml
  3. 98 0
      basics/program-derived-addresses/native/program/tests/test.rs

+ 7 - 0
Cargo.lock

@@ -2191,8 +2191,15 @@ version = "0.1.0"
 dependencies = [
  "borsh 1.5.7",
  "borsh-derive 1.5.7",
+ "litesvm",
+ "solana-instruction 3.0.0",
+ "solana-keypair",
+ "solana-native-token 3.0.0",
  "solana-program 3.0.0",
+ "solana-pubkey 3.0.0",
+ "solana-rent 3.0.0",
  "solana-system-interface 2.0.0",
+ "solana-transaction",
 ]
 
 [[package]]

+ 9 - 1
basics/program-derived-addresses/native/program/Cargo.toml

@@ -13,9 +13,17 @@ solana-system-interface.workspace = true
 crate-type = ["cdylib", "lib"]
 
 [features]
-anchor-debug = []
 custom-heap = []
 custom-panic = []
 
 [lints.rust]
 unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
+
+[dev-dependencies]
+litesvm = "0.8.1"
+solana-instruction = "3.0.0"
+solana-keypair = "3.0.1"
+solana-pubkey = "3.0.0"
+solana-transaction = "3.0.1"
+solana-native-token = "3.0.0"
+solana-rent = "3.0.0"

+ 98 - 0
basics/program-derived-addresses/native/program/tests/test.rs

@@ -0,0 +1,98 @@
+use borsh::BorshDeserialize;
+use litesvm::LiteSVM;
+use program_derived_addresses_native_program::state::{IncrementPageVisits, PageVisits};
+use solana_instruction::{AccountMeta, Instruction};
+use solana_keypair::{Keypair, Signer};
+use solana_native_token::LAMPORTS_PER_SOL;
+use solana_pubkey::Pubkey;
+use solana_rent::Rent;
+use solana_system_interface::instruction::create_account;
+use solana_transaction::Transaction;
+
+#[test]
+fn test_pda() {
+    let mut svm = LiteSVM::new();
+
+    let program_id = Pubkey::new_unique();
+    let program_bytes =
+        include_bytes!("../../tests/fixtures/program_derived_addresses_native_program.so");
+    svm.add_program(program_id, program_bytes).unwrap();
+
+    let payer = Keypair::new();
+    svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap();
+
+    let test_user = Keypair::new();
+
+    let rent = Rent::default();
+
+    let create_ix = create_account(
+        &payer.pubkey(),
+        &test_user.pubkey(),
+        solana_rent::Rent::minimum_balance(&rent, 0),
+        0,
+        &solana_system_interface::program::ID,
+    );
+
+    let tx = Transaction::new_signed_with_payer(
+        &[create_ix],
+        Some(&payer.pubkey()),
+        &[&payer, &test_user],
+        svm.latest_blockhash(),
+    );
+
+    let _ = svm.send_transaction(tx).is_ok();
+
+    let (pda, bump) =
+        Pubkey::find_program_address(&[b"page_visits", test_user.pubkey().as_ref()], &program_id);
+
+    let data = borsh::to_vec(&PageVisits {
+        page_visits: 0,
+        bump,
+    })
+    .unwrap();
+
+    let ix = Instruction {
+        program_id,
+        accounts: vec![
+            AccountMeta::new(pda, false),
+            AccountMeta::new(test_user.pubkey(), false),
+            AccountMeta::new(payer.pubkey(), true),
+            AccountMeta::new(solana_system_interface::program::ID, false),
+        ],
+        data,
+    };
+
+    let tx = Transaction::new_signed_with_payer(
+        &[ix],
+        Some(&payer.pubkey()),
+        &[&payer],
+        svm.latest_blockhash(),
+    );
+
+    let _ = svm.send_transaction(tx).is_ok();
+
+    let data = borsh::to_vec(&IncrementPageVisits {}).unwrap();
+
+    let ix = Instruction {
+        program_id,
+        accounts: vec![
+            AccountMeta::new(pda, false),
+            AccountMeta::new(payer.pubkey(), true),
+        ],
+        data,
+    };
+
+    let tx = Transaction::new_signed_with_payer(
+        &[ix],
+        Some(&payer.pubkey()),
+        &[&payer],
+        svm.latest_blockhash(),
+    );
+
+    let _ = svm.send_transaction(tx).is_ok();
+
+    // read page visits
+    let account_info = svm.get_account(&pda).unwrap();
+    let read_page_visits = PageVisits::try_from_slice(&account_info.data).unwrap();
+    assert_eq!(read_page_visits.page_visits, 1);
+}