Struct abstract_testing::prelude::MockQuerierBuilder
source · pub struct MockQuerierBuilder { /* private fields */ }
Expand description
MockQuerierBuilder
is a helper to build a MockQuerier
.
Usage:
use cosmwasm_std::{from_json, to_json_binary};
use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::MockQuerier;
use abstract_sdk::mock_module::MockModuleExecuteMsg;
let querier = MockQuerierBuilder::default().with_smart_handler("contract_address", |msg| {
// handle the message
let res = match from_json::<MockModuleExecuteMsg>(msg).unwrap() {
// handle the message
_ => panic!("unexpected message"),
};
Ok(to_json_binary(&msg).unwrap())
}).build();
Implementations§
source§impl MockQuerierBuilder
impl MockQuerierBuilder
pub fn with_fallback_smart_handler<SH>(self, handler: SH) -> Self
pub fn with_fallback_raw_handler<RH>(self, handler: RH) -> Self
sourcepub fn with_smart_handler<SH>(self, contract: &str, handler: SH) -> Self
pub fn with_smart_handler<SH>(self, contract: &str, handler: SH) -> Self
Add a smart query contract handler to the mock querier. The handler will be called when the contract address is queried with the given message. Usage:
use cosmwasm_std::{from_json, to_json_binary};
use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::MockQuerier;
use abstract_sdk::mock_module::{MockModuleQueryMsg, MockModuleQueryResponse};
let querier = MockQuerierBuilder::default().with_smart_handler("contract_address", |msg| {
// handle the message
let res = match from_json::<MockModuleQueryMsg>(msg).unwrap() {
// handle the message
MockModuleQueryMsg =>
return to_json_binary(&MockModuleQueryResponse {}).map_err(|e| e.to_string())
};
}).build();
sourcepub fn with_raw_handler<RH>(self, contract: &str, handler: RH) -> Self
pub fn with_raw_handler<RH>(self, contract: &str, handler: RH) -> Self
Add a raw query contract handler to the mock querier. The handler will be called when the contract address is queried with the given message. Usage:
use cosmwasm_std::{from_json, to_json_binary};
use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::MockQuerier;
use abstract_sdk::mock_module::{MockModuleQueryMsg, MockModuleQueryResponse};
let querier = MockQuerierBuilder::default().with_raw_handler("contract1", |key: &str| {
// Example: Let's say, in the raw storage, the key "the key" maps to the value "the value"
match key {
"the key" => to_json_binary("the value").map_err(|e| e.to_string()),
_ => to_json_binary("").map_err(|e| e.to_string())
}
}).build();
sourcepub fn with_contract_map_entry<'a, K, V>(
self,
contract: &str,
cw_map: Map<'a, K, V>,
entry: (K, V)
) -> Self
pub fn with_contract_map_entry<'a, K, V>( self, contract: &str, cw_map: Map<'a, K, V>, entry: (K, V) ) -> Self
Add a map entry to the querier for the given contract.
use cw_storage_plus::Map;
use abstract_testing::MockQuerierBuilder;
const MAP: Map<String, String> = Map::new("map");
MockQuerierBuilder::default()
.with_contract_map_entry(
"contract_address",
MAP,
("key".to_string(), "value".to_string())
);
pub fn with_contract_map_entries<'a, K, V>( self, contract: &str, cw_map: Map<'a, K, V>, entries: Vec<(K, V)> ) -> Self
sourcepub fn with_contract_map_key<'a, K, V>(
self,
contract: &str,
cw_map: Map<'a, K, V>,
key: K
) -> Self
pub fn with_contract_map_key<'a, K, V>( self, contract: &str, cw_map: Map<'a, K, V>, key: K ) -> Self
Add an empty map key to the querier for the given contract. This is useful when you want the item to exist, but not have a value.
sourcepub fn with_empty_contract_item<T>(
self,
contract: &str,
cw_item: Item<'_, T>
) -> Selfwhere
T: Serialize + DeserializeOwned,
pub fn with_empty_contract_item<T>(
self,
contract: &str,
cw_item: Item<'_, T>
) -> Selfwhere
T: Serialize + DeserializeOwned,
Add an empty item key to the querier for the given contract. This is useful when you want the item to exist, but not have a value.
sourcepub fn with_contract_item<T>(
self,
contract: &str,
cw_item: Item<'_, T>,
value: &T
) -> Selfwhere
T: Serialize + DeserializeOwned,
pub fn with_contract_item<T>(
self,
contract: &str,
cw_item: Item<'_, T>,
value: &T
) -> Selfwhere
T: Serialize + DeserializeOwned,
Include a contract item in the mock querier.
use cw_storage_plus::Item;
use abstract_testing::MockQuerierBuilder;
const ITEM: Item<String> = Item::new("item");
MockQuerierBuilder::default()
.with_contract_item(
"contract_address",
ITEM,
&"value".to_string(),
);
sourcepub fn with_contract_version(
self,
contract: &str,
version: impl ToString
) -> Self
pub fn with_contract_version( self, contract: &str, version: impl ToString ) -> Self
Add a specific version of the contract to the mock querier.
use abstract_testing::MockQuerierBuilder;
MockQuerierBuilder::default()
.with_contract_version("contract_address", "v1.0.0");
sourcepub fn with_contract_admin(
self,
contract: impl ToString,
admin: impl ToString
) -> Self
pub fn with_contract_admin( self, contract: impl ToString, admin: impl ToString ) -> Self
set the SDK-level contract admin for a contract.
sourcepub fn build(self) -> MockQuerier
pub fn build(self) -> MockQuerier
Build the MockQuerier
.
Trait Implementations§
source§impl MockQuerierOwnership for MockQuerierBuilder
impl MockQuerierOwnership for MockQuerierBuilder
source§fn with_owner(self, contract: &str, owner: Option<impl ToString>) -> Self
fn with_owner(self, contract: &str, owner: Option<impl ToString>) -> Self
cw_ownable::Ownership
to the querier.Auto Trait Implementations§
impl Freeze for MockQuerierBuilder
impl !RefUnwindSafe for MockQuerierBuilder
impl !Send for MockQuerierBuilder
impl !Sync for MockQuerierBuilder
impl Unpin for MockQuerierBuilder
impl !UnwindSafe for MockQuerierBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more