Skip to content

Commit

Permalink
Add tests for tag storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Taztingo committed Feb 9, 2024
1 parent c17ba2a commit 4bc3d24
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions contracts/nav/src/storage/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,121 @@ pub fn get_types(storage: &dyn Storage) -> Result<Vec<String>, 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<String> = 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<String> = 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<String> = 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<String> = 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<String> = 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<String> = 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<String> = 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<String> = 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<String> = vec!["tag3".to_string()];
let types = get_types(&deps.storage).unwrap();
assert_eq!(expected, types);
}
}

0 comments on commit 4bc3d24

Please sign in to comment.