mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 05:41:47 +08:00
Merge pull request #1251 from gwenn/one_column
Intoduce one_column for tests
This commit is contained in:
commit
12a6d3c1b1
@ -336,7 +336,7 @@ mod test {
|
||||
backup.step(-1)?;
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst.query_row("SELECT x FROM foo", [], |r| r.get(0))?;
|
||||
let the_answer: i64 = dst.one_column("SELECT x FROM foo")?;
|
||||
assert_eq!(42, the_answer);
|
||||
|
||||
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
||||
@ -346,7 +346,7 @@ mod test {
|
||||
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
||||
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo")?;
|
||||
assert_eq!(42 + 43, the_answer);
|
||||
Ok(())
|
||||
}
|
||||
@ -368,7 +368,7 @@ mod test {
|
||||
backup.step(-1)?;
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst.query_row("SELECT x FROM foo", [], |r| r.get(0))?;
|
||||
let the_answer: i64 = dst.one_column("SELECT x FROM foo")?;
|
||||
assert_eq!(42, the_answer);
|
||||
|
||||
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
||||
@ -379,7 +379,7 @@ mod test {
|
||||
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
||||
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo")?;
|
||||
assert_eq!(42 + 43, the_answer);
|
||||
Ok(())
|
||||
}
|
||||
@ -406,7 +406,7 @@ mod test {
|
||||
backup.step(-1)?;
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst.query_row("SELECT x FROM foo", [], |r| r.get(0))?;
|
||||
let the_answer: i64 = dst.one_column("SELECT x FROM foo")?;
|
||||
assert_eq!(42, the_answer);
|
||||
|
||||
src.execute_batch("INSERT INTO foo VALUES(43)")?;
|
||||
@ -421,7 +421,7 @@ mod test {
|
||||
backup.run_to_completion(5, Duration::from_millis(250), None)?;
|
||||
}
|
||||
|
||||
let the_answer: i64 = dst.query_row("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
||||
let the_answer: i64 = dst.one_column("SELECT SUM(x) FROM foo")?;
|
||||
assert_eq!(42 + 43, the_answer);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -820,9 +820,9 @@ mod test {
|
||||
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|
||||
half,
|
||||
)?;
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", [], |r| r.get(0));
|
||||
let result: f64 = db.one_column("SELECT half(6)")?;
|
||||
|
||||
assert!((3f64 - result?).abs() < f64::EPSILON);
|
||||
assert!((3f64 - result).abs() < f64::EPSILON);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -835,11 +835,11 @@ mod test {
|
||||
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|
||||
half,
|
||||
)?;
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", [], |r| r.get(0));
|
||||
assert!((3f64 - result?).abs() < f64::EPSILON);
|
||||
let result: f64 = db.one_column("SELECT half(6)")?;
|
||||
assert!((3f64 - result).abs() < f64::EPSILON);
|
||||
|
||||
db.remove_function("half", 1)?;
|
||||
let result: Result<f64> = db.query_row("SELECT half(6)", [], |r| r.get(0));
|
||||
let result: Result<f64> = db.one_column("SELECT half(6)");
|
||||
result.unwrap_err();
|
||||
Ok(())
|
||||
}
|
||||
@ -885,18 +885,14 @@ mod test {
|
||||
regexp_with_auxilliary,
|
||||
)?;
|
||||
|
||||
let result: Result<bool> =
|
||||
db.query_row("SELECT regexp('l.s[aeiouy]', 'lisa')", [], |r| r.get(0));
|
||||
let result: bool = db.one_column("SELECT regexp('l.s[aeiouy]', 'lisa')")?;
|
||||
|
||||
assert!(result?);
|
||||
assert!(result);
|
||||
|
||||
let result: Result<i64> = db.query_row(
|
||||
"SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
);
|
||||
let result: i64 =
|
||||
db.one_column("SELECT COUNT(*) FROM foo WHERE regexp('l.s[aeiouy]', x) == 1")?;
|
||||
|
||||
assert_eq!(2, result?);
|
||||
assert_eq!(2, result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -924,7 +920,7 @@ mod test {
|
||||
("onetwo", "SELECT my_concat('one', 'two')"),
|
||||
("abc", "SELECT my_concat('a', 'b', 'c')"),
|
||||
] {
|
||||
let result: String = db.query_row(query, [], |r| r.get(0))?;
|
||||
let result: String = db.one_column(query)?;
|
||||
assert_eq!(expected, result);
|
||||
}
|
||||
Ok(())
|
||||
@ -943,11 +939,8 @@ mod test {
|
||||
Ok(true)
|
||||
})?;
|
||||
|
||||
let res: bool = db.query_row(
|
||||
"SELECT example(0, i) FROM (SELECT 0 as i UNION SELECT 1)",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let res: bool =
|
||||
db.one_column("SELECT example(0, i) FROM (SELECT 0 as i UNION SELECT 1)")?;
|
||||
// Doesn't actually matter, we'll assert in the function if there's a problem.
|
||||
assert!(res);
|
||||
Ok(())
|
||||
@ -998,11 +991,11 @@ mod test {
|
||||
|
||||
// sum should return NULL when given no columns (contrast with count below)
|
||||
let no_result = "SELECT my_sum(i) FROM (SELECT 2 AS i WHERE 1 <> 1)";
|
||||
let result: Option<i64> = db.query_row(no_result, [], |r| r.get(0))?;
|
||||
let result: Option<i64> = db.one_column(no_result)?;
|
||||
assert!(result.is_none());
|
||||
|
||||
let single_sum = "SELECT my_sum(i) FROM (SELECT 2 AS i UNION ALL SELECT 2)";
|
||||
let result: i64 = db.query_row(single_sum, [], |r| r.get(0))?;
|
||||
let result: i64 = db.one_column(single_sum)?;
|
||||
assert_eq!(4, result);
|
||||
|
||||
let dual_sum = "SELECT my_sum(i), my_sum(j) FROM (SELECT 2 AS i, 1 AS j UNION ALL SELECT \
|
||||
@ -1024,11 +1017,11 @@ mod test {
|
||||
|
||||
// count should return 0 when given no columns (contrast with sum above)
|
||||
let no_result = "SELECT my_count(i) FROM (SELECT 2 AS i WHERE 1 <> 1)";
|
||||
let result: i64 = db.query_row(no_result, [], |r| r.get(0))?;
|
||||
let result: i64 = db.one_column(no_result)?;
|
||||
assert_eq!(result, 0);
|
||||
|
||||
let single_sum = "SELECT my_count(i) FROM (SELECT 2 AS i UNION ALL SELECT 2)";
|
||||
let result: i64 = db.query_row(single_sum, [], |r| r.get(0))?;
|
||||
let result: i64 = db.one_column(single_sum)?;
|
||||
assert_eq!(2, result);
|
||||
Ok(())
|
||||
}
|
||||
|
44
src/lib.rs
44
src/lib.rs
@ -665,6 +665,12 @@ impl Connection {
|
||||
stmt.query_row(params, f)
|
||||
}
|
||||
|
||||
// https://sqlite.org/tclsqlite.html#onecolumn
|
||||
#[cfg(test)]
|
||||
pub(crate) fn one_column<T: crate::types::FromSql>(&self, sql: &str) -> Result<T> {
|
||||
self.query_row(sql, [], |r| r.get(0))
|
||||
}
|
||||
|
||||
/// Convenience method to execute a query with named parameter(s) that is
|
||||
/// expected to return a single row.
|
||||
///
|
||||
@ -1261,9 +1267,9 @@ mod test {
|
||||
|
||||
let path_string = path.to_str().unwrap();
|
||||
let db = Connection::open(path_string)?;
|
||||
let the_answer: Result<i64> = db.query_row("SELECT x FROM foo", [], |r| r.get(0));
|
||||
let the_answer: i64 = db.one_column("SELECT x FROM foo")?;
|
||||
|
||||
assert_eq!(42i64, the_answer?);
|
||||
assert_eq!(42i64, the_answer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1318,9 +1324,9 @@ mod test {
|
||||
}
|
||||
|
||||
let db = Connection::open(&db_path)?;
|
||||
let the_answer: Result<i64> = db.query_row("SELECT x FROM foo", [], |r| r.get(0));
|
||||
let the_answer: i64 = db.one_column("SELECT x FROM foo")?;
|
||||
|
||||
assert_eq!(42i64, the_answer?);
|
||||
assert_eq!(42i64, the_answer);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1403,10 +1409,7 @@ mod test {
|
||||
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [1i32])?);
|
||||
assert_eq!(1, db.execute("INSERT INTO foo(x) VALUES (?)", [2i32])?);
|
||||
|
||||
assert_eq!(
|
||||
3i32,
|
||||
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
assert_eq!(3i32, db.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1541,12 +1544,9 @@ mod test {
|
||||
END;";
|
||||
db.execute_batch(sql)?;
|
||||
|
||||
assert_eq!(
|
||||
10i64,
|
||||
db.query_row::<i64, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
assert_eq!(10i64, db.one_column::<i64>("SELECT SUM(x) FROM foo")?);
|
||||
|
||||
let result: Result<i64> = db.query_row("SELECT x FROM foo WHERE x > 5", [], |r| r.get(0));
|
||||
let result: Result<i64> = db.one_column("SELECT x FROM foo WHERE x > 5");
|
||||
match result.unwrap_err() {
|
||||
Error::QueryReturnedNoRows => (),
|
||||
err => panic!("Unexpected error {}", err),
|
||||
@ -1562,21 +1562,21 @@ mod test {
|
||||
fn test_optional() -> Result<()> {
|
||||
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.one_column("SELECT 1 WHERE 0 <> 0");
|
||||
let result = result.optional();
|
||||
match result? {
|
||||
None => (),
|
||||
_ => panic!("Unexpected result"),
|
||||
}
|
||||
|
||||
let result: Result<i64> = db.query_row("SELECT 1 WHERE 0 == 0", [], |r| r.get(0));
|
||||
let result: Result<i64> = db.one_column("SELECT 1 WHERE 0 == 0");
|
||||
let result = result.optional();
|
||||
match result? {
|
||||
Some(1) => (),
|
||||
_ => panic!("Unexpected result"),
|
||||
}
|
||||
|
||||
let bad_query_result: Result<i64> = db.query_row("NOT A PROPER QUERY", [], |r| r.get(0));
|
||||
let bad_query_result: Result<i64> = db.one_column("NOT A PROPER QUERY");
|
||||
let bad_query_result = bad_query_result.optional();
|
||||
bad_query_result.unwrap_err();
|
||||
Ok(())
|
||||
@ -1585,11 +1585,8 @@ 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))?
|
||||
);
|
||||
let mode = db.query_row::<String, _, _>("PRAGMA journal_mode=off", [], |r| r.get(0))?;
|
||||
assert_eq!("memory", db.one_column::<String>("PRAGMA journal_mode")?);
|
||||
let mode = db.one_column::<String>("PRAGMA journal_mode=off")?;
|
||||
if cfg!(features = "bundled") {
|
||||
assert_eq!(mode, "off");
|
||||
} else {
|
||||
@ -2079,10 +2076,7 @@ mod test {
|
||||
fn test_returning() -> Result<()> {
|
||||
let db = Connection::open_in_memory()?;
|
||||
db.execute_batch("CREATE TABLE foo(x INTEGER PRIMARY KEY)")?;
|
||||
let row_id =
|
||||
db.query_row::<i64, _, _>("INSERT INTO foo DEFAULT VALUES RETURNING ROWID", [], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
let row_id = db.one_column::<i64>("INSERT INTO foo DEFAULT VALUES RETURNING ROWID")?;
|
||||
assert_eq!(row_id, 1);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -333,10 +333,7 @@ mod test {
|
||||
#[cfg(feature = "modern_sqlite")]
|
||||
fn pragma_func_query_value() -> Result<()> {
|
||||
let db = Connection::open_in_memory()?;
|
||||
let user_version: i32 =
|
||||
db.query_row("SELECT user_version FROM pragma_user_version", [], |row| {
|
||||
row.get(0)
|
||||
})?;
|
||||
let user_version: i32 = db.one_column("SELECT user_version FROM pragma_user_version")?;
|
||||
assert_eq!(0, user_version);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1212,8 +1212,7 @@ mod test {
|
||||
let mut stmt = db.prepare("INSERT INTO test (x, y) VALUES (:x, :y)")?;
|
||||
stmt.execute_named(&[(":x", &"one")])?;
|
||||
|
||||
let result: Option<String> =
|
||||
db.query_row("SELECT y FROM test WHERE x = 'one'", [], |row| row.get(0))?;
|
||||
let result: Option<String> = db.one_column("SELECT y FROM test WHERE x = 'one'")?;
|
||||
assert!(result.is_none());
|
||||
Ok(())
|
||||
}
|
||||
@ -1259,8 +1258,7 @@ mod test {
|
||||
stmt.execute(&[(":x", "one")])?;
|
||||
stmt.execute(&[(":y", "two")])?;
|
||||
|
||||
let result: String =
|
||||
db.query_row("SELECT x FROM test WHERE y = 'two'", [], |row| row.get(0))?;
|
||||
let result: String = db.one_column("SELECT x FROM test WHERE y = 'two'")?;
|
||||
assert_eq!(result, "one");
|
||||
Ok(())
|
||||
}
|
||||
@ -1500,7 +1498,7 @@ mod test {
|
||||
db.execute_batch("CREATE TABLE foo(x TEXT)")?;
|
||||
let expected = "テスト";
|
||||
db.execute("INSERT INTO foo(x) VALUES (?)", [&expected])?;
|
||||
let actual: String = db.query_row("SELECT x FROM foo", [], |row| row.get(0))?;
|
||||
let actual: String = db.one_column("SELECT x FROM foo")?;
|
||||
assert_eq!(expected, actual);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -552,10 +552,7 @@ mod test {
|
||||
}
|
||||
{
|
||||
let tx = db.transaction()?;
|
||||
assert_eq!(
|
||||
2i32,
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
assert_eq!(2i32, tx.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -591,10 +588,7 @@ mod test {
|
||||
tx.commit()?;
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
2i32,
|
||||
db.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
assert_eq!(2i32, db.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -619,10 +613,7 @@ mod test {
|
||||
}
|
||||
{
|
||||
let tx = db.transaction()?;
|
||||
assert_eq!(
|
||||
6i32,
|
||||
tx.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
assert_eq!(6i32, tx.one_column::<i32>("SELECT SUM(x) FROM foo")?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -731,7 +722,7 @@ mod test {
|
||||
}
|
||||
|
||||
fn assert_current_sum(x: i32, conn: &Connection) -> Result<()> {
|
||||
let i = conn.query_row::<i32, _, _>("SELECT SUM(x) FROM foo", [], |r| r.get(0))?;
|
||||
let i = conn.one_column::<i32>("SELECT SUM(x) FROM foo")?;
|
||||
assert_eq!(x, i);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -178,9 +178,9 @@ mod test {
|
||||
let date = NaiveDate::from_ymd(2016, 2, 23);
|
||||
db.execute("INSERT INTO foo (t) VALUES (?)", [date])?;
|
||||
|
||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!("2016-02-23", s);
|
||||
let t: NaiveDate = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let t: NaiveDate = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(date, t);
|
||||
Ok(())
|
||||
}
|
||||
@ -191,9 +191,9 @@ mod test {
|
||||
let time = NaiveTime::from_hms(23, 56, 4);
|
||||
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?;
|
||||
|
||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!("23:56:04", s);
|
||||
let v: NaiveTime = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let v: NaiveTime = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(time, v);
|
||||
Ok(())
|
||||
}
|
||||
@ -207,13 +207,13 @@ mod test {
|
||||
|
||||
db.execute("INSERT INTO foo (t) VALUES (?)", [dt])?;
|
||||
|
||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!("2016-02-23 23:56:04", s);
|
||||
let v: NaiveDateTime = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let v: NaiveDateTime = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(dt, v);
|
||||
|
||||
db.execute("UPDATE foo set b = datetime(t)", [])?; // "YYYY-MM-DD HH:MM:SS"
|
||||
let hms: NaiveDateTime = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
||||
let hms: NaiveDateTime = db.one_column("SELECT b FROM foo")?;
|
||||
assert_eq!(dt, hms);
|
||||
Ok(())
|
||||
}
|
||||
@ -228,21 +228,19 @@ mod test {
|
||||
|
||||
db.execute("INSERT INTO foo (t) VALUES (?)", [utc])?;
|
||||
|
||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!("2016-02-23 23:56:04.789+00:00", s);
|
||||
|
||||
let v1: DateTime<Utc> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let v1: DateTime<Utc> = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(utc, v1);
|
||||
|
||||
let v2: DateTime<Utc> =
|
||||
db.query_row("SELECT '2016-02-23 23:56:04.789'", [], |r| r.get(0))?;
|
||||
let v2: DateTime<Utc> = db.one_column("SELECT '2016-02-23 23:56:04.789'")?;
|
||||
assert_eq!(utc, v2);
|
||||
|
||||
let v3: DateTime<Utc> = db.query_row("SELECT '2016-02-23 23:56:04'", [], |r| r.get(0))?;
|
||||
let v3: DateTime<Utc> = db.one_column("SELECT '2016-02-23 23:56:04'")?;
|
||||
assert_eq!(utc - Duration::milliseconds(789), v3);
|
||||
|
||||
let v4: DateTime<Utc> =
|
||||
db.query_row("SELECT '2016-02-23 23:56:04.789+00:00'", [], |r| r.get(0))?;
|
||||
let v4: DateTime<Utc> = db.one_column("SELECT '2016-02-23 23:56:04.789+00:00'")?;
|
||||
assert_eq!(utc, v4);
|
||||
Ok(())
|
||||
}
|
||||
@ -258,10 +256,10 @@ mod test {
|
||||
db.execute("INSERT INTO foo (t) VALUES (?)", [local])?;
|
||||
|
||||
// Stored string should be in UTC
|
||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert!(s.ends_with("+00:00"));
|
||||
|
||||
let v: DateTime<Local> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let v: DateTime<Local> = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(local, v);
|
||||
Ok(())
|
||||
}
|
||||
@ -274,10 +272,10 @@ mod test {
|
||||
db.execute("INSERT INTO foo (t) VALUES (?)", [time])?;
|
||||
|
||||
// Stored string should preserve timezone offset
|
||||
let s: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let s: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert!(s.ends_with("+04:00"));
|
||||
|
||||
let v: DateTime<FixedOffset> = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let v: DateTime<FixedOffset> = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(time.offset(), v.offset());
|
||||
assert_eq!(time, v);
|
||||
Ok(())
|
||||
@ -286,15 +284,13 @@ mod test {
|
||||
#[test]
|
||||
fn test_sqlite_functions() -> Result<()> {
|
||||
let db = checked_memory_handle()?;
|
||||
let result: Result<NaiveTime> = db.query_row("SELECT CURRENT_TIME", [], |r| r.get(0));
|
||||
let result: Result<NaiveTime> = db.one_column("SELECT CURRENT_TIME");
|
||||
result.unwrap();
|
||||
let result: Result<NaiveDate> = db.query_row("SELECT CURRENT_DATE", [], |r| r.get(0));
|
||||
let result: Result<NaiveDate> = db.one_column("SELECT CURRENT_DATE");
|
||||
result.unwrap();
|
||||
let result: Result<NaiveDateTime> =
|
||||
db.query_row("SELECT CURRENT_TIMESTAMP", [], |r| r.get(0));
|
||||
let result: Result<NaiveDateTime> = db.one_column("SELECT CURRENT_TIMESTAMP");
|
||||
result.unwrap();
|
||||
let result: Result<DateTime<Utc>> =
|
||||
db.query_row("SELECT CURRENT_TIMESTAMP", [], |r| r.get(0));
|
||||
let result: Result<DateTime<Utc>> = db.one_column("SELECT CURRENT_TIMESTAMP");
|
||||
result.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ mod test {
|
||||
let v1234 = vec![1u8, 2, 3, 4];
|
||||
db.execute("INSERT INTO foo(b) VALUES (?)", [&v1234])?;
|
||||
|
||||
let v: Vec<u8> = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
||||
let v: Vec<u8> = db.one_column("SELECT b FROM foo")?;
|
||||
assert_eq!(v, v1234);
|
||||
Ok(())
|
||||
}
|
||||
@ -167,7 +167,7 @@ mod test {
|
||||
let empty = vec![];
|
||||
db.execute("INSERT INTO foo(b) VALUES (?)", [&empty])?;
|
||||
|
||||
let v: Vec<u8> = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
||||
let v: Vec<u8> = db.one_column("SELECT b FROM foo")?;
|
||||
assert_eq!(v, empty);
|
||||
Ok(())
|
||||
}
|
||||
@ -179,7 +179,7 @@ mod test {
|
||||
let s = "hello, world!";
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", [&s])?;
|
||||
|
||||
let from: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let from: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(from, s);
|
||||
Ok(())
|
||||
}
|
||||
@ -191,7 +191,7 @@ mod test {
|
||||
let s = "hello, world!";
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", [s.to_owned()])?;
|
||||
|
||||
let from: String = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let from: String = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(from, s);
|
||||
Ok(())
|
||||
}
|
||||
@ -202,10 +202,7 @@ mod test {
|
||||
|
||||
db.execute("INSERT INTO foo(i) VALUES (?)", [Value::Integer(10)])?;
|
||||
|
||||
assert_eq!(
|
||||
10i64,
|
||||
db.query_row::<i64, _, _>("SELECT i FROM foo", [], |r| r.get(0))?
|
||||
);
|
||||
assert_eq!(10i64, db.one_column::<i64>("SELECT i FROM foo")?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,7 @@ impl FromSql for Value {
|
||||
mod test {
|
||||
use crate::types::ToSql;
|
||||
use crate::{Connection, Result};
|
||||
use serde_json::Value;
|
||||
|
||||
fn checked_memory_handle() -> Result<Connection> {
|
||||
let db = Connection::open_in_memory()?;
|
||||
@ -38,15 +39,15 @@ mod test {
|
||||
let db = checked_memory_handle()?;
|
||||
|
||||
let json = r#"{"foo": 13, "bar": "baz"}"#;
|
||||
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
||||
let data: Value = serde_json::from_str(json).unwrap();
|
||||
db.execute(
|
||||
"INSERT INTO foo (t, b) VALUES (?, ?)",
|
||||
[&data as &dyn ToSql, &json.as_bytes()],
|
||||
)?;
|
||||
|
||||
let t: serde_json::Value = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let t: Value = db.one_column("SELECT t FROM foo")?;
|
||||
assert_eq!(data, t);
|
||||
let b: serde_json::Value = db.query_row("SELECT b FROM foo", [], |r| r.get(0))?;
|
||||
let b: Value = db.one_column("SELECT b FROM foo")?;
|
||||
assert_eq!(data, b);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ mod test {
|
||||
for ts in ts_vec {
|
||||
db.execute("INSERT INTO foo(t) VALUES (?)", [ts])?;
|
||||
|
||||
let from: OffsetDateTime = db.query_row("SELECT t FROM foo", [], |r| r.get(0))?;
|
||||
let from: OffsetDateTime = db.one_column("SELECT t FROM foo")?;
|
||||
|
||||
db.execute("DELETE FROM foo", [])?;
|
||||
|
||||
@ -152,8 +152,7 @@ mod test {
|
||||
#[test]
|
||||
fn test_sqlite_functions() -> Result<()> {
|
||||
let db = Connection::open_in_memory()?;
|
||||
let result: Result<OffsetDateTime> =
|
||||
db.query_row("SELECT CURRENT_TIMESTAMP", [], |r| r.get(0));
|
||||
let result: Result<OffsetDateTime> = db.one_column("SELECT CURRENT_TIMESTAMP");
|
||||
result.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
@ -109,8 +109,8 @@ mod test {
|
||||
tx2.commit().unwrap();
|
||||
});
|
||||
assert_eq!(tx.recv().unwrap(), 1);
|
||||
let the_answer: Result<i64> = db1.query_row("SELECT x FROM foo", [], |r| r.get(0));
|
||||
assert_eq!(42i64, the_answer?);
|
||||
let the_answer: i64 = db1.one_column("SELECT x FROM foo")?;
|
||||
assert_eq!(42i64, the_answer);
|
||||
child.join().unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user