use spg_engine::{Engine, QueryResult};
fn text_of(e: &mut Engine, sql: &str) -> String {
match e.execute(sql).unwrap() {
QueryResult::Rows { rows, .. } => spg_engine::eval::value_to_text(&rows[0].values[0]),
other => panic!("{sql}: {other:?}"),
}
}
#[test]
fn array_sort_reverse_match_pg() {
let mut e = Engine::new();
assert_eq!(
text_of(&mut e, "SELECT array_sort(ARRAY[3,1,2])"),
"{1,2,3}"
);
assert_eq!(
text_of(&mut e, "SELECT array_sort(ARRAY[3,1,NULL,2])"),
"{1,2,3,NULL}"
);
assert_eq!(
text_of(&mut e, "SELECT array_sort(ARRAY[3,1,NULL,2], true)"),
"{NULL,3,2,1}"
);
assert_eq!(
text_of(&mut e, "SELECT array_sort(ARRAY['b','a'], false, true)"),
"{a,b}"
);
assert_eq!(
text_of(&mut e, "SELECT array_reverse(ARRAY[1,2,3])"),
"{3,2,1}"
);
assert_eq!(text_of(&mut e, "SELECT array_reverse(ARRAY['x'])"), "{x}");
}
#[test]
fn array_sample_and_multidim_search_error_shapes() {
let mut e = Engine::new();
let err = e
.execute("SELECT array_sample(ARRAY[1,2,3], 5)")
.unwrap_err();
assert!(
format!("{err}").contains("sample size must be between 0 and 3"),
"got {err}"
);
let err = e
.execute("SELECT array_position(ARRAY[[1,2]], 1)")
.unwrap_err();
assert!(
format!("{err}")
.contains("searching for elements in multidimensional arrays is not supported"),
"got {err}"
);
}