rust-postgres/src/postgres/test.rs

74 lines
2.0 KiB
Rust
Raw Normal View History

2013-07-25 07:10:18 +00:00
extern mod postgres;
use postgres::{PostgresConnection, ToSql};
macro_rules! params(
($($e:expr),+) => (
[$(
$e as &ToSql
),+]
)
)
2013-08-04 02:17:32 +00:00
macro_rules! chk(
($e:expr) => (
match $e {
Ok(ok) => ok,
Err(err) => fail!(err)
}
)
)
2013-07-25 07:10:18 +00:00
#[test]
2013-08-04 05:21:16 +00:00
fn test_basic() {
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
do conn.in_transaction |conn| {
chk!(conn.update("CREATE TABLE basic (id INT PRIMARY KEY)", []));
chk!(conn.update("INSERT INTO basic (id) VALUES (101)", []));
let res = chk!(conn.query("SELECT id from basic WHERE id = 101", []));
assert_eq!(1, res.len());
2013-08-04 06:52:14 +00:00
assert_eq!(1, res.get(0).len());
assert_eq!(1, res.get(0).len());
assert_eq!(Some(101), res.get(0)[0]);
2013-08-04 05:21:16 +00:00
Err::<(), ~str>(~"")
};
}
#[test]
fn test_params() {
2013-08-04 02:17:32 +00:00
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
2013-08-04 05:21:16 +00:00
do conn.in_transaction |conn| {
chk!(conn.update("CREATE TABLE basic (id INT PRIMARY KEY)", []));
chk!(conn.update("INSERT INTO basic (id) VALUES ($1)",
params!(&101)));
2013-08-04 05:21:16 +00:00
let res = chk!(conn.query("SELECT id from basic WHERE id = $1",
params!(&101)));
2013-08-04 06:52:14 +00:00
assert_eq!(Some(101), res.get(0)[0]);
2013-08-04 02:17:32 +00:00
2013-08-04 05:21:16 +00:00
Err::<(), ~str>(~"")
};
2013-07-25 07:10:18 +00:00
}
2013-08-05 00:48:48 +00:00
#[test]
fn test_null() {
let conn = chk!(PostgresConnection::new("postgres://postgres@localhost"));
do conn.in_transaction |conn| {
chk!(conn.update("CREATE TABLE basic (id INT PRIMARY KEY, foo INT)",
[]));
chk!(conn.update("INSERT INTO basic (id, foo) VALUES ($1, $2), ($3, $4)",
params!(&101, &None::<int>, &102, &0)));
2013-08-05 00:48:48 +00:00
let res = chk!(conn.query("SELECT foo from basic ORDER BY id", []));
assert_eq!(None, res.get(0).get::<Option<int>>(0));
assert_eq!(Some(0), res.get(1).get::<Option<int>>(0));
Err::<(), ~str>(~"")
};
}