Skip to content

Commit 21b29df

Browse files
committed
Make MockQuerier more extensible
1 parent 80fbf56 commit 21b29df

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

packages/std/src/mock.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,34 @@ pub fn mock_env<T: Api, U: Into<HumanAddr>>(api: &T, sender: U, sent: &[Coin]) -
126126
/// TODO: also allow querying contracts
127127
#[derive(Clone)]
128128
pub struct MockQuerier {
129-
balances: HashMap<HumanAddr, Vec<Coin>>,
129+
bank: BankQuerier,
130130
}
131131

132132
impl MockQuerier {
133133
pub fn new(balances: &[(&HumanAddr, &[Coin])]) -> Self {
134+
MockQuerier {
135+
bank: BankQuerier::new(balances),
136+
}
137+
}
138+
}
139+
140+
#[derive(Clone)]
141+
struct BankQuerier {
142+
balances: HashMap<HumanAddr, Vec<Coin>>,
143+
}
144+
145+
impl BankQuerier {
146+
fn new(balances: &[(&HumanAddr, &[Coin])]) -> Self {
134147
let mut map = HashMap::new();
135148
for (addr, coins) in balances.iter() {
136149
map.insert(HumanAddr::from(addr), coins.to_vec());
137150
}
138-
MockQuerier { balances: map }
151+
BankQuerier { balances: map }
139152
}
140-
}
141153

142-
impl Querier for MockQuerier {
143-
fn query(&self, request: &QueryRequest) -> Result<Result<Binary, ApiError>, ApiSystemError> {
154+
fn query(&self, request: &BankQuery) -> Result<Result<Binary, ApiError>, ApiSystemError> {
144155
match request {
145-
QueryRequest::Bank(BankQuery::Balance { address, denom }) => {
156+
BankQuery::Balance { address, denom } => {
146157
// proper error on not found, serialize result on found
147158
let amount = self
148159
.balances
@@ -158,14 +169,22 @@ impl Querier for MockQuerier {
158169
let api_res = to_vec(&bank_res).map(Binary).map_err(|e| e.into());
159170
Ok(api_res)
160171
}
161-
QueryRequest::Bank(BankQuery::AllBalances { address }) => {
172+
BankQuery::AllBalances { address } => {
162173
// proper error on not found, serialize result on found
163174
let bank_res = AllBalanceResponse {
164175
amount: self.balances.get(address).cloned().unwrap_or_default(),
165176
};
166177
let api_res = to_vec(&bank_res).map(Binary).map_err(|e| e.into());
167178
Ok(api_res)
168179
}
180+
}
181+
}
182+
}
183+
184+
impl Querier for MockQuerier {
185+
fn query(&self, request: &QueryRequest) -> Result<Result<Binary, ApiError>, ApiSystemError> {
186+
match request {
187+
QueryRequest::Bank(bank_query) => self.bank.query(bank_query),
169188
#[cfg(feature = "staking")]
170189
QueryRequest::Staking(_) => Err(ApiSystemError::InvalidRequest {
171190
error: "staking not yet implemented".to_string(),

0 commit comments

Comments
 (0)