#![allow(deprecated)]
#[cfg(feature = "cli-tests")]
mod cli_tests {
use assert_cmd::Command;
use predicates::prelude::*;
use std::path::PathBuf;
use tempfile::TempDir;
fn cli_command() -> Command {
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let mut cmd = Command::new(cargo);
cmd.args(["run", "-p", "dbnexus-cli", "--quiet", "--"]);
cmd
}
#[test]
#[allow(deprecated)]
fn test_cli_help() {
cli_command()
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("USAGE").or(predicate::str::contains("Usage")));
}
#[test]
fn test_cli_subcommand_help() {
cli_command()
.args(["create", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("description").or(predicate::str::contains("DESCRIPTION")));
}
#[tokio::test]
async fn test_cli_status_basic() {
cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("status")
.assert()
.success();
}
#[tokio::test]
async fn test_cli_status_database_connection() {
let assert_result = cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("status")
.assert();
assert_result.success();
}
#[tokio::test]
async fn test_cli_create_migration() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("--migrations-dir")
.arg(temp_dir.path())
.arg("create")
.arg("test_migration")
.assert()
.success();
}
#[tokio::test]
async fn test_cli_up_migration() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let migrations_dir = temp_dir.path().to_str().unwrap().to_string();
let migration_content = r#"-- Migration: test_migration
-- Version: 1700000000
-- UP
CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY);
-- DOWN
DROP TABLE test_table;
"#;
std::fs::write(
PathBuf::from(&migrations_dir).join("1700000000_test_migration.sql"),
migration_content,
)
.expect("Failed to write migration file");
let output = cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("--migrations-dir")
.arg(temp_dir.path())
.arg("up")
.output()
.expect("Failed to execute command");
assert!(
output.status.success() || output.status.code() == Some(1),
"Command should be executed without argument errors"
);
}
#[tokio::test]
async fn test_cli_down_migration() {
cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("down")
.assert()
.success();
}
#[test]
fn test_cli_invalid_args() {
cli_command().args(["--invalid-option"]).assert().failure();
}
#[test]
fn test_cli_generate_help() {
cli_command().args(["generate", "--help"]).assert().success();
}
#[tokio::test]
async fn test_cli_down_with_version() {
cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("down")
.arg("--version")
.arg("1")
.assert()
.success();
}
#[tokio::test]
async fn test_cli_up_with_version() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let migration_content = r#"-- Migration: test_migration
-- Version: 1700000000
-- UP
CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY);
-- DOWN
DROP TABLE test_table;
"#;
std::fs::write(temp_dir.path().join("1700000000_test_migration.sql"), migration_content)
.expect("Failed to write migration file");
let output = cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("--migrations-dir")
.arg(temp_dir.path())
.arg("up")
.arg("--version")
.arg("1700000000")
.output()
.expect("Failed to execute command");
assert!(
output.status.success() || output.status.code() == Some(1),
"Command should be executed without argument errors"
);
}
#[tokio::test]
async fn test_cli_full_status() {
cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("status")
.assert()
.success()
.stdout(predicate::str::contains("迁移状态").or(predicate::str::contains("Migration")));
}
#[tokio::test]
async fn test_cli_up_multiple_migrations() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
for i in 1..=3 {
let migration_content = format!(
r#"-- Migration: test_migration_{}
-- Version: 170000000{}
-- UP
CREATE TABLE IF NOT EXISTS test_table_{} (id INTEGER PRIMARY KEY);
-- DOWN
DROP TABLE test_table_{};
"#,
i, i, i, i
);
std::fs::write(
temp_dir.path().join(format!("170000000{}_test_migration_{}.sql", i, i)),
migration_content,
)
.expect("Failed to write migration file");
}
let output = cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("--migrations-dir")
.arg(temp_dir.path())
.arg("up")
.output()
.expect("Failed to execute command");
assert!(
output.status.success() || output.status.code() == Some(1),
"Command should be executed without argument errors"
);
}
#[test]
fn test_cli_help_shows_all_commands() {
cli_command()
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("create"))
.stdout(predicate::str::contains("up"))
.stdout(predicate::str::contains("down"))
.stdout(predicate::str::contains("status"))
.stdout(predicate::str::contains("generate"));
}
#[tokio::test]
async fn test_cli_status_output_format() {
let assert_result = cli_command()
.arg("--database-url")
.arg("sqlite::memory:")
.arg("status")
.assert();
assert_result.success();
}
}
#[cfg(not(feature = "cli-tests"))]
mod cli_tests {
}