use quarb::{QueryResult, Value};
const T: &str =
r#"= @(%(name = "Ada"; floor = 3); %(name = "Bob"; floor = 5); %(name = "Cy"; floor = 4)) | #"#;
fn doc() -> quarb_json::JsonAdapter {
quarb_json::JsonAdapter::parse(r#"{"limit": 3, "w": [1]}"#).unwrap()
}
fn values(q: &str) -> Vec<String> {
match quarb::run(q, &doc()).unwrap_or_else(|e| panic!("{q}: {e}")) {
QueryResult::Values(vs) => vs.iter().map(Value::to_quarb).collect(),
other => panic!("{q}: expected values, got {other:?}"),
}
}
fn one(q: &str) -> String {
let v = values(q);
assert_eq!(v.len(), 1, "{q}: {v:?}");
v.into_iter().next().unwrap()
}
fn refused(q: &str) -> String {
match quarb::run(q, &doc()) {
Ok(v) => panic!("{q}: expected a refusal, got {v:?}"),
Err(e) => e.to_string(),
}
}
const NO_COLUMN: &str = "no column 'nosuch' (the columns: name, floor)";
#[test]
fn a_read_of_a_missing_column_is_refused() {
for tail in [
":nosuch",
"$_:nosuch",
"%(a = :nosuch)",
"@(:nosuch)",
".t | $.t:nosuch",
r#"[:nosuch = "x"]"#,
"[:nosuch]",
"[!:nosuch]",
"[:floor > 3 && :nosuch > 1]",
"sort_by(:nosuch)",
"unique_by(:nosuch)",
"min_by(:nosuch)",
"max_by(:nosuch)",
"top(1; :nosuch)",
"bottom(1; :nosuch)",
"sum(--of=:nosuch)",
"sum(--of=:floor + :nosuch)",
"map(:nosuch)",
"#(:name; :nosuch)",
] {
let q = format!("{T} | {tail}");
let e = refused(&q);
assert!(e.contains(NO_COLUMN), "{tail}: {e}");
}
for tail in [
"sort_by(:nosuch)",
"unique_by(:nosuch)",
"min_by(:nosuch)",
"max_by(:nosuch)",
"top(1; :nosuch)",
"bottom(1; :nosuch)",
"sum(--of=:nosuch)",
] {
let q = format!("{T} @| {tail}");
let e = refused(&q);
assert!(e.contains(NO_COLUMN), "@| {tail}: {e}");
}
for tail in ["melt(:nosuch)", "melt(:name; :nosuch)"] {
let e = refused(&format!("{T} | {tail}"));
assert!(e.contains(&format!("melt: {NO_COLUMN}")), "{tail}: {e}");
}
for tail in [":nosuch", "[:nosuch > 1]"] {
let e = refused(&format!("{T} | #(-i=:name) | {tail}"));
assert!(e.contains(NO_COLUMN), "{tail}: {e}");
}
}
#[test]
fn the_columns_a_table_has_answer_as_before() {
assert_eq!(one(&format!("{T} | :name")), r#"@("Ada"; "Bob"; "Cy")"#);
assert_eq!(
one(&format!("{T} | [:floor > 3] | :name")),
r#"@("Bob"; "Cy")"#
);
assert_eq!(
one(&format!("{T} | sort_by(-:floor) | :name")),
r#"@("Bob"; "Cy"; "Ada")"#
);
assert_eq!(one(&format!("{T} | sum(--of=:floor)")), "12");
assert_eq!(one(&format!("{T} | melt(:name) | :value")), "@(3; 5; 4)");
assert_eq!(
one(&format!("{T} @| sort_by(-:floor) | :name")),
r#"@("Bob"; "Cy"; "Ada")"#
);
assert_eq!(
one(&format!("{T} @| top(1; :floor) | :name")),
r#"@("Bob")"#
);
assert_eq!(
one(&format!("{T} | #(-i=:name) | :floor")),
"%(Ada = 3; Bob = 5; Cy = 4)"
);
assert_eq!(
one(&format!(r#"{T} | [(:name | upper) = "ADA"] | :floor"#)),
"@(3)"
);
}
#[test]
fn two_colons_on_a_row_point_at_the_column() {
let e = refused(&format!("{T} | [::floor > 3]"));
assert!(
e.contains("'::floor' reads a property of the thread's node")
&& e.contains("the row's column is ':floor'"),
"{e}"
);
assert_eq!(
one(&format!("{T} | [:floor > ::limit] | :name")),
r#"@("Bob"; "Cy")"#
);
assert_eq!(one(&format!("{T} | [::nosuch > 3]")), "#(@(name; floor))");
}
#[test]
fn a_header_less_table_names_its_positions() {
let t = "= @(@(1; 2); @(3; 4)) | #";
assert_eq!(one(&format!("{t} | :1")), "@(1; 3)");
assert_eq!(one(&format!("{t} | [:1 = 3]")), "#(@(3; 4))");
for tail in [":x", "[:x = 1]"] {
let e = refused(&format!("{t} | {tail}"));
assert!(
e.contains(
"'x' names no column: the table has no header, its columns are positions (1 to 2)"
),
"{tail}: {e}"
);
}
}
#[test]
fn a_record_s_missing_field_stays_null() {
assert_eq!(one("= %(a = 1) | :nosuch"), "null");
assert!(values("= @(%(a = 1); %(a = 2)) | [:nosuch = 1]").is_empty());
assert_eq!(one(&format!("{T} | [1] | :nosuch")), "null");
assert_eq!(one(&format!("{T} | first | :nosuch")), "null");
assert_eq!(
values(&format!("{T} | @ | ... | :nosuch")),
["null", "null", "null"]
);
let e = refused(&format!("{T} | first(2) | :nosuch"));
assert!(e.contains(NO_COLUMN), "{e}");
}