From 4c6cee6cd6c45f25b7641b79bb311dc2db3ae188 Mon Sep 17 00:00:00 2001 From: Matthew Witkowski Date: Fri, 9 Feb 2024 16:45:36 -0500 Subject: [PATCH] Start adding tests for execute validate. Mocks still need to be updated. --- contracts/nav/src/execute/validate.rs | 97 ++++++++++++++++++++++----- contracts/nav/src/testing/msg.rs | 21 ++++++ contracts/nav/src/testing/setup.rs | 10 +++ 3 files changed, 112 insertions(+), 16 deletions(-) diff --git a/contracts/nav/src/execute/validate.rs b/contracts/nav/src/execute/validate.rs index 28195205..8eef7263 100644 --- a/contracts/nav/src/execute/validate.rs +++ b/contracts/nav/src/execute/validate.rs @@ -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 = 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 = 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 = 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); + } } } diff --git a/contracts/nav/src/testing/msg.rs b/contracts/nav/src/testing/msg.rs index 2ea480fb..ea3488eb 100644 --- a/contracts/nav/src/testing/msg.rs +++ b/contracts/nav/src/testing/msg.rs @@ -1,3 +1,5 @@ +use std::vec; + use cosmwasm_std::Addr; use crate::core::msg::{ExecuteMsg, InstantiateMsg}; @@ -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(), + } +} diff --git a/contracts/nav/src/testing/setup.rs b/contracts/nav/src/testing/setup.rs index fe570275..a93f17cc 100644 --- a/contracts/nav/src/testing/setup.rs +++ b/contracts/nav/src/testing/setup.rs @@ -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. +}