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

tests: Multiple generic interfaces #239

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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
170 changes: 168 additions & 2 deletions sylvia/tests/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
where
for<'msg_de> Msg: CustomMsg + Deserialize<'msg_de>,
Param: sylvia::types::CustomMsg,
for<'msg_de> QueryRet: CustomQuery + DeserializeOwned,
QueryRet: CustomQuery + DeserializeOwned,
{
type Error: From<StdError>;

Expand All @@ -29,6 +29,56 @@
}
}

pub mod whitelist {
use cosmwasm_std::{CosmosMsg, CustomMsg, CustomQuery, Response, StdError};

use serde::de::DeserializeOwned;
use serde::Deserialize;
use sylvia::types::{ExecCtx, QueryCtx};
use sylvia_derive::interface;

#[interface(module=msg)]
pub trait Whitelist<Msg, QueryRet>

Check warning on line 41 in sylvia/tests/generics.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/generics.rs#L41

Added line #L41 was not covered by tests
where
for<'msg_de> Msg: CustomMsg + Deserialize<'msg_de>,
for<'msg_de> QueryRet: CustomQuery + DeserializeOwned,
jawoznia marked this conversation as resolved.
Show resolved Hide resolved
{
type Error: From<StdError>;

#[msg(exec)]
fn update_admins(
&self,
ctx: ExecCtx,
msgs: Vec<CosmosMsg<Msg>>,
) -> Result<Response, Self::Error>;

#[msg(query)]
fn admins_list(&self, ctx: QueryCtx) -> Result<QueryRet, Self::Error>;
}
}

pub mod non_generic {
use cosmwasm_std::{CosmosMsg, Empty, Response, StdError};

use sylvia::types::{ExecCtx, QueryCtx};
use sylvia_derive::interface;

#[interface(module=msg)]
pub trait NonGeneric {

Check warning on line 67 in sylvia/tests/generics.rs

View check run for this annotation

Codecov / codecov/patch

sylvia/tests/generics.rs#L67

Added line #L67 was not covered by tests
type Error: From<StdError>;

#[msg(exec)]
fn non_generic_exec(
&self,
ctx: ExecCtx,
msgs: Vec<CosmosMsg<Empty>>,
) -> Result<Response, Self::Error>;

#[msg(query)]
fn non_generic_query(&self, ctx: QueryCtx) -> Result<Response, Self::Error>;
}
}

pub mod cw1_contract {
use cosmwasm_std::{Response, StdResult};
use sylvia::types::InstantiateCtx;
Expand All @@ -40,6 +90,8 @@

#[contract]
#[messages(crate::cw1<ExternalMsg, ExternalMsg, ExternalQuery> as Cw1)]
#[messages(crate::whitelist<ExternalMsg, ExternalQuery> as Whitelist: custom(msg))]
#[messages(crate::non_generic as NonGeneric: custom(msg))]
/// Required if interface returns generic `Response`
#[sv::custom(msg=ExternalMsg)]
impl Cw1Contract {
Expand All @@ -54,6 +106,66 @@
}
}

pub mod impl_non_generic {
use crate::cw1_contract::Cw1Contract;
use crate::non_generic::NonGeneric;
use cosmwasm_std::{CosmosMsg, Empty, Response, StdError};
use sylvia::types::{ExecCtx, QueryCtx};
use sylvia_derive::contract;

#[contract(module = crate::cw1_contract)]
#[messages(crate::non_generic as NonGeneric)]
#[sv::custom(msg=crate::ExternalMsg)]
impl NonGeneric for Cw1Contract {
type Error = StdError;

#[msg(exec)]
fn non_generic_exec(
&self,
_ctx: ExecCtx,
_msgs: Vec<CosmosMsg<Empty>>,
) -> Result<Response, Self::Error> {
Ok(Response::new())
}

#[msg(query)]
fn non_generic_query(&self, _ctx: QueryCtx) -> Result<Response, Self::Error> {
Ok(Response::default())
}
}
}

pub mod impl_whitelist {
use cosmwasm_std::{CosmosMsg, Response, StdError};
use sylvia::types::{ExecCtx, QueryCtx};
use sylvia_derive::contract;

use crate::cw1_contract::Cw1Contract;
use crate::whitelist::Whitelist;
use crate::{ExternalMsg, ExternalQuery};

#[contract(module = crate::cw1_contract)]
#[messages(crate::whitelist as Whitelist)]
#[sv::custom(msg=ExternalMsg)]
impl Whitelist<ExternalMsg, ExternalQuery> for Cw1Contract {
type Error = StdError;

#[msg(exec)]
fn update_admins(
&self,
_ctx: ExecCtx,
_msgs: Vec<CosmosMsg<ExternalMsg>>,
) -> Result<Response, Self::Error> {
Ok(Response::new())
}

#[msg(query)]
fn admins_list(&self, _ctx: QueryCtx) -> Result<ExternalQuery, Self::Error> {
Ok(ExternalQuery {})
}
}
}

pub mod impl_cw1 {
use cosmwasm_std::{CosmosMsg, Response, StdError};
use sylvia::types::{ExecCtx, QueryCtx};
Expand All @@ -63,6 +175,7 @@

#[contract(module = crate::cw1_contract)]
#[messages(crate::cw1 as Cw1)]
#[sv::custom(msg=ExternalMsg)]
impl Cw1<ExternalMsg, crate::ExternalMsg, crate::ExternalQuery> for Cw1Contract {
type Error = StdError;

Expand Down Expand Up @@ -93,22 +206,38 @@
#[cw_serde]
pub struct ExternalQuery;
impl cosmwasm_std::CustomQuery for ExternalQuery {}

#[cfg(all(test, feature = "mt"))]
mod tests {
use crate::cw1::{InterfaceTypes, Querier as Cw1Querier};
use crate::cw1_contract::Cw1Contract;
use crate::impl_cw1::test_utils::Cw1;
use crate::impl_non_generic::test_utils::NonGeneric;
use crate::impl_whitelist::test_utils::Whitelist;
use crate::non_generic::Querier as NonGenericQuerier;
use crate::whitelist::Querier as WhitelistQuerier;
use crate::{ExternalMsg, ExternalQuery};
use cosmwasm_std::{testing::mock_dependencies, Addr, CosmosMsg, Empty, QuerierWrapper};
use sylvia::multitest::App;
use sylvia::types::InterfaceMessages;

#[test]
fn construct_messages() {
let contract = Addr::unchecked("contract");

// Direct message construction
// cw1
let _ = crate::cw1::QueryMsg::<_, Empty>::some_query(ExternalMsg {});
let _ = crate::cw1::ExecMsg::execute(vec![CosmosMsg::Custom(ExternalMsg {})]);
let _ = crate::cw1::ExecMsg::execute(vec![CosmosMsg::Custom(Empty {})]);

// whitelist
let _ = crate::whitelist::QueryMsg::<ExternalQuery>::admins_list();
let _ = crate::whitelist::ExecMsg::update_admins(vec![CosmosMsg::Custom(ExternalMsg {})]);

// non_generic
let _ = crate::non_generic::QueryMsg::non_generic_query();
let _ = crate::non_generic::ExecMsg::non_generic_exec(vec![]);

// Generic Querier
let deps = mock_dependencies();
let querier: QuerierWrapper<ExternalQuery> = QuerierWrapper::new(&deps.querier);
Expand All @@ -120,6 +249,8 @@

let contract_querier = crate::cw1_contract::BoundQuerier::borrowed(&contract, &querier);
let _: Result<ExternalQuery, _> = contract_querier.some_query(ExternalMsg {});
let _: Result<ExternalQuery, _> = contract_querier.admins_list();
let _ = contract_querier.non_generic_query();

// Construct messages with Interface extension
let _ =
Expand All @@ -131,4 +262,39 @@
CosmosMsg::Custom(ExternalMsg {}),
]);
}

#[test]
fn mt_helpers() {
let _ = Cw1Contract::new();
let app = App::<cw_multi_test::BasicApp<ExternalMsg>>::custom(|_, _, _| {});
let code_id = crate::cw1_contract::multitest_utils::CodeId::store_code(&app);

let owner = "owner";

let contract = code_id
.instantiate()
.with_label("Cw1Contract")
.call(owner)
.unwrap();

// CustomMsg generic Interface
contract.cw1_proxy().some_query(ExternalMsg {}).unwrap();
contract.cw1_proxy().execute(vec![]).call(owner).unwrap();

// Non-CustomMsg generic Interface
contract.whitelist_proxy().admins_list().unwrap();
contract
.whitelist_proxy()
.update_admins(vec![])
.call(owner)
.unwrap();

// Non-CustomMsg non-generic Interface
contract.non_generic_proxy().non_generic_query().unwrap();
contract
.non_generic_proxy()
.non_generic_exec(vec![])
.call(owner)
.unwrap();
}
}
Loading