refactor: reorder tests to ensure save precedes get and delete
This commit is contained in:
118
src/tests.rs
118
src/tests.rs
@@ -71,36 +71,6 @@ mod tests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test: Get an item and verify the content
|
|
||||||
#[test]
|
|
||||||
fn test_get_item() {
|
|
||||||
with_temp_env(|data_dir| {
|
|
||||||
// Set the data directory for this test
|
|
||||||
let env = format!("KEEP_DIR={}", data_dir.display());
|
|
||||||
|
|
||||||
// Save an item with some content
|
|
||||||
let input = "test content";
|
|
||||||
// Create a command that pipes input to keep with the specified environment
|
|
||||||
let mut cmd = Command::new("sh");
|
|
||||||
cmd.arg("-c")
|
|
||||||
.arg(format!("echo {} | {}", input, env));
|
|
||||||
|
|
||||||
cmd.spawn()
|
|
||||||
.expect("Failed to spawn process")
|
|
||||||
.wait()
|
|
||||||
.expect("Failed to wait for child process");
|
|
||||||
|
|
||||||
// Get the item and verify the content
|
|
||||||
let output = Command::new("sh")
|
|
||||||
.arg("-c")
|
|
||||||
.arg(format!("{} cargo run -- --get", env))
|
|
||||||
.output()
|
|
||||||
.expect("Failed to execute command");
|
|
||||||
|
|
||||||
assert_eq!(String::from_utf8_lossy(&output.stdout), input);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test: List items and verify the output
|
// Test: List items and verify the output
|
||||||
#[test]
|
#[test]
|
||||||
fn test_list_items() {
|
fn test_list_items() {
|
||||||
@@ -131,9 +101,9 @@ mod tests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test: Delete an item and verify it's removed
|
// Test: Get an item and verify the content
|
||||||
#[test]
|
#[test]
|
||||||
fn test_delete_item() {
|
fn test_get_item() {
|
||||||
with_temp_env(|data_dir| {
|
with_temp_env(|data_dir| {
|
||||||
// Set the data directory for this test
|
// Set the data directory for this test
|
||||||
let env = format!("KEEP_DIR={}", data_dir.display());
|
let env = format!("KEEP_DIR={}", data_dir.display());
|
||||||
@@ -141,39 +111,23 @@ mod tests {
|
|||||||
// Save an item with some content
|
// Save an item with some content
|
||||||
let input = "test content";
|
let input = "test content";
|
||||||
// Create a command that pipes input to keep with the specified environment
|
// Create a command that pipes input to keep with the specified environment
|
||||||
let cmd = format!("echo {} | {}", input, env);
|
let mut cmd = Command::new("sh");
|
||||||
let mut child = Command::new("sh")
|
cmd.arg("-c")
|
||||||
.arg("-c")
|
.arg(format!("echo {} | {}", input, env));
|
||||||
.arg(cmd)
|
|
||||||
.spawn()
|
|
||||||
.expect("Failed to spawn process");
|
|
||||||
|
|
||||||
child.wait().expect("Failed to wait for child process");
|
cmd.spawn()
|
||||||
|
.expect("Failed to spawn process")
|
||||||
|
.wait()
|
||||||
|
.expect("Failed to wait for child process");
|
||||||
|
|
||||||
// Get the item ID
|
// Get the item and verify the content
|
||||||
let list_output = Command::new("sh")
|
let output = Command::new("sh")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(format!("{} cargo run -- --list", env))
|
.arg(format!("{} cargo run -- --get", env))
|
||||||
.output()
|
|
||||||
.expect("Failed to execute command");
|
|
||||||
let list_output_str = String::from_utf8_lossy(&list_output.stdout);
|
|
||||||
let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::<i64>().expect("Invalid ID");
|
|
||||||
|
|
||||||
// Delete the item
|
|
||||||
let _output = Command::new("sh")
|
|
||||||
.arg("-c")
|
|
||||||
.arg(format!("{} cargo run -- --delete {}", env, id))
|
|
||||||
.output()
|
.output()
|
||||||
.expect("Failed to execute command");
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
// Verify the item was deleted
|
assert_eq!(String::from_utf8_lossy(&output.stdout), input);
|
||||||
let list_output = Command::new("sh")
|
|
||||||
.arg("-c")
|
|
||||||
.arg(format!("{} cargo run -- --list", env))
|
|
||||||
.output()
|
|
||||||
.expect("Failed to execute command");
|
|
||||||
|
|
||||||
assert_eq!(String::from_utf8_lossy(&list_output.stdout).trim(), "");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,4 +229,50 @@ mod tests {
|
|||||||
assert_eq!(String::from_utf8_lossy(&output.stdout), input);
|
assert_eq!(String::from_utf8_lossy(&output.stdout), input);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test: Delete an item and verify it's removed
|
||||||
|
#[test]
|
||||||
|
fn test_delete_item() {
|
||||||
|
with_temp_env(|data_dir| {
|
||||||
|
// Set the data directory for this test
|
||||||
|
let env = format!("KEEP_DIR={}", data_dir.display());
|
||||||
|
|
||||||
|
// Save an item with some content
|
||||||
|
let input = "test content";
|
||||||
|
// Create a command that pipes input to keep with the specified environment
|
||||||
|
let cmd = format!("echo {} | {}", input, env);
|
||||||
|
let mut child = Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(cmd)
|
||||||
|
.spawn()
|
||||||
|
.expect("Failed to spawn process");
|
||||||
|
|
||||||
|
child.wait().expect("Failed to wait for child process");
|
||||||
|
|
||||||
|
// Get the item ID
|
||||||
|
let list_output = Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(format!("{} cargo run -- --list", env))
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
let list_output_str = String::from_utf8_lossy(&list_output.stdout);
|
||||||
|
let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::<i64>().expect("Invalid ID");
|
||||||
|
|
||||||
|
// Delete the item
|
||||||
|
let _output = Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(format!("{} cargo run -- --delete {}", env, id))
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
// Verify the item was deleted
|
||||||
|
let list_output = Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(format!("{} cargo run -- --list", env))
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
assert_eq!(String::from_utf8_lossy(&list_output.stdout).trim(), "");
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user