Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/querier error to anyhow #362

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ mod tests {
t.instantiate(0, &Empty {}, None, None, &[])?;
Ok(())
}

#[test]
fn tx_handler_error_usable_on_anyhow() -> anyhow::Result<()> {
associated_error(MockHandler {})?;
Expand Down
179 changes: 177 additions & 2 deletions packages/cw-orch-core/src/environment/queriers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/// This trait acts as the high-level trait bound for supported queries on a `CwEnv` environment.
/// It also implements some high-level functionality to make it easy to access.
pub trait QueryHandler: DefaultQueriers {
type Error: Into<CwEnvError> + Debug;
type Error: Into<CwEnvError> + Debug + std::error::Error + Send + Sync + 'static;

/// Wait for an amount of blocks.
fn wait_blocks(&self, amount: u64) -> Result<(), Self::Error>;
Expand Down Expand Up @@ -44,7 +44,7 @@
}

pub trait Querier {
type Error: Into<CwEnvError> + Debug;
type Error: Into<CwEnvError> + Debug + std::error::Error + Send + Sync + 'static;
}

pub trait DefaultQueriers:
Expand All @@ -69,3 +69,178 @@
self.querier()
}
}

#[cfg(test)]
pub mod test {
use cosmwasm_std::{Binary, Coin};
use serde::Serialize;

use crate::{
environment::{DefaultQueriers, EnvironmentQuerier, IndexResponse, NodeQuerier},
CwEnvError,
};

use super::{bank::BankQuerier, wasm::WasmQuerier, QuerierGetter, QueryHandler};

impl crate::environment::queriers::Querier for MockQuerier {
type Error = CwEnvError;
}

#[derive(Clone)]
struct MockHandler {}

impl BankQuerier for MockQuerier {
fn balance(
&self,
_address: impl Into<String>,
_denom: Option<String>,
) -> Result<Vec<Coin>, Self::Error> {
// Returns an empty balance
Ok(vec![])
}

fn total_supply(&self) -> Result<Vec<Coin>, Self::Error> {
unimplemented!()

Check warning on line 103 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L102-L103

Added lines #L102 - L103 were not covered by tests
}

fn supply_of(&self, _denom: impl Into<String>) -> Result<Coin, Self::Error> {
unimplemented!()

Check warning on line 107 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L106-L107

Added lines #L106 - L107 were not covered by tests
}
}
impl WasmQuerier for MockQuerier {
fn code_id_hash(&self, _code_id: u64) -> Result<cosmwasm_std::HexBinary, Self::Error> {
unimplemented!()

Check warning on line 112 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L111-L112

Added lines #L111 - L112 were not covered by tests
}

fn contract_info(
&self,
_address: impl Into<String>,
) -> Result<cosmwasm_std::ContractInfoResponse, Self::Error> {
unimplemented!()

Check warning on line 119 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L115-L119

Added lines #L115 - L119 were not covered by tests
}

fn raw_query(
&self,
_address: impl Into<String>,
_query_keys: Vec<u8>,
) -> Result<Vec<u8>, Self::Error> {
unimplemented!()

Check warning on line 127 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L122-L127

Added lines #L122 - L127 were not covered by tests
}

fn smart_query<Q: Serialize, T: serde::de::DeserializeOwned>(
&self,
_address: impl Into<String>,
_query_msg: &Q,
) -> Result<T, Self::Error> {
unimplemented!()

Check warning on line 135 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L130-L135

Added lines #L130 - L135 were not covered by tests
}

fn code(&self, _code_id: u64) -> Result<cosmwasm_std::CodeInfoResponse, Self::Error> {
unimplemented!()

Check warning on line 139 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L138-L139

Added lines #L138 - L139 were not covered by tests
}

fn instantiate2_addr(
&self,
_code_id: u64,
_creator: impl Into<String>,
_salt: cosmwasm_std::Binary,
) -> Result<String, Self::Error> {
unimplemented!()

Check warning on line 148 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L142-L148

Added lines #L142 - L148 were not covered by tests
}
}

impl NodeQuerier for MockQuerier {
type Response = MockQuerier;

fn latest_block(&self) -> Result<cosmwasm_std::BlockInfo, Self::Error> {
unimplemented!()

Check warning on line 156 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L155-L156

Added lines #L155 - L156 were not covered by tests
}

fn block_by_height(&self, _height: u64) -> Result<cosmwasm_std::BlockInfo, Self::Error> {
unimplemented!()

Check warning on line 160 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L159-L160

Added lines #L159 - L160 were not covered by tests
}

fn block_height(&self) -> Result<u64, Self::Error> {
unimplemented!()

Check warning on line 164 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L163-L164

Added lines #L163 - L164 were not covered by tests
}

fn block_time(&self) -> Result<u128, Self::Error> {
unimplemented!()

Check warning on line 168 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L167-L168

Added lines #L167 - L168 were not covered by tests
}

fn simulate_tx(&self, _tx_bytes: Vec<u8>) -> Result<u64, Self::Error> {
unimplemented!()

Check warning on line 172 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L171-L172

Added lines #L171 - L172 were not covered by tests
}

fn find_tx(&self, _hash: String) -> Result<Self::Response, Self::Error> {
unimplemented!()

Check warning on line 176 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L175-L176

Added lines #L175 - L176 were not covered by tests
}
}

#[derive(Clone, Debug)]
pub struct MockQuerier {}

impl IndexResponse for MockQuerier {
fn events(&self) -> Vec<cosmwasm_std::Event> {
unimplemented!()

Check warning on line 185 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L184-L185

Added lines #L184 - L185 were not covered by tests
}

fn event_attr_value(
&self,
_event_type: &str,
_attr_key: &str,
) -> cosmwasm_std::StdResult<String> {
unimplemented!()

Check warning on line 193 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L188-L193

Added lines #L188 - L193 were not covered by tests
}

fn data(&self) -> Option<Binary> {
unimplemented!()

Check warning on line 197 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L196-L197

Added lines #L196 - L197 were not covered by tests
}
}

impl QuerierGetter<MockQuerier> for MockHandler {
fn querier(&self) -> MockQuerier {
MockQuerier {}
}
}

impl EnvironmentQuerier for MockHandler {
fn env_info(&self) -> crate::environment::EnvironmentInfo {
unimplemented!()

Check warning on line 209 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L208-L209

Added lines #L208 - L209 were not covered by tests
}
}

impl DefaultQueriers for MockHandler {
type Bank = MockQuerier;
type Wasm = MockQuerier;
type Node = MockQuerier;
}

impl QueryHandler for MockHandler {
type Error = CwEnvError;

fn wait_blocks(&self, _amount: u64) -> Result<(), Self::Error> {
Ok(())
}

fn wait_seconds(&self, _secs: u64) -> Result<(), Self::Error> {
unimplemented!()

Check warning on line 227 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L226-L227

Added lines #L226 - L227 were not covered by tests
}

fn next_block(&self) -> Result<(), Self::Error> {
unimplemented!()

Check warning on line 231 in packages/cw-orch-core/src/environment/queriers/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/queriers/mod.rs#L230-L231

Added lines #L230 - L231 were not covered by tests
}
}

fn associated_querier_error<T: QueryHandler>(t: T) -> anyhow::Result<()> {
t.bank_querier().balance("anyone".to_string(), None)?;
t.wait_blocks(7)?;
Ok(())
}

#[test]
fn query_handler_error_usable_on_anyhow() -> anyhow::Result<()> {
associated_querier_error(MockHandler {})?;
Ok(())
}
}
Loading