Struct abstract_testing::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

source

pub fn with_fallback_smart_handler<SH>(self, handler: SH) -> Self
where SH: 'static + Fn(&str, &Binary) -> Result<Binary, String>,

source

pub fn with_fallback_raw_handler<RH>(self, handler: RH) -> Self
where RH: 'static + Fn(&str, &Binary) -> Result<Binary, String>,

source

pub fn with_smart_handler<SH>(self, contract: &str, handler: SH) -> Self
where SH: 'static + Fn(&Binary) -> Result<Binary, String>,

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();
source

pub fn with_raw_handler<RH>(self, contract: &str, handler: RH) -> Self
where RH: 'static + Fn(&str) -> Result<Binary, String>,

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();
source

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())
);
source

pub fn with_contract_map_entries<'a, K, V>( self, contract: &str, cw_map: Map<'a, K, V>, entries: Vec<(K, V)> ) -> Self

source

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.

source

pub fn with_empty_contract_item<T>( self, contract: &str, cw_item: Item<'_, T> ) -> Self

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.

source

pub fn with_contract_item<T>( self, contract: &str, cw_item: Item<'_, T>, value: &T ) -> Self

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(),
);
source

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");
source

pub fn with_contract_admin( self, contract: impl ToString, admin: impl ToString ) -> Self

set the SDK-level contract admin for a contract.

source

pub fn build(self) -> MockQuerier

Build the MockQuerier.

Trait Implementations§

source§

impl Default for MockQuerierBuilder

source§

fn default() -> Self

Create a default

source§

impl MockQuerierOwnership for MockQuerierBuilder

source§

fn with_owner(self, contract: &str, owner: Option<impl ToString>) -> Self

Add the cw_ownable::Ownership to the querier.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<U> As for U

source§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.