#![cfg(feature = "std")]
use graphitesql::{Connection, Value};
fn one(c: &Connection, sql: &str) -> Value {
c.query(sql).unwrap().rows.into_iter().next().unwrap()[0].clone()
}
fn text(c: &Connection, sql: &str) -> String {
match one(c, sql) {
Value::Text(t) => String::from(t.as_str()),
v => panic!("expected text, got {v:?}"),
}
}
#[test]
fn concat_of_non_utf8_is_text() {
let c = Connection::open_memory().unwrap();
assert_eq!(text(&c, "SELECT typeof(x'ff' || x'00')"), "text");
assert_eq!(text(&c, "SELECT typeof('a' || x'ff' || 'b')"), "text");
assert_eq!(text(&c, "SELECT typeof('a' || 'b')"), "text");
assert_eq!(text(&c, "SELECT hex(x'ff' || x'00')"), "FF00");
assert_eq!(text(&c, "SELECT hex('a' || x'ff' || 'b')"), "61FF62");
}
#[test]
fn cast_blob_to_text_keeps_bytes() {
let c = Connection::open_memory().unwrap();
assert_eq!(text(&c, "SELECT typeof(CAST(x'ff' AS TEXT))"), "text");
assert_eq!(text(&c, "SELECT hex(CAST(x'ff' AS TEXT))"), "FF");
assert_eq!(text(&c, "SELECT CAST(x'6162' AS TEXT)"), "ab");
}
#[test]
fn non_utf8_text_round_trips_through_storage() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a)").unwrap();
c.execute("INSERT INTO t VALUES(x'ff' || x'00' || x'fe')")
.unwrap();
assert_eq!(text(&c, "SELECT typeof(a) FROM t"), "text");
assert_eq!(text(&c, "SELECT hex(a) FROM t"), "FF00FE");
c.execute("INSERT INTO t VALUES('héllo')").unwrap();
assert_eq!(
text(
&c,
"SELECT a FROM t WHERE typeof(a)='text' AND hex(a)<>'FF00FE'"
),
"héllo"
);
}