Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeshadow committed Feb 7, 2025
1 parent 9ad2acf commit 9a4475a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions postgres/examples/pooling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use xitca_postgres::{
dev::{ClientBorrowMut, Encode, Prepare, Query, Response},
error::{DriverDown, Error},
iter::AsyncLendingIterator,
transaction::Transaction,
transaction::{Transaction, TransactionBuilder},
types::{Oid, Type},
Client, Config, Execute, Postgres,
};
Expand Down Expand Up @@ -103,7 +103,7 @@ impl ClientBorrowMut for PoolConnection {
impl PoolConnection {
// with above traits implements you can begin a transaction with your client new type
pub async fn transaction(&mut self) -> Result<Transaction<Self>, Error> {
Transaction::<Self>::builder().begin(self).await
TransactionBuilder::new().begin(self).await
}
}

Expand Down
4 changes: 2 additions & 2 deletions postgres/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::{
query::Query,
session::Session,
statement::Statement,
transaction::Transaction,
transaction::{Transaction, TransactionBuilder},
types::{Oid, Type},
};

Expand Down Expand Up @@ -128,7 +128,7 @@ impl Client {
/// start a transaction
#[inline]
pub fn transaction(&mut self) -> impl Future<Output = Result<Transaction<Self>, Error>> + Send {
Transaction::<Self>::builder().begin(self)
TransactionBuilder::new().begin(self)
}

/// Executes a `COPY FROM STDIN` statement, returning a sink used to write the copy data.
Expand Down
4 changes: 2 additions & 2 deletions postgres/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::{
query::Query,
session::Session,
statement::{Statement, StatementNamed},
transaction::Transaction,
transaction::{Transaction, TransactionBuilder},
types::{Oid, Type},
BoxedFuture, Postgres,
};
Expand Down Expand Up @@ -148,7 +148,7 @@ impl PoolConnection<'_> {
/// function the same as [`Client::transaction`]
#[inline]
pub fn transaction(&mut self) -> impl Future<Output = Result<Transaction<Self>, Error>> + Send {
Transaction::<Self>::builder().begin(self)
TransactionBuilder::new().begin(self)
}

/// function the same as [`Client::copy_in`]
Expand Down
4 changes: 0 additions & 4 deletions postgres/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ impl<C> Transaction<'_, C>
where
C: Prepare + ClientBorrowMut,
{
pub fn builder() -> TransactionBuilder {
TransactionBuilder::new()
}

/// Binds a statement to a set of parameters, creating a [`Portal`] which can be incrementally queried.
///
/// Portals only last for the duration of the transaction in which they are created, and can only be used on the
Expand Down
2 changes: 1 addition & 1 deletion postgres/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct TransactionBuilder {
}

impl TransactionBuilder {
pub(crate) fn new() -> Self {
pub const fn new() -> Self {
Self {
isolation_level: None,
read_only: None,
Expand Down
27 changes: 27 additions & 0 deletions postgres/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use xitca_postgres::{
iter::AsyncLendingIterator,
pipeline::Pipeline,
statement::Statement,
transaction::{IsolationLevel, TransactionBuilder},
types::Type,
Client, Execute, Postgres,
};
Expand Down Expand Up @@ -224,6 +225,32 @@ async fn query_portal() {
assert!(stream3.try_next().await.unwrap().is_none());
}

#[tokio::test]
async fn transaction_isolation() {
let mut client = connect("postgres://postgres:postgres@localhost:5432").await;

std::path::Path::new("samples/test.sql").execute(&client).await.unwrap();

let transaction = TransactionBuilder::new()
.isolation_level(IsolationLevel::Serializable)
.read_only(true)
.deferrable(true)
.begin(&mut client)
.await
.unwrap();

let stmt = Statement::named("SELECT id, name FROM foo ORDER BY id", &[])
.execute(&transaction)
.await
.unwrap();

let mut res = stmt.query(&transaction).await.unwrap();

let row = res.try_next().await.unwrap().unwrap();
assert_eq!(row.get::<i32>(0), 1);
assert_eq!(row.get::<&str>(1), "alice");
}

#[tokio::test]
async fn query_unnamed_with_transaction() {
let mut client = connect("postgres://postgres:postgres@localhost:5432").await;
Expand Down

0 comments on commit 9a4475a

Please sign in to comment.