mirror of
https://github.com/isar/rusqlite.git
synced 2025-10-19 06:18:56 +08:00
Make tests return Result
This commit is contained in:
@@ -500,35 +500,35 @@ impl Connection {
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::DropBehavior;
|
||||
use crate::{Connection, Error};
|
||||
use crate::{Connection, Error, Result};
|
||||
|
||||
fn checked_memory_handle() -> Connection {
|
||||
let db = Connection::open_in_memory().unwrap();
|
||||
db.execute_batch("CREATE TABLE foo (x INTEGER)").unwrap();
|
||||
db
|
||||
fn checked_memory_handle() -> Result<Connection> {
|
||||
let db = Connection::open_in_memory()?;
|
||||
db.execute_batch("CREATE TABLE foo (x INTEGER)")?;
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_drop() {
|
||||
let mut db = checked_memory_handle();
|
||||
fn test_drop() -> Result<()> {
|
||||
let mut db = checked_memory_handle()?;
|
||||
{
|
||||
let tx = db.transaction().unwrap();
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)").unwrap();
|
||||
let tx = db.transaction()?;
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)")?;
|
||||
// default: rollback
|
||||
}
|
||||
{
|
||||
let mut tx = db.transaction().unwrap();
|
||||
tx.execute_batch("INSERT INTO foo VALUES(2)").unwrap();
|
||||
let mut tx = db.transaction()?;
|
||||
tx.execute_batch("INSERT INTO foo VALUES(2)")?;
|
||||
tx.set_drop_behavior(DropBehavior::Commit)
|
||||
}
|
||||
{
|
||||
let tx = db.transaction().unwrap();
|
||||
let tx = db.transaction()?;
|
||||
assert_eq!(
|
||||
2i32,
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))
|
||||
.unwrap()
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn assert_nested_tx_error(e: crate::Error) {
|
||||
if let Error::SqliteFailure(e, Some(m)) = &e {
|
||||
@@ -542,165 +542,168 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unchecked_nesting() {
|
||||
let db = checked_memory_handle();
|
||||
fn test_unchecked_nesting() -> Result<()> {
|
||||
let db = checked_memory_handle()?;
|
||||
|
||||
{
|
||||
let tx = db.unchecked_transaction().unwrap();
|
||||
let tx = db.unchecked_transaction()?;
|
||||
let e = tx.unchecked_transaction().unwrap_err();
|
||||
assert_nested_tx_error(e);
|
||||
// default: rollback
|
||||
}
|
||||
{
|
||||
let tx = db.unchecked_transaction().unwrap();
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)").unwrap();
|
||||
let tx = db.unchecked_transaction()?;
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)")?;
|
||||
// Ensure this doesn't interfere with ongoing transaction
|
||||
let e = tx.unchecked_transaction().unwrap_err();
|
||||
assert_nested_tx_error(e);
|
||||
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)").unwrap();
|
||||
tx.commit().unwrap();
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)")?;
|
||||
tx.commit()?;
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
2i32,
|
||||
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))
|
||||
.unwrap()
|
||||
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_explicit_rollback_commit() {
|
||||
let mut db = checked_memory_handle();
|
||||
fn test_explicit_rollback_commit() -> Result<()> {
|
||||
let mut db = checked_memory_handle()?;
|
||||
{
|
||||
let mut tx = db.transaction().unwrap();
|
||||
let mut tx = db.transaction()?;
|
||||
{
|
||||
let mut sp = tx.savepoint().unwrap();
|
||||
sp.execute_batch("INSERT INTO foo VALUES(1)").unwrap();
|
||||
sp.rollback().unwrap();
|
||||
sp.execute_batch("INSERT INTO foo VALUES(2)").unwrap();
|
||||
sp.commit().unwrap();
|
||||
let mut sp = tx.savepoint()?;
|
||||
sp.execute_batch("INSERT INTO foo VALUES(1)")?;
|
||||
sp.rollback()?;
|
||||
sp.execute_batch("INSERT INTO foo VALUES(2)")?;
|
||||
sp.commit()?;
|
||||
}
|
||||
tx.commit().unwrap();
|
||||
tx.commit()?;
|
||||
}
|
||||
{
|
||||
let tx = db.transaction().unwrap();
|
||||
tx.execute_batch("INSERT INTO foo VALUES(4)").unwrap();
|
||||
tx.commit().unwrap();
|
||||
let tx = db.transaction()?;
|
||||
tx.execute_batch("INSERT INTO foo VALUES(4)")?;
|
||||
tx.commit()?;
|
||||
}
|
||||
{
|
||||
let tx = db.transaction().unwrap();
|
||||
let tx = db.transaction()?;
|
||||
assert_eq!(
|
||||
6i32,
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))
|
||||
.unwrap()
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_savepoint() {
|
||||
let mut db = checked_memory_handle();
|
||||
fn test_savepoint() -> Result<()> {
|
||||
let mut db = checked_memory_handle()?;
|
||||
{
|
||||
let mut tx = db.transaction().unwrap();
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)").unwrap();
|
||||
assert_current_sum(1, &tx);
|
||||
let mut tx = db.transaction()?;
|
||||
tx.execute_batch("INSERT INTO foo VALUES(1)")?;
|
||||
assert_current_sum(1, &tx)?;
|
||||
tx.set_drop_behavior(DropBehavior::Commit);
|
||||
{
|
||||
let mut sp1 = tx.savepoint().unwrap();
|
||||
sp1.execute_batch("INSERT INTO foo VALUES(2)").unwrap();
|
||||
assert_current_sum(3, &sp1);
|
||||
let mut sp1 = tx.savepoint()?;
|
||||
sp1.execute_batch("INSERT INTO foo VALUES(2)")?;
|
||||
assert_current_sum(3, &sp1)?;
|
||||
// will rollback sp1
|
||||
{
|
||||
let mut sp2 = sp1.savepoint().unwrap();
|
||||
sp2.execute_batch("INSERT INTO foo VALUES(4)").unwrap();
|
||||
assert_current_sum(7, &sp2);
|
||||
let mut sp2 = sp1.savepoint()?;
|
||||
sp2.execute_batch("INSERT INTO foo VALUES(4)")?;
|
||||
assert_current_sum(7, &sp2)?;
|
||||
// will rollback sp2
|
||||
{
|
||||
let sp3 = sp2.savepoint().unwrap();
|
||||
sp3.execute_batch("INSERT INTO foo VALUES(8)").unwrap();
|
||||
assert_current_sum(15, &sp3);
|
||||
sp3.commit().unwrap();
|
||||
let sp3 = sp2.savepoint()?;
|
||||
sp3.execute_batch("INSERT INTO foo VALUES(8)")?;
|
||||
assert_current_sum(15, &sp3)?;
|
||||
sp3.commit()?;
|
||||
// committed sp3, but will be erased by sp2 rollback
|
||||
}
|
||||
assert_current_sum(15, &sp2);
|
||||
assert_current_sum(15, &sp2)?;
|
||||
}
|
||||
assert_current_sum(3, &sp1);
|
||||
assert_current_sum(3, &sp1)?;
|
||||
}
|
||||
assert_current_sum(1, &tx);
|
||||
assert_current_sum(1, &tx)?;
|
||||
}
|
||||
assert_current_sum(1, &db);
|
||||
assert_current_sum(1, &db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ignore_drop_behavior() {
|
||||
let mut db = checked_memory_handle();
|
||||
fn test_ignore_drop_behavior() -> Result<()> {
|
||||
let mut db = checked_memory_handle()?;
|
||||
|
||||
let mut tx = db.transaction().unwrap();
|
||||
let mut tx = db.transaction()?;
|
||||
{
|
||||
let mut sp1 = tx.savepoint().unwrap();
|
||||
insert(1, &sp1);
|
||||
sp1.rollback().unwrap();
|
||||
insert(2, &sp1);
|
||||
let mut sp1 = tx.savepoint()?;
|
||||
insert(1, &sp1)?;
|
||||
sp1.rollback()?;
|
||||
insert(2, &sp1)?;
|
||||
{
|
||||
let mut sp2 = sp1.savepoint().unwrap();
|
||||
let mut sp2 = sp1.savepoint()?;
|
||||
sp2.set_drop_behavior(DropBehavior::Ignore);
|
||||
insert(4, &sp2);
|
||||
insert(4, &sp2)?;
|
||||
}
|
||||
assert_current_sum(6, &sp1);
|
||||
sp1.commit().unwrap();
|
||||
assert_current_sum(6, &sp1)?;
|
||||
sp1.commit()?;
|
||||
}
|
||||
assert_current_sum(6, &tx);
|
||||
assert_current_sum(6, &tx)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_savepoint_names() {
|
||||
let mut db = checked_memory_handle();
|
||||
fn test_savepoint_names() -> Result<()> {
|
||||
let mut db = checked_memory_handle()?;
|
||||
|
||||
{
|
||||
let mut sp1 = db.savepoint_with_name("my_sp").unwrap();
|
||||
insert(1, &sp1);
|
||||
assert_current_sum(1, &sp1);
|
||||
let mut sp1 = db.savepoint_with_name("my_sp")?;
|
||||
insert(1, &sp1)?;
|
||||
assert_current_sum(1, &sp1)?;
|
||||
{
|
||||
let mut sp2 = sp1.savepoint_with_name("my_sp").unwrap();
|
||||
let mut sp2 = sp1.savepoint_with_name("my_sp")?;
|
||||
sp2.set_drop_behavior(DropBehavior::Commit);
|
||||
insert(2, &sp2);
|
||||
assert_current_sum(3, &sp2);
|
||||
sp2.rollback().unwrap();
|
||||
assert_current_sum(1, &sp2);
|
||||
insert(4, &sp2);
|
||||
insert(2, &sp2)?;
|
||||
assert_current_sum(3, &sp2)?;
|
||||
sp2.rollback()?;
|
||||
assert_current_sum(1, &sp2)?;
|
||||
insert(4, &sp2)?;
|
||||
}
|
||||
assert_current_sum(5, &sp1);
|
||||
sp1.rollback().unwrap();
|
||||
assert_current_sum(5, &sp1)?;
|
||||
sp1.rollback()?;
|
||||
{
|
||||
let mut sp2 = sp1.savepoint_with_name("my_sp").unwrap();
|
||||
let mut sp2 = sp1.savepoint_with_name("my_sp")?;
|
||||
sp2.set_drop_behavior(DropBehavior::Ignore);
|
||||
insert(8, &sp2);
|
||||
insert(8, &sp2)?;
|
||||
}
|
||||
assert_current_sum(8, &sp1);
|
||||
sp1.commit().unwrap();
|
||||
assert_current_sum(8, &sp1)?;
|
||||
sp1.commit()?;
|
||||
}
|
||||
assert_current_sum(8, &db);
|
||||
assert_current_sum(8, &db)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rc() {
|
||||
fn test_rc() -> Result<()> {
|
||||
use std::rc::Rc;
|
||||
let mut conn = Connection::open_in_memory().unwrap();
|
||||
let rc_txn = Rc::new(conn.transaction().unwrap());
|
||||
let mut conn = Connection::open_in_memory()?;
|
||||
let rc_txn = Rc::new(conn.transaction()?);
|
||||
|
||||
// This will compile only if Transaction is Debug
|
||||
Rc::try_unwrap(rc_txn).unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert(x: i32, conn: &Connection) {
|
||||
conn.execute("INSERT INTO foo VALUES(?)", [x]).unwrap();
|
||||
fn insert(x: i32, conn: &Connection) -> Result<usize> {
|
||||
conn.execute("INSERT INTO foo VALUES(?)", [x])
|
||||
}
|
||||
|
||||
fn assert_current_sum(x: i32, conn: &Connection) {
|
||||
let i = conn
|
||||
.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))
|
||||
.unwrap();
|
||||
fn assert_current_sum(x: i32, conn: &Connection) -> Result<()> {
|
||||
let i = conn.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
||||
assert_eq!(x, i);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user