Archived
1
0
Fork 0
This repository has been archived on 2024-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
migra/migra_cli/tests/commands.rs

742 lines
20 KiB
Rust
Raw Normal View History

pub use assert_cmd::prelude::*;
2021-03-26 02:10:41 +03:00
pub use cfg_if::cfg_if;
2021-06-06 13:27:17 +03:00
use client_mysql::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/");
2021-03-26 02:10:41 +03:00
pub fn path_to_file<D: std::fmt::Display>(file_name: D) -> String {
format!("{}{}", ROOT_PATH, file_name)
}
pub fn database_manifest_path<D: std::fmt::Display>(database_name: D) -> String {
path_to_file(format!("Migra_{}.toml", database_name))
}
pub const DATABASE_URL_DEFAULT_ENV_NAME: &str = "DATABASE_URL";
2021-03-26 02:10:41 +03:00
pub const POSTGRES_URL: &str = "postgres://postgres:postgres@localhost:6000/migra_tests";
pub const MYSQL_URL: &str = "mysql://mysql:mysql@localhost:6001/migra_tests";
2021-05-17 10:06:33 +03:00
pub const SQLITE_URL: &str = "local.db";
pub fn remove_sqlite_db() -> TestResult {
std::fs::remove_file(SQLITE_URL).or(Ok(()))
}
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";
fs::remove_file(&manifest_path).ok();
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"
[migrations]
directory = "migrations"
table_name = "migrations"
"#
);
fs::remove_file(&manifest_path)?;
Ok(())
}
#[test]
fn init_manifest_in_custom_path() -> TestResult {
let manifest_path = path_to_file("Migra.toml");
fs::remove_file(&manifest_path).ok();
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"
[migrations]
directory = "migrations"
table_name = "migrations"
"#
);
fs::remove_file(&manifest_path)?;
Ok(())
}
}
mod list {
use super::*;
#[test]
fn empty_migration_list() -> TestResult {
Command::cargo_bin("migra")?
.arg("ls")
.assert()
.success()
2021-02-22 23:06:08 +03:00
.stderr(contains(
r#"WARNING: Missed "DATABASE_URL" environment variable
WARNING: No connection to database"#,
))
.stdout(contains(
2021-02-22 23:06:08 +03:00
r#"
Pending migrations:
"#,
));
Ok(())
}
#[test]
fn empty_migration_list_with_db() -> TestResult {
2021-03-26 02:10:41 +03:00
fn inner(connection_string: &'static str) -> TestResult {
let env = Env::new(DATABASE_URL_DEFAULT_ENV_NAME, connection_string);
Command::cargo_bin("migra")?
.arg("ls")
.assert()
.success()
.stdout(contains(
r#"Applied migrations:
Pending migrations:
"#,
2021-03-26 02:10:41 +03:00
));
2021-03-26 02:10:41 +03:00
drop(env);
Ok(())
}
#[cfg(feature = "postgres")]
inner(POSTGRES_URL)?;
#[cfg(feature = "mysql")]
inner(MYSQL_URL)?;
#[cfg(feature = "sqlite")]
2021-05-17 10:06:33 +03:00
remove_sqlite_db().and_then(|_| inner(SQLITE_URL))?;
Ok(())
}
#[test]
2021-03-26 02:10:41 +03:00
#[cfg(feature = "postgres")]
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]
2021-03-26 02:10:41 +03:00
#[cfg(feature = "postgres")]
fn empty_migration_list_with_env_in_manifest() -> TestResult {
2021-03-26 02:10:41 +03:00
let env = Env::new("DB_URL", POSTGRES_URL);
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 {
2021-03-26 02:10:41 +03:00
fn inner(database_name: &'static str) -> TestResult {
Command::cargo_bin("migra")?
.arg("-c")
.arg(database_manifest_path(database_name))
.arg("ls")
.assert()
.success()
.stdout(contains(
r#"Applied migrations:
Pending migrations:
210218232851_create_articles
210218233414_create_persons
"#,
2021-03-26 02:10:41 +03:00
));
2021-03-26 02:10:41 +03:00
Ok(())
}
#[cfg(feature = "postgres")]
inner("postgres")?;
#[cfg(feature = "mysql")]
inner("mysql")?;
#[cfg(feature = "sqlite")]
2021-05-17 10:06:33 +03:00
remove_sqlite_db().and_then(|_| inner("sqlite"))?;
Ok(())
}
#[test]
fn applied_all_migrations() -> TestResult {
2021-03-26 02:10:41 +03:00
fn inner(database_name: &'static str) -> TestResult {
let manifest_path = database_manifest_path(database_name);
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("up")
.assert()
.success();
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("ls")
.assert()
.success()
.stdout(contains(
r#"Applied migrations:
210218232851_create_articles
210218233414_create_persons
Pending migrations:
"#,
2021-03-26 02:10:41 +03:00
));
2021-03-26 02:10:41 +03:00
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("down")
.arg("--all")
.assert()
.success();
2021-03-26 02:10:41 +03:00
Ok(())
}
#[cfg(feature = "postgres")]
inner("postgres")?;
#[cfg(feature = "mysql")]
inner("mysql")?;
#[cfg(feature = "sqlite")]
2021-05-17 10:06:33 +03:00
remove_sqlite_db().and_then(|_| inner("sqlite"))?;
Ok(())
}
#[test]
fn applied_one_migrations() -> TestResult {
2021-03-26 02:10:41 +03:00
fn inner(database_name: &'static str) -> TestResult {
let manifest_path = database_manifest_path(database_name);
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("up")
.arg("-n")
.arg("1")
.assert()
.success();
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("ls")
.assert()
.success()
.stdout(contains(
r#"Applied migrations:
210218232851_create_articles
Pending migrations:
210218233414_create_persons
"#,
2021-03-26 02:10:41 +03:00
));
2021-03-26 02:10:41 +03:00
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("down")
.assert()
.success();
2021-03-26 02:10:41 +03:00
Ok(())
}
#[cfg(feature = "postgres")]
inner("postgres")?;
#[cfg(feature = "mysql")]
inner("mysql")?;
#[cfg(feature = "sqlite")]
2021-05-17 10:06:33 +03:00
remove_sqlite_db().and_then(|_| inner("sqlite"))?;
Ok(())
}
}
2021-02-21 23:57:13 +03:00
mod make {
use super::*;
use std::fs;
#[test]
fn make_migration_directory() -> TestResult {
2021-03-26 02:10:41 +03:00
fn inner(database_name: &'static str) -> TestResult {
Command::cargo_bin("migra")?
.arg("-c")
.arg(database_manifest_path(database_name))
.arg("make")
.arg("test")
.assert()
.success()
.stdout(contains("Structure for migration has been created in"));
let entries = fs::read_dir(path_to_file(format!("{}/migrations", database_name)))?
.map(|entry| entry.map(|e| e.path()))
.collect::<Result<Vec<_>, std::io::Error>>()?;
let dir_paths = entries
.iter()
.filter_map(|path| {
path.to_str().and_then(|path| {
if path.ends_with("_test") {
Some(path)
} else {
None
}
})
2021-02-21 23:57:13 +03:00
})
2021-03-26 02:10:41 +03:00
.collect::<Vec<_>>();
2021-02-21 23:57:13 +03:00
2021-03-26 02:10:41 +03:00
for dir_path in dir_paths.iter() {
let upgrade_content = fs::read_to_string(format!("{}/up.sql", dir_path))?;
let downgrade_content = fs::read_to_string(format!("{}/down.sql", dir_path))?;
2021-02-21 23:57:13 +03:00
2021-03-26 02:10:41 +03:00
assert_eq!(upgrade_content, "-- Your SQL goes here\n\n");
2021-02-21 23:57:13 +03:00
2021-03-26 02:10:41 +03:00
assert_eq!(
downgrade_content,
"-- This file should undo anything in `up.sql`\n\n"
);
2021-02-21 23:57:13 +03:00
2021-03-26 02:10:41 +03:00
fs::remove_dir_all(dir_path)?;
}
Ok(())
2021-02-21 23:57:13 +03:00
}
2021-03-26 02:10:41 +03:00
#[cfg(feature = "postgres")]
inner("postgres")?;
#[cfg(feature = "mysql")]
inner("mysql")?;
#[cfg(feature = "sqlite")]
2021-05-17 10:06:33 +03:00
remove_sqlite_db().and_then(|_| inner("sqlite"))?;
2021-02-21 23:57:13 +03:00
Ok(())
}
}
2021-02-22 00:34:13 +03:00
mod upgrade {
use super::*;
#[test]
fn applied_all_migrations() -> TestResult {
2021-03-26 02:10:41 +03:00
fn inner<ValidateFn>(database_name: &'static str, validate: ValidateFn) -> TestResult
where
ValidateFn: Fn() -> TestResult,
{
let manifest_path = database_manifest_path(database_name);
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("up")
.assert()
.success();
validate()?;
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("down")
.arg("--all")
2021-03-26 02:10:41 +03:00
.assert()
.success();
Ok(())
}
2021-02-22 00:34:13 +03:00
2021-03-26 02:10:41 +03:00
#[cfg(feature = "postgres")]
inner("postgres", || {
2021-06-06 13:27:17 +03:00
let mut conn = client_postgres::Client::connect(POSTGRES_URL, client_postgres::NoTls)?;
2021-03-26 02:10:41 +03:00
let res = conn.query("SELECT p.id, a.id FROM persons AS p, articles AS a", &[])?;
2021-02-22 00:34:13 +03:00
2021-03-26 02:10:41 +03:00
assert_eq!(
res.into_iter()
.map(|row| (row.get(0), row.get(1)))
.collect::<Vec<(i32, i32)>>(),
Vec::new()
);
2021-02-22 00:34:13 +03:00
2021-03-26 02:10:41 +03:00
Ok(())
})?;
2021-02-22 00:34:13 +03:00
2021-03-26 02:10:41 +03:00
#[cfg(feature = "mysql")]
inner("mysql", || {
2021-06-06 13:27:17 +03:00
let pool = client_mysql::Pool::new(MYSQL_URL)?;
2021-03-26 02:10:41 +03:00
let mut conn = pool.get_conn()?;
2021-02-22 00:34:13 +03:00
2021-03-26 02:10:41 +03:00
let res = conn.query_drop("SELECT p.id, a.id FROM persons AS p, articles AS a")?;
assert_eq!(res, ());
Ok(())
})?;
2021-02-22 00:34:13 +03:00
#[cfg(feature = "sqlite")]
2021-05-17 10:06:33 +03:00
remove_sqlite_db().and_then(|_| {
inner("sqlite", || {
2021-06-06 13:27:17 +03:00
let conn = client_rusqlite::Connection::open(SQLITE_URL)?;
2021-05-17 10:06:33 +03:00
let res =
conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?;
assert_eq!(res, ());
Ok(())
})
})?;
2021-02-22 00:34:13 +03:00
Ok(())
}
#[test]
fn partial_applied_invalid_migrations() -> TestResult {
fn inner<ValidateFn>(database_name: &'static str, validate: ValidateFn) -> TestResult
where
ValidateFn: Fn() -> TestResult,
{
let manifest_path = database_manifest_path(database_name);
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("up")
.assert()
.failure();
validate()?;
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("down")
.assert()
.success();
Ok(())
}
#[cfg(feature = "postgres")]
inner("postgres_invalid", || {
2021-06-06 13:27:17 +03:00
let mut conn = client_postgres::Client::connect(POSTGRES_URL, client_postgres::NoTls)?;
let articles_res = conn.query("SELECT a.id FROM articles AS a", &[]);
let persons_res = conn.query("SELECT p.id FROM persons AS p", &[]);
assert!(articles_res.is_ok());
assert!(persons_res.is_err());
Ok(())
})?;
#[cfg(feature = "sqlite")]
remove_sqlite_db().and_then(|_| {
inner("sqlite_invalid", || {
2021-06-06 13:27:17 +03:00
let conn = client_rusqlite::Connection::open(SQLITE_URL)?;
let articles_res = conn.execute_batch("SELECT a.id FROM articles AS a");
let persons_res = conn.execute_batch("SELECT p.id FROM persons AS p");
2021-05-23 00:30:43 +03:00
assert!(articles_res.is_ok());
assert!(persons_res.is_err());
2021-05-23 00:30:43 +03:00
Ok(())
})
2021-05-23 00:30:43 +03:00
})?;
Ok(())
}
#[test]
fn cannot_applied_invalid_migrations_in_single_transaction() -> TestResult {
fn inner<ValidateFn>(database_name: &'static str, validate: ValidateFn) -> TestResult
where
ValidateFn: Fn() -> TestResult,
{
let manifest_path = database_manifest_path(database_name);
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("up")
.arg("--single-transaction")
.assert()
.failure();
validate()?;
Ok(())
}
#[cfg(feature = "postgres")]
inner("postgres_invalid", || {
2021-06-06 13:27:17 +03:00
let mut conn = client_postgres::Client::connect(POSTGRES_URL, client_postgres::NoTls)?;
let articles_res = conn.query("SELECT a.id FROM articles AS a", &[]);
let persons_res = conn.query("SELECT p.id FROM persons AS p", &[]);
assert!(articles_res.is_err());
assert!(persons_res.is_err());
Ok(())
})?;
#[cfg(feature = "sqlite")]
remove_sqlite_db().and_then(|_| {
inner("sqlite_invalid", || {
2021-06-06 13:27:17 +03:00
let conn = client_rusqlite::Connection::open(SQLITE_URL)?;
let articles_res = conn.execute_batch("SELECT a.id FROM articles AS a");
let persons_res = conn.execute_batch("SELECT p.id FROM persons AS p");
2021-05-23 00:30:43 +03:00
assert!(articles_res.is_err());
assert!(persons_res.is_err());
2021-05-23 00:30:43 +03:00
Ok(())
})
2021-05-23 00:30:43 +03:00
})?;
Ok(())
}
2021-02-22 00:34:13 +03:00
}
2021-04-09 01:09:49 +03:00
mod apply {
use super::*;
#[test]
fn apply_files() -> TestResult {
fn inner<ValidateFn>(
database_name: &'static str,
file_paths: Vec<&'static str>,
validate: ValidateFn,
) -> TestResult
where
ValidateFn: Fn() -> TestResult,
{
let manifest_path = database_manifest_path(database_name);
Command::cargo_bin("migra")?
.arg("-c")
.arg(&manifest_path)
.arg("apply")
.args(file_paths)
.assert()
.success();
validate()?;
Ok(())
}
cfg_if! {
if #[cfg(feature = "postgres")] {
inner(
"postgres",
vec![
"migrations/210218232851_create_articles/up",
"migrations/210218233414_create_persons/up",
],
|| {
2021-06-06 13:27:17 +03:00
let mut conn = client_postgres::Client::connect(POSTGRES_URL, client_postgres::NoTls)?;
2021-04-09 01:09:49 +03:00
let res = conn.query("SELECT p.id, a.id FROM persons AS p, articles AS a", &[])?;
assert_eq!(
res.into_iter()
.map(|row| (row.get(0), row.get(1)))
.collect::<Vec<(i32, i32)>>(),
Vec::new()
);
Ok(())
},
)?;
inner(
"postgres",
vec![
"migrations/210218233414_create_persons/down",
"migrations/210218232851_create_articles/down",
],
|| {
2021-06-06 13:27:17 +03:00
let mut conn = client_postgres::Client::connect(POSTGRES_URL, client_postgres::NoTls)?;
2021-04-09 01:09:49 +03:00
let res = conn.query("SELECT p.id, a.id FROM persons AS p, articles AS a", &[]);
assert!(res.is_err());
Ok(())
},
)?;
}
}
cfg_if! {
if #[cfg(feature = "mysql")] {
inner(
"mysql",
vec![
"migrations/210218232851_create_articles/up",
"migrations/210218233414_create_persons/up",
],
|| {
2021-06-06 13:27:17 +03:00
let pool = client_mysql::Pool::new(MYSQL_URL)?;
2021-04-09 01:09:49 +03:00
let mut conn = pool.get_conn()?;
let res = conn.query_drop("SELECT p.id, a.id FROM persons AS p, articles AS a")?;
assert_eq!(res, ());
Ok(())
},
)?;
inner(
"mysql",
vec![
"migrations/210218233414_create_persons/down",
"migrations/210218232851_create_articles/down",
],
|| {
2021-06-06 13:27:17 +03:00
let pool = client_mysql::Pool::new(MYSQL_URL)?;
2021-04-09 01:09:49 +03:00
let mut conn = pool.get_conn()?;
let res = conn.query_drop("SELECT p.id, a.id FROM persons AS p, articles AS a");
assert!(res.is_err());
Ok(())
}
)?;
}
}
#[cfg(feature = "sqlite")]
2021-05-17 10:06:33 +03:00
remove_sqlite_db().and_then(|_| {
inner(
"sqlite",
vec![
"migrations/210218232851_create_articles/up",
"migrations/210218233414_create_persons/up",
],
|| {
2021-06-06 13:27:17 +03:00
let conn = client_rusqlite::Connection::open(SQLITE_URL)?;
2021-05-17 10:06:33 +03:00
let res =
conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a")?;
assert_eq!(res, ());
Ok(())
},
)?;
inner(
"sqlite",
vec![
"migrations/210218233414_create_persons/down",
"migrations/210218232851_create_articles/down",
],
|| {
2021-06-06 13:27:17 +03:00
let conn = client_rusqlite::Connection::open(SQLITE_URL)?;
2021-05-17 10:06:33 +03:00
let res =
conn.execute_batch("SELECT p.id, a.id FROM persons AS p, articles AS a");
assert!(res.is_err());
Ok(())
},
)
})?;
2021-04-09 01:09:49 +03:00
Ok(())
}
}