test.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use processing_instructions_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. async fn setup() -> (BanksClient, Keypair, Hash) {
  6. let mut program_test = ProgramTest::new(
  7. "processing_instructions_program",
  8. processing_instructions_api::ID,
  9. processor!(processing_instructions_program::process_instruction),
  10. );
  11. program_test.prefer_bpf(true);
  12. program_test.start().await
  13. }
  14. // Helper function to assert the presence of log messages
  15. fn assert_log_contains(logs: &[String], expected: &str) {
  16. assert!(
  17. logs.iter().any(|msg| msg.contains(expected)),
  18. "Missing log: {}",
  19. expected
  20. );
  21. }
  22. #[tokio::test]
  23. async fn run_test() {
  24. // Setup test
  25. let (mut banks, payer, blockhash) = setup().await;
  26. // Submit transaction.
  27. let ix1 = go_to_the_park(payer.pubkey(), GoToTheParkData::new("Jimmy".to_string(), 3));
  28. let ix2 = go_to_the_park(payer.pubkey(), GoToTheParkData::new("Mary".to_string(), 10));
  29. let tx = Transaction::new_signed_with_payer(
  30. &[ix1, ix2],
  31. Some(&payer.pubkey()),
  32. &[&payer],
  33. blockhash,
  34. );
  35. let res = banks.process_transaction_with_metadata(tx).await;
  36. assert!(res.is_ok(), "Process Transaction failed: {:?}", res.err());
  37. let tx_result = res.unwrap();
  38. assert!(
  39. tx_result.result.is_ok(),
  40. "Transaction failed: {:?}",
  41. tx_result.result.err()
  42. );
  43. let metadata = tx_result.metadata.unwrap();
  44. // check the logs
  45. // we got 2 instructions, we must see 2 consecutive logs
  46. // - Welcome to the park, {name}!
  47. // - You are NOT tall enough... and You are tall enough...
  48. assert_log_contains(&metadata.log_messages, "Welcome to the park, Jimmy!");
  49. assert_log_contains(&metadata.log_messages, "You are NOT tall enough");
  50. assert_log_contains(&metadata.log_messages, "Welcome to the park, Mary!");
  51. assert_log_contains(&metadata.log_messages, "You are tall enough");
  52. }