Skip to content

Commit a44657a

Browse files
committed
Test OptionalExt too
1 parent 8abf96c commit a44657a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

crates/duckdb/src/statement.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,42 @@ mod test {
881881
Ok(())
882882
}
883883

884+
#[test]
885+
fn test_query_one_optional() -> Result<()> {
886+
use crate::OptionalExt;
887+
888+
let db = Connection::open_in_memory()?;
889+
let sql = "BEGIN;
890+
CREATE TABLE foo(x INTEGER, y INTEGER);
891+
INSERT INTO foo VALUES(1, 3);
892+
INSERT INTO foo VALUES(2, 4);
893+
END;";
894+
db.execute_batch(sql)?;
895+
896+
// Exactly one row
897+
let y: Option<i32> = db
898+
.prepare("SELECT y FROM foo WHERE x = ?")?
899+
.query_one([1], |r| r.get(0))
900+
.optional()?;
901+
assert_eq!(y, Some(3));
902+
903+
// No rows
904+
let y: Option<i32> = db
905+
.prepare("SELECT y FROM foo WHERE x = ?")?
906+
.query_one([99], |r| r.get(0))
907+
.optional()?;
908+
assert_eq!(y, None);
909+
910+
// Multiple rows - should still return error (not converted by optional)
911+
let res = db
912+
.prepare("SELECT y FROM foo")?
913+
.query_one([], |r| r.get::<_, i32>(0))
914+
.optional();
915+
assert_eq!(res.unwrap_err(), Error::QueryReturnedMoreThanOneRow);
916+
917+
Ok(())
918+
}
919+
884920
#[test]
885921
fn test_query_by_column_name() -> Result<()> {
886922
let db = Connection::open_in_memory()?;

0 commit comments

Comments
 (0)