Setup done in TestContext

This commit is contained in:
Sam Samai 2021-07-13 10:03:47 +10:00
parent ba77fb80aa
commit c7ff5d8d05
4 changed files with 34 additions and 24 deletions

View File

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

15
tests/common/setup/mod.rs Normal file
View File

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

View File

@ -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<ExecResult, DbErr> {
let builder = db.get_schema_builder_backend();

View File

@ -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::Model> = Bakery::find_by_id(res.last_insert_id)
.one(&ctx.db_conn)
.one(&ctx.db)
.await
.expect("could not find bakery");