github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/dev/wasm/escrow/tests/integration.rs (about)

     1  //! This integration test tries to run and call the generated wasm.
     2  //! It depends on a Wasm build being available, which you can create with `cargo wasm`.
     3  //! Then running `cargo integration-test` will validate we can properly call into that generated Wasm.
     4  //!
     5  //! You can easily convert unit tests to integration tests as follows:
     6  //! 1. Copy them over verbatim
     7  //! 2. Then change
     8  //!      let mut deps = mock_dependencies(20, &[]);
     9  //!    to
    10  //!      let mut deps = mock_instance(WASM, &[]);
    11  //! 3. If you access raw storage, where ever you see something like:
    12  //!      deps.storage.get(CONFIG_KEY).expect("no data stored");
    13  //!    replace it with:
    14  //!      deps.with_storage(|store| {
    15  //!          let data = store.get(CONFIG_KEY).expect("no data stored");
    16  //!          //...
    17  //!      });
    18  //! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)
    19  
    20  use cosmwasm_std::{
    21      coins, Addr, BlockInfo, Coin, ContractInfo, Env, MessageInfo, Response, Timestamp,
    22      TransactionInfo,
    23  };
    24  use cosmwasm_storage::to_length_prefixed;
    25  use cosmwasm_vm::testing::{instantiate, mock_info, mock_instance};
    26  use cosmwasm_vm::{from_slice, Storage};
    27  
    28  use cosmwasm_std::testing::MOCK_CONTRACT_ADDR;
    29  use cw_escrow::msg::InstantiateMsg;
    30  use cw_escrow::state::State;
    31  
    32  // This line will test the output of cargo wasm
    33  static WASM: &[u8] = include_bytes!("../target/wasm32-unknown-unknown/release/cw_escrow.wasm");
    34  // You can uncomment this line instead to test productionified build from rust-optimizer
    35  // static WASM: &[u8] = include_bytes!("../contract.wasm");
    36  
    37  fn init_msg_expire_by_height(height: u64) -> InstantiateMsg {
    38      InstantiateMsg {
    39          arbiter: String::from("verifies"),
    40          recipient: String::from("benefits"),
    41          end_height: Some(height),
    42          end_time: None,
    43      }
    44  }
    45  
    46  fn mock_env_info_height(signer: &str, sent: &[Coin], height: u64, time: u64) -> (Env, MessageInfo) {
    47      let env = Env {
    48          block: BlockInfo {
    49              height,
    50              time: Timestamp::from_nanos(time),
    51              chain_id: String::from("test"),
    52          },
    53          contract: ContractInfo {
    54              address: Addr::unchecked(MOCK_CONTRACT_ADDR),
    55          },
    56          transaction: Some(TransactionInfo { index: 3 }),
    57      };
    58      let info = mock_info(signer, sent);
    59      return (env, info);
    60  }
    61  
    62  #[test]
    63  fn proper_initialization() {
    64      let mut deps = mock_instance(WASM, &[]);
    65  
    66      let msg = init_msg_expire_by_height(1000);
    67      let (env, info) = mock_env_info_height("creator", &coins(1000, "earth"), 876, 0);
    68      let res: Response = instantiate(&mut deps, env, info, msg).unwrap();
    69      assert_eq!(0, res.messages.len());
    70  
    71      // it worked, let's query the state
    72      deps.with_storage(|store| {
    73          let config_key_raw = to_length_prefixed(b"config");
    74          let state: State =
    75              from_slice(&store.get(&config_key_raw).0.unwrap().unwrap(), 2048).unwrap();
    76          assert_eq!(
    77              state,
    78              State {
    79                  arbiter: Addr::unchecked("verifies"),
    80                  recipient: Addr::unchecked("benefits"),
    81                  source: Addr::unchecked("creator"),
    82                  end_height: Some(1000),
    83                  end_time: None,
    84              }
    85          );
    86          Ok(())
    87      })
    88      .unwrap();
    89  }