use rusqlite::{Connection, params};
use sqlite_err_parser::{InterpretedError, deconstruct_error};
#[test]
fn insert_null_ref() {
let conn = Connection::open_in_memory().unwrap();
conn.pragma_update(None, "foreign_keys", "ON").unwrap();
conn
.execute(
"CREATE TABLE roles (
id INTEGER PRIMARY KEY,
title TEXT UNIQUE NOT NULL
)",
()
)
.unwrap();
conn
.execute("INSERT INTO roles (id, title) VALUES (1, 'janitor');", [])
.unwrap();
conn
.execute("INSERT INTO roles (id, title) VALUES (2, 'spy');", [])
.unwrap();
conn
.execute(
"CREATE TABLE agents (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL,
role_id INTEGER NOT NULL
REFERENCES roles(id)
ON DELETE RESTRICT
)",
()
)
.unwrap();
let Err(err) = conn.execute(
"INSERT INTO agents (name, role_id) VALUES (?, (SELECT id FROM roles \
WHERE title=?))",
params!("frank", "nonexistent")
) else {
panic!("Unexpectedly not Err()");
};
let Some(InterpretedError::NotNull(v)) = deconstruct_error(&err) else {
panic!("Not treated as InterpretedError::NotNull(); {err}");
};
assert_eq!(&v, &[("agents", "role_id")]);
}