48 lines
1.3 KiB
Rust
Raw Normal View History

2015-05-28 19:19:08 -04:00
extern crate sqlite;
extern crate temporary;
use std::path::PathBuf;
use temporary::Directory;
macro_rules! ok(
($result:expr) => ($result.unwrap());
);
#[test]
2015-05-29 11:24:01 -04:00
fn workflow() {
use sqlite::Binding::*;
use sqlite::ResultCode;
2015-05-28 22:49:00 -04:00
macro_rules! pair(
($one:expr, $two:expr) => ((String::from($one), String::from($two)));
);
2015-05-28 19:19:08 -04:00
let (path, _directory) = setup();
2015-05-28 21:30:02 -04:00
let mut database = ok!(sqlite::open(&path));
let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#;
2015-05-29 08:58:19 -04:00
ok!(database.execute(sql, None));
2015-05-28 21:30:02 -04:00
2015-05-29 11:24:01 -04:00
let sql = r#"INSERT INTO `users` (id, name, age) VALUES (?, ?, ?);"#;
let mut statement = ok!(database.statement(sql));
ok!(statement.bind(&[Integer(1, 1), Text(2, "Alice"), Float(3, 20.99)]));
assert!(statement.step() == ResultCode::Done);
2015-05-28 21:30:02 -04:00
2015-05-28 22:49:00 -04:00
let mut done = false;
2015-05-28 21:30:02 -04:00
let sql = r#"SELECT * FROM `users`;"#;
2015-05-29 08:58:19 -04:00
ok!(database.execute(sql, Some(&mut |pairs: Vec<(String, String)>| -> bool {
2015-05-28 22:49:00 -04:00
assert!(pairs.len() == 3);
assert!(pairs[0] == pair!("id", "1"));
assert!(pairs[1] == pair!("name", "Alice"));
assert!(pairs[2] == pair!("age", "20.99"));
done = true;
2015-05-28 21:30:02 -04:00
true
})));
2015-05-28 22:49:00 -04:00
assert!(done);
2015-05-28 19:19:08 -04:00
}
fn setup() -> (PathBuf, Directory) {
let directory = ok!(Directory::new("sqlite"));
(directory.path().join("database.sqlite3"), directory)
}