Test suit load environment variable from env files (#1701)

This commit is contained in:
Billy Chan 2023-06-12 17:19:30 +08:00 committed by GitHub
parent 1431d8027c
commit d35cd6aa3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 1 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ Cargo.lock
*.sublime*
.vscode
.idea/*
*/.idea/*
*/.idea/*
.env.local

View File

@ -61,6 +61,7 @@ time = { version = "0.3", features = ["macros"] }
uuid = { version = "1", features = ["v4"] }
once_cell = "1.8"
arraystring = "0.3"
dotenv = "0.15"
[features]
debug-print = []

View File

@ -8,6 +8,9 @@ pub use sea_orm::{entity::*, error::*, query::*, sea_query, tests_cfg::*, Databa
#[sea_orm_macros::test]
#[cfg(feature = "sqlx-sqlite")]
async fn main() -> Result<(), DbErr> {
dotenv::from_filename(".env.local").ok();
dotenv::from_filename(".env").ok();
let base_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite::memory:".to_owned());
let db: DbConn = Database::connect(&base_url).await?;

View File

@ -13,6 +13,9 @@ pub struct TestContext {
impl TestContext {
pub async fn new(test_name: &str) -> Self {
dotenv::from_filename(".env.local").ok();
dotenv::from_filename(".env").ok();
let base_url =
std::env::var("DATABASE_URL").expect("Enviroment variable 'DATABASE_URL' not set");
let db: DatabaseConnection = setup::setup(&base_url, test_name).await;