Skip to content

Commit

Permalink
test-cases: add test cases for hset methods
Browse files Browse the repository at this point in the history
  • Loading branch information
he-ym committed Jan 12, 2024
1 parent ff55b78 commit 0c23db2
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/data_store/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl KeyValueStore {
}

/// Gets a Value (converted to set<String> type) associated to the Key in the KeyValueStore
pub fn get_hset(&self, key: String) -> Option<Result<&HashSet<String>, ValueError>> {
pub fn get_hset(&self, key: String) -> Option<Result<HashSet<String>, ValueError>> {
match self._get_or_none_if_expired(&key) {
Some(value_entry) => Some(value_entry.get_value_as_hset()),
_ => None,
Expand Down
7 changes: 5 additions & 2 deletions src/data_store/value_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,12 @@ impl ValueEntry {
}
}

pub fn get_value_as_hset(&self) -> Result<&HashSet<String>, ValueError> {
pub fn get_value_as_hset(&self) -> Result<HashSet<String>, ValueError> {
match &self.value {
ValueType::Set(hash_set) => Ok(hash_set),
ValueType::Set(hash_set) => Ok(hash_set.to_owned()),
ValueType::String(string) => Ok(HashSet::from_iter(
string.chars().map(|item| item.to_string()),
)),
_ => Err(ValueError::TypeConversionImpossible),
}
}
Expand Down
146 changes: 143 additions & 3 deletions src/public_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::KeyValueStore;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use std::{collections::HashSet, time::Duration};

#[test]
fn test_contains() {
Expand Down Expand Up @@ -214,7 +214,7 @@ fn test_incr_decr() {
}

#[test]
fn test_set_get_list() {
fn test_set_get_deque() {
let mut store = KeyValueStore::new(5000);
store.set_list(
"ABC".to_string(),
Expand All @@ -234,7 +234,7 @@ fn test_set_get_list() {
}

#[test]
fn test_list_methods() {
fn test_deque_methods() {
let mut store = KeyValueStore::new(5000);
store.set_list("ABC".to_string(), vec!["X".to_string()], None);
assert_eq!(
Expand Down Expand Up @@ -317,3 +317,143 @@ fn test_list_methods() {
);
assert_eq!(store.list_size("ABC".to_string()).unwrap().unwrap(), 0);
}

#[test]
fn test_set_get_hset() {
let mut store = KeyValueStore::new(5000);
store.set_hset(
"ABC".to_string(),
vec![
"X".to_string(),
"Y".to_string(),
"Z".to_string(),
"Y".to_string(),
"Z".to_string(),
],
None,
);

let mut sorted_list_in_store = store.get_list("ABC".to_string()).unwrap().unwrap();
sorted_list_in_store.sort();
assert_eq!(
sorted_list_in_store,
vec!["X".to_string(), "Y".to_string(), "Z".to_string()]
);

store.set_string("DEF".to_string(), "XYZYZYZ".to_string(), None);
assert_eq!(
store.get_hset("DEF".to_string()).unwrap().unwrap(),
HashSet::from_iter(vec!["X".to_string(), "Y".to_string(), "Z".to_string()])
);
}

#[test]
fn test_add_remove_hset() {
let mut store = KeyValueStore::new(5000);
store.set_hset(
"ABC".to_string(),
vec![
"X".to_string(),
"Y".to_string(),
"Z".to_string(),
"Y".to_string(),
"Z".to_string(),
],
None,
);

assert_eq!(store.hset_size("ABC".to_string()).unwrap().unwrap(), 3);

assert_eq!(
store
.hset_add("ABC".to_string(), "Q".to_string())
.unwrap()
.unwrap(),
4
);
assert!(store
.hset_contains("ABC".to_string(), "Q".to_string())
.unwrap()
.unwrap());

assert!(!store
.hset_contains("ABC".to_string(), "A".to_string())
.unwrap()
.unwrap());
assert_eq!(
store
.hset_remove("ABC".to_string(), "A".to_string())
.unwrap()
.unwrap(),
4
);

assert_eq!(
store
.hset_remove("ABC".to_string(), "X".to_string())
.unwrap()
.unwrap(),
3
);

let mut sorted_list_in_store = store.get_list("ABC".to_string()).unwrap().unwrap();
sorted_list_in_store.sort();
assert_eq!(
sorted_list_in_store,
vec!["Q".to_string(), "Y".to_string(), "Z".to_string()]
);
}

#[test]
fn test_hset_union_intersection_difference() {
let mut store = KeyValueStore::new(5000);
store.set_hset(
"ABC".to_string(),
vec![
"X".to_string(),
"Y".to_string(),
"Z".to_string(),
"A".to_string(),
"B".to_string(),
"B".to_string(),
],
None,
);

store.set_hset(
"DEF".to_string(),
vec![
"A".to_string(),
"B".to_string(),
"C".to_string(),
"D".to_string(),
"E".to_string(),
],
None,
);

assert_eq!(store.hset_size("ABC".to_string()).unwrap().unwrap(), 5);
assert_eq!(store.hset_size("DEF".to_string()).unwrap().unwrap(), 5);

let mut intersection = store
.hset_intersection("ABC".to_string(), "DEF".to_string())
.unwrap()
.unwrap();
intersection.sort();

let mut union = store
.hset_union("ABC".to_string(), "DEF".to_string())
.unwrap()
.unwrap();
union.sort();

let mut difference = store
.hset_difference("ABC".to_string(), "DEF".to_string())
.unwrap()
.unwrap();
difference.sort();

assert_eq!(intersection, vec!["A", "B"]);
assert_eq!(union, vec!["A", "B", "C", "D", "E", "X", "Y", "Z"]);
assert_eq!(difference, vec!["X", "Y", "Z"]);
}

0 comments on commit 0c23db2

Please sign in to comment.