mirror of https://github.com/buster-so/buster.git
Use ctor for proper database pool initialization
This commit is contained in:
parent
8c8cca7b62
commit
bdd9dd90e2
|
@ -12,49 +12,27 @@ use serde_json::json;
|
||||||
use std::sync::Once;
|
use std::sync::Once;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
// Used to initialize the database pool once for all tests
|
// This constructor runs when the test binary loads
|
||||||
static INIT: Once = Once::new();
|
// It initializes the database pool once for all tests
|
||||||
static mut POOL_INITIALIZED: bool = false;
|
#[ctor::ctor]
|
||||||
|
fn init_test_env() {
|
||||||
// Initialize database pool
|
// Set environment variables for database connection
|
||||||
async fn initialize() -> Result<()> {
|
std::env::set_var("DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
|
||||||
unsafe {
|
std::env::set_var("TEST_DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
|
||||||
if POOL_INITIALIZED {
|
|
||||||
return Ok(());
|
// Create a runtime for initialization
|
||||||
}
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
INIT.call_once(|| {
|
// Initialize the pools
|
||||||
println!("Database pool initialization called");
|
rt.block_on(async {
|
||||||
// Set environment variables for database connection
|
match init_pools().await {
|
||||||
std::env::set_var("DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
|
Ok(_) => println!("Database pool initialized successfully for tests"),
|
||||||
std::env::set_var("TEST_DATABASE_URL", "postgresql://postgres:postgres@127.0.0.1:54322/postgres");
|
Err(e) => println!("Database pool initialization error: {}", e),
|
||||||
|
|
||||||
// Create a runtime for initialization
|
|
||||||
let rt = tokio::runtime::Builder::new_current_thread()
|
|
||||||
.enable_all()
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Initialize the pools
|
|
||||||
rt.block_on(async {
|
|
||||||
match init_pools().await {
|
|
||||||
Ok(_) => {
|
|
||||||
println!("Database pool initialized successfully");
|
|
||||||
POOL_INITIALIZED = true;
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
println!("Database pool initialization error: {}", e);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if POOL_INITIALIZED {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(anyhow::anyhow!("Failed to initialize database pool"))
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a simplified test setup for our test
|
// Create a simplified test setup for our test
|
||||||
|
@ -67,8 +45,8 @@ struct TestSetup {
|
||||||
// Integration test that tests metric status update functionality
|
// Integration test that tests metric status update functionality
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_update_metric_status() -> Result<()> {
|
async fn test_update_metric_status() -> Result<()> {
|
||||||
// Initialize the database pool
|
// Pool is already initialized at test binary startup
|
||||||
initialize().await?;
|
// No need to initialize it again
|
||||||
|
|
||||||
// Create a test ID for unique naming
|
// Create a test ID for unique naming
|
||||||
let test_id = format!("test-{}", Uuid::new_v4());
|
let test_id = format!("test-{}", Uuid::new_v4());
|
||||||
|
@ -322,8 +300,8 @@ fn create_default_chart_config() -> database::types::metric_yml::ChartConfig {
|
||||||
// Test unauthorized access
|
// Test unauthorized access
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_update_metric_status_unauthorized() -> Result<()> {
|
async fn test_update_metric_status_unauthorized() -> Result<()> {
|
||||||
// Initialize the database pool
|
// Pool is already initialized at test binary startup
|
||||||
initialize().await?;
|
// No need to initialize it again
|
||||||
|
|
||||||
// Create a test ID for unique naming
|
// Create a test ID for unique naming
|
||||||
let test_id = format!("test-{}", Uuid::new_v4());
|
let test_id = format!("test-{}", Uuid::new_v4());
|
||||||
|
@ -542,8 +520,8 @@ async fn test_update_metric_status_unauthorized() -> Result<()> {
|
||||||
// Test edge cases for status updates
|
// Test edge cases for status updates
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_update_metric_status_null_value() -> Result<()> {
|
async fn test_update_metric_status_null_value() -> Result<()> {
|
||||||
// Initialize the database pool
|
// Pool is already initialized at test binary startup
|
||||||
initialize().await?;
|
// No need to initialize it again
|
||||||
|
|
||||||
// Create a test ID for unique naming
|
// Create a test ID for unique naming
|
||||||
let test_id = format!("test-{}", Uuid::new_v4());
|
let test_id = format!("test-{}", Uuid::new_v4());
|
||||||
|
|
Loading…
Reference in New Issue