Skip to content

Commit 06db735

Browse files
committed
Add tests for transaction checks
1 parent ec7dd96 commit 06db735

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,9 @@ pub enum SslMode {
928928
RequireSsl(SslContext)
929929
}
930930

931-
/// Represents a transaction on a database connection
931+
/// Represents a transaction on a database connection.
932+
///
933+
/// The transaction will commit by default.
932934
pub struct PostgresTransaction<'conn> {
933935
conn: &'conn PostgresConnection,
934936
commit: Cell<bool>,

src/test.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use error::{PgConnectDbError,
3131
SyntaxError,
3232
InvalidPassword,
3333
QueryCanceled,
34-
InvalidCatalogName};
34+
InvalidCatalogName,
35+
PgWrongTransaction};
3536
use types::{ToSql, FromSql, PgInt4, PgVarchar};
3637
use types::array::{ArrayBase};
3738
use types::range::{Range, Inclusive, Exclusive, RangeBound};
@@ -295,6 +296,39 @@ fn test_nested_transactions_finish() {
295296
assert_eq!(vec![1i32], result.map(|row| row[1]).collect());
296297
}
297298

299+
#[test]
300+
fn test_conn_prepare_with_trans() {
301+
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
302+
let _trans = or_fail!(conn.transaction());
303+
match conn.prepare("") {
304+
Err(PgWrongTransaction) => {}
305+
Err(r) => fail!("Unexpected error {}", r),
306+
Ok(_) => fail!("Unexpected success"),
307+
}
308+
match conn.transaction() {
309+
Err(PgWrongTransaction) => {}
310+
Err(r) => fail!("Unexpected error {}", r),
311+
Ok(_) => fail!("Unexpected success"),
312+
}
313+
}
314+
315+
#[test]
316+
fn test_trans_prepare_with_nested_trans() {
317+
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));
318+
let trans = or_fail!(conn.transaction());
319+
let _trans2 = or_fail!(trans.transaction());
320+
match trans.prepare("") {
321+
Err(PgWrongTransaction) => {}
322+
Err(r) => fail!("Unexpected error {}", r),
323+
Ok(_) => fail!("Unexpected success"),
324+
}
325+
match trans.transaction() {
326+
Err(PgWrongTransaction) => {}
327+
Err(r) => fail!("Unexpected error {}", r),
328+
Ok(_) => fail!("Unexpected success"),
329+
}
330+
}
331+
298332
#[test]
299333
fn test_stmt_finish() {
300334
let conn = or_fail!(PostgresConnection::connect("postgres://postgres@localhost", &NoSsl));

0 commit comments

Comments
 (0)