|
| 1 | +use assert_cmd::Command; |
| 2 | +use predicates::prelude::*; |
| 3 | +use std::env; |
| 4 | + |
| 5 | +fn get_api_url() -> String { |
| 6 | + env::var("AERON_CACHE_API_URL").unwrap_or_else(|_| "http://localhost:7070/api/v1/cache".to_string()) |
| 7 | +} |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_help() -> Result<(), Box<dyn std::error::Error>> { |
| 11 | + let mut cmd = Command::cargo_bin("CacheCLI")?; |
| 12 | + cmd.arg("--help"); |
| 13 | + cmd.assert() |
| 14 | + .success() |
| 15 | + .stdout(predicate::str::contains("A CLI for interacting with Aeron Cache")); |
| 16 | + Ok(()) |
| 17 | +} |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn test_cache_lifecycle() -> Result<(), Box<dyn std::error::Error>> { |
| 21 | + let api_url = get_api_url(); |
| 22 | + let cache_name = "test-cache-lifecycle"; |
| 23 | + |
| 24 | + // 1. Create Cache |
| 25 | + let mut cmd = Command::cargo_bin("CacheCLI")?; |
| 26 | + cmd.args(&["--api-url", &api_url, "create", cache_name]); |
| 27 | + cmd.assert() |
| 28 | + .success() |
| 29 | + .stdout(predicate::str::contains(format!("Created cache with id: {}", cache_name))); |
| 30 | + |
| 31 | + // 2. Insert Item |
| 32 | + let mut cmd = Command::cargo_bin("CacheCLI")?; |
| 33 | + cmd.args(&["--api-url", &api_url, "insert", cache_name, "mykey", "myvalue"]); |
| 34 | + cmd.assert() |
| 35 | + .success() |
| 36 | + .stdout(predicate::str::contains(format!("Put item into cache with id: {}", cache_name))); |
| 37 | + |
| 38 | + // 3. Get Item |
| 39 | + let mut cmd = Command::cargo_bin("CacheCLI")?; |
| 40 | + cmd.args(&["--api-url", &api_url, "get", cache_name, "mykey"]); |
| 41 | + cmd.assert() |
| 42 | + .success() |
| 43 | + .stdout(predicate::str::contains(format!("Got item from cache {} on key mykey with value myvalue", cache_name))); |
| 44 | + |
| 45 | + // 4. Remove Item |
| 46 | + let mut cmd = Command::cargo_bin("CacheCLI")?; |
| 47 | + cmd.args(&["--api-url", &api_url, "remove", cache_name, "mykey"]); |
| 48 | + cmd.assert() |
| 49 | + .success() |
| 50 | + .stdout(predicate::str::contains(format!("Removed item from cache {} on key mykey", cache_name))); |
| 51 | + |
| 52 | + // 5. Delete Cache (using the --yes flag) |
| 53 | + let mut cmd = Command::cargo_bin("CacheCLI")?; |
| 54 | + cmd.args(&["--api-url", &api_url, "delete", cache_name, "--yes"]); |
| 55 | + cmd.assert() |
| 56 | + .success() |
| 57 | + .stdout(predicate::str::contains(format!("Deleted cache: {}", cache_name))); |
| 58 | + |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | + |
0 commit comments