Skip to content

Commit

Permalink
Fix non-bundled tests against macOS system SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
thomcc committed Apr 3, 2022
1 parent f8b9ad8 commit 9699b4a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
23 changes: 18 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,15 +1515,28 @@ mod test {
#[test]
fn test_pragma_query_row() -> Result<()> {
let db = Connection::open_in_memory()?;

assert_eq!(
"memory",
db.query_row::<String, _, _>("PRAGMA journal_mode", [], |r| r.get(0))?
);
assert_eq!(
"off",
db.query_row::<String, _, _>("PRAGMA journal_mode=off", [], |r| r.get(0))?
);
let mode = db.query_row::<String, _, _>("PRAGMA journal_mode=off", [], |r| r.get(0))?;
if cfg!(features = "bundled") {
assert_eq!(mode, "off");
} else {
// Note: system SQLite on macOS defaults to "off" rather than
// "memory" for the journal mode (which cannot be changed for
// in-memory connections). This seems like it's *probably* legal
// according to the docs below, so we relax this test when not
// bundling:
//
// From https://www.sqlite.org/pragma.html#pragma_journal_mode
// > Note that the journal_mode for an in-memory database is either
// > MEMORY or OFF and can not be changed to a different value. An
// > attempt to change the journal_mode of an in-memory database to
// > any setting other than MEMORY or OFF is ignored.
assert!(mode == "memory" || mode == "off", "Got mode {:?}", mode);
}

Ok(())
}

Expand Down
22 changes: 12 additions & 10 deletions src/pragma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,20 @@ mod test {
let db = Connection::open_in_memory()?;
let journal_mode: String =
db.pragma_update_and_check(None, "journal_mode", "OFF", |row| row.get(0))?;
assert_eq!("off", &journal_mode);
// Sanity checks to ensure the move to a generic `ToSql` wasn't breaking
assert_eq!(
"off",
db.pragma_update_and_check(None, "journal_mode", &"OFF", |row| row
.get::<_, String>(0))?,
assert!(
journal_mode == "off" || journal_mode == "memory",
"mode: {:?}",
journal_mode,
);
// Sanity checks to ensure the move to a generic `ToSql` wasn't breaking
let mode = db
.pragma_update_and_check(None, "journal_mode", &"OFF", |row| row.get::<_, String>(0))?;
assert!(mode == "off" || mode == "memory", "mode: {:?}", mode);

let param: &dyn crate::ToSql = &"OFF";
assert_eq!(
"off",
db.pragma_update_and_check(None, "journal_mode", param, |row| row.get::<_, String>(0))?,
);
let mode =
db.pragma_update_and_check(None, "journal_mode", param, |row| row.get::<_, String>(0))?;
assert!(mode == "off" || mode == "memory", "mode: {:?}", mode);
Ok(())
}

Expand Down
11 changes: 5 additions & 6 deletions tests/deny_single_threaded_sqlite_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ use rusqlite::ffi;
use rusqlite::Connection;

#[test]
#[should_panic]
fn test_error_when_singlethread_mode() {
// put SQLite into single-threaded mode
unsafe {
// Note: macOS system SQLite seems to return an error if you attempt to
// reconfigure to single-threaded mode.
if ffi::sqlite3_config(ffi::SQLITE_CONFIG_SINGLETHREAD) != ffi::SQLITE_OK {
return;
}
if ffi::sqlite3_initialize() != ffi::SQLITE_OK {
return;
}
assert_eq!(ffi::sqlite3_initialize(), ffi::SQLITE_OK);
}

let _ = Connection::open_in_memory().unwrap();
let res = Connection::open_in_memory();
assert!(res.is_err());
}

0 comments on commit 9699b4a

Please sign in to comment.