Test Batch iterator

This commit is contained in:
gwenn 2020-10-31 11:11:12 +01:00
parent f3c2b63836
commit 1103febb1e

View File

@ -1810,63 +1810,77 @@ mod test {
err => panic!("Unexpected error {}", err), err => panic!("Unexpected error {}", err),
} }
} }
}
#[test] #[test]
fn test_dynamic() { fn test_dynamic() {
let db = checked_memory_handle(); let db = checked_memory_handle();
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\");
END;"; END;";
db.execute_batch(sql).unwrap(); db.execute_batch(sql).unwrap();
db.query_row("SELECT * FROM foo", params![], |r| { db.query_row("SELECT * FROM foo", NO_PARAMS, |r| {
assert_eq!(2, r.column_count()); assert_eq!(2, r.column_count());
Ok(()) Ok(())
}) })
.unwrap(); .unwrap();
} }
#[test] #[test]
fn test_dyn_box() { fn test_dyn_box() {
let db = checked_memory_handle(); let db = checked_memory_handle();
db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap(); db.execute_batch("CREATE TABLE foo(x INTEGER);").unwrap();
let b: Box<dyn ToSql> = Box::new(5); let b: Box<dyn ToSql> = Box::new(5);
db.execute("INSERT INTO foo VALUES(?)", &[b]).unwrap(); db.execute("INSERT INTO foo VALUES(?)", &[b]).unwrap();
db.query_row("SELECT x FROM foo", params![], |r| { db.query_row("SELECT x FROM foo", NO_PARAMS, |r| {
assert_eq!(5, r.get_unwrap::<_, i32>(0)); assert_eq!(5, r.get_unwrap::<_, i32>(0));
Ok(()) Ok(())
}) })
.unwrap(); .unwrap();
} }
#[test] #[test]
fn test_params() { fn test_params() {
let db = checked_memory_handle(); let db = checked_memory_handle();
db.query_row( db.query_row(
"SELECT "SELECT
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?;", ?, ?, ?, ?;",
params![ params![
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
], ],
|r| { |r| {
assert_eq!(1, r.get_unwrap::<_, i32>(0)); assert_eq!(1, r.get_unwrap::<_, i32>(0));
Ok(()) Ok(())
}, },
) )
.unwrap(); .unwrap();
} }
#[test] #[test]
#[cfg(not(feature = "extra_check"))] #[cfg(not(feature = "extra_check"))]
fn test_alter_table() { fn test_alter_table() {
let db = checked_memory_handle(); let db = checked_memory_handle();
db.execute_batch("CREATE TABLE x(t);").unwrap(); db.execute_batch("CREATE TABLE x(t);").unwrap();
// `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;", params![]).unwrap(); db.execute("ALTER TABLE x RENAME TO y;", NO_PARAMS).unwrap();
}
#[test]
fn test_batch() {
let db = checked_memory_handle();
let sql = r"
CREATE TABLE tbl1 (col);
CREATE TABLE tbl2 (col);
";
let batch = Batch::new(&db, sql);
for stmt in batch {
let mut stmt = stmt.unwrap();
stmt.execute(NO_PARAMS).unwrap();
} }
} }
} }