mod common;
use std::collections::BTreeSet;
use std::fs;
use std::path::{
Path,
PathBuf,
};
use predicates::prelude::*;
fn v1_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../midas-schema/tests/fixtures/dsl/v1")
}
fn v1_files() -> Vec<PathBuf> {
let mut files = fs::read_dir(v1_dir())
.expect("fixture dir")
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().and_then(|extension| extension.to_str()) == Some("toml"))
.collect::<Vec<_>>();
files.sort();
files
}
#[test]
fn schema_validate_json_shape_is_stable_for_every_v1_fixture() {
let temp = assert_fs::TempDir::new().expect("temp dir");
let mut paths = v1_files();
paths.push(v1_dir().join("directory"));
paths.push(v1_dir().join("comments/commented.toml"));
for path in paths {
let assert = common::run_midas(
&["schema", "validate", "--json", &path.to_string_lossy()],
temp.path(),
)
.success();
let value: serde_json::Value = serde_json::from_slice(&assert.get_output().stdout).expect("json output");
assert_eq!(value["valid"], serde_json::json!(true), "{path:?}");
assert!(value["warnings"].is_array(), "{path:?}");
let keys = value
.as_object()
.expect("object")
.keys()
.cloned()
.collect::<BTreeSet<_>>();
assert_eq!(
keys,
BTreeSet::from(["objects".to_string(), "valid".to_string(), "warnings".to_string()]),
"{path:?}"
);
let objects = value["objects"].as_object().expect("objects");
assert_eq!(
objects.keys().cloned().collect::<BTreeSet<_>>(),
[
"collations",
"composites",
"default_privileges",
"domains",
"enums",
"extensions",
"functions",
"grants",
"ignores",
"policies",
"procedures",
"publications",
"ranges",
"raw",
"roles",
"schemas",
"sequences",
"statistics",
"subscriptions",
"tables",
"triggers",
"views",
]
.into_iter()
.map(ToString::to_string)
.collect::<BTreeSet<_>>()
);
}
}
#[test]
fn schema_format_check_accepts_canonical_fixtures() {
let temp = assert_fs::TempDir::new().expect("temp dir");
let mut paths = v1_files();
for entry in fs::read_dir(v1_dir().join("directory")).expect("directory fixture") {
paths.push(entry.expect("entry").path());
}
paths.push(Path::new("../..").join("schemas/todo-app.toml"));
paths.push(Path::new("../..").join("contrib/todo-app/schema.toml"));
for path in paths {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(path);
common::run_midas(
&["schema", "format", "--check", &path.to_string_lossy()],
temp.path(),
)
.success();
}
}
#[test]
fn schema_format_rejects_comments() {
let temp = assert_fs::TempDir::new().expect("temp dir");
let path = v1_dir().join("comments/commented.toml");
common::run_midas(&["schema", "format", &path.to_string_lossy()], temp.path())
.failure()
.stderr(predicate::str::contains("cannot preserve comments"));
}
#[test]
fn json_schema_output_is_versioned() {
let temp = assert_fs::TempDir::new().expect("temp dir");
let assert = common::run_midas(&["schema", "json-schema"], temp.path()).success();
let value: serde_json::Value = serde_json::from_slice(&assert.get_output().stdout).expect("json output");
assert_eq!(value["x-midas-dsl-version"], serde_json::json!(1));
assert_eq!(
value["$id"],
serde_json::json!("https://github.com/ffimnsr/midas-rs/schemas/schema.schema.json")
);
}