test(export): 添加导出功能的测试用例
添加对zoxide导出功能的测试,包括JSON和CSV格式的导出验证 重构测试代码,提取公共测试数据和辅助函数
This commit is contained in:
parent
0e5333d0d8
commit
ab72fdfcb1
|
|
@ -53,14 +53,16 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::db::Dir;
|
||||
|
||||
const TEST_ENTRIES: [(&str, f64, u64); 3] = [
|
||||
("/home/alice/projects/zoxide", 42.5, 1714000000),
|
||||
("/home/alice/downloads", 7.0, 1713000000),
|
||||
(r#"/tmp"quotes,commas""#, 1.0, 1712000000),
|
||||
];
|
||||
|
||||
fn create_test_db() -> tempfile::TempDir {
|
||||
let data_dir = tempfile::tempdir().unwrap();
|
||||
let mut db = Database::open_dir(data_dir.path()).unwrap();
|
||||
for (path, rank, last_accessed) in [
|
||||
("/home/alice/projects/zoxide", 42.5, 1714000000),
|
||||
("/home/alice/downloads", 7.0, 1713000000),
|
||||
(r#"/tmp"quotes,commas""#, 1.0, 1712000000),
|
||||
] {
|
||||
for (path, rank, last_accessed) in TEST_ENTRIES {
|
||||
db.add_unchecked(path, rank, last_accessed);
|
||||
}
|
||||
db.save().unwrap();
|
||||
|
|
@ -73,6 +75,14 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
fn find_entry_by_path<'a>(entries: &'a [Dir], path: &str) -> &'a Dir<'a> {
|
||||
entries.iter().find(|d| d.path == path).unwrap()
|
||||
}
|
||||
|
||||
fn find_record_by_path<'a>(records: &'a [csv::StringRecord], path: &str) -> &'a csv::StringRecord {
|
||||
records.iter().find(|r| r.get(0).unwrap() == path).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_json() {
|
||||
let data_dir = create_test_db();
|
||||
|
|
@ -89,9 +99,18 @@ mod tests {
|
|||
let result: Vec<Dir> = serde_json::from_str(&content).unwrap();
|
||||
|
||||
assert_eq!(result.len(), 3);
|
||||
assert!(result.iter().any(|d| d.path == "/home/alice/projects/zoxide"));
|
||||
assert!(result.iter().any(|d| d.path == "/home/alice/downloads"));
|
||||
assert!(result.iter().any(|d| d.path == r#"/tmp"quotes,commas""#));
|
||||
|
||||
let entry1 = find_entry_by_path(&result, "/home/alice/projects/zoxide");
|
||||
assert!((entry1.rank - 42.5).abs() < 0.001);
|
||||
assert_eq!(entry1.last_accessed, 1714000000);
|
||||
|
||||
let entry2 = find_entry_by_path(&result, "/home/alice/downloads");
|
||||
assert!((entry2.rank - 7.0).abs() < 0.001);
|
||||
assert_eq!(entry2.last_accessed, 1713000000);
|
||||
|
||||
let entry3 = find_entry_by_path(&result, r#"/tmp"quotes,commas""#);
|
||||
assert!((entry3.rank - 1.0).abs() < 0.001);
|
||||
assert_eq!(entry3.last_accessed, 1712000000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -115,10 +134,17 @@ mod tests {
|
|||
let records: Vec<csv::StringRecord> = rdr.records().map(|r| r.unwrap()).collect();
|
||||
assert_eq!(records.len(), 3);
|
||||
|
||||
let paths: Vec<&str> = records.iter().map(|r| r.get(0).unwrap()).collect();
|
||||
assert!(paths.contains(&"/home/alice/projects/zoxide"));
|
||||
assert!(paths.contains(&"/home/alice/downloads"));
|
||||
assert!(paths.contains(&r#"/tmp"quotes,commas""#));
|
||||
let record1 = find_record_by_path(&records, "/home/alice/projects/zoxide");
|
||||
assert_eq!(record1.get(1).unwrap().parse::<f64>().unwrap(), 42.5);
|
||||
assert_eq!(record1.get(2).unwrap().parse::<u64>().unwrap(), 1714000000);
|
||||
|
||||
let record2 = find_record_by_path(&records, "/home/alice/downloads");
|
||||
assert_eq!(record2.get(1).unwrap().parse::<f64>().unwrap(), 7.0);
|
||||
assert_eq!(record2.get(2).unwrap().parse::<u64>().unwrap(), 1713000000);
|
||||
|
||||
let record3 = find_record_by_path(&records, r#"/tmp"quotes,commas""#);
|
||||
assert_eq!(record3.get(1).unwrap().parse::<f64>().unwrap(), 1.0);
|
||||
assert_eq!(record3.get(2).unwrap().parse::<u64>().unwrap(), 1712000000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
use std::fs;
|
||||
|
||||
use assert_cmd::Command;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn zoxide() -> Command {
|
||||
Command::cargo_bin("zoxide").unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_missing_format() {
|
||||
let out_dir = TempDir::new().unwrap();
|
||||
let out_file = out_dir.path().join("export.json");
|
||||
|
||||
zoxide()
|
||||
.args(["export", "--out", out_file.to_str().unwrap()])
|
||||
.assert()
|
||||
.failure();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_invalid_format() {
|
||||
let out_dir = TempDir::new().unwrap();
|
||||
let out_file = out_dir.path().join("export.json");
|
||||
|
||||
zoxide()
|
||||
.args(["export", "--format", "xml", "--out", out_file.to_str().unwrap()])
|
||||
.assert()
|
||||
.failure();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_missing_out() {
|
||||
zoxide()
|
||||
.args(["export", "--format", "json"])
|
||||
.assert()
|
||||
.failure();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_valid_json() {
|
||||
let data_dir = TempDir::new().unwrap();
|
||||
let test_dir = TempDir::new().unwrap();
|
||||
let out_dir = TempDir::new().unwrap();
|
||||
let out_file = out_dir.path().join("export.json");
|
||||
|
||||
zoxide()
|
||||
.env("_ZO_DATA_DIR", data_dir.path())
|
||||
.args(["add", test_dir.path().to_str().unwrap()])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
zoxide()
|
||||
.env("_ZO_DATA_DIR", data_dir.path())
|
||||
.args(["export", "--format", "json", "--out", out_file.to_str().unwrap()])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
assert!(out_file.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_valid_csv() {
|
||||
let data_dir = TempDir::new().unwrap();
|
||||
let test_dir = TempDir::new().unwrap();
|
||||
let out_dir = TempDir::new().unwrap();
|
||||
let out_file = out_dir.path().join("export.csv");
|
||||
|
||||
zoxide()
|
||||
.env("_ZO_DATA_DIR", data_dir.path())
|
||||
.args(["add", test_dir.path().to_str().unwrap()])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
zoxide()
|
||||
.env("_ZO_DATA_DIR", data_dir.path())
|
||||
.args(["export", "--format", "csv", "--out", out_file.to_str().unwrap()])
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
assert!(out_file.exists());
|
||||
|
||||
let content = fs::read_to_string(&out_file).unwrap();
|
||||
assert!(content.contains("path,rank,last_accessed"));
|
||||
}
|
||||
Loading…
Reference in New Issue