Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify INSERT statements in the tests #83

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading