mod common;
use std::fs;
use std::path::{
Path,
PathBuf,
};
use predicates::prelude::*;
fn fixture(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../midas/tests/fixtures")
.join(name)
}
fn temp() -> PathBuf {
let directory = std::env::temp_dir().join(common::unique_name("phase6"));
fs::create_dir_all(&directory).expect("temp dir");
directory
}
#[test]
fn generate_writes_models_rs() {
let out = temp();
let schema = fixture("codegen/schema.toml");
common::run_midas(
&[
"generate",
"--sqlx",
"--schema",
&schema.to_string_lossy(),
"--out",
&out.to_string_lossy(),
],
&out,
)
.success();
let models = out.join("models.rs");
let source = fs::read_to_string(&models).expect("models.rs");
assert!(source.contains("sqlx::FromRow"));
assert!(source.contains("pub struct Accounts"));
fs::remove_dir_all(out).expect("cleanup");
}
#[test]
fn generate_requires_the_sqlx_selector() {
let out = temp();
let schema = fixture("codegen/schema.toml");
common::run_midas(
&[
"generate",
"--schema",
&schema.to_string_lossy(),
"--out",
&out.to_string_lossy(),
],
&out,
)
.failure()
.stderr(predicate::str::contains("--sqlx"));
assert!(!out.join("models.rs").exists());
fs::remove_dir_all(out).expect("cleanup");
}
#[test]
fn generate_dry_run_prints_and_writes_nothing() {
let out = temp();
let schema = fixture("codegen/schema.toml");
common::run_midas(
&[
"--dry-run",
"generate",
"--sqlx",
"--schema",
&schema.to_string_lossy(),
"--out",
&out.to_string_lossy(),
],
&out,
)
.success()
.stdout(predicate::str::contains("pub struct Accounts"));
assert!(!out.join("models.rs").exists());
fs::remove_dir_all(out).expect("cleanup");
}
#[test]
fn prepare_writes_metadata_and_check_passes() {
let root = temp();
fs::create_dir_all(root.join("src")).expect("src");
let source = fs::read_to_string(fixture("offline/src/main.rs")).expect("source");
fs::write(root.join("src/main.rs"), source).expect("write");
let schema = fixture("offline/schema.toml");
common::run_midas(
&[
"prepare",
"--schema",
&schema.to_string_lossy(),
"--path",
&root.to_string_lossy(),
],
&root,
)
.success();
let entries = fs::read_dir(root.join(".sqlx"))
.expect("sqlx dir")
.collect::<Result<Vec<_>, _>>()
.expect("entries");
assert_eq!(entries.len(), 4);
common::run_midas(
&[
"prepare",
"--check",
"--schema",
&schema.to_string_lossy(),
"--path",
&root.to_string_lossy(),
],
&root,
)
.success();
fs::remove_dir_all(root).expect("cleanup");
}
#[test]
fn prepare_check_fails_when_a_query_changes() {
let root = temp();
fs::create_dir_all(root.join("src")).expect("src");
let source = fs::read_to_string(fixture("offline/src/main.rs")).expect("source");
let path = root.join("src/main.rs");
fs::write(&path, &source).expect("write");
let schema = fixture("offline/schema.toml");
common::run_midas(
&[
"prepare",
"--schema",
&schema.to_string_lossy(),
"--path",
&root.to_string_lossy(),
],
&root,
)
.success();
fs::write(&path, source.replace("order by id", "order by id desc")).expect("mutate");
common::run_midas(
&[
"prepare",
"--check",
"--schema",
&schema.to_string_lossy(),
"--path",
&root.to_string_lossy(),
],
&root,
)
.failure()
.stderr(predicate::str::contains("stale"));
fs::remove_dir_all(root).expect("cleanup");
}
#[test]
fn schema_only_database_fixture_documents_prepare_workflow() {
let fixture = fixture("sqlx-prepare");
assert!(fixture.join("migrations/20240101000000_users.sql").is_file());
assert!(fixture.join("src/main.rs").is_file());
let _ = Path::new("DATABASE_URL");
}