From 5190278e1a9319038a40eb470e4f9cd7f05b0747 Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Tue, 13 Jul 2021 20:01:01 +1000 Subject: [PATCH] Setup and tear down DB --- tests/common/mod.rs | 77 ++++++--------------------------------- tests/common/setup/mod.rs | 30 ++++++++++++--- tests/relational_tests.rs | 3 +- 3 files changed, 36 insertions(+), 74 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 414bbfac..72679aad 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,81 +1,26 @@ pub mod setup; -use sea_orm::{DatabaseConnection, Statement}; +use sea_orm::DatabaseConnection; pub mod bakery_chain; pub use bakery_chain::*; -#[macro_export] -macro_rules! function { - () => {{ - fn f() {} - fn type_name_of(_: T) -> &'static str { - std::any::type_name::() - } - let name = type_name_of(f); - &name[..name.len() - 3] - }}; -} - pub struct TestContext { + base_url: String, + db_name: String, pub db: DatabaseConnection, } impl TestContext { - pub async fn new() -> Self { - let db: DatabaseConnection = setup::setup().await; + pub async fn new(base_url: &str, db_name: &str) -> Self { + let db: DatabaseConnection = setup::setup(base_url, db_name).await; - // let stmt: Statement = Statement::from("SET autocommit=0;\nSTART TRANSACTION;".to_string()); - // let _ = db.execute(stmt).await; - - Self { db } + Self { + base_url: base_url.to_string(), + db_name: db_name.to_string(), + db, + } } pub async fn delete(&self) { - // let stmt = sea_query::Table::drop() - // .table(baker::Entity) - // .table(baker::Entity) - // .to_owned(); - - // let builder = self.db.get_schema_builder_backend(); - // let result = self.db.execute(builder.build(&stmt)).await; - - let _ = self - .db - .execute(Statement::from("SET FOREIGN_KEY_CHECKS = 0;".to_string())) - .await; - let _ = self - .db - .execute(Statement::from("TRUNCATE TABLE `baker`;".to_string())) - .await; - let result = self - .db - .execute(Statement::from("TRUNCATE TABLE `bakery`;".to_string())) - .await; - let result = self - .db - .execute(Statement::from("TRUNCATE TABLE `cake`;".to_string())) - .await; - let result = self - .db - .execute(Statement::from( - "TRUNCATE TABLE `cakes_bakers`;".to_string(), - )) - .await; - let result = self - .db - .execute(Statement::from("TRUNCATE TABLE `customer`;".to_string())) - .await; - let result = self - .db - .execute(Statement::from("TRUNCATE TABLE `order`;".to_string())) - .await; - let result = self - .db - .execute(Statement::from("TRUNCATE TABLE `lineitem`;".to_string())) - .await; - let _ = self - .db - .execute(Statement::from("SET FOREIGN_KEY_CHECKS = 1;".to_string())) - .await; - println!("result: {:#?}", result); + setup::tear_down(&self.base_url, &self.db_name).await; } } diff --git a/tests/common/setup/mod.rs b/tests/common/setup/mod.rs index 30aa1295..3fe98881 100644 --- a/tests/common/setup/mod.rs +++ b/tests/common/setup/mod.rs @@ -1,12 +1,19 @@ -use sea_orm::{Database, DatabaseConnection}; +use sea_orm::{Database, DatabaseConnection, Statement}; pub mod schema; pub use schema::*; -pub async fn setup() -> DatabaseConnection { - // let db = Database::connect("sqlite::memory:").await.unwrap(); - let db = Database::connect("mysql://sea:sea@localhost/seaorm_test") - .await - .unwrap(); +pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection { + let url = format!("{}/mysql", base_url); + let db = Database::connect(&url).await.unwrap(); + let _create_db_result = db + .execute(Statement::from(format!( + "CREATE DATABASE IF NOT EXISTS `{}`;", + db_name + ))) + .await; + + let url = format!("{}/{}", base_url, db_name); + let db = Database::connect(&url).await.unwrap(); assert!(schema::create_bakery_table(&db).await.is_ok()); assert!(schema::create_baker_table(&db).await.is_ok()); @@ -17,3 +24,14 @@ pub async fn setup() -> DatabaseConnection { assert!(schema::create_lineitem_table(&db).await.is_ok()); db } + +pub async fn tear_down(base_url: &str, db_name: &str) { + let url = format!("{}/mysql", base_url); + let db = Database::connect(&url).await.unwrap(); + let _drop_db_result = db + .execute(Statement::from(format!( + "DROP DATABASE IF EXISTS `{}`;", + db_name + ))) + .await; +} diff --git a/tests/relational_tests.rs b/tests/relational_tests.rs index 2d14d90f..26b374c5 100644 --- a/tests/relational_tests.rs +++ b/tests/relational_tests.rs @@ -13,8 +13,7 @@ async fn main() { } pub async fn test_left_join() { - let ctx = TestContext::new().await; - + let ctx = TestContext::new("mysql://root:@localhost", "test_left_join").await; let seaside_bakery = bakery::ActiveModel { name: Set("SeaSide Bakery".to_owned()), profit_margin: Set(10.4),