Replace some usages of checked_memory_handle

In tests which return Result, we don't need to unwrap.
This commit is contained in:
gwenn 2021-07-04 16:04:13 +02:00
parent 10da56788c
commit 7ddf1316a2

View File

@ -1083,7 +1083,7 @@ mod test {
ensure_sync::<InterruptHandle>(); ensure_sync::<InterruptHandle>();
} }
pub fn checked_memory_handle() -> Connection { fn checked_memory_handle() -> Connection {
Connection::open_in_memory().unwrap() Connection::open_in_memory().unwrap()
} }
@ -1211,7 +1211,7 @@ mod test {
#[test] #[test]
fn test_close_retry() -> Result<()> { fn test_close_retry() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
// force the DB to be busy by preparing a statement; this must be done at the // force the DB to be busy by preparing a statement; this must be done at the
// FFI level to allow us to call .close() without dropping the prepared // FFI level to allow us to call .close() without dropping the prepared
@ -1264,7 +1264,7 @@ mod test {
#[test] #[test]
fn test_execute_batch() -> Result<()> { fn test_execute_batch() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER); CREATE TABLE foo(x INTEGER);
INSERT INTO foo VALUES(1); INSERT INTO foo VALUES(1);
@ -1282,7 +1282,7 @@ mod test {
#[test] #[test]
fn test_execute() -> Result<()> { fn test_execute() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER)")?; db.execute_batch("CREATE TABLE foo(x INTEGER)")?;
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [1i32])?); assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [1i32])?);
@ -1323,7 +1323,7 @@ mod test {
#[test] #[test]
fn test_prepare_column_names() -> Result<()> { fn test_prepare_column_names() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let stmt = db.prepare("SELECT * FROM foo")?; let stmt = db.prepare("SELECT * FROM foo")?;
@ -1338,7 +1338,7 @@ mod test {
#[test] #[test]
fn test_prepare_execute() -> Result<()> { fn test_prepare_execute() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?; let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?;
@ -1359,7 +1359,7 @@ mod test {
#[test] #[test]
fn test_prepare_query() -> Result<()> { fn test_prepare_query() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?; let mut insert_stmt = db.prepare("INSERT INTO foo(x) VALUES(?)")?;
@ -1394,7 +1394,7 @@ mod test {
#[test] #[test]
fn test_query_map() -> Result<()> { fn test_query_map() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1413,7 +1413,7 @@ mod test {
#[test] #[test]
fn test_query_row() -> Result<()> { fn test_query_row() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER); CREATE TABLE foo(x INTEGER);
INSERT INTO foo VALUES(1); INSERT INTO foo VALUES(1);
@ -1442,7 +1442,7 @@ mod test {
#[test] #[test]
fn test_optional() -> Result<()> { fn test_optional() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 <> 0", [], |r| r.get(0)); let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 <> 0", [], |r| r.get(0));
let result = result.optional(); let result = result.optional();
@ -1466,7 +1466,7 @@ mod test {
#[test] #[test]
fn test_pragma_query_row() -> Result<()> { fn test_pragma_query_row() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
assert_eq!( assert_eq!(
"memory", "memory",
@ -1481,7 +1481,7 @@ mod test {
#[test] #[test]
fn test_prepare_failures() -> Result<()> { fn test_prepare_failures() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let err = db.prepare("SELECT * FROM does_not_exist").unwrap_err(); let err = db.prepare("SELECT * FROM does_not_exist").unwrap_err();
@ -1491,7 +1491,7 @@ mod test {
#[test] #[test]
fn test_last_insert_rowid() -> Result<()> { fn test_last_insert_rowid() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?; db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?;
db.execute_batch("INSERT INTO foo DEFAULT VALUES")?; db.execute_batch("INSERT INTO foo DEFAULT VALUES")?;
@ -1506,18 +1506,19 @@ mod test {
} }
#[test] #[test]
fn test_is_autocommit() { fn test_is_autocommit() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
assert!( assert!(
db.is_autocommit(), db.is_autocommit(),
"autocommit expected to be active by default" "autocommit expected to be active by default"
); );
Ok(())
} }
#[test] #[test]
#[cfg(feature = "modern_sqlite")] #[cfg(feature = "modern_sqlite")]
fn test_is_busy() -> Result<()> { fn test_is_busy() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
assert!(!db.is_busy()); assert!(!db.is_busy());
let mut stmt = db.prepare("PRAGMA schema_version")?; let mut stmt = db.prepare("PRAGMA schema_version")?;
assert!(!db.is_busy()); assert!(!db.is_busy());
@ -1534,7 +1535,7 @@ mod test {
#[test] #[test]
fn test_statement_debugging() -> Result<()> { fn test_statement_debugging() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let query = "SELECT 12345"; let query = "SELECT 12345";
let stmt = db.prepare(query)?; let stmt = db.prepare(query)?;
@ -1553,7 +1554,7 @@ mod test {
#[cfg(not(feature = "modern_sqlite"))] #[cfg(not(feature = "modern_sqlite"))]
fn check_extended_code(_extended_code: c_int) {} fn check_extended_code(_extended_code: c_int) {}
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x NOT NULL)")?; db.execute_batch("CREATE TABLE foo(x NOT NULL)")?;
let result = db.execute("INSERT INTO foo (x) VALUES (NULL)", []); let result = db.execute("INSERT INTO foo (x) VALUES (NULL)", []);
@ -1582,7 +1583,7 @@ mod test {
#[test] #[test]
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
fn test_interrupt() -> Result<()> { fn test_interrupt() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let interrupt_handle = db.get_interrupt_handle(); let interrupt_handle = db.get_interrupt_handle();
@ -1630,7 +1631,7 @@ mod test {
#[test] #[test]
fn test_get_raw() -> Result<()> { fn test_get_raw() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(i, x);")?; db.execute_batch("CREATE TABLE foo(i, x);")?;
let vals = ["foobar", "1234", "qwerty"]; let vals = ["foobar", "1234", "qwerty"];
let mut insert_stmt = db.prepare("INSERT INTO foo(i, x) VALUES(?, ?)")?; let mut insert_stmt = db.prepare("INSERT INTO foo(i, x) VALUES(?, ?)")?;
@ -1663,7 +1664,7 @@ mod test {
#[test] #[test]
fn test_from_handle() -> Result<()> { fn test_from_handle() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let handle = unsafe { db.handle() }; let handle = unsafe { db.handle() };
{ {
let db = unsafe { Connection::from_handle(handle) }?; let db = unsafe { Connection::from_handle(handle) }?;
@ -1715,7 +1716,7 @@ mod test {
#[test] #[test]
fn test_query_and_then() -> Result<()> { fn test_query_and_then() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1735,7 +1736,7 @@ mod test {
#[test] #[test]
fn test_query_and_then_fails() -> Result<()> { fn test_query_and_then_fails() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1765,7 +1766,7 @@ mod test {
#[test] #[test]
fn test_query_and_then_custom_error() -> CustomResult<()> { fn test_query_and_then_custom_error() -> CustomResult<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1786,7 +1787,7 @@ mod test {
#[test] #[test]
fn test_query_and_then_custom_error_fails() -> Result<()> { fn test_query_and_then_custom_error_fails() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1828,7 +1829,7 @@ mod test {
#[test] #[test]
fn test_query_row_and_then_custom_error() -> CustomResult<()> { fn test_query_row_and_then_custom_error() -> CustomResult<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1845,7 +1846,7 @@ mod test {
#[test] #[test]
fn test_query_row_and_then_custom_error_fails() -> Result<()> { fn test_query_row_and_then_custom_error_fails() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1882,7 +1883,7 @@ mod test {
#[test] #[test]
fn test_dynamic() -> Result<()> { fn test_dynamic() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = "BEGIN; let sql = "BEGIN;
CREATE TABLE foo(x INTEGER, y TEXT); CREATE TABLE foo(x INTEGER, y TEXT);
INSERT INTO foo VALUES(4, \"hello\"); INSERT INTO foo VALUES(4, \"hello\");
@ -1896,7 +1897,7 @@ mod test {
} }
#[test] #[test]
fn test_dyn_box() -> Result<()> { fn test_dyn_box() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER);")?; db.execute_batch("CREATE TABLE foo(x INTEGER);")?;
let b: Box<dyn ToSql> = Box::new(5); let b: Box<dyn ToSql> = Box::new(5);
db.execute("INSERT INTO foo VALUES(?)", [b])?; db.execute("INSERT INTO foo VALUES(?)", [b])?;
@ -1908,7 +1909,7 @@ mod test {
#[test] #[test]
fn test_params() -> Result<()> { fn test_params() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.query_row( db.query_row(
"SELECT "SELECT
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
@ -1929,7 +1930,7 @@ mod test {
#[test] #[test]
#[cfg(not(feature = "extra_check"))] #[cfg(not(feature = "extra_check"))]
fn test_alter_table() -> Result<()> { fn test_alter_table() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE x(t);")?; db.execute_batch("CREATE TABLE x(t);")?;
// `execute_batch` should be used but `execute` should also work // `execute_batch` should be used but `execute` should also work
db.execute("ALTER TABLE x RENAME TO y;", [])?; db.execute("ALTER TABLE x RENAME TO y;", [])?;
@ -1938,7 +1939,7 @@ mod test {
#[test] #[test]
fn test_batch() -> Result<()> { fn test_batch() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
let sql = r" let sql = r"
CREATE TABLE tbl1 (col); CREATE TABLE tbl1 (col);
CREATE TABLE tbl2 (col); CREATE TABLE tbl2 (col);
@ -1954,7 +1955,7 @@ mod test {
#[test] #[test]
#[cfg(all(feature = "bundled", not(feature = "bundled-sqlcipher")))] // SQLite >= 3.35.0 #[cfg(all(feature = "bundled", not(feature = "bundled-sqlcipher")))] // SQLite >= 3.35.0
fn test_returning() -> Result<()> { fn test_returning() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?; db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?;
let row_id = let row_id =
db.query_row::<i64, _, _>("INSERT INTO foo DEFAULT VALUES RETURNING ROWID", [], |r| { db.query_row::<i64, _, _>("INSERT INTO foo DEFAULT VALUES RETURNING ROWID", [], |r| {
@ -1967,7 +1968,7 @@ mod test {
#[test] #[test]
#[cfg(feature = "modern_sqlite")] #[cfg(feature = "modern_sqlite")]
fn test_cache_flush() -> Result<()> { fn test_cache_flush() -> Result<()> {
let db = checked_memory_handle(); let db = Connection::open_in_memory()?;
db.cache_flush() db.cache_flush()
} }
} }