use std::fs;
use std::path::Path;
use warble::ContextLoader;
use warble_mdl_context::{read_project_dir, MdlContext};
fn write(dir: &Path, rel: &str, contents: &str) {
let path = dir.join(rel);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, contents).unwrap();
}
fn bare_tableref_project(dir: &Path) {
write(
dir,
"wren_project.yml",
"schema_version: 2\nname: filedemo\nversion: '1.0'\ncatalog: wren\nschema: public\ndata_source: local_file\n",
);
write(
dir,
"models/customers/metadata.yml",
r#"name: customers
table_reference:
table: /abs/path/to/customers.parquet
primary_key: customer_id
columns:
- { name: customer_id, type: INTEGER }
- { name: email, type: VARCHAR }
"#,
);
}
#[test]
fn bare_table_key_table_reference_still_parses() {
let tmp = tempfile::tempdir().unwrap();
bare_tableref_project(tmp.path());
let sources = read_project_dir(tmp.path())
.expect("read project dir")
.expect("fixture is a wren project");
let ctx = MdlContext::try_from_sources(&sources)
.expect("a table_reference with only `table` (no catalog/schema) must still assemble");
assert!(ctx.is_parseable());
let model = ctx
.model("customers")
.expect("customers model must be present");
assert!(model.columns.contains(&"customer_id".to_string()));
assert!(model.columns.contains(&"email".to_string()));
}
#[test]
fn empty_relationships_yml_is_treated_as_no_relationships() {
let tmp = tempfile::tempdir().unwrap();
bare_tableref_project(tmp.path());
write(
tmp.path(),
"relationships.yml",
"# Relationships between models\n",
);
let sources = read_project_dir(tmp.path())
.expect("read project dir")
.expect("fixture is a wren project");
let ctx = MdlContext::try_from_sources(&sources)
.expect("a comment-only/empty relationships.yml must assemble as zero relationships");
assert!(ctx.is_parseable());
assert!(
ctx.model("customers").is_some(),
"the model must still load when relationships.yml is empty"
);
}