Refactor basic test

This commit is contained in:
Sam Samai 2021-08-06 22:08:59 +10:00
parent 5930c4b278
commit e7d2899019
2 changed files with 11 additions and 13 deletions

View File

@ -1,6 +1,5 @@
use sea_orm::{entity::*, error::*, sea_query, tests_cfg::*, DbBackend, DbConn, Statement}; #[allow(unused_imports)]
use sea_orm::{entity::*, error::*, sea_query, tests_cfg::*, Database, DbConn};
mod setup;
// cargo test --test basic -- --nocapture // cargo test --test basic -- --nocapture
#[cfg_attr(feature = "runtime-async-std", async_std::test)] #[cfg_attr(feature = "runtime-async-std", async_std::test)]
@ -8,13 +7,17 @@ mod setup;
#[cfg_attr(feature = "runtime-tokio", tokio::test)] #[cfg_attr(feature = "runtime-tokio", tokio::test)]
#[cfg(feature = "sqlx-sqlite")] #[cfg(feature = "sqlx-sqlite")]
async fn main() { async fn main() {
let db: DbConn = setup::setup().await; use std::env;
let base_url = env::var("DATABASE_URL").expect("Enviroment variable 'DATABASE_URL' not set");
let db: DbConn = Database::connect(&base_url).await.unwrap();
setup_schema(&db).await; setup_schema(&db).await;
crud_cake(&db).await.unwrap(); crud_cake(&db).await.unwrap();
} }
#[cfg(feature = "sqlx-sqlite")]
async fn setup_schema(db: &DbConn) { async fn setup_schema(db: &DbConn) {
use sea_query::*; use sea_query::*;
@ -28,14 +31,14 @@ async fn setup_schema(db: &DbConn) {
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(cake::Column::Name).string()) .col(ColumnDef::new(cake::Column::Name).string())
.build(SqliteQueryBuilder); .to_owned();
let result = db let builder = db.get_database_backend();
.execute(Statement::from_string(DbBackend::Sqlite, stmt)) let result = db.execute(builder.build(&stmt)).await;
.await;
println!("Create table cake: {:?}", result); println!("Create table cake: {:?}", result);
} }
#[cfg(feature = "sqlx-sqlite")]
async fn crud_cake(db: &DbConn) -> Result<(), DbErr> { async fn crud_cake(db: &DbConn) -> Result<(), DbErr> {
let apple = cake::ActiveModel { let apple = cake::ActiveModel {
name: Set("Apple Pie".to_owned()), name: Set("Apple Pie".to_owned()),

View File

@ -1,5 +0,0 @@
use sea_orm::{Database, DatabaseConnection};
pub async fn setup() -> DatabaseConnection {
Database::connect("sqlite::memory:").await.unwrap()
}