fn qc(query: &str) -> (String, String, bool) {
let dir = std::env::temp_dir().join(format!("quarb-declared-cells-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("e.json"), "{}").unwrap();
let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
.env("QUARB_LOCALE", "C")
.current_dir(&dir)
.args([query, "e.json"])
.stdin(std::process::Stdio::null())
.output()
.unwrap();
(
String::from_utf8_lossy(&out.stdout)
.split_whitespace()
.collect::<Vec<_>>()
.join(" "),
String::from_utf8_lossy(&out.stderr).trim().to_string(),
out.status.success(),
)
}
fn ok(query: &str) -> String {
let (out, err, ran) = qc(query);
assert!(ran, "{query}: {err}");
assert_eq!(err, "", "{query}");
out
}
const ROWS: &str = r#"@(%(isbn = "0140449132"; p = "12.00"); %(isbn = "0486280616"; p = "9.50"))"#;
#[test]
fn a_declared_text_column_keeps_its_cells_as_written() {
assert_eq!(
ok(&format!("= {ROWS} | #(--header=@(:isbn<text>; :p<text>))")),
r#"#(@(isbn; p); %("0140449132"; "12.00"); %("0486280616"; "9.50"))"#
);
assert_eq!(
ok(&format!("= {ROWS} | #(--cast=:isbn<text>)")),
r#"#(@(isbn; p); %("0140449132"; 12.0); %("0486280616"; 9.5))"#
);
assert_eq!(
ok(&format!("= {ROWS} | #(--header=@(:p<float>; :isbn<text>))")),
r#"#(@(p; isbn); %(12.0; "0140449132"); %(9.5; "0486280616"))"#
);
assert_eq!(
ok(r#"= %(isbn = @("0140449132"; "0486280616"); n = @(1; 2)) | #(--cast=:isbn<text>)"#),
r#"#(@(isbn; n); %("0140449132"; 1); %("0486280616"; 2))"#
);
assert_eq!(
ok(r#"= %(isbn = "0140449132"; n = 1) | #(--header=@(:isbn<text>; :n))"#),
r#"#(@(isbn; n); %("0140449132"; 1))"#
);
}
#[test]
fn what_was_right_stays() {
assert_eq!(
ok(&format!("= {ROWS} | #")),
"#(@(isbn; p); %(140449132; 12.0); %(486280616; 9.5))"
);
assert_eq!(
ok(&format!("= {ROWS} | #(--header=@(:isbn<int>; :p<float>))")),
"#(@(isbn; p); %(140449132; 12.0); %(486280616; 9.5))"
);
assert_eq!(
ok(&format!("= {ROWS} | # | #(--cast=:isbn<text>)")),
r#"#(@(isbn; p); %("140449132"; 12.0); %("486280616"; 9.5))"#
);
for (q, why) in [
(
r#"= @(%(a = "x"; b = 1)) | #(--cast=:a<int>)"#,
r#"#: column 'a' is declared int, and row 1 holds "x", which does not cast to it"#,
),
(
r#"= @(%(a = "x"; b = 1)) | #(--header=@(:a<int>; :b))"#,
r#"#: #(--header=@(:a<int>; :b)): column 'a' is declared int, and row 1 holds "x", which does not cast to it"#,
),
(
r#"= @(%(a<int> = "7"); %(a = "08")) | #(--cast=:a<text>)"#,
"#: column 'a' is declared int by the header and cast to text by -c: the two must agree",
),
(
r#"= @(%(a = "07"; b = 1)) | #(--header=@(:a<int>; :b) --cast=:a<text>)"#,
"#: column 'a' is declared int by the header and cast to text by -c: the two must agree",
),
(
&format!("= {ROWS} | #(--cast=:nosuch<text>)"),
"#: --cast names column 'nosuch', and the table has no such column (its columns: isbn, p)",
),
] {
let (out, err, ran) = qc(q);
assert!(!ran, "{q} ran: {out}");
assert!(err.contains(why), "{q}: {err}");
}
}