mod common;
use std::fs;
use std::path::{
Path,
PathBuf,
};
use assert_fs::TempDir;
use predicates::prelude::*;
fn parity(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../midas-schema/tests/fixtures/parity/v1")
.join(name)
}
fn run(args: &[&str], cwd: &Path) -> assert_cmd::assert::Assert {
common::run_midas(args, cwd)
}
#[test]
fn schema_validate_accepts_midas_fixtures() {
let temp = TempDir::new().expect("temp dir");
for name in ["full.midas", "kitchen-sink.midas", "minimal.midas"] {
run(
&["schema", "validate", &parity(name).to_string_lossy()],
temp.path(),
)
.success();
}
}
#[test]
fn schema_sql_is_byte_identical_between_formats() {
let temp = TempDir::new().expect("temp dir");
for name in ["full", "kitchen-sink", "minimal"] {
let toml = run(
&[
"schema",
"sql",
&parity(&format!("{name}.toml")).to_string_lossy(),
],
temp.path(),
)
.success();
let midas = run(
&[
"schema",
"sql",
&parity(&format!("{name}.midas")).to_string_lossy(),
],
temp.path(),
)
.success();
assert_eq!(toml.get_output().stdout, midas.get_output().stdout, "{name}");
}
}
#[test]
fn schema_format_check_accepts_canonical_midas() {
let temp = TempDir::new().expect("temp dir");
for name in ["full.midas", "kitchen-sink.midas", "minimal.midas"] {
run(
&["schema", "format", "--check", &parity(name).to_string_lossy()],
temp.path(),
)
.success();
}
}
#[test]
fn schema_format_check_rejects_non_canonical_midas() {
let temp = TempDir::new().expect("temp dir");
let path = temp.path().join("schema.midas");
fs::write(&path, "version 1\n\nschema app\n").expect("write");
run(
&["schema", "format", "--check", &path.to_string_lossy()],
temp.path(),
)
.failure()
.stderr(predicate::str::contains("not canonically formatted"));
}
#[test]
fn schema_convert_round_trips_toml_to_midas_to_toml() {
let temp = TempDir::new().expect("temp dir");
let midas_path = temp.path().join("schema.midas");
run(
&[
"schema",
"convert",
&parity("full.toml").to_string_lossy(),
"--to",
"midas",
"--output",
&midas_path.to_string_lossy(),
],
temp.path(),
)
.success();
assert!(midas_path.is_file());
run(
&["schema", "validate", &midas_path.to_string_lossy()],
temp.path(),
)
.success();
let toml_path = temp.path().join("roundtrip.toml");
run(
&[
"schema",
"convert",
&midas_path.to_string_lossy(),
"--to",
"toml",
"--output",
&toml_path.to_string_lossy(),
],
temp.path(),
)
.success();
let diff = run(
&[
"migrate",
"diff",
"--from",
&parity("full.toml").to_string_lossy(),
"--to",
&toml_path.to_string_lossy(),
"--script",
],
temp.path(),
)
.success();
assert_eq!(
String::from_utf8_lossy(&diff.get_output().stdout),
"-- !UP\n-- !DOWN\n"
);
}
#[test]
fn conflicting_default_files_fail_without_a_selection() {
let temp = TempDir::new().expect("temp dir");
fs::write(temp.path().join("schema.toml"), "version = 1\n").expect("toml");
fs::write(temp.path().join("schema.midas"), "version 1\n").expect("midas");
run(&["schema", "validate"], temp.path())
.failure()
.stderr(predicate::str::contains(
"both `schema.toml` and `schema.midas` exist",
))
.stderr(predicate::str::contains("--schema"));
}
#[test]
fn project_schema_setting_selects_the_midas_source() {
let temp = TempDir::new().expect("temp dir");
fs::create_dir_all(temp.path().join("db")).expect("db dir");
fs::write(
temp.path().join("midas.toml"),
"[project]\nschema = \"db/schema.midas\"\n",
)
.expect("midas.toml");
fs::copy(parity("minimal.midas"), temp.path().join("db/schema.midas")).expect("copy");
run(&["schema", "validate"], temp.path()).success();
}
#[test]
fn explicit_path_wins_over_project_settings() {
let temp = TempDir::new().expect("temp dir");
fs::write(
temp.path().join("midas.toml"),
"[project]\nschema = \"missing.midas\"\n",
)
.expect("midas.toml");
run(
&["schema", "validate", &parity("minimal.midas").to_string_lossy()],
temp.path(),
)
.success();
}
#[test]
fn migrate_diff_accepts_midas_sources() {
let temp = TempDir::new().expect("temp dir");
let diff = run(
&[
"migrate",
"diff",
"--from",
&parity("full.midas").to_string_lossy(),
"--to",
&parity("full.toml").to_string_lossy(),
"--script",
],
temp.path(),
)
.success();
assert_eq!(
String::from_utf8_lossy(&diff.get_output().stdout),
"-- !UP\n-- !DOWN\n"
);
}
#[test]
fn generate_accepts_midas_sources() {
let temp = TempDir::new().expect("temp dir");
let generated = run(
&[
"generate",
"--sqlx",
"--schema",
&parity("full.midas").to_string_lossy(),
"--dry-run",
],
temp.path(),
)
.success();
let stdout = String::from_utf8_lossy(&generated.get_output().stdout);
assert!(stdout.contains("pub struct Users"), "{stdout}");
}
#[test]
fn parser_errors_include_file_line_column_and_token() {
let temp = TempDir::new().expect("temp dir");
let path = temp.path().join("bad.midas");
fs::write(&path, "version 1\n\nmodel User {\n id BigInt @bogus\n}\n").expect("write");
run(&["schema", "validate", &path.to_string_lossy()], temp.path())
.failure()
.stderr(predicate::str::contains("bad.midas:4:"))
.stderr(predicate::str::contains("bogus"));
}
#[test]
fn convert_requires_a_target_format() {
let temp = TempDir::new().expect("temp dir");
run(
&["schema", "convert", &parity("minimal.toml").to_string_lossy()],
temp.path(),
)
.failure()
.stderr(predicate::str::contains("--to"));
}
#[test]
fn schema_validate_json_shape_matches_toml() {
let temp = TempDir::new().expect("temp dir");
let assert = run(
&[
"schema",
"validate",
"--json",
&parity("kitchen-sink.midas").to_string_lossy(),
],
temp.path(),
)
.success();
let value: serde_json::Value = serde_json::from_slice(&assert.get_output().stdout).expect("json");
assert_eq!(value["valid"], serde_json::json!(true));
assert!(value["objects"].is_object());
assert!(value["warnings"].is_array());
}
#[test]
fn prepare_accepts_midas_sources() {
let temp = TempDir::new().expect("temp dir");
fs::create_dir_all(temp.path().join("src")).expect("src");
run(
&[
"prepare",
"--schema",
&parity("minimal.midas").to_string_lossy(),
"--path",
&temp.path().to_string_lossy(),
"--dry-run",
],
temp.path(),
)
.success();
}
#[test]
fn migrate_dev_dry_run_accepts_midas_without_writing_files() {
let temp = TempDir::new().expect("temp dir");
fs::create_dir_all(temp.path().join("migrations")).expect("migrations");
run(
&[
"migrate",
"dev",
"--schema",
&parity("minimal.midas").to_string_lossy(),
"--dry-run",
"--accept-data-loss",
],
temp.path(),
)
.success();
assert!(!temp.path().join(".midas").exists());
assert_eq!(
fs::read_dir(temp.path().join("migrations"))
.expect("migrations")
.count(),
0
);
}