Update
This commit is contained in:
parent
764937e034
commit
8882712925
9 changed files with 346 additions and 3 deletions
104
tests/storage.rs
Normal file
104
tests/storage.rs
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
extern crate regex;
|
||||
extern crate schematics;
|
||||
extern crate tempdir;
|
||||
|
||||
use regex::Regex;
|
||||
use std::fs::{create_dir, File};
|
||||
use std::path::Path;
|
||||
use tempdir::TempDir;
|
||||
use schematics::storage::{SimpleStorage, Storage};
|
||||
|
||||
fn get_dir<'a, I: IntoIterator<Item=&'a &'a str>>(content: I) -> TempDir {
|
||||
let tmp_dir = TempDir::new("unittest").expect("create temp dir");
|
||||
let storage_path = tmp_dir.path().join("_filename");
|
||||
create_dir(storage_path.clone()).unwrap();
|
||||
for path in content {
|
||||
let file_path = storage_path.join(path);
|
||||
File::create(file_path).expect("create temp file");
|
||||
}
|
||||
tmp_dir
|
||||
}
|
||||
|
||||
fn get_storage(path: &Path) -> SimpleStorage {
|
||||
SimpleStorage::new(path.to_str().unwrap().into(), "filename".into())
|
||||
}
|
||||
|
||||
// Normal cases
|
||||
|
||||
#[test]
|
||||
fn single_file() {
|
||||
let dir = get_dir(&["12345"]);
|
||||
let storage = get_storage(dir.path());
|
||||
|
||||
assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
|
||||
assert_eq!(dir.path().join("_filename").join("12345"), Path::new(&storage.read_filename().unwrap()));
|
||||
assert_eq!(storage.recent_date().unwrap(), 12345);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_files() {
|
||||
let dir = get_dir(&["12345", "23456"]);
|
||||
let storage = get_storage(dir.path());
|
||||
|
||||
assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
|
||||
assert_eq!(dir.path().join("_filename").join("23456"), Path::new(&storage.read_filename().unwrap()));
|
||||
assert_eq!(storage.recent_date().unwrap(), 23456);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn another_two_files() {
|
||||
let dir = get_dir(&["23456", "12345"]);
|
||||
let storage = get_storage(dir.path());
|
||||
|
||||
assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
|
||||
assert_eq!(dir.path().join("_filename").join("23456"), Path::new(&storage.read_filename().unwrap()));
|
||||
assert_eq!(storage.recent_date().unwrap(), 23456);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn three_files() {
|
||||
let dir = get_dir(&["23456", "9", "12345"]);
|
||||
let storage = get_storage(dir.path());
|
||||
|
||||
assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
|
||||
assert_eq!(dir.path().join("_filename").join("23456"), Path::new(&storage.read_filename().unwrap()));
|
||||
assert_eq!(storage.recent_date().unwrap(), 23456);
|
||||
}
|
||||
|
||||
// Bad cases
|
||||
|
||||
#[test]
|
||||
fn empty_storage() {
|
||||
let dir = TempDir::new("unittest").expect("create temp dir");
|
||||
let storage = get_storage(dir.path());
|
||||
|
||||
assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
|
||||
assert!(storage.read_filename().is_err());
|
||||
assert_eq!(storage.read_filename().unwrap_err().description(), "entity not found");
|
||||
assert!(storage.recent_date().is_err());
|
||||
assert_eq!(storage.recent_date().unwrap_err().description(), "entity not found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_storage_for_filename() {
|
||||
let dir = get_dir(&[]);
|
||||
let storage = get_storage(dir.path());
|
||||
|
||||
assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
|
||||
assert!(storage.read_filename().is_err());
|
||||
assert_eq!(storage.read_filename().unwrap_err().description(), "Not found");
|
||||
assert!(storage.recent_date().is_err());
|
||||
assert_eq!(storage.recent_date().unwrap_err().description(), "Not found");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_file() {
|
||||
let dir = get_dir(&["abba"]);
|
||||
let storage = get_storage(dir.path());
|
||||
|
||||
assert!(Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display())).unwrap().is_match(&storage.write_filename()));
|
||||
assert!(storage.read_filename().is_err());
|
||||
assert_eq!(storage.read_filename().unwrap_err().description(), "Not found");
|
||||
assert!(storage.recent_date().is_err());
|
||||
assert_eq!(storage.recent_date().unwrap_err().description(), "Not found");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue