Skip to content

Commit

Permalink
Start adding tests for execute validate. Mocks still need to be updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Taztingo committed Feb 9, 2024
1 parent 1ccaa49 commit 4c6cee6
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 16 deletions.
97 changes: 81 additions & 16 deletions contracts/nav/src/execute/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,46 +70,111 @@ impl Validate for ExecuteMsg {

#[cfg(test)]
mod tests {
use cosmwasm_std::Coin;
use cosmwasm_std::{Addr, Coin};
use provwasm_mocks::mock_provenance_dependencies;

use crate::{
core::error::ContractError,
core::{
error::{self, ContractError},
msg::ExecuteMsg,
},
testing::{
constants::{TEST_AMOUNT, TEST_DENOM},
msg::mock_change_owner_msg,
msg::{
mock_add_tag_types_msg, mock_change_owner_msg, mock_remove_tag_types_msg,
mock_set_tag_msg,
},
setup::{mock_markers, mock_scopes},
},
util::validate::Validate,
};

#[test]
fn test_validate_always_succeeds() {
let deps = mock_provenance_dependencies();
let msg = mock_change_owner_msg();
fn test_validate_succeeds_for_marker_set_tag() {
let mut deps = mock_provenance_dependencies();
mock_markers(deps.as_mut());
mock_scopes(deps.as_mut());

let asset_addr = Addr::unchecked("marker".to_string());
let msg = mock_set_tag_msg(&asset_addr);
let res = msg.validate(deps.as_ref()).unwrap();
assert_eq!((), res);
}

#[test]
fn test_change_owner_should_fail_with_funds() {
let msg = mock_change_owner_msg();
let funds = vec![Coin::new(TEST_AMOUNT, TEST_DENOM)];
fn test_validate_succeeds_for_scope_set_tag() {
let mut deps = mock_provenance_dependencies();
mock_markers(deps.as_mut());
mock_scopes(deps.as_mut());

let asset_addr = Addr::unchecked("scope".to_string());
let msg = mock_set_tag_msg(&asset_addr);
let res = msg.validate(deps.as_ref()).unwrap();
assert_eq!((), res);
}

let err = msg.validate_funds(&funds).unwrap_err();
#[test]
fn test_validate_fails_for_invalid_asset() {
let mut deps = mock_provenance_dependencies();
mock_markers(deps.as_mut());
mock_scopes(deps.as_mut());

let asset_addr = Addr::unchecked("invalid".to_string());
let msg = mock_set_tag_msg(&asset_addr);
let error = msg.validate(deps.as_ref()).unwrap_err();
assert_eq!(
ContractError::UnexpectedFunds {}.to_string(),
err.to_string()
ContractError::AssetDoesNotExist(asset_addr.to_string()).to_string(),
error.to_string()
);
}

#[test]
fn test_validate_succeeds_for_remaining_messages() {
let deps = mock_provenance_dependencies();
let msgs: Vec<ExecuteMsg> = vec![
mock_add_tag_types_msg(),
mock_remove_tag_types_msg(),
mock_change_owner_msg(),
];

for msg in msgs {
let res = msg.validate(deps.as_ref()).unwrap();
assert_eq!((), res);
}
}

#[test]
fn test_messages_should_fail_with_funds() {
let msgs: Vec<ExecuteMsg> = vec![
mock_add_tag_types_msg(),
mock_remove_tag_types_msg(),
mock_change_owner_msg(),
mock_set_tag_msg(&Addr::unchecked("test")),
];

let funds = vec![Coin::new(TEST_AMOUNT, TEST_DENOM)];
for msg in msgs {
let err = msg.validate_funds(&funds).unwrap_err();
assert_eq!(
ContractError::UnexpectedFunds {}.to_string(),
err.to_string()
);
}
}

#[test]
fn test_change_owner_should_succeed_with_no_funds() {
let msg = mock_change_owner_msg();
let funds = vec![];
let msgs: Vec<ExecuteMsg> = vec![
mock_add_tag_types_msg(),
mock_remove_tag_types_msg(),
mock_change_owner_msg(),
mock_set_tag_msg(&Addr::unchecked("test")),
];

let res = msg.validate_funds(&funds).unwrap();
assert_eq!((), res);
let funds = vec![];
for msg in msgs {
let res = msg.validate_funds(&funds).unwrap();
assert_eq!((), res);
}
}
}
21 changes: 21 additions & 0 deletions contracts/nav/src/testing/msg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::vec;

use cosmwasm_std::Addr;

use crate::core::msg::{ExecuteMsg, InstantiateMsg};
Expand All @@ -16,3 +18,22 @@ pub fn mock_change_owner_msg() -> ExecuteMsg {
new_owner: Addr::unchecked(NEW_OWNER),
}
}

pub fn mock_add_tag_types_msg() -> ExecuteMsg {
ExecuteMsg::AddTagTypes {
tag_types: vec![TAG1.to_string(), TAG2.to_string()],
}
}

pub fn mock_remove_tag_types_msg() -> ExecuteMsg {
ExecuteMsg::RemoveTagTypes {
tag_types: vec![TAG1.to_string(), TAG2.to_string()],
}
}

pub fn mock_set_tag_msg(asset: &Addr) -> ExecuteMsg {
ExecuteMsg::SetTag {
asset_addr: asset.clone(),
tag: TAG1.to_string(),
}
}
10 changes: 10 additions & 0 deletions contracts/nav/src/testing/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ pub fn mock_contract(deps: DepsMut) {
let msg = mock_instantiate_msg();
contract::instantiate(deps, env, info, msg).unwrap();
}

pub fn mock_scopes(deps: DepsMut) {
let path = "/provenance.metadata.v1.Query/Scope";
//deps.querier.
}

pub fn mock_markers(deps: DepsMut) {
let path = "/provenance.marker.v1.Query/Marker";
//deps.querier.
}

0 comments on commit 4c6cee6

Please sign in to comment.