Add tests for transaction checks

This commit is contained in:
Steven Fackler 2014-05-18 19:46:21 -07:00
parent ec7dd96a30
commit 06db735101
2 changed files with 38 additions and 2 deletions

View File

@ -928,7 +928,9 @@ pub enum SslMode {
RequireSsl(SslContext) RequireSsl(SslContext)
} }
/// Represents a transaction on a database connection /// Represents a transaction on a database connection.
///
/// The transaction will commit by default.
pub struct PostgresTransaction<'conn> { pub struct PostgresTransaction<'conn> {
conn: &'conn PostgresConnection, conn: &'conn PostgresConnection,
commit: Cell<bool>, commit: Cell<bool>,

View File

@ -31,7 +31,8 @@ use error::{PgConnectDbError,
SyntaxError, SyntaxError,
InvalidPassword, InvalidPassword,
QueryCanceled, QueryCanceled,
InvalidCatalogName}; InvalidCatalogName,
PgWrongTransaction};
use types::{ToSql, FromSql, PgInt4, PgVarchar}; use types::{ToSql, FromSql, PgInt4, PgVarchar};
use types::array::{ArrayBase}; use types::array::{ArrayBase};
use types::range::{Range, Inclusive, Exclusive, RangeBound}; use types::range::{Range, Inclusive, Exclusive, RangeBound};
@ -295,6 +296,39 @@ fn test_nested_transactions_finish() {
assert_eq!(vec![1i32], result.map(|row| row[1]).collect()); assert_eq!(vec![1i32], result.map(|row| row[1]).collect());
} }
#[test]
fn test_conn_prepare_with_trans() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
let _trans = or_fail!(conn.transaction());
match conn.prepare("") {
Err(PgWrongTransaction) => {}
Err(r) => fail!("Unexpected error {}", r),
Ok(_) => fail!("Unexpected success"),
}
match conn.transaction() {
Err(PgWrongTransaction) => {}
Err(r) => fail!("Unexpected error {}", r),
Ok(_) => fail!("Unexpected success"),
}
}
#[test]
fn test_trans_prepare_with_nested_trans() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
let trans = or_fail!(conn.transaction());
let _trans2 = or_fail!(trans.transaction());
match trans.prepare("") {
Err(PgWrongTransaction) => {}
Err(r) => fail!("Unexpected error {}", r),
Ok(_) => fail!("Unexpected success"),
}
match trans.transaction() {
Err(PgWrongTransaction) => {}
Err(r) => fail!("Unexpected error {}", r),
Ok(_) => fail!("Unexpected success"),
}
}
#[test] #[test]
fn test_stmt_finish() { fn test_stmt_finish() {
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl)); let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));