rust-postgres/src/test.rs

70 lines
2.0 KiB
Rust
Raw Normal View History

2013-07-25 07:10:18 +00:00
extern mod postgres;
2013-08-25 03:47:36 +00:00
use postgres::{PostgresConnection, ToSql};
2013-08-18 03:30:31 +00:00
2013-08-04 05:21:16 +00:00
#[test]
2013-08-23 05:24:14 +00:00
fn test_basic() {
2013-08-23 02:47:06 +00:00
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
2013-08-23 05:24:14 +00:00
do conn.in_transaction |conn| {
2013-08-25 03:47:36 +00:00
conn.prepare("CREATE TABLE foo (id BIGINT PRIMARY KEY)").update([]);
2013-08-23 05:24:14 +00:00
Err::<(), ()>(())
};
}
2013-08-23 07:13:42 +00:00
#[test]
fn test_query() {
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
do conn.in_transaction |conn| {
2013-08-25 03:47:36 +00:00
conn.prepare("CREATE TABLE foo (id BIGINT PRIMARY KEY)").update([]);
conn.prepare("INSERT INTO foo (id) VALUES ($1), ($2)")
.update([&1 as &ToSql, &2 as &ToSql]);
2013-08-23 07:13:42 +00:00
let stmt = conn.prepare("SELECT * from foo ORDER BY id");
2013-08-25 03:47:36 +00:00
let result = stmt.query([]);
2013-08-23 07:13:42 +00:00
assert_eq!(~[1, 2], result.iter().map(|row| { row[0] }).collect());
Err::<(), ()>(())
};
}
2013-08-25 03:47:36 +00:00
#[test]
fn test_nulls() {
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
do conn.in_transaction |conn| {
conn.prepare("CREATE TABLE foo (
id BIGINT PRIMARY KEY,
val VARCHAR
)").update([]);
2013-08-25 03:47:36 +00:00
conn.prepare("INSERT INTO foo (id, val) VALUES ($1, $2), ($3, $4)")
.update([&1 as &ToSql, & &"foobar" as &ToSql,
&2 as &ToSql, &None::<~str> as &ToSql]);
let stmt = conn.prepare("SELECT id, val FROM foo ORDER BY id");
let result = stmt.query([]);
assert_eq!(~[Some(~"foobar"), None],
result.iter().map(|row| { row[1] }).collect());
Err::<(), ()>(())
};
}
#[test]
fn test_plaintext_pass() {
PostgresConnection::connect("postgres://pass_user:password@127.0.0.1:5432");
}
#[test]
#[should_fail]
fn test_plaintext_pass_no_pass() {
PostgresConnection::connect("postgres://pass_user@127.0.0.1:5432");
}
#[test]
fn test_md5_pass() {
PostgresConnection::connect("postgres://md5_user:password@127.0.0.1:5432");
}