Setup and tear down DB

This commit is contained in:
Sam Samai 2021-07-13 20:01:01 +10:00
parent 8bfad93d30
commit 5190278e1a
3 changed files with 36 additions and 74 deletions

View File

@ -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>(_: T) -> &'static str {
std::any::type_name::<T>()
}
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;
}
}

View File

@ -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;
}

View File

@ -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),