Fix tests
This commit is contained in:
parent
f331b99d5c
commit
4b8d055c77
3 changed files with 99 additions and 117 deletions
|
@ -199,70 +199,124 @@ pub mod query {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
|
||||
use cosmwasm_std::{coins, from_json};
|
||||
use cosmwasm_std::testing::{
|
||||
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
|
||||
};
|
||||
use cosmwasm_std::{coins, from_json, OwnedDeps};
|
||||
use cw20::BalanceResponse;
|
||||
|
||||
use crate::msg::GetObligationsResponse;
|
||||
|
||||
const ALICE_ADDRESS: &str = "wasm19xlctyn7ha6pqg7pk9lnk8y60rk8646dm86qgv";
|
||||
const BOB_ADDRESS: &str = "wasm19u72czh0w4jraan8esalv48nrwemh8kgax69yw";
|
||||
const CHARLIE_ADDRESS: &str = "wasm12r9t5wmre89rwakr0e5nyhfmaf4kdleyltsm9f";
|
||||
|
||||
#[test]
|
||||
fn proper_initialization() {
|
||||
fn test_initialization() {
|
||||
let mut deps = mock_dependencies();
|
||||
|
||||
let msg = InstantiateMsg { count: 17 };
|
||||
let msg = InstantiateMsg;
|
||||
let info = mock_info("creator", &coins(1000, "earth"));
|
||||
|
||||
// we can just call .unwrap() to assert this was a success
|
||||
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
|
||||
assert_eq!(0, res.messages.len());
|
||||
|
||||
// it worked, let's query the state
|
||||
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
|
||||
let value: GetCountResponse = from_json(&res).unwrap();
|
||||
assert_eq!(17, value.count);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn increment() {
|
||||
fn test_upload_obligation() {
|
||||
let mut deps = mock_dependencies();
|
||||
|
||||
let msg = InstantiateMsg { count: 17 };
|
||||
let msg = InstantiateMsg;
|
||||
let info = mock_info("creator", &coins(2, "token"));
|
||||
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
|
||||
|
||||
// beneficiary can release it
|
||||
let info = mock_info("anyone", &coins(2, "token"));
|
||||
let msg = ExecuteMsg::Increment {};
|
||||
create_obligation(&mut deps, ALICE_ADDRESS, BOB_ADDRESS, 100, "alice -> bob");
|
||||
|
||||
let res = query(
|
||||
deps.as_ref(),
|
||||
mock_env(),
|
||||
QueryMsg::GetObligations {
|
||||
creditor: BOB_ADDRESS.to_string(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let value: GetObligationsResponse = from_json(&res).unwrap();
|
||||
assert_eq!(&100u32.into(), value.obligations[0].1);
|
||||
}
|
||||
|
||||
fn create_obligation(
|
||||
deps: &mut OwnedDeps<MockStorage, MockApi, MockQuerier>,
|
||||
debtor: &str,
|
||||
creditor: &str,
|
||||
amount: u32,
|
||||
memo: &str,
|
||||
) {
|
||||
let info = mock_info(debtor, &coins(2, "token"));
|
||||
let msg = ExecuteMsg::UploadObligation {
|
||||
creditor: creditor.to_string(),
|
||||
amount: amount.into(),
|
||||
memo: memo.to_string(),
|
||||
};
|
||||
let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_cycle() {
|
||||
let mut deps = mock_dependencies();
|
||||
|
||||
let msg = InstantiateMsg;
|
||||
let info = mock_info("creator", &coins(2, "token"));
|
||||
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
|
||||
|
||||
create_obligation(&mut deps, ALICE_ADDRESS, BOB_ADDRESS, 100, "alice -> bob");
|
||||
create_obligation(
|
||||
&mut deps,
|
||||
BOB_ADDRESS,
|
||||
CHARLIE_ADDRESS,
|
||||
80,
|
||||
"bob -> charlie",
|
||||
);
|
||||
create_obligation(
|
||||
&mut deps,
|
||||
CHARLIE_ADDRESS,
|
||||
ALICE_ADDRESS,
|
||||
70,
|
||||
"charlie -> alice",
|
||||
);
|
||||
|
||||
let info = mock_info(ALICE_ADDRESS, &coins(2, "token"));
|
||||
let msg = ExecuteMsg::ApplyCycle {
|
||||
path: [ALICE_ADDRESS, BOB_ADDRESS, CHARLIE_ADDRESS, ALICE_ADDRESS]
|
||||
.into_iter()
|
||||
.map(ToString::to_string)
|
||||
.collect(),
|
||||
amount: 70u32.into(),
|
||||
};
|
||||
let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
|
||||
|
||||
// should increase counter by 1
|
||||
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
|
||||
let value: GetCountResponse = from_json(&res).unwrap();
|
||||
assert_eq!(18, value.count);
|
||||
}
|
||||
// Cycle should be cleared and only `30` should remain in `alice -> bob`
|
||||
let res = query(
|
||||
deps.as_ref(),
|
||||
mock_env(),
|
||||
QueryMsg::GetObligations {
|
||||
creditor: BOB_ADDRESS.to_string(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let value: GetObligationsResponse = from_json(&res).unwrap();
|
||||
assert_eq!(&30u32.into(), value.obligations[0].1);
|
||||
|
||||
#[test]
|
||||
fn reset() {
|
||||
let mut deps = mock_dependencies();
|
||||
|
||||
let msg = InstantiateMsg { count: 17 };
|
||||
let info = mock_info("creator", &coins(2, "token"));
|
||||
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
|
||||
|
||||
// beneficiary can release it
|
||||
let unauth_info = mock_info("anyone", &coins(2, "token"));
|
||||
let msg = ExecuteMsg::Reset { count: 5 };
|
||||
let res = execute(deps.as_mut(), mock_env(), unauth_info, msg);
|
||||
match res {
|
||||
Err(ContractError::Unauthorized {}) => {}
|
||||
_ => panic!("Must return unauthorized error"),
|
||||
}
|
||||
|
||||
// only the original creator can reset the counter
|
||||
let auth_info = mock_info("creator", &coins(2, "token"));
|
||||
let msg = ExecuteMsg::Reset { count: 5 };
|
||||
let _res = execute(deps.as_mut(), mock_env(), auth_info, msg).unwrap();
|
||||
|
||||
// should now be 5
|
||||
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
|
||||
let value: GetCountResponse = from_json(&res).unwrap();
|
||||
assert_eq!(5, value.count);
|
||||
// Check that alice received her karma tokens
|
||||
let res = query(
|
||||
deps.as_ref(),
|
||||
mock_env(),
|
||||
QueryMsg::Balance {
|
||||
address: ALICE_ADDRESS.to_string(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let value: BalanceResponse = from_json(&res).unwrap();
|
||||
assert_eq!(&210u32.into(), value.balance);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::helpers::CwTemplateContract;
|
||||
use crate::msg::InstantiateMsg;
|
||||
use cosmwasm_std::{Addr, Coin, Empty, Uint128};
|
||||
use cw_multi_test::{App, AppBuilder, Contract, ContractWrapper, Executor};
|
||||
|
||||
pub fn contract_template() -> Box<dyn Contract<Empty>> {
|
||||
let contract = ContractWrapper::new(
|
||||
crate::contract::execute,
|
||||
crate::contract::instantiate,
|
||||
crate::contract::query,
|
||||
);
|
||||
Box::new(contract)
|
||||
}
|
||||
|
||||
const USER: &str = "USER";
|
||||
const ADMIN: &str = "ADMIN";
|
||||
const NATIVE_DENOM: &str = "denom";
|
||||
|
||||
fn mock_app() -> App {
|
||||
AppBuilder::new().build(|router, _, storage| {
|
||||
router
|
||||
.bank
|
||||
.init_balance(
|
||||
storage,
|
||||
&Addr::unchecked(USER),
|
||||
vec![Coin {
|
||||
denom: NATIVE_DENOM.to_string(),
|
||||
amount: Uint128::new(1),
|
||||
}],
|
||||
)
|
||||
.unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
fn proper_instantiate() -> (App, CwTemplateContract) {
|
||||
let mut app = mock_app();
|
||||
let cw_template_id = app.store_code(contract_template());
|
||||
|
||||
let msg = InstantiateMsg { count: 1i32 };
|
||||
let cw_template_contract_addr = app
|
||||
.instantiate_contract(
|
||||
cw_template_id,
|
||||
Addr::unchecked(ADMIN),
|
||||
&msg,
|
||||
&[],
|
||||
"test",
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let cw_template_contract = CwTemplateContract(cw_template_contract_addr);
|
||||
|
||||
(app, cw_template_contract)
|
||||
}
|
||||
|
||||
mod count {
|
||||
use super::*;
|
||||
use crate::msg::ExecuteMsg;
|
||||
|
||||
#[test]
|
||||
fn count() {
|
||||
let (mut app, cw_template_contract) = proper_instantiate();
|
||||
|
||||
let msg = ExecuteMsg::Increment {};
|
||||
let cosmos_msg = cw_template_contract.call(msg).unwrap();
|
||||
app.execute(Addr::unchecked(USER), cosmos_msg).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,7 +9,6 @@
|
|||
|
||||
pub mod contract;
|
||||
mod error;
|
||||
pub mod integration_tests;
|
||||
pub mod msg;
|
||||
pub mod state;
|
||||
|
||||
|
|
Loading…
Reference in a new issue