feat(machoke): add in-memory store
This commit was merged in pull request #4.
This commit is contained in:
147
src/machoke/lib_test.rs
Normal file
147
src/machoke/lib_test.rs
Normal file
@@ -0,0 +1,147 @@
|
||||
use machoke::*;
|
||||
|
||||
#[test]
|
||||
fn get_existing_key_returns_stored_value() {
|
||||
let mut machoke = Machoke::new(Options::default());
|
||||
machoke.set("home".to_string(), "plex".to_string()).unwrap();
|
||||
|
||||
let value = machoke.get("home").unwrap();
|
||||
|
||||
assert_eq!(value, "plex");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_missing_key_returns_not_found() {
|
||||
let machoke: Machoke<String> = Machoke::new(Options::default());
|
||||
|
||||
let result = machoke.get("home");
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
Err(Error::NotFound {
|
||||
key: "home".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_empty_key_returns_invalid_key() {
|
||||
let machoke: Machoke<String> = Machoke::new(Options::default());
|
||||
|
||||
let result = machoke.get("");
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
Err(Error::InvalidKey {
|
||||
key: "".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_new_key_stores_value() {
|
||||
let mut machoke = Machoke::new(Options::default());
|
||||
|
||||
machoke.set("home".to_string(), "plex".to_string()).unwrap();
|
||||
|
||||
assert_eq!(machoke.get("home").unwrap(), "plex");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_existing_key_overwrites_value() {
|
||||
let mut machoke = Machoke::new(Options::default());
|
||||
machoke
|
||||
.set("home".to_string(), "jellyfin".to_string())
|
||||
.unwrap();
|
||||
|
||||
machoke.set("home".to_string(), "plex".to_string()).unwrap();
|
||||
|
||||
assert_eq!(machoke.get("home").unwrap(), "plex");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_empty_key_returns_invalid_key() {
|
||||
let mut machoke = Machoke::new(Options::default());
|
||||
|
||||
let result = machoke.set("".to_string(), "plex".to_string());
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
Err(Error::InvalidKey {
|
||||
key: "".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_existing_key_removes_value() {
|
||||
let mut machoke = Machoke::new(Options::default());
|
||||
machoke.set("home".to_string(), "plex".to_string()).unwrap();
|
||||
|
||||
let deleted = machoke.delete("home").unwrap();
|
||||
|
||||
assert!(deleted);
|
||||
assert_eq!(
|
||||
machoke.get("home"),
|
||||
Err(Error::NotFound {
|
||||
key: "home".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_missing_key_returns_false() {
|
||||
let mut machoke: Machoke<String> = Machoke::new(Options::default());
|
||||
|
||||
let deleted = machoke.delete("home").unwrap();
|
||||
|
||||
assert!(!deleted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_empty_key_returns_invalid_key() {
|
||||
let mut machoke: Machoke<String> = Machoke::new(Options::default());
|
||||
|
||||
let result = machoke.delete("");
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
Err(Error::InvalidKey {
|
||||
key: "".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_populated_store_returns_all_entries() {
|
||||
let mut machoke = Machoke::new(Options::default());
|
||||
machoke.set("home".to_string(), "plex".to_string()).unwrap();
|
||||
machoke
|
||||
.set("docs".to_string(), "readme".to_string())
|
||||
.unwrap();
|
||||
|
||||
let mut entries = machoke
|
||||
.list()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|entry| (entry.key.to_string(), entry.value.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
entries.sort();
|
||||
|
||||
assert_eq!(
|
||||
entries,
|
||||
vec![
|
||||
("docs".to_string(), "readme".to_string()),
|
||||
("home".to_string(), "plex".to_string()),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_empty_store_returns_empty_entries() {
|
||||
let machoke: Machoke<String> = Machoke::new(Options::default());
|
||||
|
||||
let entries = machoke.list().unwrap();
|
||||
|
||||
assert!(entries.is_empty());
|
||||
}
|
||||
Reference in New Issue
Block a user