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-18 02:42:12 +00:00
|
|
|
|
2013-08-27 05:40:23 +00:00
|
|
|
do conn.in_transaction |trans| {
|
|
|
|
trans.prepare("CREATE TABLE foo (id BIGINT PRIMARY KEY)").update([]);
|
2013-08-23 05:24:14 +00:00
|
|
|
|
2013-08-27 05:40:23 +00:00
|
|
|
trans.set_rollback();
|
2013-08-23 05:24:14 +00:00
|
|
|
};
|
2013-08-18 02:42:12 +00:00
|
|
|
}
|
2013-08-23 07:13:42 +00:00
|
|
|
|
2013-08-27 05:06:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_prepare_err() {
|
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
|
|
|
|
assert!(conn.try_prepare("invalid sql statment").is_err());
|
|
|
|
}
|
|
|
|
|
2013-08-23 07:13:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_query() {
|
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
|
|
|
|
|
2013-08-27 05:40:23 +00:00
|
|
|
do conn.in_transaction |trans| {
|
|
|
|
trans.prepare("CREATE TABLE foo (id BIGINT PRIMARY KEY)").update([]);
|
|
|
|
trans.prepare("INSERT INTO foo (id) VALUES ($1), ($2)")
|
2013-08-25 03:47:36 +00:00
|
|
|
.update([&1 as &ToSql, &2 as &ToSql]);
|
2013-08-27 05:40:23 +00:00
|
|
|
let stmt = trans.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());
|
|
|
|
|
2013-08-27 05:40:23 +00:00
|
|
|
trans.set_rollback();
|
2013-08-23 07:13:42 +00:00
|
|
|
};
|
|
|
|
}
|
2013-08-25 03:47:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nulls() {
|
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@127.0.0.1:5432");
|
|
|
|
|
2013-08-27 05:40:23 +00:00
|
|
|
do conn.in_transaction |trans| {
|
2013-08-25 03:47:36 +00:00
|
|
|
conn.prepare("CREATE TABLE foo (
|
|
|
|
id BIGINT PRIMARY KEY,
|
|
|
|
val VARCHAR
|
2013-08-26 05:08:37 +00:00
|
|
|
)").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());
|
|
|
|
|
2013-08-27 05:40:23 +00:00
|
|
|
trans.set_rollback();
|
2013-08-25 03:47:36 +00:00
|
|
|
};
|
|
|
|
}
|
2013-08-26 05:08:37 +00:00
|
|
|
|
|
|
|
#[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");
|
|
|
|
}
|
|
|
|
|
2013-08-26 07:36:09 +00:00
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_plaintext_pass_wrong_pass() {
|
|
|
|
PostgresConnection::connect("postgres://pass_user:asdf@127.0.0.1:5432");
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:08:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_md5_pass() {
|
|
|
|
PostgresConnection::connect("postgres://md5_user:password@127.0.0.1:5432");
|
|
|
|
}
|
2013-08-26 07:36:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_md5_pass_no_pass() {
|
|
|
|
PostgresConnection::connect("postgres://md5_user@127.0.0.1:5432");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_md5_pass_wrong_pass() {
|
|
|
|
PostgresConnection::connect("postgres://md5_user:asdf@127.0.0.1:5432");
|
|
|
|
}
|