2014-07-01 04:30:39 +00:00
|
|
|
extern crate postgres;
|
2015-01-17 06:07:04 +00:00
|
|
|
extern crate url;
|
2015-04-28 05:52:06 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
2014-07-01 04:30:39 +00:00
|
|
|
extern crate openssl;
|
2015-12-05 22:35:17 +00:00
|
|
|
#[cfg(feature = "security-framework")]
|
|
|
|
extern crate security_framework;
|
2014-07-01 04:30:39 +00:00
|
|
|
|
2015-04-28 05:52:06 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
use openssl::ssl::{SslContext, SslMethod};
|
2015-02-19 17:17:48 +00:00
|
|
|
use std::thread;
|
2015-06-12 07:39:12 +00:00
|
|
|
use std::io;
|
2015-08-16 06:21:39 +00:00
|
|
|
use std::io::prelude::*;
|
2015-10-21 06:13:26 +00:00
|
|
|
use std::time::Duration;
|
2014-07-01 04:30:39 +00:00
|
|
|
|
2015-02-22 23:05:19 +00:00
|
|
|
use postgres::{HandleNotice,
|
2014-11-01 23:21:47 +00:00
|
|
|
Connection,
|
2014-10-09 03:29:26 +00:00
|
|
|
GenericConnection,
|
2014-11-17 21:46:33 +00:00
|
|
|
SslMode,
|
2015-04-16 03:31:15 +00:00
|
|
|
IntoConnectParams,
|
2015-05-23 15:13:17 +00:00
|
|
|
IsolationLevel};
|
2015-05-26 05:47:25 +00:00
|
|
|
use postgres::error::{Error, ConnectError, DbError};
|
2015-09-27 05:57:36 +00:00
|
|
|
use postgres::types::{Oid, Type, Kind};
|
2015-05-26 05:47:25 +00:00
|
|
|
use postgres::error::SqlState::{SyntaxError,
|
|
|
|
QueryCanceled,
|
|
|
|
UndefinedTable,
|
|
|
|
InvalidCatalogName,
|
|
|
|
InvalidPassword,
|
|
|
|
CardinalityViolation};
|
|
|
|
use postgres::error::ErrorPosition::Normal;
|
2015-07-31 05:04:22 +00:00
|
|
|
use postgres::rows::RowIndex;
|
2015-09-19 03:55:01 +00:00
|
|
|
use postgres::notification::Notification;
|
2013-09-29 04:19:27 +00:00
|
|
|
|
2014-12-19 18:43:42 +00:00
|
|
|
macro_rules! or_panic {
|
2014-03-28 05:43:10 +00:00
|
|
|
($e:expr) => (
|
|
|
|
match $e {
|
|
|
|
Ok(ok) => ok,
|
2015-05-06 04:54:08 +00:00
|
|
|
Err(err) => panic!("{:#?}", err)
|
2014-03-28 05:43:10 +00:00
|
|
|
}
|
|
|
|
)
|
2014-12-19 18:43:42 +00:00
|
|
|
}
|
2014-03-28 05:43:10 +00:00
|
|
|
|
2014-07-07 23:08:50 +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() {
|
2015-12-06 00:15:19 +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() {
|
2015-12-06 00:15:19 +00:00
|
|
|
or_panic!(Connection::connect("postgres://postgres@localhost/", SslMode::None));
|
2013-10-25 05:01:40 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 05:06:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_prepare_err() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-02-12 21:29:50 +00:00
|
|
|
let stmt = conn.prepare("invalid sql database");
|
|
|
|
match stmt {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(ref e)) if e.code == SyntaxError && e.position == Some(Normal(1)) => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(e) => panic!("Unexpected result {:?}", e),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Unexpected result"),
|
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() {
|
2015-12-06 00:15:19 +00:00
|
|
|
match Connection::connect("postgres://postgres@localhost/asdf", SslMode::None) {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(ConnectError::Db(ref e)) if e.code == InvalidCatalogName => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(resp) => panic!("Unexpected result {:?}", resp),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Unexpected result"),
|
2013-10-25 05:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 07:55:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_connection_finish() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(conn.finish().is_ok());
|
|
|
|
}
|
|
|
|
|
2014-04-18 21:29:51 +00:00
|
|
|
#[test]
|
2015-02-26 17:02:32 +00:00
|
|
|
#[cfg_attr(not(feature = "unix_socket"), ignore)]
|
2014-04-18 21:29:51 +00:00
|
|
|
fn test_unix_connection() {
|
2015-12-06 00:15:19 +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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2015-02-19 07:05:11 +00:00
|
|
|
let unix_socket_directories: String = result.iter().map(|row| row.get(0)).next().unwrap();
|
2014-04-19 09:04:37 +00:00
|
|
|
|
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-04-19 09:04:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 04:17:47 +00:00
|
|
|
let unix_socket_directory = unix_socket_directories.split(',').next().unwrap();
|
2014-04-19 09:04:37 +00:00
|
|
|
|
2015-04-23 05:09:07 +00:00
|
|
|
let path = url::percent_encoding::utf8_percent_encode(
|
|
|
|
unix_socket_directory, url::percent_encoding::USERNAME_ENCODE_SET);
|
2015-01-17 06:07:04 +00:00
|
|
|
let url = format!("postgres://postgres@{}", path);
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect(&url[..], SslMode::None));
|
2014-04-18 21:29:51 +00:00
|
|
|
assert!(conn.finish().is_ok());
|
|
|
|
}
|
|
|
|
|
2013-09-03 07:00:43 +00:00
|
|
|
#[test]
|
2013-09-04 03:50:02 +00:00
|
|
|
fn test_transaction_commit() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
|
2013-09-04 03:50:02 +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
|
|
|
trans.set_commit();
|
2013-12-05 06:24:49 +00:00
|
|
|
drop(trans);
|
2013-09-03 07:00:43 +00:00
|
|
|
|
2014-10-30 04:26:03 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2013-09-03 07:00:43 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2013-09-03 07:00:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 07:55:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_transaction_commit_finish() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
|
2014-02-10 07:55:16 +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
|
|
|
trans.set_commit();
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(trans.finish().is_ok());
|
|
|
|
|
2014-10-30 04:26:03 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-02-10 07:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 05:41:10 +00:00
|
|
|
#[test]
|
|
|
|
fn test_transaction_commit_method() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-07-26 05:41:10 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-07-26 05:41:10 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 03:50:02 +00:00
|
|
|
#[test]
|
|
|
|
fn test_transaction_rollback() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
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);
|
2013-08-31 23:11:42 +00:00
|
|
|
|
2014-10-30 04:26:03 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2013-09-04 03:50:02 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2013-08-23 07:13:42 +00:00
|
|
|
}
|
2013-08-25 03:47:36 +00:00
|
|
|
|
2014-02-10 07:55:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_transaction_rollback_finish() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
2014-10-30 04:26:03 +00:00
|
|
|
or_panic!(conn.execute("INSERT INTO foo (id) VALUES ($1)", &[&1i32]));
|
2014-02-10 07:55:16 +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]));
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(trans.finish().is_ok());
|
|
|
|
|
2014-10-30 04:26:03 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT * FROM foo"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-02-10 07:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 06:28:44 +00:00
|
|
|
#[test]
|
|
|
|
fn test_nested_transactions() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
|
2013-09-05 06:28:44 +00:00
|
|
|
|
2014-11-19 18:20:20 +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());
|
2014-11-19 18:20:20 +00:00
|
|
|
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());
|
2014-11-19 18:20:20 +00:00
|
|
|
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());
|
2014-11-19 18:20:20 +00:00
|
|
|
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());
|
2014-11-19 18:20:20 +00:00
|
|
|
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());
|
2014-11-19 18:20:20 +00:00
|
|
|
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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2013-09-05 06:28:44 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32, 2, 4, 6], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2013-09-05 06:28:44 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2013-09-05 06:28:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 07:55:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_nested_transactions_finish() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("INSERT INTO foo (id) VALUES (1)", &[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
|
|
|
{
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans1 = or_panic!(conn.transaction());
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(trans1.execute("INSERT INTO foo (id) VALUES (2)", &[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
|
|
|
{
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans2 = or_panic!(trans1.transaction());
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(trans2.execute("INSERT INTO foo (id) VALUES (3)", &[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(trans2.finish().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans2 = or_panic!(trans1.transaction());
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(trans2.execute("INSERT INTO foo (id) VALUES (4)", &[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
|
|
|
{
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans3 = or_panic!(trans2.transaction());
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(trans3.execute("INSERT INTO foo (id) VALUES (5)", &[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(trans3.finish().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans3 = or_panic!(trans2.transaction());
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(trans3.execute("INSERT INTO foo (id) VALUES (6)", &[]));
|
2014-07-26 05:41:10 +00:00
|
|
|
trans3.set_commit();
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(trans3.finish().is_ok());
|
|
|
|
}
|
|
|
|
|
2014-07-26 05:41:10 +00:00
|
|
|
trans2.set_commit();
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(trans2.finish().is_ok());
|
|
|
|
}
|
|
|
|
|
2014-05-19 02:34:01 +00:00
|
|
|
// 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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32, 2, 4, 6], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-05-19 02:34:01 +00:00
|
|
|
}
|
2014-02-10 07:55:16 +00:00
|
|
|
|
|
|
|
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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-02-10 07:55:16 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i32], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-02-10 07:55:16 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 02:46:21 +00:00
|
|
|
#[test]
|
2015-03-10 16:16:46 +00:00
|
|
|
#[should_panic(expected = "active transaction")]
|
2015-01-23 06:24:47 +00:00
|
|
|
fn test_conn_trans_when_nested() {
|
2015-12-06 00:15:19 +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());
|
2015-01-27 04:59:31 +00:00
|
|
|
conn.transaction().unwrap();
|
2014-05-19 02:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-10 16:16:46 +00:00
|
|
|
#[should_panic(expected = "active transaction")]
|
2015-01-27 04:59:31 +00:00
|
|
|
fn test_trans_with_nested_trans() {
|
2015-12-06 00:15:19 +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());
|
2015-01-27 04:59:31 +00:00
|
|
|
trans.transaction().unwrap();
|
2014-05-19 02:46:21 +00:00
|
|
|
}
|
|
|
|
|
2014-12-02 06:42:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_stmt_execute_after_transaction() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-12-02 06:42:36 +00:00
|
|
|
let trans = or_panic!(conn.transaction());
|
|
|
|
let stmt = or_panic!(trans.prepare("SELECT 1"));
|
|
|
|
or_panic!(trans.finish());
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
|
|
|
assert_eq!(1i32, result.iter().next().unwrap().get(0));
|
2014-12-02 06:42:36 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 07:55:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_stmt_finish() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
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"));
|
2014-02-10 07:55:16 +00:00
|
|
|
assert!(stmt.finish().is_ok());
|
|
|
|
}
|
|
|
|
|
2014-06-08 23:07:15 +00:00
|
|
|
#[test]
|
|
|
|
fn test_batch_execute() {
|
2015-12-06 00:15:19 +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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-06-08 23:07:15 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![10i64], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-06-08 23:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_batch_execute_error() {
|
2015-12-06 00:15:19 +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)";
|
2015-01-23 19:10:04 +00:00
|
|
|
conn.batch_execute(query).err().unwrap();
|
2014-06-08 23:07:15 +00:00
|
|
|
|
2015-02-12 21:29:50 +00:00
|
|
|
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
|
|
|
|
match stmt {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(ref e)) if e.code == UndefinedTable => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(e) => panic!("unexpected error {:?}", e),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => 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() {
|
2015-12-06 00:15:19 +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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-07-30 02:48:56 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![10i64], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2014-07-30 02:48:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-25 03:47:36 +00:00
|
|
|
#[test]
|
2013-09-04 03:50:02 +00:00
|
|
|
fn test_query() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2013-08-25 03:47:36 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![1i64, 2], result.iter().map(|row| row.get(0)).collect::<Vec<_>>());
|
2013-08-31 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 01:00:28 +00:00
|
|
|
#[test]
|
|
|
|
fn test_error_after_datarow() {
|
2015-12-06 00:15:19 +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("
|
2014-07-25 01:00:28 +00:00
|
|
|
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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
match stmt.query(&[]) {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(ref e)) if e.code == CardinalityViolation => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
Ok(_) => panic!("Expected failure"),
|
2015-11-13 07:52:33 +00:00
|
|
|
};
|
2014-07-25 01:00:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 05:20:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_lazy_query() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2013-09-04 05:20:21 +00:00
|
|
|
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans = or_panic!(conn.transaction());
|
2014-11-19 18:20:20 +00:00
|
|
|
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)"));
|
2014-03-28 02:45:36 +00:00
|
|
|
let values = vec!(0i32, 1, 2, 3, 4, 5);
|
2014-03-26 04:01:38 +00:00
|
|
|
for value in values.iter() {
|
2014-10-30 04:26:03 +00:00
|
|
|
or_panic!(stmt.execute(&[value]));
|
2014-03-26 04:01:38 +00:00
|
|
|
}
|
2014-10-30 04:26:03 +00:00
|
|
|
let stmt = or_panic!(trans.prepare("SELECT id FROM foo ORDER BY id"));
|
2015-01-23 06:52:29 +00:00
|
|
|
let result = or_panic!(stmt.lazy_query(&trans, &[], 2));
|
2014-12-05 15:58:11 +00:00
|
|
|
assert_eq!(values, result.map(|row| row.unwrap().get(0)).collect::<Vec<_>>());
|
2014-03-26 04:01:38 +00:00
|
|
|
}
|
2013-09-04 05:20:21 +00:00
|
|
|
|
2014-03-26 04:01:38 +00:00
|
|
|
#[test]
|
2015-03-10 16:16:46 +00:00
|
|
|
#[should_panic(expected = "same `Connection` as")]
|
2014-03-26 04:01:38 +00:00
|
|
|
fn test_lazy_query_wrong_conn() {
|
2015-12-06 00:15:19 +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-03-26 04:01:38 +00:00
|
|
|
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans = or_panic!(conn1.transaction());
|
|
|
|
let stmt = or_panic!(conn2.prepare("SELECT 1::INT"));
|
2015-01-27 04:59:31 +00:00
|
|
|
stmt.lazy_query(&trans, &[], 1).unwrap();
|
2013-09-04 05:20:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 04:51:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_param_types() {
|
2015-12-06 00:15:19 +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"));
|
2015-03-26 02:11:40 +00:00
|
|
|
assert_eq!(stmt.param_types(), &[Type::Int4, Type::Varchar][..]);
|
2013-09-05 04:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-02-06 07:08:53 +00:00
|
|
|
fn test_columns() {
|
2015-12-06 00:15:19 +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"));
|
2015-02-06 07:08:53 +00:00
|
|
|
let cols = stmt.columns();
|
|
|
|
assert_eq!(2, cols.len());
|
|
|
|
assert_eq!(cols[0].name(), "a");
|
|
|
|
assert_eq!(cols[0].type_(), &Type::Int4);
|
|
|
|
assert_eq!(cols[1].name(), "b");
|
|
|
|
assert_eq!(cols[1].type_(), &Type::Varchar);
|
2013-09-05 04:51:21 +00:00
|
|
|
}
|
|
|
|
|
2014-01-02 05:54:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_execute_counts() {
|
2015-12-06 00:15:19 +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 (
|
2014-03-28 04:08:22 +00:00
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
b INT
|
2014-11-19 18:20:20 +00:00
|
|
|
)", &[])));
|
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])));
|
2014-11-19 18:20:20 +00:00
|
|
|
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() {
|
2015-12-06 00:15:19 +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]) {
|
2014-11-21 05:47:13 +00:00
|
|
|
Err(Error::WrongType(_)) => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
res => panic!("unexpected result {:?}", res)
|
2014-03-28 05:43:10 +00:00
|
|
|
}
|
2013-08-29 05:33:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 05:33:19 +00:00
|
|
|
#[test]
|
2015-03-10 16:16:46 +00:00
|
|
|
#[should_panic(expected = "expected 2 parameters but got 1")]
|
2013-09-12 05:33:19 +00:00
|
|
|
fn test_too_few_params() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-02-01 03:44:39 +00:00
|
|
|
let _ = conn.execute("SELECT $1::INT, $2::INT", &[&1i32]);
|
2013-09-12 05:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-10 16:16:46 +00:00
|
|
|
#[should_panic(expected = "expected 2 parameters but got 3")]
|
2013-09-12 05:33:19 +00:00
|
|
|
fn test_too_many_params() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-02-01 03:44:39 +00:00
|
|
|
let _ = conn.execute("SELECT $1::INT, $2::INT", &[&1i32, &2i32, &3i32]);
|
2013-09-12 05:33:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 00:07:08 +00:00
|
|
|
#[test]
|
2014-03-30 00:01:41 +00:00
|
|
|
fn test_index_named() {
|
2015-12-06 00:15:19 +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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2013-09-04 03:50:02 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
assert_eq!(vec![10i32], result.iter().map(|row| row.get("val")).collect::<Vec<_>>());
|
2013-09-03 01:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-10 16:16:46 +00:00
|
|
|
#[should_panic]
|
2014-03-30 00:01:41 +00:00
|
|
|
fn test_index_named_fail() {
|
2015-12-06 00:15:19 +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"));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2013-09-03 01:53:03 +00:00
|
|
|
|
2015-02-19 07:05:11 +00:00
|
|
|
let _: i32 = result.iter().next().unwrap().get("asdf");
|
2013-09-03 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-30 00:01:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_get_named_err() {
|
2015-12-06 00:15:19 +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"));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-03-30 00:01:41 +00:00
|
|
|
|
2015-12-26 02:32:51 +00:00
|
|
|
match result.iter().next().unwrap().get_opt::<_, i32>("asdf") {
|
|
|
|
None => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
res => panic!("unexpected result {:?}", res),
|
2014-03-30 00:01:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-03-30 02:01:23 +00:00
|
|
|
#[test]
|
|
|
|
fn test_get_was_null() {
|
2015-12-06 00:15:19 +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"));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-03-30 02:01:23 +00:00
|
|
|
|
2015-12-26 02:32:51 +00:00
|
|
|
match result.iter().next().unwrap().get_opt::<_, i32>(0) {
|
|
|
|
Some(Err(Error::Conversion(..))) => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
res => panic!("unexpected result {:?}", res),
|
2014-03-30 02:01:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-12-06 18:41:49 +00:00
|
|
|
#[test]
|
|
|
|
fn test_get_off_by_one() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-12-06 18:41:49 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT 10::INT as id"));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
2014-12-06 18:41:49 +00:00
|
|
|
|
2015-12-26 02:32:51 +00:00
|
|
|
match result.iter().next().unwrap().get_opt::<_, i32>(1) {
|
|
|
|
None => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
res => panic!("unexpected result {:?}", res),
|
2014-12-06 18:41:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-09-12 02:57:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_custom_notice_handler() {
|
2015-01-09 18:50:54 +00:00
|
|
|
static mut count: usize = 0;
|
2013-09-12 02:57:26 +00:00
|
|
|
struct Handler;
|
|
|
|
|
2015-02-22 23:05:19 +00:00
|
|
|
impl HandleNotice for Handler {
|
|
|
|
fn handle_notice(&mut self, notice: DbError) {
|
2015-12-13 04:30:50 +00:00
|
|
|
assert_eq!("note", notice.message);
|
2013-09-12 02:57:26 +00:00
|
|
|
unsafe { count += 1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-01 23:21:47 +00:00
|
|
|
let conn = or_panic!(Connection::connect(
|
2015-12-06 00:15:19 +00:00
|
|
|
"postgres://postgres@localhost?client_min_messages=NOTICE", SslMode::None));
|
2015-01-09 18:50:54 +00:00
|
|
|
conn.set_notice_handler(Box::new(Handler));
|
2014-10-30 04:26:03 +00:00
|
|
|
or_panic!(conn.execute("CREATE FUNCTION pg_temp.note() RETURNS INT AS $$
|
2014-03-28 04:08:22 +00:00
|
|
|
BEGIN
|
|
|
|
RAISE NOTICE 'note';
|
|
|
|
RETURN 1;
|
2014-11-19 18:20:20 +00:00
|
|
|
END; $$ LANGUAGE plpgsql", &[]));
|
|
|
|
or_panic!(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() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-09-19 05:51:11 +00:00
|
|
|
assert!(conn.notifications().iter().next().is_none());
|
2013-10-15 05:41:03 +00:00
|
|
|
}
|
|
|
|
|
2014-11-28 23:01:01 +00:00
|
|
|
fn check_notification(expected: Notification, actual: Notification) {
|
2014-11-26 02:30:28 +00:00
|
|
|
assert_eq!(&expected.channel, &actual.channel);
|
|
|
|
assert_eq!(&expected.payload, &actual.payload);
|
|
|
|
}
|
|
|
|
|
2013-10-15 05:41:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_notification_iterator_some() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-09-19 05:51:11 +00:00
|
|
|
let notifications = conn.notifications();
|
|
|
|
let mut it = notifications.iter();
|
2014-11-19 18:20:20 +00:00
|
|
|
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'", &[]));
|
2013-10-15 05:41:03 +00:00
|
|
|
|
2014-11-01 23:18:09 +00:00
|
|
|
check_notification(Notification {
|
2013-10-15 05:41:03 +00:00
|
|
|
pid: 0,
|
2014-07-10 19:35:57 +00:00
|
|
|
channel: "test_notification_iterator_one_channel".to_string(),
|
|
|
|
payload: "hello".to_string()
|
2014-11-26 02:30:28 +00:00
|
|
|
}, it.next().unwrap());
|
2014-11-01 23:18:09 +00:00
|
|
|
check_notification(Notification {
|
2013-10-15 05:41:03 +00:00
|
|
|
pid: 0,
|
2014-07-10 19:35:57 +00:00
|
|
|
channel: "test_notification_iterator_one_channel2".to_string(),
|
|
|
|
payload: "world".to_string()
|
2014-11-26 02:30:28 +00:00
|
|
|
}, it.next().unwrap());
|
2013-10-15 05:41:03 +00:00
|
|
|
assert!(it.next().is_none());
|
|
|
|
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("NOTIFY test_notification_iterator_one_channel, '!'", &[]));
|
2014-11-01 23:18:09 +00:00
|
|
|
check_notification(Notification {
|
2013-10-15 05:41:03 +00:00
|
|
|
pid: 0,
|
2014-07-10 19:35:57 +00:00
|
|
|
channel: "test_notification_iterator_one_channel".to_string(),
|
|
|
|
payload: "!".to_string()
|
2014-11-26 02:30:28 +00:00
|
|
|
}, it.next().unwrap());
|
2013-10-15 05:41:03 +00:00
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
2014-11-26 02:30:28 +00:00
|
|
|
#[test]
|
|
|
|
fn test_notifications_next_block() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-26 02:30:28 +00:00
|
|
|
or_panic!(conn.execute("LISTEN test_notifications_next_block", &[]));
|
|
|
|
|
2015-02-19 17:17:48 +00:00
|
|
|
let _t = thread::spawn(|| {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-12-06 02:57:17 +00:00
|
|
|
thread::sleep(Duration::from_millis(500));
|
2014-11-26 02:30:28 +00:00
|
|
|
or_panic!(conn.execute("NOTIFY test_notifications_next_block, 'foo'", &[]));
|
|
|
|
});
|
|
|
|
|
2015-09-19 05:51:11 +00:00
|
|
|
let notifications = conn.notifications();
|
2014-11-26 02:30:28 +00:00
|
|
|
check_notification(Notification {
|
|
|
|
pid: 0,
|
|
|
|
channel: "test_notifications_next_block".to_string(),
|
|
|
|
payload: "foo".to_string()
|
2015-09-19 05:51:11 +00:00
|
|
|
}, or_panic!(notifications.blocking_iter().next().unwrap()));
|
2014-11-26 02:30:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 06:13:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_notification_next_timeout() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-10-21 06:13:26 +00:00
|
|
|
or_panic!(conn.execute("LISTEN test_notifications_next_timeout", &[]));
|
|
|
|
|
|
|
|
let _t = thread::spawn(|| {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-12-06 02:57:17 +00:00
|
|
|
thread::sleep(Duration::from_millis(500));
|
2015-10-21 06:13:26 +00:00
|
|
|
or_panic!(conn.execute("NOTIFY test_notifications_next_timeout, 'foo'", &[]));
|
2015-12-06 02:57:17 +00:00
|
|
|
thread::sleep(Duration::from_millis(1500));
|
2015-10-21 06:13:26 +00:00
|
|
|
or_panic!(conn.execute("NOTIFY test_notifications_next_timeout, 'foo'", &[]));
|
|
|
|
});
|
|
|
|
|
|
|
|
let notifications = conn.notifications();
|
|
|
|
let mut it = notifications.timeout_iter(Duration::from_secs(1));
|
|
|
|
check_notification(Notification {
|
|
|
|
pid: 0,
|
|
|
|
channel: "test_notifications_next_timeout".to_string(),
|
|
|
|
payload: "foo".to_string()
|
|
|
|
}, or_panic!(it.next().unwrap()));
|
|
|
|
|
|
|
|
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() {
|
2015-12-06 00:15:19 +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();
|
|
|
|
|
2015-12-07 07:39:40 +00:00
|
|
|
let t = thread::spawn(move || {
|
2015-12-06 02:57:17 +00:00
|
|
|
thread::sleep(Duration::from_millis(500));
|
2015-12-06 00:15:19 +00:00
|
|
|
assert!(postgres::cancel_query("postgres://postgres@localhost", SslMode::None,
|
2015-12-07 07:38:26 +00:00
|
|
|
&cancel_data).is_ok());
|
2014-01-31 04:44:44 +00:00
|
|
|
});
|
2013-10-21 00:32:14 +00:00
|
|
|
|
2014-11-19 18:20:20 +00:00
|
|
|
match conn.execute("SELECT pg_sleep(10)", &[]) {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(ref e)) if e.code == QueryCanceled => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(res) => panic!("Unexpected result {:?}", res),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Unexpected result"),
|
2013-10-21 00:32:14 +00:00
|
|
|
}
|
2015-12-07 07:39:40 +00:00
|
|
|
|
|
|
|
t.join().unwrap();
|
2013-10-21 00:32:14 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 05:55:29 +00:00
|
|
|
#[test]
|
2015-04-28 05:52:06 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
2013-11-10 06:30:20 +00:00
|
|
|
fn test_require_ssl_conn() {
|
2014-12-22 17:14:20 +00:00
|
|
|
let ctx = SslContext::new(SslMethod::Sslv23).unwrap();
|
2014-11-01 23:21:47 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
|
2015-12-06 00:15:19 +00:00
|
|
|
SslMode::Require(&ctx)));
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
|
2013-11-10 05:55:29 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 06:30:20 +00:00
|
|
|
#[test]
|
2015-04-28 05:52:06 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
2013-11-10 06:30:20 +00:00
|
|
|
fn test_prefer_ssl_conn() {
|
2014-12-22 17:14:20 +00:00
|
|
|
let ctx = SslContext::new(SslMethod::Sslv23).unwrap();
|
2014-11-01 23:21:47 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
|
2015-12-06 00:15:19 +00:00
|
|
|
SslMode::Prefer(&ctx)));
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
|
2013-11-10 06:30:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 22:35:17 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "security-framework")]
|
|
|
|
fn security_framework_ssl() {
|
|
|
|
use security_framework::certificate::SecCertificate;
|
|
|
|
use security_framework::secure_transport::ClientBuilder;
|
|
|
|
|
|
|
|
let certificate = include_bytes!("../.travis/server.der");
|
|
|
|
let certificate = or_panic!(SecCertificate::from_der(certificate));
|
|
|
|
let mut builder = ClientBuilder::new();
|
|
|
|
builder.anchor_certificates(&[certificate]);
|
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost",
|
2015-12-06 00:15:19 +00:00
|
|
|
SslMode::Require(&builder)));
|
2015-12-05 22:35:17 +00:00
|
|
|
or_panic!(conn.execute("SELECT 1::VARCHAR", &[]));
|
|
|
|
}
|
|
|
|
|
2013-08-26 05:08:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_plaintext_pass() {
|
2015-12-06 00:15:19 +00:00
|
|
|
or_panic!(Connection::connect("postgres://pass_user:password@localhost/postgres", SslMode::None));
|
2013-08-26 05:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_plaintext_pass_no_pass() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let ret = Connection::connect("postgres://pass_user@localhost/postgres", SslMode::None);
|
2013-08-29 04:24:43 +00:00
|
|
|
match ret {
|
2014-11-21 05:47:13 +00:00
|
|
|
Err(ConnectError::MissingPassword) => (),
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("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() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let ret = Connection::connect("postgres://pass_user:asdf@localhost/postgres", SslMode::None);
|
2013-08-29 04:24:43 +00:00
|
|
|
match ret {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(ConnectError::Db(ref e)) if e.code == InvalidPassword => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Expected error")
|
2013-08-29 04:24:43 +00:00
|
|
|
}
|
2013-08-26 07:36:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 07:43:01 +00:00
|
|
|
#[test]
|
2013-11-06 06:04:12 +00:00
|
|
|
fn test_md5_pass() {
|
2015-12-06 00:15:19 +00:00
|
|
|
or_panic!(Connection::connect("postgres://md5_user:password@localhost/postgres", SslMode::None));
|
2013-11-06 06:04:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_md5_pass_no_pass() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let ret = Connection::connect("postgres://md5_user@localhost/postgres", SslMode::None);
|
2013-11-06 06:04:12 +00:00
|
|
|
match ret {
|
2014-11-21 05:47:13 +00:00
|
|
|
Err(ConnectError::MissingPassword) => (),
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Expected error")
|
2013-11-06 06:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_md5_pass_wrong_pass() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let ret = Connection::connect("postgres://md5_user:asdf@localhost/postgres", SslMode::None);
|
2013-11-06 06:04:12 +00:00
|
|
|
match ret {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(ConnectError::Db(ref e)) if e.code == InvalidPassword => {}
|
2015-01-09 18:50:54 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Expected error")
|
2013-11-06 06:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 04:05:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_execute_copy_from_err() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
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"));
|
2014-11-19 18:20:20 +00:00
|
|
|
match stmt.execute(&[]) {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(ref err)) if err.message.contains("COPY") => {}
|
2015-06-04 05:04:15 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Expected error"),
|
2014-09-30 04:05:42 +00:00
|
|
|
}
|
2014-11-19 18:20:20 +00:00
|
|
|
match stmt.query(&[]) {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(ref err)) if err.message.contains("COPY") => {}
|
2015-06-04 05:04:15 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Expected error"),
|
2015-11-13 07:52:33 +00:00
|
|
|
};
|
2014-09-30 04:05:42 +00:00
|
|
|
}
|
2014-09-30 05:56:43 +00:00
|
|
|
|
2014-10-06 00:31:25 +00:00
|
|
|
#[test]
|
|
|
|
fn test_batch_execute_copy_from_err() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-11-19 18:20:20 +00:00
|
|
|
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") {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(ref err)) if err.message.contains("COPY") => {}
|
2015-06-04 05:04:15 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
2014-10-30 04:26:03 +00:00
|
|
|
_ => panic!("Expected error"),
|
2014-10-06 00:31:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-09 03:29:26 +00:00
|
|
|
|
2015-06-12 07:39:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_copy_io_error() {
|
|
|
|
struct ErrorReader;
|
|
|
|
|
|
|
|
impl io::Read for ErrorReader {
|
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
|
|
|
|
Err(io::Error::new(io::ErrorKind::AddrNotAvailable, "boom"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-06-12 07:39:12 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT)", &[]));
|
|
|
|
let stmt = or_panic!(conn.prepare("COPY foo (id) FROM STDIN"));
|
|
|
|
match stmt.copy_in(&[], &mut ErrorReader) {
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Io(ref e)) if e.kind() == io::ErrorKind::AddrNotAvailable => {}
|
2015-06-12 07:39:12 +00:00
|
|
|
Err(err) => panic!("Unexpected error {:?}", err),
|
|
|
|
_ => panic!("Expected error"),
|
|
|
|
}
|
|
|
|
|
|
|
|
or_panic!(conn.execute("SELECT 1", &[]));
|
|
|
|
}
|
|
|
|
|
2015-05-26 03:40:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_copy() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-05-26 03:40:57 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT)", &[]));
|
|
|
|
let stmt = or_panic!(conn.prepare("COPY foo (id) FROM STDIN"));
|
|
|
|
let mut data = &b"1\n2\n3\n5\n8\n"[..];
|
|
|
|
assert_eq!(5, or_panic!(stmt.copy_in(&[], &mut data)));
|
|
|
|
let stmt = or_panic!(conn.prepare("SELECT id FROM foo ORDER BY id"));
|
|
|
|
assert_eq!(vec![1i32, 2, 3, 5, 8],
|
|
|
|
stmt.query(&[]).unwrap().iter().map(|r| r.get(0)).collect::<Vec<i32>>());
|
|
|
|
}
|
|
|
|
|
2015-08-16 03:39:13 +00:00
|
|
|
#[test]
|
2015-08-16 06:21:39 +00:00
|
|
|
fn test_query_copy_out_err() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-08-16 03:39:13 +00:00
|
|
|
or_panic!(conn.batch_execute("
|
|
|
|
CREATE TEMPORARY TABLE foo (id INT);
|
|
|
|
INSERT INTO foo (id) VALUES (0), (1), (2), (3)"));
|
|
|
|
let stmt = or_panic!(conn.prepare("COPY foo (id) TO STDOUT"));
|
|
|
|
match stmt.query(&[]) {
|
|
|
|
Ok(_) => panic!("unexpected success"),
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Io(ref e)) if e.to_string().contains("COPY") => {}
|
2015-08-16 03:39:13 +00:00
|
|
|
Err(e) => panic!("unexpected error {:?}", e),
|
2015-11-13 07:52:33 +00:00
|
|
|
};
|
2015-08-16 03:39:13 +00:00
|
|
|
}
|
2015-05-26 03:40:57 +00:00
|
|
|
|
2015-08-16 06:21:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_copy_out() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-08-16 06:21:39 +00:00
|
|
|
or_panic!(conn.batch_execute("
|
|
|
|
CREATE TEMPORARY TABLE foo (id INT);
|
|
|
|
INSERT INTO foo (id) VALUES (0), (1), (2), (3)"));
|
|
|
|
let stmt = or_panic!(conn.prepare("COPY (SELECT id FROM foo ORDER BY id) TO STDOUT"));
|
2015-09-16 06:11:14 +00:00
|
|
|
let mut buf = vec![];
|
|
|
|
let count = or_panic!(stmt.copy_out(&[], &mut buf));
|
|
|
|
assert_eq!(count, 4);
|
|
|
|
assert_eq!(buf, b"0\n1\n2\n3\n");
|
2015-08-16 06:21:39 +00:00
|
|
|
or_panic!(conn.batch_execute("SELECT 1"));
|
|
|
|
}
|
|
|
|
|
2015-10-24 05:55:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_copy_out_error() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-10-24 05:55:39 +00:00
|
|
|
or_panic!(conn.batch_execute("
|
|
|
|
CREATE TEMPORARY TABLE foo (id INT);
|
|
|
|
INSERT INTO foo (id) VALUES (0), (1), (2), (3)"));
|
|
|
|
let stmt = or_panic!(conn.prepare("COPY (SELECT id FROM foo ORDER BY id) TO STDOUT (OIDS)"));
|
|
|
|
let mut buf = vec![];
|
|
|
|
match stmt.copy_out(&[], &mut buf) {
|
|
|
|
Ok(_) => panic!("unexpected success"),
|
2015-12-26 03:14:09 +00:00
|
|
|
Err(Error::Db(..)) => {}
|
2015-10-24 05:55:39 +00:00
|
|
|
Err(e) => panic!("unexpected error {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 03:29:26 +00:00
|
|
|
#[test]
|
|
|
|
// Just make sure the impls don't infinite loop
|
|
|
|
fn test_generic_connection() {
|
|
|
|
fn f<T>(t: &T) where T: GenericConnection {
|
2014-11-19 18:20:20 +00:00
|
|
|
or_panic!(t.execute("SELECT 1", &[]));
|
2014-10-09 03:29:26 +00:00
|
|
|
}
|
|
|
|
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2014-10-09 03:29:26 +00:00
|
|
|
f(&conn);
|
2014-10-30 04:26:03 +00:00
|
|
|
let trans = or_panic!(conn.transaction());
|
2014-10-09 03:29:26 +00:00
|
|
|
f(&trans);
|
|
|
|
}
|
2015-01-22 08:04:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_range_element_type() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-01-22 08:04:58 +00:00
|
|
|
let trans = or_panic!(conn.transaction());
|
|
|
|
or_panic!(trans.execute("CREATE TYPE floatrange AS RANGE (
|
|
|
|
subtype = float8,
|
|
|
|
subtype_diff = float8mi
|
|
|
|
)", &[]));
|
|
|
|
let stmt = or_panic!(trans.prepare("SELECT $1::floatrange"));
|
|
|
|
match &stmt.param_types()[0] {
|
2015-02-19 05:04:44 +00:00
|
|
|
&Type::Other(ref u) => {
|
2015-02-06 07:23:20 +00:00
|
|
|
assert_eq!("floatrange", u.name());
|
2015-02-14 20:46:18 +00:00
|
|
|
assert_eq!(&Kind::Range(Type::Float8), u.kind());
|
2015-09-26 20:23:55 +00:00
|
|
|
assert_eq!("public", u.schema());
|
2015-01-22 08:04:58 +00:00
|
|
|
}
|
|
|
|
t => panic!("Unexpected type {:?}", t)
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 06:24:47 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prepare_cached() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-01-23 06:24:47 +00:00
|
|
|
or_panic!(conn.execute("CREATE TEMPORARY TABLE foo (id INT)", &[]));
|
|
|
|
or_panic!(conn.execute("INSERT INTO foo (id) VALUES (1), (2)", &[]));
|
|
|
|
|
|
|
|
let stmt = or_panic!(conn.prepare_cached("SELECT id FROM foo ORDER BY id"));
|
2015-04-02 18:34:42 +00:00
|
|
|
assert_eq!(vec![1, 2], or_panic!(stmt.query(&[])).iter().map(|r| r.get(0)).collect::<Vec<i32>>());
|
2015-01-23 06:33:35 +00:00
|
|
|
or_panic!(stmt.finish());
|
2015-01-23 06:24:47 +00:00
|
|
|
|
|
|
|
let stmt = or_panic!(conn.prepare_cached("SELECT id FROM foo ORDER BY id"));
|
2015-04-02 18:34:42 +00:00
|
|
|
assert_eq!(vec![1, 2], or_panic!(stmt.query(&[])).iter().map(|r| r.get(0)).collect::<Vec<i32>>());
|
2015-01-23 06:33:35 +00:00
|
|
|
or_panic!(stmt.finish());
|
2015-01-23 06:24:47 +00:00
|
|
|
|
|
|
|
let stmt = or_panic!(conn.prepare_cached("SELECT id FROM foo ORDER BY id DESC"));
|
2015-04-02 18:34:42 +00:00
|
|
|
assert_eq!(vec![2, 1], or_panic!(stmt.query(&[])).iter().map(|r| r.get(0)).collect::<Vec<i32>>());
|
2015-01-23 06:33:35 +00:00
|
|
|
or_panic!(stmt.finish());
|
2015-01-23 06:24:47 +00:00
|
|
|
}
|
2015-01-27 05:09:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_active() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-01-27 05:09:50 +00:00
|
|
|
assert!(conn.is_active());
|
|
|
|
let trans = or_panic!(conn.transaction());
|
|
|
|
assert!(!conn.is_active());
|
|
|
|
assert!(trans.is_active());
|
|
|
|
{
|
|
|
|
let trans2 = or_panic!(trans.transaction());
|
|
|
|
assert!(!conn.is_active());
|
|
|
|
assert!(!trans.is_active());
|
|
|
|
assert!(trans2.is_active());
|
|
|
|
or_panic!(trans2.finish());
|
|
|
|
}
|
|
|
|
assert!(!conn.is_active());
|
|
|
|
assert!(trans.is_active());
|
|
|
|
or_panic!(trans.finish());
|
|
|
|
assert!(conn.is_active());
|
|
|
|
}
|
2015-02-16 04:22:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parameter() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-02-16 04:22:56 +00:00
|
|
|
assert_eq!(Some("UTF8".to_string()), conn.parameter("client_encoding"));
|
|
|
|
assert_eq!(None, conn.parameter("asdf"));
|
|
|
|
}
|
2015-02-16 07:24:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_bytes() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-02-16 07:24:09 +00:00
|
|
|
let stmt = or_panic!(conn.prepare("SELECT '\\x00010203'::BYTEA"));
|
2015-02-19 07:05:11 +00:00
|
|
|
let result = or_panic!(stmt.query(&[]));
|
|
|
|
assert_eq!(b"\x00\x01\x02\x03", result.iter().next().unwrap().get_bytes(0).unwrap());
|
2015-02-16 07:24:09 +00:00
|
|
|
}
|
2015-02-15 01:16:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_opt_wrong_type() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-02-15 01:16:25 +00:00
|
|
|
let stmt = conn.prepare("SELECT 1::INT").unwrap();
|
2015-02-19 07:05:11 +00:00
|
|
|
let res = stmt.query(&[]).unwrap();
|
|
|
|
match res.iter().next().unwrap().get_opt::<_, String>(0) {
|
2015-12-26 02:32:51 +00:00
|
|
|
Some(Ok(_)) => panic!("unexpected success"),
|
|
|
|
Some(Err(Error::WrongType(Type::Int4))) => {}
|
|
|
|
Some(Err(e)) => panic!("unexpected error {}", e),
|
|
|
|
None => panic!("unexpected None"),
|
2015-02-15 01:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-16 03:31:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn url_encoded_password() {
|
|
|
|
let params = "postgresql://username%7b%7c:password%7b%7c@localhost".into_connect_params().unwrap();
|
|
|
|
assert_eq!("username{|", ¶ms.user.as_ref().unwrap().user[..]);
|
|
|
|
assert_eq!("password{|", ¶ms.user.as_ref().unwrap().password.as_ref().unwrap()[..]);
|
|
|
|
}
|
2015-04-23 05:03:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transaction_isolation_level() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", SslMode::None));
|
2015-04-25 05:10:57 +00:00
|
|
|
assert_eq!(IsolationLevel::ReadCommitted, or_panic!(conn.transaction_isolation()));
|
2015-04-23 05:03:09 +00:00
|
|
|
or_panic!(conn.set_transaction_isolation(IsolationLevel::ReadUncommitted));
|
2015-04-25 05:10:57 +00:00
|
|
|
assert_eq!(IsolationLevel::ReadUncommitted, or_panic!(conn.transaction_isolation()));
|
2015-04-23 05:03:09 +00:00
|
|
|
or_panic!(conn.set_transaction_isolation(IsolationLevel::RepeatableRead));
|
2015-04-25 05:10:57 +00:00
|
|
|
assert_eq!(IsolationLevel::RepeatableRead, or_panic!(conn.transaction_isolation()));
|
2015-04-23 05:03:09 +00:00
|
|
|
or_panic!(conn.set_transaction_isolation(IsolationLevel::Serializable));
|
2015-04-25 05:10:57 +00:00
|
|
|
assert_eq!(IsolationLevel::Serializable, or_panic!(conn.transaction_isolation()));
|
2015-04-23 05:03:09 +00:00
|
|
|
or_panic!(conn.set_transaction_isolation(IsolationLevel::ReadCommitted));
|
2015-04-25 05:10:57 +00:00
|
|
|
assert_eq!(IsolationLevel::ReadCommitted, or_panic!(conn.transaction_isolation()));
|
2015-04-23 05:03:09 +00:00
|
|
|
}
|
2015-06-07 16:15:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rows_index() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-06-07 16:15:52 +00:00
|
|
|
conn.batch_execute("
|
|
|
|
CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY);
|
|
|
|
INSERT INTO foo (id) VALUES (1), (2), (3);
|
|
|
|
").unwrap();
|
|
|
|
let stmt = conn.prepare("SELECT id FROM foo ORDER BY id").unwrap();
|
|
|
|
let rows = stmt.query(&[]).unwrap();
|
|
|
|
assert_eq!(3, rows.len());
|
|
|
|
assert_eq!(2i32, rows.get(1).get(0));
|
|
|
|
}
|
2015-07-31 05:04:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_row_case_insensitive() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-07-31 05:04:22 +00:00
|
|
|
conn.batch_execute("CREATE TEMPORARY TABLE foo (foo INT, \"bAr\" INT, \"Bar\" INT);").unwrap();
|
|
|
|
let stmt = conn.prepare("SELECT * FROM foo").unwrap();
|
|
|
|
assert_eq!(Some(0), "foo".idx(&stmt));
|
|
|
|
assert_eq!(Some(0), "FOO".idx(&stmt));
|
|
|
|
assert_eq!(Some(1), "bar".idx(&stmt));
|
|
|
|
assert_eq!(Some(1), "bAr".idx(&stmt));
|
|
|
|
assert_eq!(Some(2), "Bar".idx(&stmt));
|
|
|
|
}
|
2015-09-27 05:57:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_type_names() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-09-27 05:57:36 +00:00
|
|
|
let stmt = conn.prepare("SELECT t.oid, t.typname
|
|
|
|
FROM pg_catalog.pg_type t, pg_namespace n
|
|
|
|
WHERE n.oid = t.typnamespace
|
|
|
|
AND n.nspname = 'pg_catalog'
|
|
|
|
AND t.oid < 10000
|
|
|
|
AND t.typtype != 'c'").unwrap();
|
2015-11-13 06:56:05 +00:00
|
|
|
for row in &stmt.query(&[]).unwrap() {
|
2015-09-27 05:57:36 +00:00
|
|
|
let id: Oid = row.get(0);
|
|
|
|
let name: String = row.get(1);
|
|
|
|
assert_eq!(Type::from_oid(id).unwrap().name(), name);
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 03:34:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_conn_query() {
|
2015-12-06 00:15:19 +00:00
|
|
|
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
|
2015-11-14 03:34:11 +00:00
|
|
|
conn.batch_execute("
|
|
|
|
CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY);
|
|
|
|
INSERT INTO foo (id) VALUES (1), (2), (3);
|
|
|
|
").unwrap();
|
|
|
|
let ids = conn.query("SELECT id FROM foo ORDER BY id", &[])
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|r| r.get(0))
|
|
|
|
.collect::<Vec<i32>>();
|
|
|
|
assert_eq!(ids, [1, 2, 3]);
|
|
|
|
}
|