From bf0dde1e556d7e1687cf674be0939ae8fc1f1d75 Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Fri, 24 Nov 2023 16:33:59 +0200 Subject: [PATCH] verify INSERT statements in the tests --- tests/common/mod.rs | 21 ++++++++++++++++++++- tests/statement.rs | 9 ++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index d8d88162..1feee00d 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use sqlite::Connection; +use sqlite::{Connection, Value}; use std::path::Path; macro_rules! ok(($result:expr) => ($result.unwrap())); @@ -32,3 +32,22 @@ pub fn setup_users>(path: T) -> Connection { )); connection } + +pub fn count_users(connection: &Connection) -> i64 { + let query = "SELECT COUNT(*) FROM users"; + let mut statement = ok!(connection.prepare(query)); + ok!(statement.next()); + ok!(statement.read::(0)) +} + +pub fn check_user(connection: &Connection, id: i64, name: &str, age: f64, photo: Vec) { + let query = "SELECT * FROM users WHERE id = :id"; + let mut statement = ok!(connection.prepare(query)); + ok!(statement.bind((":id", id))); + ok!(statement.next()); + assert_eq!(ok!(statement.read::(0)), id); + assert_eq!(ok!(statement.read::(1)), name); + assert_eq!(ok!(statement.read::(2)), age); + assert_eq!(ok!(statement.read::, _>(3)), photo); + assert_eq!(ok!(statement.read::(4)), Value::Null); +} diff --git a/tests/statement.rs b/tests/statement.rs index baa2510f..9f4fec32 100644 --- a/tests/statement.rs +++ b/tests/statement.rs @@ -4,13 +4,16 @@ use sqlite::{Connection, State, Statement, Type, Value}; mod common; -use common::{setup_english, setup_users}; +use common::{check_user, count_users, setup_english, setup_users}; macro_rules! ok(($result:expr) => ($result.unwrap())); #[test] fn bind_with_index() { let connection = setup_users(":memory:"); + assert_eq!(count_users(&connection), 1); + check_user(&connection, 1i64, "Alice", 42.69, vec![0x42u8, 0x69u8]); + let query = "INSERT INTO users VALUES (?, ?, ?, ?, ?)"; let mut statement = ok!(connection.prepare(query)); @@ -20,7 +23,10 @@ fn bind_with_index() { ok!(statement.bind((3, 69.42))); ok!(statement.bind((4, &[0x69u8, 0x42u8][..]))); ok!(statement.bind((5, ()))); + assert_eq!(count_users(&connection), 1); assert_eq!(ok!(statement.next()), State::Done); + assert_eq!(count_users(&connection), 2); + check_user(&connection, 2i64, "Bob", 69.42, vec![0x69u8, 0x42u8]); ok!(statement.reset()); ok!(statement.bind((1, Some(2i64)))); @@ -29,6 +35,7 @@ fn bind_with_index() { ok!(statement.bind((4, Some(&[0x69u8, 0x42u8][..])))); ok!(statement.bind((5, None::<&str>))); assert_eq!(ok!(statement.next()), State::Done); + assert_eq!(count_users(&connection), 3); ok!(statement.reset()); ok!(statement.bind(