use regex::Regex; use schematics::storage::{SimpleStorage, Storage}; use std::fs::{create_dir, File}; use std::path::Path; use tempdir::TempDir; fn get_dir<'a, I: IntoIterator>(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" ); }