test.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use counter_api::prelude::*;
  2. use solana_program::hash::Hash;
  3. use solana_program_test::{processor, BanksClient, ProgramTest};
  4. use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction};
  5. use steel::*;
  6. async fn setup() -> (BanksClient, Keypair, Hash) {
  7. let mut program_test = ProgramTest::new(
  8. "counter_program",
  9. counter_api::ID,
  10. processor!(counter_program::process_instruction),
  11. );
  12. program_test.prefer_bpf(true);
  13. program_test.start().await
  14. }
  15. #[tokio::test]
  16. async fn should_initialize_and_increment_counter() {
  17. // Setup test
  18. let (mut banks, payer, blockhash) = setup().await;
  19. // Submit initialize transaction.
  20. let ix = initialize(payer.pubkey());
  21. let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
  22. let res = banks.process_transaction(tx).await;
  23. assert!(res.is_ok());
  24. // Verify counter was initialized.
  25. let counter_address = counter_pda().0;
  26. let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
  27. let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
  28. assert_eq!(counter_account.owner, counter_api::ID);
  29. assert_eq!(counter.value, 0);
  30. // Submit add transaction.
  31. let ix = increment(payer.pubkey(), 42);
  32. let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash);
  33. let res = banks.process_transaction(tx).await;
  34. assert!(res.is_ok());
  35. // Verify counter was incremented.
  36. let counter_account = banks.get_account(counter_address).await.unwrap().unwrap();
  37. let counter = Counter::try_from_bytes(&counter_account.data).unwrap();
  38. assert_eq!(counter.value, 42);
  39. }