Skip to content

Commit

Permalink
Move code
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Sep 18, 2021
1 parent 46bb460 commit 110e2b1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 85 deletions.
86 changes: 1 addition & 85 deletions tests/query_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod common;

pub use common::{bakery_chain::*, setup::*, TestContext};
use sea_orm::{DatabaseTransaction, DbErr};
use sea_orm::{DbErr};
pub use sea_orm::entity::*;
pub use sea_orm::{QueryFilter, ConnectionTrait};

Expand Down Expand Up @@ -215,87 +215,3 @@ pub async fn find_all_filter_with_results() {

ctx.delete().await;
}

#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
pub async fn transaction() {
let ctx = TestContext::new("transaction_test").await;

ctx.db.transaction::<_, (), DbErr>(|txn| Box::pin(async move {
let _ = bakery::ActiveModel {
name: Set("SeaSide Bakery".to_owned()),
profit_margin: Set(10.4),
..Default::default()
}
.save(txn)
.await?;

let _ = bakery::ActiveModel {
name: Set("Top Bakery".to_owned()),
profit_margin: Set(15.0),
..Default::default()
}
.save(txn)
.await?;

let bakeries = Bakery::find()
.filter(bakery::Column::Name.contains("Bakery"))
.all(txn)
.await?;

assert_eq!(bakeries.len(), 2);

Ok(())
})).await.unwrap();

ctx.delete().await;
}

#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
pub async fn transaction_with_reference() {
let ctx = TestContext::new("transaction_with_reference_test").await;
let name1 = "SeaSide Bakery";
let name2 = "Top Bakery";
let search_name = "Bakery";
ctx.db.transaction(|txn| _transaction_with_reference(txn, name1, name2, search_name)).await.unwrap();

ctx.delete().await;
}

fn _transaction_with_reference<'a>(txn: &'a DatabaseTransaction, name1: &'a str, name2: &'a str, search_name: &'a str) -> std::pin::Pin<Box<dyn std::future::Future<Output=Result<(), DbErr>> + Send + 'a>> {
Box::pin(async move {
let _ = bakery::ActiveModel {
name: Set(name1.to_owned()),
profit_margin: Set(10.4),
..Default::default()
}
.save(txn)
.await?;

let _ = bakery::ActiveModel {
name: Set(name2.to_owned()),
profit_margin: Set(15.0),
..Default::default()
}
.save(txn)
.await?;

let bakeries = Bakery::find()
.filter(bakery::Column::Name.contains(search_name))
.all(txn)
.await?;

assert_eq!(bakeries.len(), 2);

Ok(())
})
}
90 changes: 90 additions & 0 deletions tests/transaction_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
pub mod common;

pub use common::{bakery_chain::*, setup::*, TestContext};
use sea_orm::{DatabaseTransaction, DbErr};
pub use sea_orm::entity::*;
pub use sea_orm::{QueryFilter, ConnectionTrait};

#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
pub async fn transaction() {
let ctx = TestContext::new("transaction_test").await;

ctx.db.transaction::<_, (), DbErr>(|txn| Box::pin(async move {
let _ = bakery::ActiveModel {
name: Set("SeaSide Bakery".to_owned()),
profit_margin: Set(10.4),
..Default::default()
}
.save(txn)
.await?;

let _ = bakery::ActiveModel {
name: Set("Top Bakery".to_owned()),
profit_margin: Set(15.0),
..Default::default()
}
.save(txn)
.await?;

let bakeries = Bakery::find()
.filter(bakery::Column::Name.contains("Bakery"))
.all(txn)
.await?;

assert_eq!(bakeries.len(), 2);

Ok(())
})).await.unwrap();

ctx.delete().await;
}

#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
pub async fn transaction_with_reference() {
let ctx = TestContext::new("transaction_with_reference_test").await;
let name1 = "SeaSide Bakery";
let name2 = "Top Bakery";
let search_name = "Bakery";
ctx.db.transaction(|txn| _transaction_with_reference(txn, name1, name2, search_name)).await.unwrap();

ctx.delete().await;
}

fn _transaction_with_reference<'a>(txn: &'a DatabaseTransaction, name1: &'a str, name2: &'a str, search_name: &'a str) -> std::pin::Pin<Box<dyn std::future::Future<Output=Result<(), DbErr>> + Send + 'a>> {
Box::pin(async move {
let _ = bakery::ActiveModel {
name: Set(name1.to_owned()),
profit_margin: Set(10.4),
..Default::default()
}
.save(txn)
.await?;

let _ = bakery::ActiveModel {
name: Set(name2.to_owned()),
profit_margin: Set(15.0),
..Default::default()
}
.save(txn)
.await?;

let bakeries = Bakery::find()
.filter(bakery::Column::Name.contains(search_name))
.all(txn)
.await?;

assert_eq!(bakeries.len(), 2);

Ok(())
})
}

0 comments on commit 110e2b1

Please sign in to comment.