Skip to content

Commit

Permalink
test(dataverse): add instantiate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Nov 15, 2023
1 parent 7aecef8 commit a7ef8d1
Showing 1 changed file with 81 additions and 4 deletions.
85 changes: 81 additions & 4 deletions contracts/okp4-dataverse/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ pub fn instantiate(
.query_wasm_code_info(msg.triplestore_config.code_id.u64())?;
let salt = Binary::from(msg.name.as_bytes());

let triplestore_address = deps
.api
.addr_humanize(&instantiate2_address(&checksum, &creator, &salt)?)?;
/// Necessary stuff for testing purposes, see: https://github.com/CosmWasm/cosmwasm/issues/1648
#[allow(unused)]
let triplestore_address = instantiate2_address(&checksum, &creator, &salt)?;
let triplestore_address = {
#[cfg(not(test))]
{
deps.api.addr_humanize(&triplestore_address)?
}
#[cfg(test)]
cosmwasm_std::Addr::unchecked("predicted address")
};

DATAVERSE.save(
deps.storage,
Expand Down Expand Up @@ -75,4 +83,73 @@ pub fn query(_deps: Deps<'_>, _env: Env, _msg: QueryMsg) -> StdResult<Binary> {
pub mod query {}

#[cfg(test)]
mod tests {}
mod tests {
use super::*;
use crate::msg::TripleStoreConfig;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{
Attribute, ContractResult, HexBinary, SubMsg, SystemError, SystemResult, Uint128, Uint64,
WasmQuery,
};
use okp4_cognitarium::msg::StoreLimitsInputBuilder;

#[test]
fn proper_instantiate() {
let mut deps = mock_dependencies();
deps.querier.update_wasm(|query| match query {
WasmQuery::CodeInfo { code_id, .. } => {
let resp = CodeInfoResponse::new(
code_id.clone(),
"creator".to_string(),
HexBinary::from_hex(
"3B94AAF0B7D804B5B458DED0D20CACF95D2A1C8DF78ED3C89B61291760454AEC",
)
.unwrap(),
);
SystemResult::Ok(ContractResult::Ok(to_binary(&resp).unwrap()))
}
_ => SystemResult::Err(SystemError::Unknown {}),
});

let store_limits = StoreLimitsInputBuilder::default()
.max_byte_size(Uint128::from(50000u128))
.build()
.unwrap();

let msg = InstantiateMsg {
name: "my-dataverse".to_string(),
triplestore_config: TripleStoreConfig {
code_id: Uint64::from(17u64),
limits: store_limits.clone(),
},
};

let res = instantiate(deps.as_mut(), mock_env(), mock_info("creator", &[]), msg).unwrap();

assert_eq!(
res.attributes,
vec![Attribute::new("triplestore_address", "predicted address")]
);
assert_eq!(
res.messages,
vec![SubMsg::new(WasmMsg::Instantiate2 {
admin: Some("cosmos2contract".to_string()),
code_id: 17,
label: "my-dataverse_triplestore".to_string(),
msg: to_binary(&okp4_cognitarium::msg::InstantiateMsg {
limits: store_limits,
})
.unwrap(),
funds: vec![],
salt: Binary::from("my-dataverse".as_bytes()),
})]
);
assert_eq!(
DATAVERSE.load(&deps.storage).unwrap(),
Dataverse {
name: "my-dataverse".to_string(),
triplestore_address: "predicted address".to_string(),
}
)
}
}

0 comments on commit a7ef8d1

Please sign in to comment.