From 06db735101151b714900e3362711f6b22d4899f0 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 18 May 2014 19:46:21 -0700 Subject: [PATCH] Add tests for transaction checks --- src/lib.rs | 4 +++- src/test.rs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f8a41089..e4fb4bba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -928,7 +928,9 @@ pub enum SslMode { 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> { conn: &'conn PostgresConnection, commit: Cell, diff --git a/src/test.rs b/src/test.rs index fd063443..b719c7ac 100644 --- a/src/test.rs +++ b/src/test.rs @@ -31,7 +31,8 @@ use error::{PgConnectDbError, SyntaxError, InvalidPassword, QueryCanceled, - InvalidCatalogName}; + InvalidCatalogName, + PgWrongTransaction}; use types::{ToSql, FromSql, PgInt4, PgVarchar}; use types::array::{ArrayBase}; 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()); } +#[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] fn test_stmt_finish() { let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));