feat: tests for list command

This commit is contained in:
Dmitriy Pleshevskiy 2021-02-12 01:25:55 +03:00
parent ba73786a38
commit 20a520b662
7 changed files with 147 additions and 0 deletions

18
docker-compose.yml Normal file
View File

@ -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

View File

@ -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"

View File

@ -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<dyn std::error::Error>>;
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);
}
}

View File

@ -0,0 +1,4 @@
directory = "database"
[database]
connection = "$DB_URL"

View File

@ -0,0 +1,4 @@
directory = "database"
[database]
connection = "postgres://postgres:postgres@localhost:6000/migra_tests"

3
migra-cli/tests/lib.rs Normal file
View File

@ -0,0 +1,3 @@
mod common;
pub use common::*;

83
migra-cli/tests/list.rs Normal file
View File

@ -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(())
}