#![cfg(feature = "std")]
use graphitesql::{Connection, Value};
fn t(c: &Connection, sql: &str) -> String {
match c.query(sql).unwrap().rows.remove(0).remove(0) {
Value::Text(s) => s,
other => panic!("expected text from {sql}, got {other:?}"),
}
}
#[test]
fn json_group_array_distinct() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(x)").unwrap();
c.execute("INSERT INTO t VALUES(1),(2),(2),(3),(1)")
.unwrap();
assert_eq!(
t(&c, "SELECT json_group_array(DISTINCT x) FROM t"),
"[1,2,3]"
);
assert_eq!(t(&c, "SELECT json_group_array(x) FROM t"), "[1,2,2,3,1]");
let mut c2 = Connection::open_memory().unwrap();
c2.execute("CREATE TABLE s(x)").unwrap();
c2.execute("INSERT INTO s VALUES('a'),('a'),('b')").unwrap();
assert_eq!(
t(&c2, "SELECT json_group_array(DISTINCT x) FROM s"),
r#"["a","b"]"#
);
assert_eq!(
t(
&c,
"SELECT json_group_array(DISTINCT x) FROM t WHERE x > 99"
),
"[]"
);
}