Skip to content

Commit

Permalink
feat(logic-bindings): prune mocking elements
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 30, 2023
1 parent ed12830 commit 49157aa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 61 deletions.
7 changes: 3 additions & 4 deletions contracts/cw-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,7 @@ mod tests {
SubMsgResponse, SubMsgResult, SystemError, SystemResult, WasmQuery,
};
use cw_storage::msg::PageInfo;
use logic_bindings::testing::mock::{
mock_dependencies_with_logic_and_balance, mock_dependencies_with_logic_handler,
};
use logic_bindings::testing::mock::mock_dependencies_with_logic_handler;
use logic_bindings::uri::CosmwasmUri;
use logic_bindings::{
Answer, AskResponse, LogicCustomQuery, Result as LogicResult, Substitution, Term,
Expand Down Expand Up @@ -352,7 +350,8 @@ mod tests {

#[test]
fn program() {
let mut deps = mock_dependencies_with_logic_and_balance(&[]);
let mut deps =
mock_dependencies_with_logic_handler(|_| SystemResult::Err(SystemError::Unknown {}));

let object_id =
"4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05".to_string();
Expand Down
60 changes: 3 additions & 57 deletions packages/logic-bindings/src/testing/mock.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
use crate::{Answer, AskResponse, LogicCustomQuery, Substitution, Term};
use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{to_binary, Coin, OwnedDeps, QuerierResult, SystemResult};
use crate::LogicCustomQuery;
use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{Coin, OwnedDeps, QuerierResult};
use std::marker::PhantomData;

/// Creates all external requirements that can be injected for unit tests.
///
/// It sets the given balance for the contract itself, nothing else and set the custom default logic
/// querier handler.
pub fn mock_dependencies_with_logic_and_balance(
contract_balance: &[Coin],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier<LogicCustomQuery>, LogicCustomQuery> {
mock_dependencies_with_logic_and_balances(&[(MOCK_CONTRACT_ADDR, contract_balance)])
}

pub fn mock_dependencies_with_logic_handler<LH: 'static>(
handler: LH,
) -> OwnedDeps<MockStorage, MockApi, MockQuerier<LogicCustomQuery>, LogicCustomQuery>
Expand All @@ -27,21 +17,6 @@ where
}
}

/// Initializes the querier along with the mock_dependencies.
///
/// Set the logic querier mock handler.
/// Sets all balances provided (you must explicitly set contract balance if desired).
pub fn mock_dependencies_with_logic_and_balances(
balances: &[(&str, &[Coin])],
) -> OwnedDeps<MockStorage, MockApi, MockQuerier<LogicCustomQuery>, LogicCustomQuery> {
OwnedDeps {
storage: MockStorage::default(),
api: MockApi::default(),
querier: MockLogicQuerier::new(LogicQuerier::default(), balances),
custom_query_type: PhantomData,
}
}

trait MockLogicQuerier {
fn new(logic: LogicQuerier, balances: &[(&str, &[Coin])]) -> Self;
}
Expand Down Expand Up @@ -73,32 +48,3 @@ impl LogicQuerier {
self.handler = Box::from(handler)
}
}

impl Default for LogicQuerier {
fn default() -> Self {
let handler = Box::from(|request: &LogicCustomQuery| -> QuerierResult {
let result = match request {
LogicCustomQuery::Ask { .. } => to_binary(&AskResponse {
height: 1,
gas_used: 1000,
answer: Some(Answer {
success: true,
has_more: false,
variables: vec!["foo".to_string()],
results: vec![crate::Result {
substitutions: vec![Substitution {
variable: "foo".to_string(),
term: Term {
name: "bar".to_string(),
arguments: vec![],
},
}],
}],
}),
}),
};
SystemResult::Ok(result.into())
});
Self::new(handler)
}
}

0 comments on commit 49157aa

Please sign in to comment.