Use DATABASE_URL env variable for sequential_op_tests

This commit is contained in:
Sam Samai 2021-07-31 15:59:54 +10:00
parent d5f63a05c4
commit b2ed13868e
2 changed files with 17 additions and 6 deletions

View File

@ -2,6 +2,7 @@ pub mod setup;
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
pub mod bakery_chain; pub mod bakery_chain;
pub use bakery_chain::*; pub use bakery_chain::*;
use std::env;
pub struct TestContext { pub struct TestContext {
base_url: String, base_url: String,
@ -10,12 +11,14 @@ pub struct TestContext {
} }
impl TestContext { impl TestContext {
pub async fn new(base_url: &str, db_name: &str) -> Self { pub async fn new(test_name: &str) -> Self {
let db: DatabaseConnection = setup::setup(base_url, db_name).await; let base_url =
env::var("DATABASE_URL").expect("Enviroment variable 'DATABASE_URL' not set");
let db: DatabaseConnection = setup::setup(&base_url, test_name).await;
Self { Self {
base_url: base_url.to_string(), base_url: base_url,
db_name: db_name.to_string(), db_name: test_name.to_string(),
db, db,
} }
} }

View File

@ -7,10 +7,16 @@ use uuid::Uuid;
pub mod common; pub mod common;
pub use common::{bakery_chain::*, setup::*, TestContext}; pub use common::{bakery_chain::*, setup::*, TestContext};
// Run the test locally:
// DATABASE_URL="mysql://root:@localhost" cargo test --features sqlx-mysql --test sequential_op_tests
#[async_std::test] #[async_std::test]
#[cfg(feature = "sqlx-mysql")] #[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
pub async fn test_multiple_operations() { pub async fn test_multiple_operations() {
let ctx = TestContext::new("mysql://root:@localhost", "multiple_sequential_operations").await; let ctx = TestContext::new("multiple_sequential_operations").await;
init_setup(&ctx.db).await; init_setup(&ctx.db).await;
let baker_least_sales = find_baker_least_sales(&ctx.db).await.unwrap(); let baker_least_sales = find_baker_least_sales(&ctx.db).await.unwrap();
@ -21,6 +27,8 @@ pub async fn test_multiple_operations() {
let baker_least_sales = find_baker_least_sales(&ctx.db).await.unwrap(); let baker_least_sales = find_baker_least_sales(&ctx.db).await.unwrap();
assert_eq!(baker_least_sales.name, "Baker 1"); assert_eq!(baker_least_sales.name, "Baker 1");
ctx.delete().await;
} }
async fn init_setup(db: &DatabaseConnection) { async fn init_setup(db: &DatabaseConnection) {