From 20a520b66238b6184b18a749938dba4bd0866bd3 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 12 Feb 2021 01:25:55 +0300 Subject: [PATCH] feat: tests for list command --- docker-compose.yml | 18 +++++ migra-cli/Cargo.toml | 4 ++ migra-cli/tests/common/mod.rs | 31 +++++++++ migra-cli/tests/data/Migra_env_empty.toml | 4 ++ migra-cli/tests/data/Migra_url_empty.toml | 4 ++ migra-cli/tests/lib.rs | 3 + migra-cli/tests/list.rs | 83 +++++++++++++++++++++++ 7 files changed, 147 insertions(+) create mode 100644 docker-compose.yml create mode 100644 migra-cli/tests/common/mod.rs create mode 100644 migra-cli/tests/data/Migra_env_empty.toml create mode 100644 migra-cli/tests/data/Migra_url_empty.toml create mode 100644 migra-cli/tests/lib.rs create mode 100644 migra-cli/tests/list.rs diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..576fa52 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3' + +services: + postgres: + image: postgres + container_name: migra.postgres + environment: + POSTGRES_DB: "migra_tests" + POSTGRES_USER: "postgres" + POSTGRES_PASSWORD: "postgres" + volumes: + - postgres_data:/var/lib/postgres/data + ports: + - 6000:5432 + +volumes: + postgres_data: + driver: local diff --git a/migra-cli/Cargo.toml b/migra-cli/Cargo.toml index 6433bc0..80682b8 100644 --- a/migra-cli/Cargo.toml +++ b/migra-cli/Cargo.toml @@ -22,3 +22,7 @@ serde = { version = "1.0", features = ["derive"] } toml = "0.5" chrono = "0.4.19" postgres = "0.19.0" + +[dev-dependencies] +assert_cmd = "1.0.3" +predicates = "1.0.7" diff --git a/migra-cli/tests/common/mod.rs b/migra-cli/tests/common/mod.rs new file mode 100644 index 0000000..2fc2f7c --- /dev/null +++ b/migra-cli/tests/common/mod.rs @@ -0,0 +1,31 @@ +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/data/Migra_env_empty.toml b/migra-cli/tests/data/Migra_env_empty.toml new file mode 100644 index 0000000..1eeb5b7 --- /dev/null +++ b/migra-cli/tests/data/Migra_env_empty.toml @@ -0,0 +1,4 @@ +directory = "database" + +[database] +connection = "$DB_URL" diff --git a/migra-cli/tests/data/Migra_url_empty.toml b/migra-cli/tests/data/Migra_url_empty.toml new file mode 100644 index 0000000..0099afe --- /dev/null +++ b/migra-cli/tests/data/Migra_url_empty.toml @@ -0,0 +1,4 @@ +directory = "database" + +[database] +connection = "postgres://postgres:postgres@localhost:6000/migra_tests" diff --git a/migra-cli/tests/lib.rs b/migra-cli/tests/lib.rs new file mode 100644 index 0000000..07441e6 --- /dev/null +++ b/migra-cli/tests/lib.rs @@ -0,0 +1,3 @@ +mod common; + +pub use common::*; diff --git a/migra-cli/tests/list.rs b/migra-cli/tests/list.rs new file mode 100644 index 0000000..9851c9a --- /dev/null +++ b/migra-cli/tests/list.rs @@ -0,0 +1,83 @@ +mod common; + +use std::io::Write; +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(()) +}