From 4bc3d24bd9a84dbb71b533485dacf4b8fc0daffb Mon Sep 17 00:00:00 2001 From: Matthew Witkowski Date: Fri, 9 Feb 2024 09:38:30 -0500 Subject: [PATCH] Add tests for tag storage. --- contracts/nav/src/storage/tag.rs | 118 +++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/contracts/nav/src/storage/tag.rs b/contracts/nav/src/storage/tag.rs index 92665a0c..804858f8 100644 --- a/contracts/nav/src/storage/tag.rs +++ b/contracts/nav/src/storage/tag.rs @@ -67,3 +67,121 @@ pub fn get_types(storage: &dyn Storage) -> Result, ContractError> { .collect(); keys } + +#[cfg(test)] +mod tests { + use provwasm_mocks::mock_provenance_dependencies; + + use crate::storage::tag::{add_type, get_types, has_type, remove_type}; + + #[test] + fn test_empty_get_types() { + let deps = mock_provenance_dependencies(); + let types = get_types(&deps.storage).unwrap(); + let expected: Vec = vec![]; + assert_eq!(expected, types); + } + + #[test] + fn test_add_and_get_one_item() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + let expected: Vec = vec!["tag1".to_string()]; + let types = get_types(&deps.storage).unwrap(); + assert_eq!(expected, types); + } + + #[test] + fn test_add_and_get_multi_item() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + add_type(deps.as_mut().storage, "tag2").unwrap(); + let types = get_types(&deps.storage).unwrap(); + let expected: Vec = vec!["tag1".to_string(), "tag2".to_string()]; + assert_eq!(expected, types); + } + + #[test] + fn test_add_and_get_duplicate_entry() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + add_type(deps.as_mut().storage, "tag2").unwrap(); + add_type(deps.as_mut().storage, "tag2").unwrap(); + let types = get_types(&deps.storage).unwrap(); + let expected: Vec = vec!["tag1".to_string(), "tag2".to_string()]; + assert_eq!(expected, types); + } + + #[test] + fn test_has_type_is_false_on_empty() { + let deps = mock_provenance_dependencies(); + let value = has_type(&deps.storage, "tag1"); + let expected = false; + assert_eq!(expected, value); + } + + #[test] + fn test_has_type_is_false_when_missing() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + let value = has_type(&deps.storage, "tag2"); + let expected = false; + assert_eq!(expected, value); + } + + #[test] + fn test_has_type_success() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + let value = has_type(&deps.storage, "tag1"); + let expected = true; + assert_eq!(expected, value); + } + + #[test] + fn test_remove_type_empty() { + let mut deps = mock_provenance_dependencies(); + remove_type(deps.as_mut().storage, "tag1"); + let expected: Vec = vec![]; + let types = get_types(&deps.storage).unwrap(); + assert_eq!(expected, types); + } + + #[test] + fn test_remove_type_invalid() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + remove_type(deps.as_mut().storage, "tag2"); + let expected: Vec = vec!["tag1".to_string()]; + let types = get_types(&deps.storage).unwrap(); + assert_eq!(expected, types); + } + + #[test] + fn test_remove_type_single() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + remove_type(deps.as_mut().storage, "tag1"); + let expected: Vec = vec![]; + let types = get_types(&deps.storage).unwrap(); + assert_eq!(expected, types); + } + + #[test] + fn test_remove_type_multiple() { + let mut deps = mock_provenance_dependencies(); + add_type(deps.as_mut().storage, "tag1").unwrap(); + add_type(deps.as_mut().storage, "tag2").unwrap(); + add_type(deps.as_mut().storage, "tag3").unwrap(); + + remove_type(deps.as_mut().storage, "tag1"); + let expected: Vec = vec!["tag2".to_string(), "tag3".to_string()]; + let types = get_types(&deps.storage).unwrap(); + assert_eq!(expected, types); + + remove_type(deps.as_mut().storage, "tag2"); + let expected: Vec = vec!["tag3".to_string()]; + let types = get_types(&deps.storage).unwrap(); + assert_eq!(expected, types); + } +}