Setup and tear down DB
This commit is contained in:
parent
8bfad93d30
commit
5190278e1a
@ -1,81 +1,26 @@
|
|||||||
pub mod setup;
|
pub mod setup;
|
||||||
use sea_orm::{DatabaseConnection, Statement};
|
use sea_orm::DatabaseConnection;
|
||||||
pub mod bakery_chain;
|
pub mod bakery_chain;
|
||||||
pub use 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 {
|
pub struct TestContext {
|
||||||
|
base_url: String,
|
||||||
|
db_name: String,
|
||||||
pub db: DatabaseConnection,
|
pub db: DatabaseConnection,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestContext {
|
impl TestContext {
|
||||||
pub async fn new() -> Self {
|
pub async fn new(base_url: &str, db_name: &str) -> Self {
|
||||||
let db: DatabaseConnection = setup::setup().await;
|
let db: DatabaseConnection = setup::setup(base_url, db_name).await;
|
||||||
|
|
||||||
// let stmt: Statement = Statement::from("SET autocommit=0;\nSTART TRANSACTION;".to_string());
|
Self {
|
||||||
// let _ = db.execute(stmt).await;
|
base_url: base_url.to_string(),
|
||||||
|
db_name: db_name.to_string(),
|
||||||
Self { db }
|
db,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&self) {
|
pub async fn delete(&self) {
|
||||||
// let stmt = sea_query::Table::drop()
|
setup::tear_down(&self.base_url, &self.db_name).await;
|
||||||
// .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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,19 @@
|
|||||||
use sea_orm::{Database, DatabaseConnection};
|
use sea_orm::{Database, DatabaseConnection, Statement};
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub use schema::*;
|
pub use schema::*;
|
||||||
|
|
||||||
pub async fn setup() -> DatabaseConnection {
|
pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
|
||||||
// let db = Database::connect("sqlite::memory:").await.unwrap();
|
let url = format!("{}/mysql", base_url);
|
||||||
let db = Database::connect("mysql://sea:sea@localhost/seaorm_test")
|
let db = Database::connect(&url).await.unwrap();
|
||||||
.await
|
let _create_db_result = db
|
||||||
.unwrap();
|
.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_bakery_table(&db).await.is_ok());
|
||||||
assert!(schema::create_baker_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());
|
assert!(schema::create_lineitem_table(&db).await.is_ok());
|
||||||
db
|
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;
|
||||||
|
}
|
||||||
|
@ -13,8 +13,7 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn test_left_join() {
|
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 {
|
let seaside_bakery = bakery::ActiveModel {
|
||||||
name: Set("SeaSide Bakery".to_owned()),
|
name: Set("SeaSide Bakery".to_owned()),
|
||||||
profit_margin: Set(10.4),
|
profit_margin: Set(10.4),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user