From b57f7ee30762e63e9f00cd89ba31845e02e3e8a6 Mon Sep 17 00:00:00 2001 From: Dariusz Depta <141360751+DariuszDepta@users.noreply.github.com> Date: Thu, 20 Jun 2024 10:43:30 +0200 Subject: [PATCH] Enabled using `addr_make` inside `AppBuilder::build` (#189) Enabled using addr_make inside AppBuilder::build --- Cargo.lock | 4 +- src/app.rs | 12 +-- src/app_builder.rs | 35 +++---- src/tests/test_ibc.rs | 2 +- src/wasm.rs | 6 +- tests/mod.rs | 1 + tests/test_app/mod.rs | 1 + tests/test_app/test_initialize_app.rs | 21 +++++ tests/test_app_builder/test_with_bank.rs | 3 +- tests/test_app_builder/test_with_gov.rs | 3 +- tests/test_app_builder/test_with_ibc.rs | 3 +- tests/test_app_builder/test_with_staking.rs | 5 +- tests/test_app_builder/test_with_stargate.rs | 15 +-- tests/test_app_builder/test_with_storage.rs | 3 +- tests/test_app_builder/test_with_wasm.rs | 8 +- tests/test_bank/mod.rs | 1 + tests/test_bank/test_init_balance.rs | 96 ++++++++++++++++++++ 17 files changed, 170 insertions(+), 49 deletions(-) create mode 100644 tests/test_app/test_initialize_app.rs create mode 100644 tests/test_bank/mod.rs create mode 100644 tests/test_bank/test_init_balance.rs diff --git a/Cargo.lock b/Cargo.lock index eb015e47..ecd78733 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,9 +789,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "subtle" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "0d0208408ba0c3df17ed26eb06992cb1a1268d41b2c0e12e65203fbe3972cee5" [[package]] name = "syn" diff --git a/src/app.rs b/src/app.rs index 39540a69..b0798d2d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -67,9 +67,9 @@ pub struct App< } /// No-op application initialization function. -pub fn no_init( +pub fn no_init( router: &mut Router, - api: &dyn Api, + api: &ApiT, storage: &mut dyn Storage, ) { let _ = (router, api, storage); @@ -96,7 +96,7 @@ impl BasicApp { GovFailingModule, StargateFailing, >, - &dyn Api, + &MockApi, &mut dyn Storage, ), { @@ -121,7 +121,7 @@ where GovFailingModule, StargateFailing, >, - &dyn Api, + &MockApi, &mut dyn Storage, ), { @@ -216,7 +216,7 @@ where where F: FnOnce( &mut Router, - &dyn Api, + &ApiT, &mut dyn Storage, ) -> T, { @@ -228,7 +228,7 @@ where where F: FnOnce( &Router, - &dyn Api, + &ApiT, &dyn Storage, ) -> T, { diff --git a/src/app_builder.rs b/src/app_builder.rs index ad9b0dc5..7d49815d 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -16,7 +16,7 @@ use std::fmt::Debug; /// /// ``` /// # use cosmwasm_std::Empty; -/// # use cw_multi_test::{BasicAppBuilder, FailingModule, Module, no_init}; +/// # use cw_multi_test::{no_init, BasicAppBuilder, FailingModule, Module}; /// # type MyHandler = FailingModule; /// # type MyExecC = Empty; /// # type MyQueryC = Empty; @@ -530,9 +530,9 @@ where self } - /// Builds final `App`. At this point all components type have to be properly related to each - /// other. If there are some generics related compilation errors, make sure that all components - /// are properly relating to each other. + /// Builds the final [App] with initialization. + /// + /// At this point all component types have to be properly related to each other. pub fn build( self, init_fn: F, @@ -550,28 +550,29 @@ where StargateT: Stargate, F: FnOnce( &mut Router, - &dyn Api, + &ApiT, &mut dyn Storage, ), { - let router = Router { - wasm: self.wasm, - bank: self.bank, - custom: self.custom, - staking: self.staking, - distribution: self.distribution, - ibc: self.ibc, - gov: self.gov, - stargate: self.stargate, - }; - + // build the final application let mut app = App { - router, + router: Router { + wasm: self.wasm, + bank: self.bank, + custom: self.custom, + staking: self.staking, + distribution: self.distribution, + ibc: self.ibc, + gov: self.gov, + stargate: self.stargate, + }, api: self.api, block: self.block, storage: self.storage, }; + // execute initialization provided by the caller app.init_modules(init_fn); + // return already initialized application app } } diff --git a/src/tests/test_ibc.rs b/src/tests/test_ibc.rs index ed32377e..ab70567c 100644 --- a/src/tests/test_ibc.rs +++ b/src/tests/test_ibc.rs @@ -20,7 +20,7 @@ fn default_ibc() { #[test] fn accepting_ibc() { - let mut app = AppBuilder::new() + let mut app = AppBuilder::default() .with_ibc(IbcAcceptingModule::new()) .build(no_init); diff --git a/src/wasm.rs b/src/wasm.rs index 04493cd2..b92a8d18 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -433,7 +433,7 @@ where /// # Example /// /// ``` - /// use cw_multi_test::{AppBuilder, no_init, WasmKeeper}; + /// use cw_multi_test::{no_init, AppBuilder, WasmKeeper}; /// /// // create wasm keeper /// let wasm_keeper = WasmKeeper::new(); @@ -451,7 +451,7 @@ where /// /// ``` /// use cosmwasm_std::{Addr, Api, Storage}; - /// use cw_multi_test::{AddressGenerator, AppBuilder, no_init, WasmKeeper}; + /// use cw_multi_test::{no_init, AddressGenerator, AppBuilder, WasmKeeper}; /// use cw_multi_test::error::AnyResult; /// # use cosmwasm_std::testing::MockApi; /// @@ -490,7 +490,7 @@ where /// /// ``` /// use cosmwasm_std::{Addr, Checksum}; - /// use cw_multi_test::{AppBuilder, ChecksumGenerator, no_init, WasmKeeper}; + /// use cw_multi_test::{no_init, AppBuilder, ChecksumGenerator, WasmKeeper}; /// /// struct MyChecksumGenerator; /// diff --git a/tests/mod.rs b/tests/mod.rs index 9001d161..feb7b9ea 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -4,6 +4,7 @@ mod test_api; mod test_app; mod test_app_builder; mod test_attributes; +mod test_bank; mod test_contract_storage; mod test_module; mod test_prefixed_storage; diff --git a/tests/test_app/mod.rs b/tests/test_app/mod.rs index 6592434b..00601b0d 100644 --- a/tests/test_app/mod.rs +++ b/tests/test_app/mod.rs @@ -1,4 +1,5 @@ mod test_block_info; +mod test_initialize_app; #[cfg(feature = "cosmwasm_1_2")] mod test_instantiate2; mod test_store_code; diff --git a/tests/test_app/test_initialize_app.rs b/tests/test_app/test_initialize_app.rs new file mode 100644 index 00000000..977d9947 --- /dev/null +++ b/tests/test_app/test_initialize_app.rs @@ -0,0 +1,21 @@ +use cw_multi_test::App; +use cw_storage_plus::Map; + +const USER: &str = "user"; +const USERS: Map<&str, u64> = Map::new("users"); +const AMOUNT: u64 = 100; + +#[test] +fn initializing_app_should_work() { + let mut app = App::default(); + let mut amount = 0; + app.init_modules(|_router, api, storage| { + USERS + .save(storage, api.addr_make(USER).as_str(), &AMOUNT) + .unwrap(); + }); + app.read_module(|_router, api, storage| { + amount = USERS.load(storage, api.addr_make(USER).as_str()).unwrap() + }); + assert_eq!(AMOUNT, amount); +} diff --git a/tests/test_app_builder/test_with_bank.rs b/tests/test_app_builder/test_with_bank.rs index 3aba13fd..a6b00f4a 100644 --- a/tests/test_app_builder/test_with_bank.rs +++ b/tests/test_app_builder/test_with_bank.rs @@ -16,8 +16,7 @@ fn building_app_with_custom_bank_should_work() { let bank_keeper = MyBankKeeper::new(EXECUTE_MSG, QUERY_MSG, SUDO_MSG); // build the application with custom bank keeper - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_bank(bank_keeper).build(no_init); + let mut app = AppBuilder::default().with_bank(bank_keeper).build(no_init); // prepare user addresses let recipient_addr = app.api().addr_make("recipient"); diff --git a/tests/test_app_builder/test_with_gov.rs b/tests/test_app_builder/test_with_gov.rs index d086674c..f1f8e0fe 100644 --- a/tests/test_app_builder/test_with_gov.rs +++ b/tests/test_app_builder/test_with_gov.rs @@ -14,8 +14,7 @@ fn building_app_with_custom_gov_should_work() { let gov_keeper = MyGovKeeper::new(EXECUTE_MSG, NO_MESSAGE, NO_MESSAGE); // build the application with custom gov keeper - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_gov(gov_keeper).build(no_init); + let mut app = AppBuilder::default().with_gov(gov_keeper).build(no_init); // prepare addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_ibc.rs b/tests/test_app_builder/test_with_ibc.rs index a39ec0a5..f9a77ba3 100644 --- a/tests/test_app_builder/test_with_ibc.rs +++ b/tests/test_app_builder/test_with_ibc.rs @@ -15,8 +15,7 @@ fn building_app_with_custom_ibc_should_work() { let ibc_keeper = MyIbcKeeper::new(EXECUTE_MSG, QUERY_MSG, NO_MESSAGE); // build the application with custom ibc keeper - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_ibc(ibc_keeper).build(no_init); + let mut app = AppBuilder::default().with_ibc(ibc_keeper).build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_staking.rs b/tests/test_app_builder/test_with_staking.rs index ac429062..3f49b1d4 100644 --- a/tests/test_app_builder/test_with_staking.rs +++ b/tests/test_app_builder/test_with_staking.rs @@ -16,8 +16,9 @@ fn building_app_with_custom_staking_should_work() { let stake_keeper = MyStakeKeeper::new(EXECUTE_MSG, QUERY_MSG, SUDO_MSG); // build the application with custom stake keeper - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_staking(stake_keeper).build(no_init); + let mut app = AppBuilder::default() + .with_staking(stake_keeper) + .build(no_init); // prepare addresses let validator_addr = app.api().addr_make("validator"); diff --git a/tests/test_app_builder/test_with_stargate.rs b/tests/test_app_builder/test_with_stargate.rs index 51b10b80..3bb09844 100644 --- a/tests/test_app_builder/test_with_stargate.rs +++ b/tests/test_app_builder/test_with_stargate.rs @@ -113,8 +113,9 @@ fn building_app_with_custom_stargate_should_work() { #[cfg(feature = "cosmwasm_2_0")] fn building_app_with_custom_any_grpc_should_work() { // build the application with custom stargate keeper - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_stargate(StargateKeeper).build(no_init); + let mut app = AppBuilder::default() + .with_stargate(StargateKeeper) + .build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -144,8 +145,9 @@ fn building_app_with_custom_any_grpc_should_work() { #[test] fn building_app_with_accepting_stargate_should_work() { - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_stargate(StargateAccepting).build(no_init); + let mut app = AppBuilder::default() + .with_stargate(StargateAccepting) + .build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -238,8 +240,9 @@ fn default_failing_stargate_should_work() { #[test] #[cfg(feature = "cosmwasm_2_0")] fn default_failing_any_grpc_should_work() { - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_stargate(StargateFailing).build(no_init); + let mut app = AppBuilder::default() + .with_stargate(StargateFailing) + .build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_storage.rs b/tests/test_app_builder/test_with_storage.rs index 3290f9c1..6adf6909 100644 --- a/tests/test_app_builder/test_with_storage.rs +++ b/tests/test_app_builder/test_with_storage.rs @@ -41,8 +41,7 @@ fn building_app_with_custom_storage_should_work() { let label = "my-counter"; // build the application with custom storage - let app_builder = AppBuilder::default(); - let mut app = app_builder + let mut app = AppBuilder::default() .with_storage(MyStorage::default()) .build(no_init); diff --git a/tests/test_app_builder/test_with_wasm.rs b/tests/test_app_builder/test_with_wasm.rs index 390c8684..258894c2 100644 --- a/tests/test_app_builder/test_with_wasm.rs +++ b/tests/test_app_builder/test_with_wasm.rs @@ -91,8 +91,7 @@ fn building_app_with_custom_wasm_should_work() { let wasm_keeper = MyWasmKeeper::new(EXECUTE_MSG, QUERY_MSG, SUDO_MSG); // build the application with custom wasm keeper - let app_builder = AppBuilder::default(); - let mut app = app_builder.with_wasm(wasm_keeper).build(no_init); + let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); // prepare addresses let contract_addr = app.api().addr_make("contract"); @@ -162,6 +161,7 @@ fn building_app_with_custom_wasm_should_work() { fn compiling_with_wasm_keeper_should_work() { // this verifies only compilation errors // while our WasmKeeper does not implement Module - let app_builder = AppBuilder::default(); - let _ = app_builder.with_wasm(WasmKeeper::default()).build(no_init); + let _ = AppBuilder::default() + .with_wasm(WasmKeeper::default()) + .build(no_init); } diff --git a/tests/test_bank/mod.rs b/tests/test_bank/mod.rs new file mode 100644 index 00000000..dfe6392e --- /dev/null +++ b/tests/test_bank/mod.rs @@ -0,0 +1 @@ +mod test_init_balance; diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs new file mode 100644 index 00000000..b312b22d --- /dev/null +++ b/tests/test_bank/test_init_balance.rs @@ -0,0 +1,96 @@ +use cosmwasm_std::{Coin, CustomMsg, CustomQuery, Uint128}; +use cw_multi_test::{custom_app, App, AppBuilder, BasicApp}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +const USER: &str = "user"; +const DENOM: &str = "denom"; +const AMOUNT: u128 = 100; + +fn assert_balance(coins: Vec) { + assert_eq!(1, coins.len()); + assert_eq!(AMOUNT, coins[0].amount.u128()); + assert_eq!(DENOM, coins[0].denom); +} + +fn coins() -> Vec { + vec![Coin { + denom: DENOM.to_string(), + amount: Uint128::new(AMOUNT), + }] +} + +#[test] +fn initializing_balance_should_work() { + let app = AppBuilder::new().build(|router, api, storage| { + router + .bank + .init_balance(storage, &api.addr_make(USER), coins()) + .unwrap(); + }); + assert_balance( + app.wrap() + .query_all_balances(app.api().addr_make(USER)) + .unwrap(), + ); +} + +#[test] +fn initializing_balance_without_builder_should_work() { + let app = App::new(|router, api, storage| { + router + .bank + .init_balance(storage, &api.addr_make(USER), coins()) + .unwrap(); + }); + assert_balance( + app.wrap() + .query_all_balances(app.api().addr_make(USER)) + .unwrap(), + ); +} + +#[test] +fn initializing_balance_custom_app_should_work() { + #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] + #[serde(rename = "snake_case")] + pub enum CustomHelperMsg { + HelperMsg, + } + impl CustomMsg for CustomHelperMsg {} + + #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] + #[serde(rename = "snake_case")] + pub enum CustomHelperQuery { + HelperQuery, + } + impl CustomQuery for CustomHelperQuery {} + + let app: BasicApp = custom_app(|router, api, storage| { + router + .bank + .init_balance(storage, &api.addr_make(USER), coins()) + .unwrap(); + }); + assert_balance( + app.wrap() + .query_all_balances(app.api().addr_make(USER)) + .unwrap(), + ); +} + +#[test] +fn initializing_balance_later_should_work() { + let mut app = App::default(); + app.init_modules(|router, api, storage| { + router + .bank + .init_balance(storage, &api.addr_make(USER), coins()) + .unwrap(); + }); + assert_balance( + app.wrap() + .query_all_balances(app.api().addr_make(USER)) + .unwrap(), + ); +}