use cratefield_core::{Database, Statement};
pub async fn assert_blob_round_trips(db: &dyn Database) {
db.execute(&Statement::new(
"CREATE TABLE IF NOT EXISTS blob_round_trip_probe \
(id INTEGER PRIMARY KEY, payload BYTEA)",
))
.await
.expect("probe table");
db.execute(&Statement::new("DELETE FROM blob_round_trip_probe"))
.await
.expect("clear probe");
let payload: Vec<u8> = vec![
0x00, 0xDE, 0xAD, 0xBE, 0xEF, 0xFF, 0x80, 0xFE, 0xC3, 0x28, 0x0A,
];
db.execute(&Statement::with_values(
"INSERT INTO blob_round_trip_probe (id, payload) VALUES (?, ?)",
vec![1_i32.into(), payload.clone().into()],
))
.await
.expect("bind the byte payload");
db.execute(&Statement::with_values(
"INSERT INTO blob_round_trip_probe (id, payload) VALUES (?, ?)",
vec![2_i32.into(), None::<Vec<u8>>.into()],
))
.await
.expect("bind a NULL blob");
let stored = db
.query(&Statement::with_values(
"SELECT payload FROM blob_round_trip_probe WHERE id = ?",
vec![1_i32.into()],
))
.await
.expect("read the payload back");
let row = stored.first().expect("the payload row is visible");
assert_eq!(
row.get::<Vec<u8>>("payload"),
Some(payload),
"the bytes read back are exactly the bytes bound"
);
let nulled = db
.query(&Statement::with_values(
"SELECT payload FROM blob_round_trip_probe WHERE id = ?",
vec![2_i32.into()],
))
.await
.expect("read the NULL blob back");
let row = nulled.first().expect("the NULL row is visible");
assert!(
row.get::<Vec<u8>>("payload").is_none(),
"a NULL blob reads back as no bytes, not as empty or garbage"
);
db.execute(&Statement::new("DELETE FROM blob_round_trip_probe"))
.await
.expect("clean probe");
}