Skip to content

Commit

Permalink
test: 💍 Verify get_transaction_depth() on postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Aug 11, 2024
1 parent a234e34 commit a1e92a6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/postgres/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::postgres::{
PgPoolOptions, PgRow, PgSeverity, Postgres,
};
use sqlx::{Column, Connection, Executor, Row, Statement, TypeInfo};
use sqlx_core::connection::TransactionDepth;
use sqlx_core::{bytes::Bytes, error::BoxDynError};
use sqlx_test::{new, pool, setup_if_needed};
use std::env;
Expand Down Expand Up @@ -515,6 +516,7 @@ async fn it_can_work_with_transactions() -> anyhow::Result<()> {
#[sqlx_macros::test]
async fn it_can_work_with_nested_transactions() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
assert_eq!(conn.get_transaction_depth(), 0);

conn.execute("CREATE TABLE IF NOT EXISTS _sqlx_users_2523 (id INTEGER PRIMARY KEY)")
.await?;
Expand All @@ -523,6 +525,7 @@ async fn it_can_work_with_nested_transactions() -> anyhow::Result<()> {

// begin
let mut tx = conn.begin().await?; // transaction
assert_eq!(conn.get_transaction_depth(), 1);

// insert a user
sqlx::query("INSERT INTO _sqlx_users_2523 (id) VALUES ($1)")
Expand All @@ -532,6 +535,7 @@ async fn it_can_work_with_nested_transactions() -> anyhow::Result<()> {

// begin once more
let mut tx2 = tx.begin().await?; // savepoint
assert_eq!(conn.get_transaction_depth(), 2);

// insert another user
sqlx::query("INSERT INTO _sqlx_users_2523 (id) VALUES ($1)")
Expand All @@ -541,6 +545,7 @@ async fn it_can_work_with_nested_transactions() -> anyhow::Result<()> {

// never mind, rollback
tx2.rollback().await?; // roll that one back
assert_eq!(conn.get_transaction_depth(), 1);

// did we really?
let (count,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM _sqlx_users_2523")
Expand All @@ -551,6 +556,7 @@ async fn it_can_work_with_nested_transactions() -> anyhow::Result<()> {

// actually, commit
tx.commit().await?;
assert_eq!(conn.get_transaction_depth(), 0);

// did we really?
let (count,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM _sqlx_users_2523")
Expand Down

0 comments on commit a1e92a6

Please sign in to comment.