Manually truncate the tables for MySQL

This commit is contained in:
Sam Samai 2021-07-13 11:27:28 +10:00
parent 82b63589d3
commit 8bfad93d30
3 changed files with 59 additions and 21 deletions

View File

@ -13,7 +13,7 @@ impl EntityName for Entity {
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
pub profit_margin: f64, pub profit_margin: f32,
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]

View File

@ -16,30 +16,66 @@ macro_rules! function {
} }
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(base_url: &str, db_name: &str) -> Self { pub async fn new() -> Self {
let db: DatabaseConnection = setup::setup().await; let db: DatabaseConnection = setup::setup().await;
// let stmt: Statement = Statement::from("BEGIN".to_string()); // let stmt: Statement = Statement::from("SET autocommit=0;\nSTART TRANSACTION;".to_string());
// let _ = db.execute(stmt).await; // let _ = db.execute(stmt).await;
Self { Self { db }
base_url: base_url.to_string(),
db_name: db_name.to_string(),
db,
} }
}
}
impl Drop for TestContext { pub async fn delete(&self) {
fn drop(&mut self) { // let stmt = sea_query::Table::drop()
// println!("dropping context"); // .table(baker::Entity)
// let stmt: Statement = Statement::from("ROLLBACK".to_string()); // .table(baker::Entity)
// let _ = self.db.execute(stmt); // .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);
} }
} }

View File

@ -13,7 +13,7 @@ async fn main() {
} }
pub async fn test_left_join() { pub async fn test_left_join() {
let ctx = TestContext::new("test", function!()).await; let ctx = TestContext::new().await;
let seaside_bakery = bakery::ActiveModel { let seaside_bakery = bakery::ActiveModel {
name: Set("SeaSide Bakery".to_owned()), name: Set("SeaSide Bakery".to_owned()),
@ -30,8 +30,10 @@ pub async fn test_left_join() {
.await .await
.expect("could not find bakery"); .expect("could not find bakery");
// assert!(bakery.is_some()); assert!(bakery.is_some());
// let bakery_model = bakery.unwrap(); let bakery_model = bakery.unwrap();
// assert_eq!(bakery_model.name, "SeaSide Bakery"); assert_eq!(bakery_model.name, "SeaSide Bakery");
// assert_eq!(bakery_model.profit_margin, 10.4); assert_eq!(bakery_model.profit_margin, 10.4);
ctx.delete().await;
} }