Skip to content

Commit

Permalink
add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Art3miX committed Oct 14, 2024
1 parent b50420e commit 3a3fefb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
17 changes: 13 additions & 4 deletions contracts/services/template/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ pub enum QueryMsg {}

#[cw_serde]
#[derive(OptionalStruct)]
pub struct ServiceConfig {}
pub struct ServiceConfig {
/// We ignore this field when generating the OptionalServiceConfig
/// This means this field is not updatable
#[ignore_optional]
pub ignore_optional_admin: String,
}

impl ServiceConfigValidation<Config> for ServiceConfig {
#[cfg(not(target_arch = "wasm32"))]
fn pre_validate(&self, _api: &dyn cosmwasm_std::Api) -> Result<(), ServiceError> {
Ok(())
}

fn validate(&self, _deps: Deps) -> Result<Config, ServiceError> {
Ok(Config {})
fn validate(&self, deps: Deps) -> Result<Config, ServiceError> {
Ok(Config {
admin: deps.api.addr_validate(&self.ignore_optional_admin)?,
})
}
}

Expand All @@ -47,4 +54,6 @@ impl OptionalServiceConfig {
}

#[cw_serde]
pub struct Config {}
pub struct Config {
pub admin: Addr,
}
27 changes: 21 additions & 6 deletions contracts/services/template/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::msg::{ActionsMsgs, Config, QueryMsg, ServiceConfig};
use crate::msg::{ActionsMsgs, Config, OptionalServiceConfig, QueryMsg, ServiceConfig};
use cosmwasm_std::Addr;
use cw_multi_test::{error::AnyResult, App, AppResponse, ContractWrapper, Executor};
use cw_ownable::Ownership;
Expand Down Expand Up @@ -52,8 +52,10 @@ impl TemplateTestSuite {
self.contract_init(self.template_code_id, "template", &init_msg, &[])
}

fn template_config(&self) -> ServiceConfig {
ServiceConfig {}
fn template_config(&self, admin: String) -> ServiceConfig {
ServiceConfig {
ignore_optional_admin: admin,
}
}

fn execute_noop(&mut self, addr: Addr) -> AnyResult<AppResponse> {
Expand Down Expand Up @@ -104,7 +106,8 @@ impl ServiceTestSuite for TemplateTestSuite {
fn instantiate_with_valid_config() {
let mut suite = TemplateTestSuite::default();

let cfg = suite.template_config();
let admin_addr = suite.owner().clone();
let cfg = suite.template_config(admin_addr.to_string());

// Instantiate Template contract
let svc = suite.template_init(&cfg);
Expand All @@ -119,14 +122,26 @@ fn instantiate_with_valid_config() {

// Verify service config
let svc_cfg: Config = suite.query_wasm(&svc, &QueryMsg::GetServiceConfig {});
assert_eq!(svc_cfg, Config {});
assert_eq!(svc_cfg, Config { admin: admin_addr });

let raw_svc_cfg: ServiceConfig = suite.query_wasm(&svc, &QueryMsg::GetRawServiceConfig {});
assert_eq!(
raw_svc_cfg,
ServiceConfig {
ignore_optional_admin: suite.owner().clone().to_string(),
}
);

// Here we just want to make sure that our ignore_optional actually works
// Because we ignore the only available field, OptionalServiceConfig expected to have no fields
let _ = OptionalServiceConfig {};
}

#[test]
fn execute_action() {
let mut suite = TemplateTestSuite::default();

let cfg = suite.template_config();
let cfg = suite.template_config(suite.owner().to_string());

// Instantiate Template contract
let svc = suite.template_init(&cfg);
Expand Down

0 comments on commit 3a3fefb

Please sign in to comment.