refactor(db): refactor tests in fs store

refactor/errors
Tomáš Mládek 2023-11-04 10:34:49 +01:00
parent 2150841ee6
commit 203b105b15
1 changed files with 84 additions and 119 deletions

View File

@ -979,29 +979,52 @@ mod test {
);
}
fn _prepare_hier_vault(tree_mode: VaultTreeMode) -> UpEndConnection {
/// Prepare a temporary filesystem structure for testing
/// Returns the database connection
/// The structure is as follows:
/// ```text
/// NATIVE
/// ├── nested_directory
/// │   ├── nested_two
/// │   │   └── nested_three
/// │   │   │ └── foo.txt
/// │   │ └── nested_four
/// │   │ └── baz.txt
/// │   └── nested_three
/// │   └── bar.txt
/// └── in_root.txt
/// ```
fn _prepare_hier_vault(tree_mode: VaultTreeMode) -> (UpEndConnection, TempDir) {
// Prepare temporary filesystem structure
let temp_dir = TempDir::new().unwrap();
let temp_dir_path = temp_dir.path().canonicalize().unwrap();
let file_path = temp_dir_path
.join("foo")
.join("bar")
.join("baz")
.join("baz.txt");
std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
let mut tmp_file = File::create(&file_path).unwrap();
writeln!(tmp_file, "Hello, world!").unwrap();
let nested_directory_path = temp_dir_path.join("nested_directory");
fs::create_dir(&nested_directory_path).unwrap();
let nested_two_path = nested_directory_path.join("nested_two");
fs::create_dir(&nested_two_path).unwrap();
let nested_three_first_path = nested_directory_path.join("nested_three");
fs::create_dir(&nested_three_first_path).unwrap();
let nested_three_second_path = nested_two_path.join("nested_three");
fs::create_dir(&nested_three_second_path).unwrap();
let nested_four_path = nested_two_path.join("nested_four");
fs::create_dir(&nested_four_path).unwrap();
let file_path = temp_dir_path.join("foo").join("baz").join("qux.txt");
std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
let mut tmp_file = File::create(&file_path).unwrap();
writeln!(tmp_file, "Hello, world 2!").unwrap();
let file_path = nested_three_second_path.join("foo.txt");
let mut tmp_file = File::create(file_path).unwrap();
writeln!(tmp_file, "Hello, World! I'm foo, and deep.").unwrap();
let file_path = temp_dir_path.join("zot.txt");
std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
let mut tmp_file = File::create(&file_path).unwrap();
writeln!(tmp_file, "Hello, world 3!").unwrap();
let file_path = nested_three_first_path.join("bar.txt");
let mut tmp_file = File::create(file_path).unwrap();
writeln!(tmp_file, "Hello, World! I'm bar, and shallower.").unwrap();
let file_path = nested_four_path.join("baz.txt");
let mut tmp_file = File::create(file_path).unwrap();
writeln!(tmp_file, "Hello, World! I'm baz.").unwrap();
let file_path = temp_dir_path.join("in_root.txt");
let mut tmp_file = File::create(file_path).unwrap();
writeln!(tmp_file, "Hello, World! I'm in root.").unwrap();
// Initialize database
let open_result = UpEndDatabase::open(&temp_dir, true).unwrap();
@ -1021,122 +1044,64 @@ mod test {
)
.unwrap();
open_result.db.connection().unwrap()
(open_result.db.connection().unwrap(), temp_dir)
}
fn assert_paths(paths: Vec<&str>, connection: &UpEndConnection) {
paths.iter().for_each(|path| {
let upath: UHierPath = path.parse().unwrap();
assert!(
resolve_path(&connection, &upath, false).is_ok(),
"Failed: {}",
upath
);
});
}
fn test_initial_scan(mode: VaultTreeMode, expected_paths: Vec<&str>) {
let (connection, _vault_dir) = _prepare_hier_vault(mode);
assert_paths(expected_paths, &connection);
}
#[test]
fn test_mirror_mode() {
let connection = _prepare_hier_vault(VaultTreeMode::Mirror);
let native_path = UHierPath(vec!["NATIVE".parse().unwrap()]);
assert!(
resolve_path(&connection, &native_path, false).is_ok(),
"Failed: NATIVE"
);
let first_path = UHierPath(vec![
"NATIVE".parse().unwrap(),
"foo".parse().unwrap(),
"bar".parse().unwrap(),
"baz".parse().unwrap(),
"baz.txt".parse().unwrap(),
]);
assert!(
resolve_path(&connection, &first_path, false).is_ok(),
"Failed: `foo/bar/baz/baz.txt`"
);
let second_path = UHierPath(vec![
"NATIVE".parse().unwrap(),
"foo".parse().unwrap(),
"baz".parse().unwrap(),
"qux.txt".parse().unwrap(),
]);
assert!(
resolve_path(&connection, &second_path, false).is_ok(),
"Failed: `foo/baz/qux.txt`"
);
let third_path = UHierPath(vec!["NATIVE".parse().unwrap(), "zot.txt".parse().unwrap()]);
assert!(
resolve_path(&connection, &third_path, false).is_ok(),
"Failed: `zot.txt`"
test_initial_scan(
VaultTreeMode::Mirror,
vec![
"NATIVE",
"NATIVE/nested_directory/nested_two/nested_three/foo.txt",
"NATIVE/nested_directory/nested_two/nested_four/baz.txt",
"NATIVE/nested_directory/nested_three/bar.txt",
"NATIVE/in_root.txt",
],
);
}
#[test]
fn test_flat_mode() {
let connection = _prepare_hier_vault(VaultTreeMode::Flat);
let native_path = UHierPath(vec!["NATIVE".parse().unwrap()]);
assert!(
resolve_path(&connection, &native_path, false).is_ok(),
"Failed: NATIVE"
);
let first_path = UHierPath(vec![
"NATIVE".parse().unwrap(),
"baz".parse().unwrap(),
"baz.txt".parse().unwrap(),
]);
assert!(
resolve_path(&connection, &first_path, false).is_ok(),
"Failed: `baz/baz.txt`"
);
let second_path = UHierPath(vec![
"NATIVE".parse().unwrap(),
"baz".parse().unwrap(),
"qux.txt".parse().unwrap(),
]);
assert!(
resolve_path(&connection, &second_path, false).is_ok(),
"Failed: `baz/qux.txt`"
);
let third_path = UHierPath(vec!["NATIVE".parse().unwrap(), "zot.txt".parse().unwrap()]);
assert!(
resolve_path(&connection, &third_path, false).is_ok(),
"Failed: `zot.txt`"
test_initial_scan(
VaultTreeMode::Flat,
vec![
"NATIVE",
"NATIVE/nested_three/foo.txt",
"NATIVE/nested_four/baz.txt",
"NATIVE/nested_three/bar.txt",
"NATIVE/in_root.txt",
],
);
}
#[test]
fn test_depth_mode() {
let connection = _prepare_hier_vault(VaultTreeMode::DepthFirst);
let native_path = UHierPath(vec!["NATIVE".parse().unwrap()]);
assert!(
resolve_path(&connection, &native_path, false).is_ok(),
"Failed: NATIVE"
);
let first_path = UHierPath(vec![
"NATIVE".parse().unwrap(),
"foo".parse().unwrap(),
"baz".parse().unwrap(),
"baz.txt".parse().unwrap(),
]);
assert!(
resolve_path(&connection, &first_path, false).is_ok(),
"Failed: `foo/baz/baz.txt`"
);
let second_path = UHierPath(vec![
"NATIVE".parse().unwrap(),
"foo".parse().unwrap(),
"baz".parse().unwrap(),
"qux.txt".parse().unwrap(),
]);
assert!(
resolve_path(&connection, &second_path, false).is_ok(),
"Failed: `foo/baz/qux.txt`"
);
let third_path = UHierPath(vec!["NATIVE".parse().unwrap(), "zot.txt".parse().unwrap()]);
assert!(
resolve_path(&connection, &third_path, false).is_ok(),
"Failed: `zot.txt`"
test_initial_scan(
VaultTreeMode::DepthFirst,
vec![
"NATIVE",
"NATIVE/nested_directory/nested_two/nested_four/baz.txt",
"NATIVE/nested_directory/nested_three/foo.txt",
"NATIVE/nested_directory/nested_three/bar.txt",
"NATIVE/in_root.txt",
],
);
}
}