Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta committed Nov 24, 2023
1 parent e0f48f1 commit b0c66e2
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ pub use crate::module::{AcceptingModule, FailingModule, Module};
pub use crate::staking::{
Distribution, DistributionKeeper, StakeKeeper, Staking, StakingInfo, StakingSudo,
};
pub use crate::stargate::{Stargate, StargateFailingModule, StargateMsg, StargateQuery};
pub use crate::stargate::{
Stargate, StargateAcceptingModule, StargateFailingModule, StargateMsg, StargateQuery,
};
pub use crate::wasm::{ContractData, Wasm, WasmKeeper, WasmSudo};
6 changes: 5 additions & 1 deletion src/stargate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FailingModule, Module};
use crate::{AcceptingModule, FailingModule, Module};
use cosmwasm_std::{Binary, CosmosMsg, Empty, QueryRequest};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -19,6 +19,10 @@ pub struct StargateQuery {

pub trait Stargate: Module<ExecT = StargateMsg, QueryT = StargateQuery, SudoT = Empty> {}

pub type StargateAcceptingModule = AcceptingModule<StargateMsg, StargateQuery, Empty>;

impl Stargate for StargateAcceptingModule {}

pub type StargateFailingModule = FailingModule<StargateMsg, StargateQuery, Empty>;

impl Stargate for StargateFailingModule {}
Expand Down
2 changes: 2 additions & 0 deletions tests/test_module/accepting_module/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod test_accepting_module;
mod test_stargate_accepting_module;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{Addr, Binary, Empty};
use cw_multi_test::{AcceptingModule, App, AppResponse, Module, StargateMsg, StargateQuery};

/// Utility function for comparing responses.
fn eq(actual: AppResponse, expected: AppResponse) {
assert_eq!(actual.events, expected.events);
assert_eq!(actual.data, expected.data);
}

/// Utility function for asserting default outputs returned from accepting module.
fn assert_results(accepting_module: AcceptingModule<StargateMsg, StargateQuery, Empty>) {
let app = App::default();
let mut storage = MockStorage::default();
eq(
AppResponse::default(),
accepting_module
.execute(
app.api(),
&mut storage,
app.router(),
&app.block_info(),
Addr::unchecked("sender"),
StargateMsg {
type_url: Default::default(),
value: Default::default(),
},
)
.unwrap(),
);
assert_eq!(
Binary::default(),
accepting_module
.query(
app.api(),
&storage,
&(*app.wrap()),
&app.block_info(),
StargateQuery {
path: Default::default(),
data: Default::default()
}
)
.unwrap()
);
eq(
AppResponse::default(),
accepting_module
.sudo(
app.api(),
&mut storage,
app.router(),
&app.block_info(),
Empty {},
)
.unwrap(),
);
}

#[test]
fn stargate_accepting_module_default_works() {
assert_results(AcceptingModule::default());
}

#[test]
fn stargate_accepting_module_new_works() {
assert_results(AcceptingModule::new());
}
2 changes: 2 additions & 0 deletions tests/test_module/failing_module/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod test_failing_module;
mod test_stargate_failing_module;
File renamed without changes.
65 changes: 65 additions & 0 deletions tests/test_module/failing_module/test_stargate_failing_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{Addr, Empty};
use cw_multi_test::{App, FailingModule, Module, StargateMsg, StargateQuery};

/// Utility function for asserting outputs returned from failing module.
fn assert_results(failing_module: FailingModule<StargateMsg, StargateQuery, Empty>) {
let app = App::default();
let mut storage = MockStorage::default();
assert_eq!(
r#"Unexpected exec msg StargateMsg { type_url: "", value: Binary() } from Addr("sender")"#,
failing_module
.execute(
app.api(),
&mut storage,
app.router(),
&app.block_info(),
Addr::unchecked("sender"),
StargateMsg {
type_url: Default::default(),
value: Default::default(),
}
)
.unwrap_err()
.to_string()
);
assert_eq!(
r#"Unexpected custom query StargateQuery { path: "", data: Binary() }"#,
failing_module
.query(
app.api(),
&storage,
&(*app.wrap()),
&app.block_info(),
StargateQuery {
path: Default::default(),
data: Default::default()
}
)
.unwrap_err()
.to_string()
);
assert_eq!(
"Unexpected sudo msg Empty",
failing_module
.sudo(
app.api(),
&mut storage,
app.router(),
&app.block_info(),
Empty {}
)
.unwrap_err()
.to_string()
);
}

#[test]
fn stargate_failing_module_default_works() {
assert_results(FailingModule::default());
}

#[test]
fn stargate_failing_module_new_works() {
assert_results(FailingModule::new());
}
4 changes: 2 additions & 2 deletions tests/test_module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod test_accepting_module;
mod test_failing_module;
mod accepting_module;
mod failing_module;

0 comments on commit b0c66e2

Please sign in to comment.