From eda1a05d701ce300d22c4054e3ac429a64748ea9 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 21 Feb 2021 18:51:10 +0300 Subject: [PATCH] chore: change structure of integration tests --- migra-cli/Cargo.toml | 4 + migra-cli/tests/commands.rs | 289 ++++++++++++++++++++++++++++++++++ migra-cli/tests/common/mod.rs | 32 ---- migra-cli/tests/init.rs | 55 ------- migra-cli/tests/lib.rs | 1 - migra-cli/tests/list.rs | 198 ----------------------- 6 files changed, 293 insertions(+), 286 deletions(-) create mode 100644 migra-cli/tests/commands.rs delete mode 100644 migra-cli/tests/common/mod.rs delete mode 100644 migra-cli/tests/init.rs delete mode 100644 migra-cli/tests/lib.rs delete mode 100644 migra-cli/tests/list.rs diff --git a/migra-cli/Cargo.toml b/migra-cli/Cargo.toml index 0582083..36e8cca 100644 --- a/migra-cli/Cargo.toml +++ b/migra-cli/Cargo.toml @@ -16,6 +16,10 @@ readme = "../README.md" name = "migra" path = "src/main.rs" +[[test]] +name = "integration" +path = "tests/commands.rs" + [dependencies] structopt = "0.3" serde = { version = "1.0", features = ["derive"] } diff --git a/migra-cli/tests/commands.rs b/migra-cli/tests/commands.rs new file mode 100644 index 0000000..c35b1dd --- /dev/null +++ b/migra-cli/tests/commands.rs @@ -0,0 +1,289 @@ +pub use assert_cmd::prelude::*; +pub use predicates::str::contains; +pub use std::process::Command; + +pub type TestResult = std::result::Result<(), Box>; + +pub const ROOT_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/"); + +pub fn path_to_file(file_name: &'static str) -> String { + ROOT_PATH.to_owned() + file_name +} + +pub const DATABASE_URL_DEFAULT_ENV_NAME: &str = "DATABASE_URL"; +pub const DATABASE_URL_ENV_VALUE: &str = "postgres://postgres:postgres@localhost:6000/migra_tests"; + +pub struct Env { + key: &'static str, +} + +impl Env { + pub fn new(key: &'static str, value: &'static str) -> Self { + std::env::set_var(key, value); + Env { key } + } +} + +impl Drop for Env { + fn drop(&mut self) { + std::env::remove_var(self.key); + } +} + +mod init { + use super::*; + use std::fs; + + #[test] + fn init_manifest_with_default_config() -> TestResult { + let manifest_path = "Migra.toml"; + + Command::cargo_bin("migra")? + .arg("init") + .assert() + .success() + .stdout(contains(format!("Created {}", &manifest_path))); + + let content = fs::read_to_string(&manifest_path)?; + + assert_eq!( + content, + r#"root = "database" + +[database] +connection = "$DATABASE_URL" +"# + ); + + fs::remove_file(&manifest_path)?; + + Ok(()) + } + + #[test] + fn init_manifest_in_custom_path() -> TestResult { + let manifest_path = path_to_file("Migra.toml"); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(&manifest_path) + .arg("init") + .assert() + .success() + .stdout(contains(format!("Created {}", manifest_path.as_str()))); + + let content = fs::read_to_string(&manifest_path)?; + + assert_eq!( + content, + r#"root = "database" + +[database] +connection = "$DATABASE_URL" +"# + ); + + fs::remove_file(&manifest_path)?; + + Ok(()) + } +} + +mod list { + use super::*; + + #[test] + fn empty_migration_list() -> TestResult { + Command::cargo_bin("migra")? + .arg("ls") + .assert() + .success() + .stdout(contains( + r#"Missed "DATABASE_URL" environment variable +No connection to database + +Pending migrations: +—"#, + )); + Ok(()) + } + + #[test] + fn empty_migration_list_with_db() -> TestResult { + let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); + + Command::cargo_bin("migra")? + .arg("ls") + .assert() + .success() + .stdout(contains( + r#"Applied migrations: +— + +Pending migrations: +—"#, + )); + + drop(env); + + Ok(()) + } + + #[test] + fn empty_migration_list_with_url_in_manifest() -> TestResult { + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_url_empty.toml")) + .arg("ls") + .assert() + .success() + .stdout(contains( + r#"Applied migrations: +— + +Pending migrations: +—"#, + )); + + Ok(()) + } + + #[test] + fn empty_migration_list_with_env_in_manifest() -> TestResult { + let env = Env::new("DB_URL", DATABASE_URL_ENV_VALUE); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env_empty.toml")) + .arg("ls") + .assert() + .success() + .stdout(contains( + r#"Applied migrations: +— + +Pending migrations: +—"#, + )); + + drop(env); + + Ok(()) + } + + #[test] + fn empty_applied_migrations() -> TestResult { + let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("ls") + .assert() + .success() + .stdout(contains( + r#"Applied migrations: +— + +Pending migrations: +210218232851_create_articles +210218233414_create_persons +"#, + )); + + drop(env); + + Ok(()) + } + + #[test] + fn applied_all_migrations() -> TestResult { + let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("up") + .assert() + .success(); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("ls") + .assert() + .success() + .stdout(contains( + r#"Applied migrations: +210218232851_create_articles +210218233414_create_persons + +Pending migrations: +— +"#, + )); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("down") + .assert() + .success(); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("down") + .assert() + .success(); + + drop(env); + + Ok(()) + } + + #[test] + fn applied_one_migrations() -> TestResult { + let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("up") + .assert() + .success(); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("down") + .assert() + .success(); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("ls") + .assert() + .success() + .stdout(contains( + r#"Applied migrations: +210218232851_create_articles + +Pending migrations: +210218233414_create_persons +"#, + )); + + Command::cargo_bin("migra")? + .arg("-c") + .arg(path_to_file("Migra_env.toml")) + .arg("down") + .assert() + .success(); + + drop(env); + + Ok(()) + } +} diff --git a/migra-cli/tests/common/mod.rs b/migra-cli/tests/common/mod.rs deleted file mode 100644 index b42834f..0000000 --- a/migra-cli/tests/common/mod.rs +++ /dev/null @@ -1,32 +0,0 @@ -#![allow(dead_code)] -pub use assert_cmd::prelude::*; -pub use predicates::str::contains; -pub use std::process::Command; - -pub type TestResult = std::result::Result<(), Box>; - -pub const ROOT_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/"); - -pub fn path_to_file(file_name: &'static str) -> String { - ROOT_PATH.to_owned() + file_name -} - -pub const DATABASE_URL_DEFAULT_ENV_NAME: &str = "DATABASE_URL"; -pub const DATABASE_URL_ENV_VALUE: &str = "postgres://postgres:postgres@localhost:6000/migra_tests"; - -pub struct Env { - key: &'static str, -} - -impl Env { - pub fn new(key: &'static str, value: &'static str) -> Self { - std::env::set_var(key, value); - Env { key } - } -} - -impl Drop for Env { - fn drop(&mut self) { - std::env::remove_var(self.key); - } -} diff --git a/migra-cli/tests/init.rs b/migra-cli/tests/init.rs deleted file mode 100644 index 9581871..0000000 --- a/migra-cli/tests/init.rs +++ /dev/null @@ -1,55 +0,0 @@ -mod common; -use common::*; -use std::fs; - -#[test] -fn init_manifest_with_default_config() -> TestResult { - Command::cargo_bin("migra")? - .arg("init") - .assert() - .success() - .stdout(contains("Created Migra.toml")); - - let content = fs::read_to_string("Migra.toml")?; - - assert_eq!( - content, - r#"root = "database" - -[database] -connection = "$DATABASE_URL" -"# - ); - - fs::remove_file("Migra.toml")?; - - Ok(()) -} - -#[test] -fn init_manifest_in_custom_path() -> TestResult { - let manifest_path = path_to_file("Migra.toml"); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(&manifest_path) - .arg("init") - .assert() - .success() - .stdout(contains(format!("Created {}", manifest_path.as_str()))); - - let content = fs::read_to_string(&manifest_path)?; - - assert_eq!( - content, - r#"root = "database" - -[database] -connection = "$DATABASE_URL" -"# - ); - - fs::remove_file(&manifest_path)?; - - Ok(()) -} diff --git a/migra-cli/tests/lib.rs b/migra-cli/tests/lib.rs deleted file mode 100644 index a5961aa..0000000 --- a/migra-cli/tests/lib.rs +++ /dev/null @@ -1 +0,0 @@ -mod common; diff --git a/migra-cli/tests/list.rs b/migra-cli/tests/list.rs deleted file mode 100644 index fb92cc6..0000000 --- a/migra-cli/tests/list.rs +++ /dev/null @@ -1,198 +0,0 @@ -mod common; - -use common::*; - -#[test] -fn empty_migration_list() -> TestResult { - Command::cargo_bin("migra")? - .arg("ls") - .assert() - .success() - .stdout(contains( - r#"Missed "DATABASE_URL" environment variable -No connection to database - -Pending migrations: -—"#, - )); - Ok(()) -} - -#[test] -fn empty_migration_list_with_db() -> TestResult { - let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); - - Command::cargo_bin("migra")? - .arg("ls") - .assert() - .success() - .stdout(contains( - r#"Applied migrations: -— - -Pending migrations: -—"#, - )); - - drop(env); - - Ok(()) -} - -#[test] -fn empty_migration_list_with_url_in_manifest() -> TestResult { - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_url_empty.toml")) - .arg("ls") - .assert() - .success() - .stdout(contains( - r#"Applied migrations: -— - -Pending migrations: -—"#, - )); - - Ok(()) -} - -#[test] -fn empty_migration_list_with_env_in_manifest() -> TestResult { - let env = Env::new("DB_URL", DATABASE_URL_ENV_VALUE); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env_empty.toml")) - .arg("ls") - .assert() - .success() - .stdout(contains( - r#"Applied migrations: -— - -Pending migrations: -—"#, - )); - - drop(env); - - Ok(()) -} - -#[test] -fn empty_applied_migrations() -> TestResult { - let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("ls") - .assert() - .success() - .stdout(contains( - r#"Applied migrations: -— - -Pending migrations: -210218232851_create_articles -210218233414_create_persons -"#, - )); - - drop(env); - - Ok(()) -} - -#[test] -fn applied_all_migrations() -> TestResult { - let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("up") - .assert() - .success(); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("ls") - .assert() - .success() - .stdout(contains( - r#"Applied migrations: -210218232851_create_articles -210218233414_create_persons - -Pending migrations: -— -"#, - )); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("down") - .assert() - .success(); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("down") - .assert() - .success(); - - drop(env); - - Ok(()) -} - -#[test] -fn applied_one_migrations() -> TestResult { - let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, DATABASE_URL_ENV_VALUE); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("up") - .assert() - .success(); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("down") - .assert() - .success(); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("ls") - .assert() - .success() - .stdout(contains( - r#"Applied migrations: -210218232851_create_articles - -Pending migrations: -210218233414_create_persons -"#, - )); - - Command::cargo_bin("migra")? - .arg("-c") - .arg(path_to_file("Migra_env.toml")) - .arg("down") - .assert() - .success(); - - drop(env); - - Ok(()) -}