midas-migrate-cli 0.8.2

Do painless migration 🦀
Documentation
//! Phase 11 CLI tests: lint, fences, and formatting checks.

mod common;

use assert_fs::TempDir;
use predicates::prelude::*;

#[test]
fn schema_lint_reports_unknown_tables_and_exits_zero() {
  let temp = TempDir::new().expect("temp");
  let schema = temp.path().join("schema.toml");
  std::fs::write(
    &schema,
    "version = 1\n[[view]]\nname = \"public.broken\"\ndefinition = \"select * from public.missing\"\n",
  )
  .expect("schema");
  common::run_midas(
    &["schema", "lint", schema.to_string_lossy().as_ref()],
    temp.path(),
  )
  .success()
  .stdout(predicate::str::contains("lint complete: 1 warning"))
  .stderr(predicate::str::contains("unknown table `public.missing`"));
}

#[test]
fn schema_lint_json_shape_is_stable() {
  let temp = TempDir::new().expect("temp");
  let schema = temp.path().join("schema.toml");
  std::fs::write(
    &schema,
    "version = 1\n[[view]]\nname = \"public.ok\"\ndefinition = \"select 1\"\n",
  )
  .expect("schema");
  common::run_midas(
    &["schema", "lint", schema.to_string_lossy().as_ref(), "--json"],
    temp.path(),
  )
  .success()
  .stdout(predicate::str::contains("\"valid\":true"))
  .stdout(predicate::str::contains("\"warnings\":[]"));
}

#[test]
fn schema_format_check_accepts_canonical_fences() {
  let temp = TempDir::new().expect("temp");
  let schema = temp.path().join("schema.midas");
  std::fs::write(
    &schema,
    "version 1\n\nview recent {\n  definition = sql`\n    select 1\n    from t\n    `\n}\n",
  )
  .expect("schema");
  common::run_midas(
    &["schema", "format", schema.to_string_lossy().as_ref(), "--check"],
    temp.path(),
  )
  .success();
}

#[test]
fn schema_format_check_rejects_non_canonical_fences() {
  let temp = TempDir::new().expect("temp");
  let schema = temp.path().join("schema.midas");
  // Closing fence one column shallower than the canonical layout.
  std::fs::write(
    &schema,
    "version 1\n\nview recent {\n  definition = sql`\n    select 1\n    from t\n  `\n}\n",
  )
  .expect("schema");
  common::run_midas(
    &["schema", "format", schema.to_string_lossy().as_ref(), "--check"],
    temp.path(),
  )
  .failure()
  .stderr(predicate::str::contains("not canonically formatted"));
}

#[test]
fn schema_validate_accepts_fenced_cte_views() {
  let temp = TempDir::new().expect("temp");
  let schema = temp.path().join("schema.midas");
  std::fs::write(
    &schema,
    "version 1\n\nview rollup {\n  with = {\n    monthly = sql`\n      select 1 as m\n      `,\n  }\n  definition = \"select m from monthly\"\n}\n",
  )
  .expect("schema");
  common::run_midas(
    &["schema", "validate", schema.to_string_lossy().as_ref()],
    temp.path(),
  )
  .success()
  .stdout(predicate::str::contains("schema valid"));
}