rust-postgres/tests/test.rs

812 lines
29 KiB
Rust
Raw Normal View History

2014-10-10 03:43:14 +00:00
#![feature(macro_rules, phase, slicing_syntax)]
#[phase(plugin, link)]
extern crate postgres;
extern crate serialize;
extern crate url;
extern crate openssl;
2014-11-28 20:24:34 +00:00
use openssl::ssl::SslContext;
use openssl::ssl::SslMethod::Sslv3;
2013-11-13 05:33:52 +00:00
use std::io::timer;
2014-08-15 03:00:13 +00:00
use std::time::Duration;
2014-11-01 23:16:50 +00:00
use postgres::{NoticeHandler,
2014-11-01 23:18:09 +00:00
Notification,
2014-11-01 23:21:47 +00:00
Connection,
GenericConnection,
ResultDescription,
2014-11-17 21:46:33 +00:00
SslMode,
2014-11-04 05:29:16 +00:00
Type,
2014-11-04 06:24:11 +00:00
ToSql,
Error,
ConnectError,
2014-11-04 06:24:11 +00:00
DbError};
use postgres::SqlState::{SyntaxError,
QueryCanceled,
UndefinedTable,
InvalidCatalogName,
InvalidPassword,
CardinalityViolation};
use postgres::ErrorPosition::Normal;
2014-10-30 05:13:32 +00:00
macro_rules! or_panic(
2014-03-28 05:43:10 +00:00
($e:expr) => (
match $e {
Ok(ok) => ok,
2014-10-30 04:26:03 +00:00
Err(err) => panic!("{}", err)
2014-03-28 05:43:10 +00:00
}
)
)
mod types;
2013-08-18 03:30:31 +00:00
2013-10-05 03:26:52 +00:00
#[test]
fn test_non_default_database() {
2014-11-17 21:46:33 +00:00
or_panic!(Connection::connect("postgres://postgres@localhost/postgres", &SslMode::None));
2013-10-05 03:26:52 +00:00
}
2013-10-25 05:01:40 +00:00
#[test]
fn test_url_terminating_slash() {
2014-11-17 21:46:33 +00:00
or_panic!(Connection::connect("postgres://postgres@localhost/", &SslMode::None));
2013-10-25 05:01:40 +00:00
}
#[test]
fn test_prepare_err() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
match conn.prepare("invalid sql statment") {
Err(Error::DbError(DbError { code: SyntaxError, position: Some(Normal(1)), .. })) => (),
2014-10-30 04:26:03 +00:00
Err(e) => panic!("Unexpected result {}", e),
_ => panic!("Unexpected result"),
}
}
2013-10-25 05:01:40 +00:00
#[test]
fn test_unknown_database() {
2014-11-17 21:46:33 +00:00
match Connection::connect("postgres://postgres@localhost/asdf", &SslMode::None) {
Err(ConnectError::DbError(DbError { code: InvalidCatalogName, .. })) => {}
2014-10-30 04:26:03 +00:00
Err(resp) => panic!("Unexpected result {}", resp),
_ => panic!("Unexpected result"),
2013-10-25 05:01:40 +00:00
}
}
#[test]
fn test_connection_finish() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
assert!(conn.finish().is_ok());
}
#[test]
fn test_unix_connection() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SHOW unix_socket_directories"));
let result = or_panic!(stmt.query(&[]));
let unix_socket_directories: String = result.map(|row| row.get(0)).next().unwrap();
2014-04-22 04:01:44 +00:00
if unix_socket_directories.is_empty() {
2014-10-30 04:26:03 +00:00
panic!("can't test connect_unix; unix_socket_directories is empty");
}
2014-10-05 21:47:57 +00:00
let unix_socket_directory = unix_socket_directories[].split(',').next().unwrap();
let path = url::utf8_percent_encode(unix_socket_directory, url::USERNAME_ENCODE_SET);
2014-10-26 19:05:43 +00:00
let url = format!("postgres://postgres@{}", path);
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect(url[], &SslMode::None));
assert!(conn.finish().is_ok());
}
#[test]
fn test_transaction_commit() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
or_panic!(trans.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32]));
2014-07-26 05:41:10 +00:00
trans.set_commit();
2013-12-05 06:24:49 +00:00
drop(trans);
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
let result = or_panic!(stmt.query(&[]));
assert_eq!(vec![1i32], result.map(|row| row.get(0)).collect());
}
#[test]
fn test_transaction_commit_finish() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
or_panic!(trans.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32]));
2014-07-26 05:41:10 +00:00
trans.set_commit();
assert!(trans.finish().is_ok());
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
let result = or_panic!(stmt.query(&[]));
assert_eq!(vec![1i32], result.map(|row| row.get(0)).collect());
}
2014-07-26 05:41:10 +00:00
#[test]
fn test_transaction_commit_method() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
2014-07-26 05:41:10 +00:00
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
or_panic!(trans.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32]));
2014-07-26 05:41:10 +00:00
assert!(trans.commit().is_ok());
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
let result = or_panic!(stmt.query(&[]));
2014-07-26 05:41:10 +00:00
assert_eq!(vec![1i32], result.map(|row| row.get(0)).collect());
2014-07-26 05:41:10 +00:00
}
#[test]
fn test_transaction_rollback() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
2013-08-23 07:13:42 +00:00
2014-10-30 04:26:03 +00:00
or_panic!(conn.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32]));
2013-12-05 06:24:49 +00:00
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
or_panic!(trans.execute("INSERT INTO foo (id) VALUES ($1)", &[&2i32]));
2013-12-05 06:24:49 +00:00
drop(trans);
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
let result = or_panic!(stmt.query(&[]));
assert_eq!(vec![1i32], result.map(|row| row.get(0)).collect());
2013-08-23 07:13:42 +00:00
}
2013-08-25 03:47:36 +00:00
#[test]
fn test_transaction_rollback_finish() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
2014-10-30 04:26:03 +00:00
or_panic!(conn.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32]));
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
or_panic!(trans.execute("INSERT INTO foo (id) VALUES ($1)", &[&2i32]));
assert!(trans.finish().is_ok());
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
let result = or_panic!(stmt.query(&[]));
assert_eq!(vec![1i32], result.map(|row| row.get(0)).collect());
}
2013-09-05 06:28:44 +00:00
#[test]
fn test_nested_transactions() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
2013-09-05 06:28:44 +00:00
or_panic!(conn.execute("INSERT INTO foo (id) VALUES (1)", &[]));
2013-09-05 06:28:44 +00:00
2013-10-14 01:58:31 +00:00
{
2014-10-30 04:26:03 +00:00
let trans1 = or_panic!(conn.transaction());
or_panic!(trans1.execute("INSERT INTO foo (id) VALUES (2)", &[]));
2013-09-05 06:28:44 +00:00
2013-10-14 01:58:31 +00:00
{
2014-10-30 04:26:03 +00:00
let trans2 = or_panic!(trans1.transaction());
or_panic!(trans2.execute("INSERT INTO foo (id) VALUES (3)", &[]));
2013-09-05 06:28:44 +00:00
}
2013-10-14 01:58:31 +00:00
{
2014-10-30 04:26:03 +00:00
let trans2 = or_panic!(trans1.transaction());
or_panic!(trans2.execute("INSERT INTO foo (id) VALUES (4)", &[]));
2013-09-05 06:28:44 +00:00
2013-10-14 01:58:31 +00:00
{
2014-10-30 04:26:03 +00:00
let trans3 = or_panic!(trans2.transaction());
or_panic!(trans3.execute("INSERT INTO foo (id) VALUES (5)", &[]));
2013-09-05 06:28:44 +00:00
}
2013-10-14 01:58:31 +00:00
{
2014-10-30 04:26:03 +00:00
let trans3 = or_panic!(trans2.transaction());
or_panic!(trans3.execute("INSERT INTO foo (id) VALUES (6)", &[]));
2014-07-26 05:41:10 +00:00
assert!(trans3.commit().is_ok());
2013-09-05 06:28:44 +00:00
}
2014-07-26 05:41:10 +00:00
assert!(trans2.commit().is_ok());
2013-09-05 06:28:44 +00:00
}
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(trans1.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_panic!(stmt.query(&[]));
2013-09-05 06:28:44 +00:00
assert_eq!(vec![1i32, 2, 4, 6], result.map(|row| row.get(0)).collect());
2013-09-05 06:28:44 +00:00
}
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_panic!(stmt.query(&[]));
2013-09-05 06:28:44 +00:00
assert_eq!(vec![1i32], result.map(|row| row.get(0)).collect());
2013-09-05 06:28:44 +00:00
}
#[test]
fn test_nested_transactions_finish() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
or_panic!(conn.execute("INSERT INTO foo (id) VALUES (1)", &[]));
{
2014-10-30 04:26:03 +00:00
let trans1 = or_panic!(conn.transaction());
or_panic!(trans1.execute("INSERT INTO foo (id) VALUES (2)", &[]));
{
2014-10-30 04:26:03 +00:00
let trans2 = or_panic!(trans1.transaction());
or_panic!(trans2.execute("INSERT INTO foo (id) VALUES (3)", &[]));
assert!(trans2.finish().is_ok());
}
{
2014-10-30 04:26:03 +00:00
let trans2 = or_panic!(trans1.transaction());
or_panic!(trans2.execute("INSERT INTO foo (id) VALUES (4)", &[]));
{
2014-10-30 04:26:03 +00:00
let trans3 = or_panic!(trans2.transaction());
or_panic!(trans3.execute("INSERT INTO foo (id) VALUES (5)", &[]));
assert!(trans3.finish().is_ok());
}
{
2014-10-30 04:26:03 +00:00
let trans3 = or_panic!(trans2.transaction());
or_panic!(trans3.execute("INSERT INTO foo (id) VALUES (6)", &[]));
2014-07-26 05:41:10 +00:00
trans3.set_commit();
assert!(trans3.finish().is_ok());
}
2014-07-26 05:41:10 +00:00
trans2.set_commit();
assert!(trans2.finish().is_ok());
}
// in a block to unborrow trans1 for the finish call
{
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(trans1.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_panic!(stmt.query(&[]));
assert_eq!(vec![1i32, 2, 4, 6], result.map(|row| row.get(0)).collect());
}
assert!(trans1.finish().is_ok());
}
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo ORDER BY id"));
let result = or_panic!(stmt.query(&[]));
assert_eq!(vec![1i32], result.map(|row| row.get(0)).collect());
}
2014-05-19 02:46:21 +00:00
#[test]
fn test_conn_prepare_with_trans() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let _trans = or_panic!(conn.transaction());
2014-05-19 02:46:21 +00:00
match conn.prepare("") {
Err(Error::WrongTransaction) => {}
2014-10-30 04:26:03 +00:00
Err(r) => panic!("Unexpected error {}", r),
Ok(_) => panic!("Unexpected success"),
2014-05-19 02:46:21 +00:00
}
match conn.transaction() {
Err(Error::WrongTransaction) => {}
2014-10-30 04:26:03 +00:00
Err(r) => panic!("Unexpected error {}", r),
Ok(_) => panic!("Unexpected success"),
2014-05-19 02:46:21 +00:00
}
}
#[test]
fn test_trans_prepare_with_nested_trans() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
let _trans2 = or_panic!(trans.transaction());
let stmt = or_panic!(trans.prepare("SELECT 1"));
match trans.lazy_query(&stmt, &[], 10) {
Err(Error::WrongTransaction) => {}
2014-10-30 04:26:03 +00:00
Err(r) => panic!("Unexpected error {}", r),
Ok(_) => panic!("Unexpected success"),
2014-05-19 02:46:21 +00:00
}
match trans.transaction() {
Err(Error::WrongTransaction) => {}
2014-10-30 04:26:03 +00:00
Err(r) => panic!("Unexpected error {}", r),
Ok(_) => panic!("Unexpected success"),
2014-05-19 02:46:21 +00:00
}
}
#[test]
fn test_stmt_execute_after_transaction() {
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
let trans = or_panic!(conn.transaction());
let stmt = or_panic!(trans.prepare("SELECT 1"));
or_panic!(trans.finish());
let mut result = or_panic!(stmt.query(&[]));
assert_eq!(1i32, result.next().unwrap().get(0));
}
#[test]
fn test_stmt_finish() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", &[]));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
assert!(stmt.finish().is_ok());
}
2014-06-08 23:07:15 +00:00
#[test]
fn test_batch_execute() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-06-08 23:07:15 +00:00
let query = "CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY);
INSERT INTO foo (id) VALUES (10);";
2014-10-30 04:26:03 +00:00
or_panic!(conn.batch_execute(query));
2014-06-08 23:07:15 +00:00
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * from foo ORDER BY id"));
let result = or_panic!(stmt.query(&[]));
2014-06-08 23:07:15 +00:00
assert_eq!(vec![10i64], result.map(|row| row.get(0)).collect());
2014-06-08 23:07:15 +00:00
}
#[test]
fn test_batch_execute_error() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-06-08 23:07:15 +00:00
let query = "CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY);
INSERT INTO foo (id) VALUES (10);
asdfa;
INSERT INTO foo (id) VALUES (11)";
conn.batch_execute(query).unwrap_err();
match conn.prepare("SELECT * from foo ORDER BY id") {
Err(Error::DbError(DbError { code: UndefinedTable, .. })) => {},
2014-10-30 04:26:03 +00:00
Err(e) => panic!("unexpected error {}", e),
_ => panic!("unexpected success"),
2014-06-08 23:07:15 +00:00
}
}
2014-07-30 02:48:56 +00:00
#[test]
fn test_transaction_batch_execute() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
2014-07-30 02:48:56 +00:00
let query = "CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY);
INSERT INTO foo (id) VALUES (10);";
2014-10-30 04:26:03 +00:00
or_panic!(trans.batch_execute(query));
2014-07-30 02:48:56 +00:00
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(trans.prepare("SELECT * from foo ORDER BY id"));
let result = or_panic!(stmt.query(&[]));
2014-07-30 02:48:56 +00:00
assert_eq!(vec![10i64], result.map(|row| row.get(0)).collect());
2014-07-30 02:48:56 +00:00
}
2013-08-25 03:47:36 +00:00
#[test]
fn test_query() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", &[]));
2014-10-30 04:26:03 +00:00
or_panic!(conn.execute("INSERT INTO foo (id) VALUES ($1), ($2)",
2014-08-31 22:09:53 +00:00
&[&1i64, &2i64]));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * from foo ORDER BY id"));
let result = or_panic!(stmt.query(&[]));
2013-08-25 03:47:36 +00:00
assert_eq!(vec![1i64, 2], result.map(|row| row.get(0)).collect());
}
#[test]
fn test_error_after_datarow() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("
SELECT
(SELECT generate_series(1, ss.i))
FROM (SELECT gs.i
FROM generate_series(1, 2) gs(i)
ORDER BY gs.i
LIMIT 2) ss"));
match stmt.query(&[]) {
Err(Error::DbError(DbError { code: CardinalityViolation, .. })) => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexpected error {}", err),
Ok(_) => panic!("Expected failure"),
}
}
#[test]
fn test_result_finish() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id BIGINT PRIMARY KEY)", &[]));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
let result = or_panic!(stmt.query(&[]));
assert!(result.finish().is_ok());
}
#[test]
fn test_lazy_query() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
or_panic!(trans.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(trans.prepare("INSERT INTO foo (id) VALUES ($1)"));
let values = vec!(0i32, 1, 2, 3, 4, 5);
for value in values.iter() {
2014-10-30 04:26:03 +00:00
or_panic!(stmt.execute(&[value]));
}
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(trans.prepare("SELECT id FROM foo ORDER BY id"));
let result = or_panic!(trans.lazy_query(&stmt, &[], 2));
assert_eq!(values, result.map(|row| row.unwrap().get(0)).collect());
}
#[test]
fn test_lazy_query_wrong_conn() {
2014-11-17 21:46:33 +00:00
let conn1 = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
let conn2 = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn1.transaction());
let stmt = or_panic!(conn2.prepare("SELECT 1::INT"));
match trans.lazy_query(&stmt, &[], 1) {
Err(Error::WrongConnection) => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexpected error {}", err),
Ok(_) => panic!("Expected failure")
}
}
#[test]
fn test_param_types() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT $1::INT, $2::VARCHAR"));
2014-11-04 05:29:16 +00:00
assert_eq!(stmt.param_types(), [Type::Int4, Type::Varchar][]);
}
#[test]
fn test_result_descriptions() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT 1::INT as a, 'hi'::VARCHAR as b"));
2014-03-02 04:06:04 +00:00
assert!(stmt.result_descriptions() ==
&[ResultDescription { name: "a".to_string(), ty: Type::Int4},
2014-11-04 05:29:16 +00:00
ResultDescription { name: "b".to_string(), ty: Type::Varchar}]);
}
2014-01-02 05:54:13 +00:00
#[test]
fn test_execute_counts() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
assert_eq!(0, or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (
id SERIAL PRIMARY KEY,
b INT
)", &[])));
2014-10-30 04:26:03 +00:00
assert_eq!(3, or_panic!(conn.execute("INSERT INTO foo (b) VALUES ($1), ($2), ($2)",
2014-08-31 22:09:53 +00:00
&[&1i32, &2i32])));
assert_eq!(2, or_panic!(conn.execute("UPDATE foo SET b = 0 WHERE b = 2", &[])));
assert_eq!(3, or_panic!(conn.execute("SELECT * FROM foo", &[])));
2014-01-02 05:54:13 +00:00
}
2013-09-02 17:27:09 +00:00
#[test]
fn test_wrong_param_type() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-08-31 22:09:53 +00:00
match conn.execute("SELECT $1::VARCHAR", &[&1i32]) {
Err(Error::WrongType(_)) => {}
2014-10-30 04:26:03 +00:00
res => panic!("unexpected result {}", res)
2014-03-28 05:43:10 +00:00
}
}
2013-09-12 05:33:19 +00:00
#[test]
fn test_too_few_params() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-08-31 22:09:53 +00:00
match conn.execute("SELECT $1::INT, $2::INT", &[&1i32]) {
Err(Error::WrongParamCount { expected: 2, actual: 1 }) => {},
2014-10-30 04:26:03 +00:00
res => panic!("unexpected result {}", res)
2014-03-28 04:39:03 +00:00
}
2013-09-12 05:33:19 +00:00
}
#[test]
fn test_too_many_params() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-08-31 22:09:53 +00:00
match conn.execute("SELECT $1::INT, $2::INT", &[&1i32, &2i32, &3i32]) {
Err(Error::WrongParamCount { expected: 2, actual: 3 }) => {},
2014-10-30 04:26:03 +00:00
res => panic!("unexpected result {}", res)
2014-03-28 04:39:03 +00:00
}
2013-09-12 05:33:19 +00:00
}
2013-09-03 00:07:08 +00:00
#[test]
fn test_index_named() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT 10::INT as val"));
let result = or_panic!(stmt.query(&[]));
2014-07-10 19:35:57 +00:00
assert_eq!(vec![10i32], result.map(|row| row.get("val")).collect());
}
#[test]
#[should_fail]
fn test_index_named_fail() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT 10::INT as id"));
let mut result = or_panic!(stmt.query(&[]));
2014-07-10 19:35:57 +00:00
let _: i32 = result.next().unwrap().get("asdf");
2013-09-03 00:07:08 +00:00
}
#[test]
fn test_get_named_err() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT 10::INT as id"));
let mut result = or_panic!(stmt.query(&[]));
2014-07-10 19:35:57 +00:00
match result.next().unwrap().get_opt::<&str, i32>("asdf") {
Err(Error::InvalidColumn) => {}
2014-10-30 04:26:03 +00:00
res => panic!("unexpected result {}", res),
};
}
2014-03-30 02:01:23 +00:00
#[test]
fn test_get_was_null() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT NULL::INT as id"));
let mut result = or_panic!(stmt.query(&[]));
2014-03-30 02:01:23 +00:00
2014-07-10 19:35:57 +00:00
match result.next().unwrap().get_opt::<uint, i32>(0) {
Err(Error::WasNull) => {}
2014-10-30 04:26:03 +00:00
res => panic!("unexpected result {}", res),
2014-03-30 02:01:23 +00:00
};
}
#[test]
fn test_custom_notice_handler() {
static mut count: uint = 0;
struct Handler;
2014-11-01 23:16:50 +00:00
impl NoticeHandler for Handler {
2014-11-04 06:24:11 +00:00
fn handle(&mut self, notice: DbError) {
2014-10-05 03:08:44 +00:00
assert_eq!("note", notice.message[]);
unsafe { count += 1; }
}
}
2014-11-01 23:21:47 +00:00
let conn = or_panic!(Connection::connect(
2014-11-17 21:46:33 +00:00
"postgres://postgres@localhost?client_min_messages=NOTICE", &SslMode::None));
2014-05-04 05:38:50 +00:00
conn.set_notice_handler(box Handler);
2014-10-30 04:26:03 +00:00
or_panic!(conn.execute("CREATE FUNCTION pg_temp.note() RETURNS INT AS $$
BEGIN
RAISE NOTICE 'note';
RETURN 1;
END; $$ LANGUAGE plpgsql", &[]));
or_panic!(conn.execute("SELECT pg_temp.note()", &[]));
assert_eq!(unsafe { count }, 1);
}
#[test]
fn test_notification_iterator_none() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
assert!(conn.notifications().next().is_none());
}
fn check_notification(expected: Notification, actual: Notification) {
assert_eq!(&expected.channel, &actual.channel);
assert_eq!(&expected.payload, &actual.payload);
}
#[test]
fn test_notification_iterator_some() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
let mut it = conn.notifications();
or_panic!(conn.execute("LISTEN test_notification_iterator_one_channel", &[]));
or_panic!(conn.execute("LISTEN test_notification_iterator_one_channel2", &[]));
or_panic!(conn.execute("NOTIFY test_notification_iterator_one_channel, 'hello'", &[]));
or_panic!(conn.execute("NOTIFY test_notification_iterator_one_channel2, 'world'", &[]));
2014-11-01 23:18:09 +00:00
check_notification(Notification {
pid: 0,
2014-07-10 19:35:57 +00:00
channel: "test_notification_iterator_one_channel".to_string(),
payload: "hello".to_string()
}, it.next().unwrap());
2014-11-01 23:18:09 +00:00
check_notification(Notification {
pid: 0,
2014-07-10 19:35:57 +00:00
channel: "test_notification_iterator_one_channel2".to_string(),
payload: "world".to_string()
}, it.next().unwrap());
assert!(it.next().is_none());
or_panic!(conn.execute("NOTIFY test_notification_iterator_one_channel, '!'", &[]));
2014-11-01 23:18:09 +00:00
check_notification(Notification {
pid: 0,
2014-07-10 19:35:57 +00:00
channel: "test_notification_iterator_one_channel".to_string(),
payload: "!".to_string()
}, it.next().unwrap());
assert!(it.next().is_none());
}
#[test]
fn test_notifications_next_block() {
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("LISTEN test_notifications_next_block", &[]));
spawn(proc() {
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
timer::sleep(Duration::milliseconds(500));
or_panic!(conn.execute("NOTIFY test_notifications_next_block, 'foo'", &[]));
});
let mut notifications = conn.notifications();
check_notification(Notification {
pid: 0,
channel: "test_notifications_next_block".to_string(),
payload: "foo".to_string()
}, or_panic!(notifications.next_block()));
}
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() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
2013-10-21 00:32:14 +00:00
let cancel_data = conn.cancel_data();
spawn(proc() {
2014-08-15 03:00:13 +00:00
timer::sleep(Duration::milliseconds(500));
2014-11-17 21:46:33 +00:00
assert!(postgres::cancel_query("postgres://postgres@localhost", &SslMode::None,
cancel_data).is_ok());
});
2013-10-21 00:32:14 +00:00
match conn.execute("SELECT pg_sleep(10)", &[]) {
Err(Error::DbError(DbError { code: QueryCanceled, .. })) => {}
2014-10-30 04:26:03 +00:00
Err(res) => panic!("Unexpected result {}", res),
_ => panic!("Unexpected result"),
2013-10-21 00:32:14 +00:00
}
}
2013-11-10 05:55:29 +00:00
#[test]
fn test_require_ssl_conn() {
2014-09-06 04:57:08 +00:00
let ctx = SslContext::new(Sslv3).unwrap();
2014-11-01 23:21:47 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
2014-11-17 21:46:33 +00:00
&SslMode::Require(ctx)));
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
2013-11-10 05:55:29 +00:00
}
#[test]
fn test_prefer_ssl_conn() {
2014-09-06 04:57:08 +00:00
let ctx = SslContext::new(Sslv3).unwrap();
2014-11-01 23:21:47 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
2014-11-17 21:46:33 +00:00
&SslMode::Prefer(ctx)));
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
}
#[test]
fn test_plaintext_pass() {
2014-11-17 21:46:33 +00:00
or_panic!(Connection::connect("postgres://pass_user:password@localhost/postgres", &SslMode::None));
}
#[test]
fn test_plaintext_pass_no_pass() {
2014-11-17 21:46:33 +00:00
let ret = Connection::connect("postgres://pass_user@localhost/postgres", &SslMode::None);
match ret {
Err(ConnectError::MissingPassword) => (),
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexpected error {}", err),
_ => panic!("Expected error")
}
}
#[test]
fn test_plaintext_pass_wrong_pass() {
2014-11-17 21:46:33 +00:00
let ret = Connection::connect("postgres://pass_user:asdf@localhost/postgres", &SslMode::None);
match ret {
Err(ConnectError::DbError(DbError { code: InvalidPassword, .. })) => (),
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexpected error {}", err),
_ => panic!("Expected error")
}
}
#[test]
fn test_md5_pass() {
2014-11-17 21:46:33 +00:00
or_panic!(Connection::connect("postgres://md5_user:password@localhost/postgres", &SslMode::None));
}
#[test]
fn test_md5_pass_no_pass() {
2014-11-17 21:46:33 +00:00
let ret = Connection::connect("postgres://md5_user@localhost/postgres", &SslMode::None);
match ret {
Err(ConnectError::MissingPassword) => (),
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexpected error {}", err),
_ => panic!("Expected error")
}
}
#[test]
fn test_md5_pass_wrong_pass() {
2014-11-17 21:46:33 +00:00
let ret = Connection::connect("postgres://md5_user:asdf@localhost/postgres", &SslMode::None);
match ret {
Err(ConnectError::DbError(DbError { code: InvalidPassword, .. })) => (),
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexpected error {}", err),
_ => panic!("Expected error")
}
}
2014-09-30 04:05:42 +00:00
#[test]
fn test_execute_copy_from_err() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT)", &[]));
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("COPY foo (id) FROM STDIN"));
match stmt.execute(&[]) {
Err(Error::DbError(ref err)) if err.message[].contains("COPY") => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexptected error {}", err),
_ => panic!("Expected error"),
2014-09-30 04:05:42 +00:00
}
match stmt.query(&[]) {
Err(Error::DbError(ref err)) if err.message[].contains("COPY") => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexptected error {}", err),
_ => panic!("Expected error"),
2014-09-30 04:05:42 +00:00
}
}
2014-09-30 05:56:43 +00:00
#[test]
fn test_copy_in() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)", &[]));
2014-09-30 05:56:43 +00:00
let stmt = or_panic!(conn.prepare_copy_in("foo", &["id", "name"]));
2014-09-30 05:56:43 +00:00
let data: &[&[&ToSql]] = &[&[&0i32, &"Steven".to_string()], &[&1i32, &None::<String>]];
assert_eq!(Ok(2), stmt.execute(data.iter().map(|r| r.iter().map(|&e| e))));
2014-09-30 05:56:43 +00:00
2014-10-30 04:26:03 +00:00
let stmt = or_panic!(conn.prepare("SELECT id, name FROM foo ORDER BY id"));
2014-09-30 05:56:43 +00:00
assert_eq!(vec![(0i32, Some("Steven".to_string())), (1, None)],
or_panic!(stmt.query(&[])).map(|r| (r.get(0), r.get(1))).collect());
2014-09-30 05:56:43 +00:00
}
#[test]
fn test_copy_in_bad_column_count() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)", &[]));
let stmt = or_panic!(conn.prepare_copy_in("foo", &["id", "name"]));
let data: &[&[&ToSql]] = &[&[&0i32, &"Steven".to_string()], &[&1i32]];
let res = stmt.execute(data.iter().map(|r| r.iter().map(|&e| e)));
match res {
Err(Error::DbError(ref err)) if err.message[].contains("Invalid column count") => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("unexpected error {}", err),
_ => panic!("Expected error"),
}
let data: &[&[&ToSql]] = &[&[&0i32, &"Steven".to_string()], &[&1i32, &"Steven".to_string(), &1i32]];
let res = stmt.execute(data.iter().map(|r| r.iter().map(|&e| e)));
match res {
Err(Error::DbError(ref err)) if err.message[].contains("Invalid column count") => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("unexpected error {}", err),
_ => panic!("Expected error"),
}
or_panic!(conn.execute("SELECT 1", &[]));
}
2014-10-06 00:31:25 +00:00
2014-10-26 03:26:45 +00:00
#[test]
fn test_copy_in_bad_type() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)", &[]));
2014-10-26 03:26:45 +00:00
let stmt = or_panic!(conn.prepare_copy_in("foo", &["id", "name"]));
2014-10-26 03:26:45 +00:00
let data: &[&[&ToSql]] = &[&[&0i32, &"Steven".to_string()], &[&1i32, &2i32]];
let res = stmt.execute(data.iter().map(|r| r.iter().map(|&e| e)));
match res {
Err(Error::DbError(ref err)) if err.message[].contains("Unexpected type Varchar") => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("unexpected error {}", err),
_ => panic!("Expected error"),
2014-10-26 03:26:45 +00:00
}
or_panic!(conn.execute("SELECT 1", &[]));
2014-10-26 03:26:45 +00:00
}
2014-10-06 00:31:25 +00:00
#[test]
fn test_batch_execute_copy_from_err() {
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT)", &[]));
2014-10-06 00:31:25 +00:00
match conn.batch_execute("COPY foo (id) FROM STDIN") {
Err(Error::DbError(ref err)) if err.message[].contains("COPY") => {}
2014-10-30 04:26:03 +00:00
Err(err) => panic!("Unexptected error {}", err),
_ => panic!("Expected error"),
2014-10-06 00:31:25 +00:00
}
}
#[test]
// Just make sure the impls don't infinite loop
fn test_generic_connection() {
fn f<T>(t: &T) where T: GenericConnection {
or_panic!(t.execute("SELECT 1", &[]));
}
2014-11-17 21:46:33 +00:00
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
f(&conn);
2014-10-30 04:26:03 +00:00
let trans = or_panic!(conn.transaction());
f(&trans);
}