abstract_testing::prelude

Struct MockQuerierBuilder

Source
pub struct MockQuerierBuilder {
    pub api: MockApi,
    /* 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, MockApi};
use abstract_sdk::mock_module::MockModuleExecuteMsg;

let api = MockApi::default();
let contract_address = api.addr_make("contract_address");
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();

Fields§

§api: MockApi

Implementations§

Source§

impl MockQuerierBuilder

Source

pub fn new(api: MockApi) -> Self

Source§

impl MockQuerierBuilder

Source

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

Source

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

Source

pub fn with_smart_handler<SH>(self, contract: &Addr, 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, MockApi};
use abstract_sdk::mock_module::{MockModuleQueryMsg, MockModuleQueryResponse};

let api = MockApi::default();
let contract_address = api.addr_make("contract_address");
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: &Addr, 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, MockApi};
use abstract_sdk::mock_module::{MockModuleQueryMsg, MockModuleQueryResponse};

let api = MockApi::default();
let contract_address = api.addr_make("contract1");
let querier = MockQuerierBuilder::default().with_raw_handler(&contract_address, |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: &Addr, cw_map: Map<K, V>, entry: (K, V), ) -> Self

Add a map entry to the querier for the given contract.

use cw_storage_plus::Map;
use cosmwasm_std::testing::MockApi;
use abstract_testing::MockQuerierBuilder;

let api = MockApi::default();
let contract_address = api.addr_make("contract1");

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: &Addr, cw_map: Map<K, V>, entries: Vec<(K, V)>, ) -> Self

Source

pub fn with_contract_map_key<'a, K, V>( self, contract: &Addr, cw_map: Map<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: &Addr, 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: &Addr, cw_item: Item<T>, value: &T, ) -> Self

Include a contract item in the mock querier.

use cw_storage_plus::Item;
use cosmwasm_std::testing::MockApi;
use abstract_testing::MockQuerierBuilder;

let api = MockApi::default();
let contract_address = api.addr_make("contract1");

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: &Addr, name: impl Into<String>, version: impl Into<String>, ) -> Self

Add a specific version of the contract to the mock querier.

use abstract_testing::MockQuerierBuilder;
use cosmwasm_std::testing::MockApi;

let api = MockApi::default();
let contract_address = api.addr_make("contract1");

MockQuerierBuilder::default()
   .with_contract_version(&contract_address, "contract1", "v1.0.0");
Source

pub fn with_contract_admin(self, contract: &Addr, admin: &Addr) -> Self

set the SDK-level contract admin for a contract.

Source

pub fn build(self) -> MockQuerier

Build the MockQuerier.

Trait Implementations§

Source§

impl AbstractMockQuerier for MockQuerierBuilder

Source§

fn account(self, account: &Account, account_id: AccountId) -> Self

Mock the existence of an Account by setting the Account id for the account along with registering the account to registry.

Source§

fn assets(self, assets: Vec<(&AssetEntry, AssetInfo)>) -> Self

Add mock assets into ANS
Source§

fn contracts(self, contracts: Vec<(&ContractEntry, Addr)>) -> Self

Source§

fn channels(self, channels: Vec<(&ChannelEntry, String)>) -> Self

Source§

fn addrs(&self) -> AbstractMockAddrs

Source§

fn set_account_admin_call_to(self, account: &Account) -> Self

Source§

impl Default for MockQuerierBuilder

Source§

fn default() -> Self

Create a default

Source§

impl MockQuerierOwnership for MockQuerierBuilder

Source§

fn with_owner(self, contract: &Addr, owner: Option<&Addr>) -> Self

Add the [cw_gov_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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V