Skip to content

Commit

Permalink
verify INSERT statements in the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Nov 24, 2023
1 parent 629601d commit bf0dde1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
21 changes: 20 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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()));
Expand Down Expand Up @@ -32,3 +32,22 @@ pub fn setup_users<T: AsRef<Path>>(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::<i64, _>(0))
}

pub fn check_user(connection: &Connection, id: i64, name: &str, age: f64, photo: Vec<u8>) {
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::<i64, _>(0)), id);
assert_eq!(ok!(statement.read::<String, _>(1)), name);
assert_eq!(ok!(statement.read::<f64, _>(2)), age);
assert_eq!(ok!(statement.read::<Vec<u8>, _>(3)), photo);
assert_eq!(ok!(statement.read::<Value, _>(4)), Value::Null);
}
9 changes: 8 additions & 1 deletion tests/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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))));
Expand All @@ -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(
Expand Down

0 comments on commit bf0dde1

Please sign in to comment.