2013-09-29 04:19:27 +00:00
|
|
|
use extra::comm::DuplexStream;
|
|
|
|
use extra::future::Future;
|
2013-09-08 20:27:15 +00:00
|
|
|
use extra::time;
|
|
|
|
use extra::time::Timespec;
|
2013-09-02 20:54:02 +00:00
|
|
|
use extra::json;
|
|
|
|
use extra::uuid::Uuid;
|
2013-12-29 02:10:05 +00:00
|
|
|
use openssl::ssl::{SslContext, Sslv3};
|
2013-08-31 23:11:42 +00:00
|
|
|
use std::f32;
|
|
|
|
use std::f64;
|
2013-12-05 05:20:48 +00:00
|
|
|
use std::hashmap::HashMap;
|
2013-11-13 05:33:52 +00:00
|
|
|
use std::io::timer;
|
2013-08-31 23:11:42 +00:00
|
|
|
|
2014-01-14 05:04:19 +00:00
|
|
|
use {PostgresNoticeHandler,
|
|
|
|
PostgresNotification,
|
|
|
|
PostgresConnection,
|
|
|
|
PostgresStatement,
|
|
|
|
ResultDescription,
|
|
|
|
RequireSsl,
|
|
|
|
PreferSsl,
|
|
|
|
NoSsl};
|
|
|
|
use error::{DbError,
|
|
|
|
DnsError,
|
|
|
|
MissingPassword,
|
|
|
|
Position,
|
|
|
|
PostgresDbError,
|
|
|
|
SyntaxError,
|
|
|
|
InvalidPassword,
|
|
|
|
QueryCanceled,
|
|
|
|
InvalidCatalogName};
|
|
|
|
use types::{ToSql, FromSql, PgInt4, PgVarchar};
|
|
|
|
use types::array::{ArrayBase};
|
|
|
|
use types::range::{Range, Inclusive, Exclusive, RangeBound};
|
|
|
|
use pool::PostgresConnectionPool;
|
2013-09-29 04:19:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
// Make sure we can take both connections at once and can still get one after
|
|
|
|
fn test_pool() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let pool = PostgresConnectionPool::new("postgres://postgres@localhost",
|
|
|
|
NoSsl, 2);
|
2013-09-29 04:19:27 +00:00
|
|
|
|
2013-12-18 07:54:59 +00:00
|
|
|
let (stream1, stream2) = DuplexStream::<(), ()>::new();
|
2013-09-29 04:19:27 +00:00
|
|
|
|
2013-11-26 04:50:09 +00:00
|
|
|
let pool1 = pool.clone();
|
|
|
|
let mut fut1 = do Future::spawn {
|
|
|
|
let _conn = pool1.get_connection();
|
2013-09-29 04:19:27 +00:00
|
|
|
stream1.send(());
|
|
|
|
stream1.recv();
|
|
|
|
};
|
|
|
|
|
2013-11-26 04:50:09 +00:00
|
|
|
let pool2 = pool.clone();
|
|
|
|
let mut fut2 = do Future::spawn {
|
|
|
|
let _conn = pool2.get_connection();
|
2013-09-29 04:19:27 +00:00
|
|
|
stream2.send(());
|
|
|
|
stream2.recv();
|
|
|
|
};
|
|
|
|
|
|
|
|
fut1.get();
|
|
|
|
fut2.get();
|
|
|
|
|
|
|
|
pool.get_connection();
|
|
|
|
}
|
2013-08-18 03:30:31 +00:00
|
|
|
|
2013-10-05 03:26:52 +00:00
|
|
|
#[test]
|
|
|
|
fn test_non_default_database() {
|
2013-11-11 02:13:32 +00:00
|
|
|
PostgresConnection::connect("postgres://postgres@localhost/postgres", &NoSsl);
|
2013-10-05 03:26:52 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 05:01:40 +00:00
|
|
|
#[test]
|
|
|
|
fn test_url_terminating_slash() {
|
2013-11-11 02:13:32 +00:00
|
|
|
PostgresConnection::connect("postgres://postgres@localhost/", &NoSsl);
|
2013-10-25 05:01:40 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 05:06:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_prepare_err() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-08-29 04:24:43 +00:00
|
|
|
match conn.try_prepare("invalid sql statment") {
|
2013-11-29 18:30:03 +00:00
|
|
|
Err(PostgresDbError { code: SyntaxError, position: Some(Position(1)), .. }) => (),
|
2013-10-19 17:13:39 +00:00
|
|
|
resp => fail!("Unexpected result {:?}", resp)
|
2013-08-29 04:24:43 +00:00
|
|
|
}
|
2013-08-27 05:06:53 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 05:01:40 +00:00
|
|
|
#[test]
|
|
|
|
fn test_unknown_database() {
|
2013-11-11 02:13:32 +00:00
|
|
|
match PostgresConnection::try_connect("postgres://postgres@localhost/asdf", &NoSsl) {
|
2013-11-29 18:30:03 +00:00
|
|
|
Err(DbError(PostgresDbError { code: InvalidCatalogName, .. })) => {}
|
2013-10-25 05:01:40 +00:00
|
|
|
resp => fail!("Unexpected result {:?}", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 07:00:43 +00:00
|
|
|
#[test]
|
2013-09-04 03:50:02 +00:00
|
|
|
fn test_transaction_commit() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
|
2013-09-04 03:50:02 +00:00
|
|
|
|
2013-12-05 06:24:49 +00:00
|
|
|
let trans = conn.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans.execute("INSERT INTO foo (id) VALUES ($1)", [&1i32 as &ToSql]);
|
2013-12-05 06:24:49 +00:00
|
|
|
drop(trans);
|
2013-09-03 07:00:43 +00:00
|
|
|
|
2013-09-04 03:50:02 +00:00
|
|
|
let stmt = conn.prepare("SELECT * FROM foo");
|
|
|
|
let result = stmt.query([]);
|
2013-09-03 07:00:43 +00:00
|
|
|
|
2013-12-05 06:24:49 +00:00
|
|
|
assert_eq!(~[1i32], result.map(|row| row[1]).collect());
|
2013-09-03 07:00:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 03:50:02 +00:00
|
|
|
#[test]
|
|
|
|
fn test_transaction_rollback() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
|
2013-08-23 07:13:42 +00:00
|
|
|
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("INSERT INTO foo (id) VALUES ($1)", [&1i32 as &ToSql]);
|
2013-12-05 06:24:49 +00:00
|
|
|
|
|
|
|
let trans = conn.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans.execute("INSERT INTO foo (id) VALUES ($1)", [&2i32 as &ToSql]);
|
2013-12-05 06:24:49 +00:00
|
|
|
trans.set_rollback();
|
|
|
|
drop(trans);
|
2013-08-31 23:11:42 +00:00
|
|
|
|
2013-09-04 03:50:02 +00:00
|
|
|
let stmt = conn.prepare("SELECT * FROM foo");
|
|
|
|
let result = stmt.query([]);
|
|
|
|
|
2013-12-03 03:53:40 +00:00
|
|
|
assert_eq!(~[1i32], result.map(|row| row[1]).collect());
|
2013-08-23 07:13:42 +00:00
|
|
|
}
|
2013-08-25 03:47:36 +00:00
|
|
|
|
2013-09-05 06:28:44 +00:00
|
|
|
#[test]
|
|
|
|
fn test_nested_transactions() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
|
2013-09-05 06:28:44 +00:00
|
|
|
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("INSERT INTO foo (id) VALUES (1)", []);
|
2013-09-05 06:28:44 +00:00
|
|
|
|
2013-10-14 01:58:31 +00:00
|
|
|
{
|
|
|
|
let trans1 = conn.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans1.execute("INSERT INTO foo (id) VALUES (2)", []);
|
2013-09-05 06:28:44 +00:00
|
|
|
|
2013-10-14 01:58:31 +00:00
|
|
|
{
|
|
|
|
let trans2 = trans1.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans2.execute("INSERT INTO foo (id) VALUES (3)", []);
|
2013-09-05 06:28:44 +00:00
|
|
|
trans2.set_rollback();
|
|
|
|
}
|
|
|
|
|
2013-10-14 01:58:31 +00:00
|
|
|
{
|
|
|
|
let trans2 = trans1.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans2.execute("INSERT INTO foo (id) VALUES (4)", []);
|
2013-09-05 06:28:44 +00:00
|
|
|
|
2013-10-14 01:58:31 +00:00
|
|
|
{
|
|
|
|
let trans3 = trans2.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans3.execute("INSERT INTO foo (id) VALUES (5)", []);
|
2013-09-05 06:28:44 +00:00
|
|
|
trans3.set_rollback();
|
|
|
|
}
|
|
|
|
|
2013-10-14 01:58:31 +00:00
|
|
|
{
|
|
|
|
let trans3 = trans2.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans3.execute("INSERT INTO foo (id) VALUES (6)", []);
|
2013-09-05 06:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
|
|
|
|
let result = stmt.query([]);
|
|
|
|
|
2013-12-03 03:53:40 +00:00
|
|
|
assert_eq!(~[1i32, 2, 4, 6], result.map(|row| row[1]).collect());
|
2013-09-05 06:28:44 +00:00
|
|
|
|
|
|
|
trans1.set_rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
|
|
|
|
let result = stmt.query([]);
|
|
|
|
|
2013-12-03 03:53:40 +00:00
|
|
|
assert_eq!(~[1i32], result.map(|row| row[1]).collect());
|
2013-09-05 06:28:44 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 03:47:36 +00:00
|
|
|
#[test]
|
2013-09-04 03:50:02 +00:00
|
|
|
fn test_query() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", []);
|
|
|
|
conn.execute("INSERT INTO foo (id) VALUES ($1), ($2)",
|
2013-09-04 03:50:02 +00:00
|
|
|
[&1i64 as &ToSql, &2i64 as &ToSql]);
|
|
|
|
let stmt = conn.prepare("SELECT * from foo ORDER BY id");
|
|
|
|
let result = stmt.query([]);
|
2013-08-25 03:47:36 +00:00
|
|
|
|
2013-12-03 03:53:40 +00:00
|
|
|
assert_eq!(~[1i64, 2], result.map(|row| row[1]).collect());
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 05:20:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_lazy_query() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-09-04 05:20:21 +00:00
|
|
|
|
2013-10-14 01:58:31 +00:00
|
|
|
{
|
|
|
|
let trans = conn.transaction();
|
2013-12-29 03:24:52 +00:00
|
|
|
trans.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", []);
|
2013-09-04 05:20:21 +00:00
|
|
|
let stmt = trans.prepare("INSERT INTO foo (id) VALUES ($1)");
|
|
|
|
let values = ~[0i32, 1, 2, 3, 4, 5];
|
|
|
|
for value in values.iter() {
|
2013-12-29 03:24:52 +00:00
|
|
|
stmt.execute([value as &ToSql]);
|
2013-09-04 05:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let stmt = trans.prepare("SELECT id FROM foo ORDER BY id");
|
|
|
|
let result = stmt.lazy_query(2, []);
|
2013-12-03 03:53:40 +00:00
|
|
|
assert_eq!(values, result.map(|row| row[1]).collect());
|
2013-09-04 05:20:21 +00:00
|
|
|
|
|
|
|
trans.set_rollback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 04:51:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_param_types() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-09-05 04:51:21 +00:00
|
|
|
let stmt = conn.prepare("SELECT $1::INT, $2::VARCHAR");
|
|
|
|
assert_eq!(stmt.param_types(), [PgInt4, PgVarchar]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_result_descriptions() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-09-05 04:51:21 +00:00
|
|
|
let stmt = conn.prepare("SELECT 1::INT as a, 'hi'::VARCHAR as b");
|
|
|
|
assert_eq!(stmt.result_descriptions(),
|
|
|
|
[ResultDescription { name: ~"a", ty: PgInt4},
|
|
|
|
ResultDescription { name: ~"b", ty: PgVarchar}]);
|
|
|
|
}
|
|
|
|
|
2014-01-02 05:54:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_execute_counts() {
|
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
|
|
|
assert_eq!(0, conn.execute("CREATE TEMPORARY TABLE foo (
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
b INT
|
|
|
|
)", []));
|
|
|
|
assert_eq!(3, conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($2)",
|
|
|
|
[&1i32 as &ToSql, &2i32 as &ToSql]));
|
|
|
|
assert_eq!(2, conn.execute("UPDATE foo SET b = 0 WHERE b = 2", []));
|
|
|
|
assert_eq!(3, conn.execute("SELECT * FROM foo", []));
|
|
|
|
}
|
|
|
|
|
2013-12-06 05:51:09 +00:00
|
|
|
fn test_type<T: Eq+FromSql+ToSql, S: Str>(sql_type: &str, checks: &[(T, S)]) {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-09-08 20:27:15 +00:00
|
|
|
for &(ref val, ref repr) in checks.iter() {
|
2013-12-06 05:51:09 +00:00
|
|
|
let stmt = conn.prepare(format!("SELECT {:s}::{}", *repr, sql_type));
|
2013-12-03 03:53:40 +00:00
|
|
|
let result = stmt.query([]).next().unwrap()[1];
|
2013-09-08 20:27:15 +00:00
|
|
|
assert_eq!(val, &result);
|
|
|
|
|
|
|
|
let stmt = conn.prepare("SELECT $1::" + sql_type);
|
2013-12-03 03:53:40 +00:00
|
|
|
let result = stmt.query([val as &ToSql]).next().unwrap()[1];
|
2013-09-08 20:27:15 +00:00
|
|
|
assert_eq!(val, &result);
|
2013-09-04 03:50:02 +00:00
|
|
|
}
|
2013-08-25 03:47:36 +00:00
|
|
|
}
|
2013-08-26 05:08:37 +00:00
|
|
|
|
2013-08-30 06:28:46 +00:00
|
|
|
#[test]
|
2013-09-02 19:42:24 +00:00
|
|
|
fn test_bool_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("BOOL", [(Some(true), "'t'"), (Some(false), "'f'"),
|
|
|
|
(None, "NULL")]);
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
2013-08-30 06:28:46 +00:00
|
|
|
|
2013-09-02 22:14:22 +00:00
|
|
|
#[test]
|
|
|
|
fn test_i8_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("\"char\"", [(Some('a' as i8), "'a'"), (None, "NULL")]);
|
2013-09-02 22:14:22 +00:00
|
|
|
}
|
|
|
|
|
2013-08-31 23:11:42 +00:00
|
|
|
#[test]
|
2013-09-02 19:42:24 +00:00
|
|
|
fn test_i16_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("SMALLINT", [(Some(15001i16), "15001"),
|
|
|
|
(Some(-15001i16), "-15001"), (None, "NULL")]);
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-09-02 19:42:24 +00:00
|
|
|
fn test_i32_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("INT", [(Some(2147483548i32), "2147483548"),
|
|
|
|
(Some(-2147483548i32), "-2147483548"), (None, "NULL")]);
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-09-02 19:42:24 +00:00
|
|
|
fn test_i64_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("BIGINT", [(Some(9223372036854775708i64), "9223372036854775708"),
|
|
|
|
(Some(-9223372036854775708i64), "-9223372036854775708"),
|
|
|
|
(None, "NULL")]);
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-09-02 19:42:24 +00:00
|
|
|
fn test_f32_params() {
|
2013-10-30 02:42:05 +00:00
|
|
|
test_type("REAL", [(Some(f32::INFINITY), "'infinity'"),
|
|
|
|
(Some(f32::NEG_INFINITY), "'-infinity'"),
|
2013-09-08 20:27:15 +00:00
|
|
|
(Some(1000.55), "1000.55"), (None, "NULL")]);
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-09-02 19:42:24 +00:00
|
|
|
fn test_f64_params() {
|
2013-10-30 02:42:05 +00:00
|
|
|
test_type("DOUBLE PRECISION", [(Some(f64::INFINITY), "'infinity'"),
|
|
|
|
(Some(f64::NEG_INFINITY), "'-infinity'"),
|
2013-09-08 20:27:15 +00:00
|
|
|
(Some(10000.55), "10000.55"),
|
|
|
|
(None, "NULL")]);
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:42:24 +00:00
|
|
|
#[test]
|
|
|
|
fn test_varchar_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("VARCHAR", [(Some(~"hello world"), "'hello world'"),
|
|
|
|
(Some(~"イロハニホヘト チリヌルヲ"), "'イロハニホヘト チリヌルヲ'"),
|
|
|
|
(None, "NULL")]);
|
2013-09-02 19:42:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 20:07:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_text_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("TEXT", [(Some(~"hello world"), "'hello world'"),
|
|
|
|
(Some(~"イロハニホヘト チリヌルヲ"), "'イロハニホヘト チリヌルヲ'"),
|
|
|
|
(None, "NULL")]);
|
2013-09-02 20:07:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 21:52:23 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bpchar_params() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("CREATE TEMPORARY TABLE foo (
|
2013-09-04 03:50:02 +00:00
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
b CHAR(5)
|
|
|
|
)", []);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($3)",
|
2013-09-04 03:50:02 +00:00
|
|
|
[&Some("12345") as &ToSql, &Some("123") as &ToSql,
|
|
|
|
&None::<~str> as &ToSql]);
|
|
|
|
let stmt = conn.prepare("SELECT b FROM foo ORDER BY id");
|
|
|
|
let res = stmt.query([]);
|
|
|
|
|
|
|
|
assert_eq!(~[Some(~"12345"), Some(~"123 "), None],
|
2013-12-03 03:53:40 +00:00
|
|
|
res.map(|row| row[1]).collect());
|
2013-09-02 21:52:23 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:42:24 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bytea_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("BYTEA", [(Some(~[0u8, 1, 2, 3, 254, 255]), "'\\x00010203feff'"),
|
|
|
|
(None, "NULL")]);
|
2013-09-02 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("JSON", [(Some(json::from_str("[10, 11, 12]").unwrap()),
|
|
|
|
"'[10, 11, 12]'"),
|
|
|
|
(Some(json::from_str("{\"f\": \"asd\"}").unwrap()),
|
|
|
|
"'{\"f\": \"asd\"}'"),
|
|
|
|
(None, "NULL")])
|
2013-09-02 20:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_uuid_params() {
|
2013-09-08 20:27:15 +00:00
|
|
|
test_type("UUID", [(Some(Uuid::parse_string("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11").unwrap()),
|
|
|
|
"'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'"),
|
|
|
|
(None, "NULL")])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tm_params() {
|
|
|
|
fn make_check<'a>(time: &'a str) -> (Option<Timespec>, &'a str) {
|
|
|
|
(Some(time::strptime(time, "'%Y-%m-%d %H:%M:%S.%f'").unwrap().to_timespec()), time)
|
|
|
|
}
|
|
|
|
test_type("TIMESTAMP",
|
|
|
|
[make_check("'1970-01-01 00:00:00.01'"),
|
|
|
|
make_check("'1965-09-25 11:19:33.100314'"),
|
|
|
|
make_check("'2010-02-09 23:11:45.1202'"),
|
2013-09-09 04:33:41 +00:00
|
|
|
(None, "NULL")]);
|
|
|
|
test_type("TIMESTAMP WITH TIME ZONE",
|
|
|
|
[make_check("'1970-01-01 00:00:00.01'"),
|
|
|
|
make_check("'1965-09-25 11:19:33.100314'"),
|
|
|
|
make_check("'2010-02-09 23:11:45.1202'"),
|
2013-09-08 20:27:15 +00:00
|
|
|
(None, "NULL")]);
|
2013-09-02 19:42:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-31 02:55:25 +00:00
|
|
|
macro_rules! test_range(
|
|
|
|
($name:expr, $t:ty, $low:expr, $low_str:expr, $high:expr, $high_str:expr) => ({
|
2014-01-02 06:22:10 +00:00
|
|
|
let tests = [(Some(range!('(', ')')), ~"'(,)'"),
|
|
|
|
(Some(range!('[' $low, ')')), "'[" + $low_str + ",)'"),
|
|
|
|
(Some(range!('(' $low, ')')), "'(" + $low_str + ",)'"),
|
|
|
|
(Some(range!('(', $high ']')), "'(," + $high_str + "]'"),
|
|
|
|
(Some(range!('(', $high ')')), "'(," + $high_str + ")'"),
|
|
|
|
(Some(range!('[' $low, $high ']')),
|
2013-10-31 02:55:25 +00:00
|
|
|
"'[" + $low_str + "," + $high_str + "]'"),
|
2014-01-02 06:22:10 +00:00
|
|
|
(Some(range!('[' $low, $high ')')),
|
2013-10-31 02:55:25 +00:00
|
|
|
"'[" + $low_str + "," + $high_str + ")'"),
|
2014-01-02 06:22:10 +00:00
|
|
|
(Some(range!('(' $low, $high ']')),
|
2013-10-31 02:55:25 +00:00
|
|
|
"'(" + $low_str + "," + $high_str + "]'"),
|
2014-01-02 06:22:10 +00:00
|
|
|
(Some(range!('(' $low, $high ')')),
|
2013-10-31 02:55:25 +00:00
|
|
|
"'(" + $low_str + "," + $high_str + ")'"),
|
2014-01-02 06:22:10 +00:00
|
|
|
(Some(range!(empty)), ~"'empty'"),
|
2013-10-31 07:04:45 +00:00
|
|
|
(None, ~"NULL")];
|
2013-12-06 05:51:09 +00:00
|
|
|
test_type($name, tests);
|
2013-10-31 02:55:25 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2013-10-29 06:55:11 +00:00
|
|
|
#[test]
|
|
|
|
fn test_int4range_params() {
|
2013-10-31 02:55:25 +00:00
|
|
|
test_range!("INT4RANGE", i32, 100i32, "100", 200i32, "200")
|
2013-10-29 06:55:11 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 03:18:27 +00:00
|
|
|
#[test]
|
|
|
|
fn test_int8range_params() {
|
2013-10-31 02:55:25 +00:00
|
|
|
test_range!("INT8RANGE", i64, 100i64, "100", 200i64, "200")
|
|
|
|
}
|
|
|
|
|
2013-10-31 05:35:49 +00:00
|
|
|
fn test_timespec_range_params(sql_type: &str) {
|
2013-10-31 02:55:25 +00:00
|
|
|
fn t(time: &str) -> Timespec {
|
|
|
|
time::strptime(time, "%Y-%m-%d").unwrap().to_timespec()
|
|
|
|
}
|
|
|
|
let low = "1970-01-01";
|
|
|
|
let high = "1980-01-01";
|
2013-10-31 05:35:49 +00:00
|
|
|
test_range!(sql_type, Timespec, t(low), low, t(high), high);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tsrange_params() {
|
|
|
|
test_timespec_range_params("TSRANGE");
|
2013-10-31 02:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tstzrange_params() {
|
2013-10-31 05:35:49 +00:00
|
|
|
test_timespec_range_params("TSTZRANGE");
|
2013-10-30 03:18:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-06 05:51:09 +00:00
|
|
|
macro_rules! test_array_params(
|
|
|
|
($name:expr, $v1:expr, $s1:expr, $v2:expr, $s2:expr, $v3:expr, $s3:expr) => ({
|
|
|
|
let tests = [(Some(ArrayBase::from_vec(~[Some($v1), Some($v2), None], 1)),
|
|
|
|
"'{" + $s1 + "," + $s2 + ",NULL}'"),
|
|
|
|
(None, ~"NULL")];
|
|
|
|
test_type($name + "[]", tests);
|
|
|
|
let mut a = ArrayBase::from_vec(~[Some($v1), Some($v2)], 0);
|
|
|
|
a.wrap(-1);
|
|
|
|
a.push_move(ArrayBase::from_vec(~[None, Some($v3)], 0));
|
|
|
|
let tests = [(Some(a), "'[-1:0][0:1]={{" + $s1 + "," + $s2 + "},{NULL," + $s3 + "}}'")];
|
|
|
|
test_type($name + "[][]", tests);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2013-12-02 05:09:31 +00:00
|
|
|
#[test]
|
2013-12-08 02:58:40 +00:00
|
|
|
fn test_boolarray_params() {
|
|
|
|
test_array_params!("BOOL", false, "f", true, "t", true, "t");
|
2013-12-02 05:09:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 06:32:54 +00:00
|
|
|
#[test]
|
2013-12-08 02:58:40 +00:00
|
|
|
fn test_byteaarray_params() {
|
|
|
|
test_array_params!("BYTEA", ~[0u8, 1], r#""\\x0001""#, ~[254u8, 255u8],
|
|
|
|
r#""\\xfeff""#, ~[10u8, 11u8], r#""\\x0a0b""#);
|
2013-12-06 05:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-12-08 02:58:40 +00:00
|
|
|
fn test_chararray_params() {
|
|
|
|
test_array_params!("\"char\"", 'a' as i8, "a", 'z' as i8, "z",
|
|
|
|
'0' as i8, "0");
|
2013-12-04 06:32:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-06 05:58:22 +00:00
|
|
|
#[test]
|
2013-12-08 02:58:40 +00:00
|
|
|
fn test_int2array_params() {
|
|
|
|
test_array_params!("INT2", 0i16, "0", 1i16, "1", 2i16, "2");
|
2013-12-06 05:58:22 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 00:45:54 +00:00
|
|
|
#[test]
|
2013-12-08 02:58:40 +00:00
|
|
|
fn test_int4array_params() {
|
|
|
|
test_array_params!("INT4", 0i32, "0", 1i32, "1", 2i32, "2");
|
2013-12-08 00:45:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 03:13:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_textarray_params() {
|
|
|
|
test_array_params!("TEXT", ~"hello", "hello", ~"world", "world", ~"!", "!");
|
|
|
|
}
|
|
|
|
|
2013-12-08 19:44:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_charnarray_params() {
|
|
|
|
test_array_params!("CHAR(5)", ~"hello", "hello", ~"world", "world",
|
|
|
|
~"! ", "!");
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:58:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_varchararray_params() {
|
|
|
|
test_array_params!("VARCHAR", ~"hello", "hello", ~"world", "world", ~"!", "!");
|
|
|
|
}
|
|
|
|
|
2013-12-08 02:38:53 +00:00
|
|
|
#[test]
|
2013-12-08 02:58:40 +00:00
|
|
|
fn test_int8array_params() {
|
|
|
|
test_array_params!("INT8", 0i64, "0", 1i64, "1", 2i64, "2");
|
2013-12-08 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 22:04:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_timestamparray_params() {
|
|
|
|
fn make_check<'a>(time: &'a str) -> (Timespec, &'a str) {
|
|
|
|
(time::strptime(time, "%Y-%m-%d %H:%M:%S.%f").unwrap().to_timespec(), time)
|
|
|
|
}
|
|
|
|
let (v1, s1) = make_check("1970-01-01 00:00:00.01");
|
|
|
|
let (v2, s2) = make_check("1965-09-25 11:19:33.100314");
|
|
|
|
let (v3, s3) = make_check("2010-02-09 23:11:45.1202");
|
|
|
|
test_array_params!("TIMESTAMP", v1, s1, v2, s2, v3, s3);
|
2013-12-08 22:08:35 +00:00
|
|
|
test_array_params!("TIMESTAMPTZ", v1, s1, v2, s2, v3, s3);
|
2013-12-08 22:04:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 02:49:55 +00:00
|
|
|
#[test]
|
2013-12-08 02:58:40 +00:00
|
|
|
fn test_float4array_params() {
|
|
|
|
test_array_params!("FLOAT4", 0f32, "0", 1.5f32, "1.5", 0.009f32, ".009");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_float8array_params() {
|
|
|
|
test_array_params!("FLOAT8", 0f64, "0", 1.5f64, "1.5", 0.009f64, ".009");
|
2013-12-08 02:49:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 22:16:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_uuidarray_params() {
|
|
|
|
fn make_check<'a>(uuid: &'a str) -> (Uuid, &'a str) {
|
|
|
|
(Uuid::parse_string(uuid).unwrap(), uuid)
|
|
|
|
}
|
|
|
|
let (v1, s1) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
|
|
let (v2, s2) = make_check("00000000-0000-0000-0000-000000000000");
|
|
|
|
let (v3, s3) = make_check("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
|
|
|
|
test_array_params!("UUID", v1, s1, v2, s2, v3, s3);
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:37:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_int4rangearray_params() {
|
|
|
|
test_array_params!("INT4RANGE",
|
|
|
|
Range::new(None, None), "\"(,)\"",
|
|
|
|
Range::new(Some(RangeBound::new(10i32, Inclusive)), None), "\"[10,)\"",
|
|
|
|
Range::new(None, Some(RangeBound::new(10i32, Exclusive))), "\"(,10)\"");
|
|
|
|
}
|
|
|
|
|
2013-12-08 23:02:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tsrangearray_params() {
|
|
|
|
fn make_check<'a>(time: &'a str) -> (Timespec, &'a str) {
|
|
|
|
(time::strptime(time, "%Y-%m-%d").unwrap().to_timespec(), time)
|
|
|
|
}
|
|
|
|
let (v1, s1) = make_check("1970-10-11");
|
|
|
|
let (v2, s2) = make_check("1990-01-01");
|
|
|
|
let r1 = Range::new(None, None);
|
|
|
|
let rs1 = "\"(,)\"";
|
|
|
|
let r2 = Range::new(Some(RangeBound::new(v1, Inclusive)), None);
|
|
|
|
let rs2 = "\"[" + s1 + ",)\"";
|
|
|
|
let r3 = Range::new(None, Some(RangeBound::new(v2, Exclusive)));
|
|
|
|
let rs3 = "\"(," + s2 + ")\"";
|
|
|
|
test_array_params!("TSRANGE", r1, rs1, r2, rs2, r3, rs3);
|
|
|
|
test_array_params!("TSTZRANGE", r1, rs1, r2, rs2, r3, rs3);
|
|
|
|
}
|
|
|
|
|
2013-12-08 23:07:11 +00:00
|
|
|
#[test]
|
|
|
|
fn test_int8rangearray_params() {
|
|
|
|
test_array_params!("INT8RANGE",
|
|
|
|
Range::new(None, None), "\"(,)\"",
|
|
|
|
Range::new(Some(RangeBound::new(10i64, Inclusive)), None), "\"[10,)\"",
|
|
|
|
Range::new(None, Some(RangeBound::new(10i64, Exclusive))), "\"(,10)\"");
|
|
|
|
}
|
|
|
|
|
2013-12-05 05:20:48 +00:00
|
|
|
#[test]
|
|
|
|
fn test_hstore_params() {
|
|
|
|
macro_rules! make_map(
|
|
|
|
($($k:expr => $v:expr),+) => ({
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
$(map.insert($k, $v);)+
|
|
|
|
map
|
|
|
|
})
|
|
|
|
)
|
|
|
|
test_type("hstore",
|
|
|
|
[(Some(make_map!(~"a" => Some(~"1"))), "'a=>1'"),
|
|
|
|
(Some(make_map!(~"hello" => Some(~"world!"),
|
|
|
|
~"hola" => Some(~"mundo!"),
|
|
|
|
~"what" => None)),
|
|
|
|
"'hello=>world!,hola=>mundo!,what=>NULL'"),
|
|
|
|
(None, "NULL")]);
|
|
|
|
}
|
|
|
|
|
2013-08-31 23:11:42 +00:00
|
|
|
fn test_nan_param<T: Float+ToSql+FromSql>(sql_type: &str) {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-09-29 04:33:55 +00:00
|
|
|
let stmt = conn.prepare("SELECT 'NaN'::" + sql_type);
|
|
|
|
let mut result = stmt.query([]);
|
2013-12-03 03:53:40 +00:00
|
|
|
let val: T = result.next().unwrap()[1];
|
2013-09-29 04:33:55 +00:00
|
|
|
assert!(val.is_nan());
|
|
|
|
|
2013-09-26 00:03:41 +00:00
|
|
|
let nan: T = Float::nan();
|
2013-09-04 03:50:02 +00:00
|
|
|
let stmt = conn.prepare("SELECT $1::" + sql_type);
|
|
|
|
let mut result = stmt.query([&nan as &ToSql]);
|
2013-12-03 03:53:40 +00:00
|
|
|
let val: T = result.next().unwrap()[1];
|
2013-09-26 00:03:41 +00:00
|
|
|
assert!(val.is_nan())
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
2013-08-30 06:28:46 +00:00
|
|
|
|
2013-08-31 23:11:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_f32_nan_param() {
|
|
|
|
test_nan_param::<f32>("REAL");
|
|
|
|
}
|
2013-08-30 06:28:46 +00:00
|
|
|
|
2013-08-31 23:11:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_f64_nan_param() {
|
|
|
|
test_nan_param::<f64>("DOUBLE PRECISION");
|
2013-08-30 06:28:46 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 17:27:09 +00:00
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_wrong_param_type() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.try_execute("SELECT $1::VARCHAR", [&1i32 as &ToSql]);
|
2013-08-29 05:33:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 05:33:19 +00:00
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_too_few_params() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.try_execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql]);
|
2013-09-12 05:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_too_many_params() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.try_execute("SELECT $1::INT, $2::INT", [&1i32 as &ToSql,
|
2013-09-29 06:02:21 +00:00
|
|
|
&2i32 as &ToSql,
|
|
|
|
&3i32 as &ToSql]);
|
2013-09-12 05:33:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 00:07:08 +00:00
|
|
|
#[test]
|
2013-09-03 01:53:03 +00:00
|
|
|
fn test_get_named() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-09-04 03:50:02 +00:00
|
|
|
let stmt = conn.prepare("SELECT 10::INT as val");
|
|
|
|
let result = stmt.query([]);
|
|
|
|
|
2013-12-06 05:51:09 +00:00
|
|
|
assert_eq!(~[10i32], result.map(|row| row["val"]).collect());
|
2013-09-03 01:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_get_named_fail() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-09-04 03:50:02 +00:00
|
|
|
let stmt = conn.prepare("SELECT 10::INT as id");
|
|
|
|
let mut result = stmt.query([]);
|
2013-09-03 01:53:03 +00:00
|
|
|
|
2013-09-04 03:50:02 +00:00
|
|
|
let _: i32 = result.next().unwrap()["asdf"];
|
2013-09-03 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 02:57:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_custom_notice_handler() {
|
|
|
|
static mut count: uint = 0;
|
|
|
|
struct Handler;
|
|
|
|
|
|
|
|
impl PostgresNoticeHandler for Handler {
|
|
|
|
fn handle(&mut self, _notice: PostgresDbError) {
|
|
|
|
unsafe { count += 1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost?client_min_messages=NOTICE", &NoSsl);
|
2013-09-12 02:57:26 +00:00
|
|
|
conn.set_notice_handler(~Handler as ~PostgresNoticeHandler);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("CREATE FUNCTION pg_temp.note() RETURNS INT AS $$
|
2013-10-12 20:27:59 +00:00
|
|
|
BEGIN
|
|
|
|
RAISE NOTICE 'note';
|
|
|
|
RETURN 1;
|
|
|
|
END; $$ LANGUAGE plpgsql", []);
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("SELECT pg_temp.note()", []);
|
2013-09-12 02:57:26 +00:00
|
|
|
|
|
|
|
assert_eq!(unsafe { count }, 1);
|
|
|
|
}
|
|
|
|
|
2013-10-15 05:41:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_notification_iterator_none() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-10-15 05:41:03 +00:00
|
|
|
assert!(conn.notifications().next().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_notification_iterator_some() {
|
|
|
|
fn check_notification(expected: PostgresNotification,
|
|
|
|
actual: Option<PostgresNotification>) {
|
|
|
|
match actual {
|
2013-11-29 18:30:03 +00:00
|
|
|
Some(PostgresNotification { channel, payload, .. }) => {
|
2013-10-15 05:41:03 +00:00
|
|
|
assert_eq!(&expected.channel, &channel);
|
|
|
|
assert_eq!(&expected.payload, &payload);
|
|
|
|
}
|
2013-10-21 04:06:12 +00:00
|
|
|
x => fail!("Expected {:?} but got {:?}", expected, x)
|
2013-10-15 05:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-10-15 05:41:03 +00:00
|
|
|
let mut it = conn.notifications();
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("LISTEN test_notification_iterator_one_channel", []);
|
|
|
|
conn.execute("LISTEN test_notification_iterator_one_channel2", []);
|
|
|
|
conn.execute("NOTIFY test_notification_iterator_one_channel, 'hello'", []);
|
|
|
|
conn.execute("NOTIFY test_notification_iterator_one_channel2, 'world'", []);
|
2013-10-15 05:41:03 +00:00
|
|
|
|
|
|
|
check_notification(PostgresNotification {
|
|
|
|
pid: 0,
|
|
|
|
channel: ~"test_notification_iterator_one_channel",
|
|
|
|
payload: ~"hello"
|
|
|
|
}, it.next());
|
|
|
|
check_notification(PostgresNotification {
|
|
|
|
pid: 0,
|
|
|
|
channel: ~"test_notification_iterator_one_channel2",
|
|
|
|
payload: ~"world"
|
|
|
|
}, it.next());
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("NOTIFY test_notification_iterator_one_channel, '!'", []);
|
2013-10-15 05:41:03 +00:00
|
|
|
check_notification(PostgresNotification {
|
|
|
|
pid: 0,
|
|
|
|
channel: ~"test_notification_iterator_one_channel",
|
|
|
|
payload: ~"!"
|
|
|
|
}, it.next());
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
2013-10-21 00:32:14 +00:00
|
|
|
#[test]
|
|
|
|
// This test is pretty sad, but I don't think there's a better way :(
|
2013-10-21 00:54:50 +00:00
|
|
|
fn test_cancel_query() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
|
2013-10-21 00:32:14 +00:00
|
|
|
let cancel_data = conn.cancel_data();
|
|
|
|
|
|
|
|
do spawn {
|
|
|
|
timer::sleep(500);
|
2014-01-14 05:04:19 +00:00
|
|
|
assert!(super::cancel_query("postgres://postgres@localhost", &NoSsl,
|
|
|
|
cancel_data).is_ok());
|
2013-10-21 00:32:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 03:24:52 +00:00
|
|
|
match conn.try_execute("SELECT pg_sleep(10)", []) {
|
2013-11-29 18:30:03 +00:00
|
|
|
Err(PostgresDbError { code: QueryCanceled, .. }) => {}
|
2013-10-21 00:32:14 +00:00
|
|
|
res => fail!("Unexpected result {:?}", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-10 05:55:29 +00:00
|
|
|
#[test]
|
2013-11-10 06:30:20 +00:00
|
|
|
fn test_require_ssl_conn() {
|
2013-11-10 05:55:29 +00:00
|
|
|
let ctx = SslContext::new(Sslv3);
|
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost",
|
2013-11-11 02:13:32 +00:00
|
|
|
&RequireSsl(ctx));
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("SELECT 1::VARCHAR", []);
|
2013-11-10 05:55:29 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 06:30:20 +00:00
|
|
|
#[test]
|
|
|
|
fn test_prefer_ssl_conn() {
|
|
|
|
let ctx = SslContext::new(Sslv3);
|
|
|
|
let conn = PostgresConnection::connect("postgres://postgres@localhost",
|
2013-11-11 02:13:32 +00:00
|
|
|
&PreferSsl(ctx));
|
2013-12-29 03:24:52 +00:00
|
|
|
conn.execute("SELECT 1::VARCHAR", []);
|
2013-11-10 06:30:20 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 05:08:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_plaintext_pass() {
|
2013-11-11 02:13:32 +00:00
|
|
|
PostgresConnection::connect("postgres://pass_user:password@localhost/postgres", &NoSsl);
|
2013-08-26 05:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_plaintext_pass_no_pass() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let ret = PostgresConnection::try_connect("postgres://pass_user@localhost/postgres", &NoSsl);
|
2013-08-29 04:24:43 +00:00
|
|
|
match ret {
|
|
|
|
Err(MissingPassword) => (),
|
2013-10-21 04:06:12 +00:00
|
|
|
Err(err) => fail!("Unexpected error {}", err.to_str()),
|
|
|
|
_ => fail!("Expected error")
|
2013-08-29 04:24:43 +00:00
|
|
|
}
|
2013-08-26 05:08:37 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 07:36:09 +00:00
|
|
|
#[test]
|
|
|
|
fn test_plaintext_pass_wrong_pass() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let ret = PostgresConnection::try_connect("postgres://pass_user:asdf@localhost/postgres", &NoSsl);
|
2013-08-29 04:24:43 +00:00
|
|
|
match ret {
|
2013-11-29 18:30:03 +00:00
|
|
|
Err(DbError(PostgresDbError { code: InvalidPassword, .. })) => (),
|
2013-10-21 04:06:12 +00:00
|
|
|
Err(err) => fail!("Unexpected error {}", err.to_str()),
|
|
|
|
_ => fail!("Expected error")
|
2013-08-29 04:24:43 +00:00
|
|
|
}
|
2013-08-26 07:36:09 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 06:04:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_md5_pass() {
|
2013-11-11 02:13:32 +00:00
|
|
|
PostgresConnection::connect("postgres://md5_user:password@localhost/postgres", &NoSsl);
|
2013-11-06 06:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_md5_pass_no_pass() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let ret = PostgresConnection::try_connect("postgres://md5_user@localhost/postgres", &NoSsl);
|
2013-11-06 06:04:12 +00:00
|
|
|
match ret {
|
|
|
|
Err(MissingPassword) => (),
|
|
|
|
Err(err) => fail!("Unexpected error {}", err.to_str()),
|
|
|
|
_ => fail!("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_md5_pass_wrong_pass() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let ret = PostgresConnection::try_connect("postgres://md5_user:asdf@localhost/postgres", &NoSsl);
|
2013-11-06 06:04:12 +00:00
|
|
|
match ret {
|
2013-11-29 18:30:03 +00:00
|
|
|
Err(DbError(PostgresDbError { code: InvalidPassword, .. })) => (),
|
2013-11-06 06:04:12 +00:00
|
|
|
Err(err) => fail!("Unexpected error {}", err.to_str()),
|
|
|
|
_ => fail!("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 21:26:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_dns_failure() {
|
2013-11-11 02:13:32 +00:00
|
|
|
let ret = PostgresConnection::try_connect("postgres://postgres@asdfasdfasdf", &NoSsl);
|
2013-09-08 21:26:34 +00:00
|
|
|
match ret {
|
|
|
|
Err(DnsError) => (),
|
2013-10-21 04:06:12 +00:00
|
|
|
Err(err) => fail!("Unexpected error {}", err.to_str()),
|
|
|
|
_ => fail!("Expected error")
|
2013-09-08 21:26:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-09 00:00:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_jsonarray_params() {
|
|
|
|
test_array_params!("JSON",
|
|
|
|
json::from_str("[10, 11, 12]").unwrap(),
|
|
|
|
"\"[10,11,12]\"",
|
|
|
|
json::from_str(r#"{"a": 10, "b": null}"#).unwrap(),
|
|
|
|
r#""{\"a\": 10, \"b\": null}""#,
|
|
|
|
json::from_str(r#"{"a": [10], "b": true}"#).unwrap(),
|
|
|
|
r#""{\"a\": [10], \"b\": true}""#);
|
|
|
|
}
|