From c7ff5d8d05098984a690601489c7a70363a51bec Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Tue, 13 Jul 2021 10:03:47 +1000 Subject: [PATCH] Setup done in TestContext --- tests/common/mod.rs | 29 ++++++++++++----------------- tests/common/setup/mod.rs | 15 +++++++++++++++ tests/common/{ => setup}/schema.rs | 2 +- tests/relational_tests.rs | 12 ++++++------ 4 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 tests/common/setup/mod.rs rename tests/common/{ => setup}/schema.rs (99%) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index c891b51a..e7c56f1c 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,8 +1,9 @@ -pub mod schema; -use sea_orm::{Database, DatabaseConnection}; +pub mod setup; +use sea_orm::{DatabaseConnection, Statement}; pub mod bakery_chain; pub use bakery_chain::*; +#[macro_export] macro_rules! function { () => {{ fn f() {} @@ -17,34 +18,28 @@ macro_rules! function { pub struct TestContext { base_url: String, db_name: String, - pub db_conn: DatabaseConnection, + pub db: DatabaseConnection, } impl TestContext { pub async fn new(base_url: &str, db_name: &str) -> Self { - let db_conn = Database::connect("sqlite::memory:").await.unwrap(); - Self::setup_schema(&db_conn).await; + let db: DatabaseConnection = setup::setup().await; + + // let stmt: Statement = Statement::from("BEGIN".to_string()); + // let _ = db.execute(stmt).await; Self { base_url: base_url.to_string(), db_name: db_name.to_string(), - db_conn, + db, } } - - async fn setup_schema(db: &DatabaseConnection) { - assert!(schema::create_bakery_table(db).await.is_ok()); - assert!(schema::create_baker_table(db).await.is_ok()); - assert!(schema::create_customer_table(db).await.is_ok()); - assert!(schema::create_order_table(db).await.is_ok()); - assert!(schema::create_lineitem_table(db).await.is_ok()); - assert!(schema::create_cake_table(db).await.is_ok()); - assert!(schema::create_cakes_bakers_table(db).await.is_ok()); - } } impl Drop for TestContext { fn drop(&mut self) { - println!("dropping context"); + // println!("dropping context"); + // let stmt: Statement = Statement::from("ROLLBACK".to_string()); + // let _ = self.db.execute(stmt); } } diff --git a/tests/common/setup/mod.rs b/tests/common/setup/mod.rs new file mode 100644 index 00000000..ecdb6b39 --- /dev/null +++ b/tests/common/setup/mod.rs @@ -0,0 +1,15 @@ +use sea_orm::{Database, DatabaseConnection}; +pub mod schema; +pub use schema::*; + +pub async fn setup() -> DatabaseConnection { + let db = Database::connect("sqlite::memory:").await.unwrap(); + assert!(schema::create_bakery_table(&db).await.is_ok()); + assert!(schema::create_baker_table(&db).await.is_ok()); + assert!(schema::create_customer_table(&db).await.is_ok()); + assert!(schema::create_order_table(&db).await.is_ok()); + assert!(schema::create_lineitem_table(&db).await.is_ok()); + assert!(schema::create_cake_table(&db).await.is_ok()); + assert!(schema::create_cakes_bakers_table(&db).await.is_ok()); + db +} diff --git a/tests/common/schema.rs b/tests/common/setup/schema.rs similarity index 99% rename from tests/common/schema.rs rename to tests/common/setup/schema.rs index 6ebcc381..e165fe43 100644 --- a/tests/common/schema.rs +++ b/tests/common/setup/schema.rs @@ -1,7 +1,7 @@ use sea_orm::{error::*, sea_query, DbConn, ExecResult}; use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, TableCreateStatement}; -pub use super::bakery_chain::*; +pub use super::super::bakery_chain::*; async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result { let builder = db.get_schema_builder_backend(); diff --git a/tests/relational_tests.rs b/tests/relational_tests.rs index edefa9ef..3eecce3c 100644 --- a/tests/relational_tests.rs +++ b/tests/relational_tests.rs @@ -1,10 +1,10 @@ use sea_orm::{entity::*, InsertResult}; -pub mod bakery_chain; -pub use bakery_chain::*; +// pub mod bakery_chain; +// pub use bakery_chain::*; pub mod common; -pub use common::TestContext; +pub use common::{setup::*, TestContext}; #[async_std::test] // cargo test --test realtional_tests -- --nocapture @@ -13,7 +13,7 @@ async fn main() { } pub async fn test_left_join() { - let ctx = TestContext::new("test", "test").await; + let ctx = TestContext::new("test", function!()).await; let seaside_bakery = bakery::ActiveModel { name: Set("SeaSide Bakery".to_owned()), @@ -21,12 +21,12 @@ pub async fn test_left_join() { ..Default::default() }; let res: InsertResult = Bakery::insert(seaside_bakery) - .exec(&ctx.db_conn) + .exec(&ctx.db) .await .expect("could not insert bakery"); let bakery: Option = Bakery::find_by_id(res.last_insert_id) - .one(&ctx.db_conn) + .one(&ctx.db) .await .expect("could not find bakery");