From 1a6d712a3f3adb380352d7e91f44c83ced20db50 Mon Sep 17 00:00:00 2001 From: dal Date: Mon, 7 Apr 2025 16:28:17 -0600 Subject: [PATCH] Add proper database pool initialization --- .../tests/metrics/update_metric_test.rs | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/api/libs/handlers/tests/metrics/update_metric_test.rs b/api/libs/handlers/tests/metrics/update_metric_test.rs index ecfab69d3..8c74abd77 100644 --- a/api/libs/handlers/tests/metrics/update_metric_test.rs +++ b/api/libs/handlers/tests/metrics/update_metric_test.rs @@ -9,8 +9,23 @@ use diesel_async::RunQueryDsl; use handlers::metrics::update_metric_handler::{update_metric_handler, UpdateMetricRequest}; use middleware::{AuthenticatedUser, OrganizationMembership}; use serde_json::json; +use std::sync::Once; use uuid::Uuid; +// Used to initialize the database pool once for all tests +static INIT: Once = Once::new(); + +// Initialize database pool +fn initialize() { + INIT.call_once(|| { + let rt = tokio::runtime::Runtime::new().unwrap(); + if let Err(e) = rt.block_on(init_pools()) { + panic!("Failed to initialize test pools: {}", e); + } + println!("Database pool initialized"); + }); +} + // Create a simplified test setup for our test struct TestSetup { pub user: AuthenticatedUser, @@ -21,8 +36,8 @@ struct TestSetup { // Integration test that tests metric status update functionality #[tokio::test] async fn test_update_metric_status() -> Result<()> { - // Get database pool - it's already initialized - let _ = get_pg_pool(); + // Initialize the database pool + initialize(); // Create a test ID for unique naming let test_id = format!("test-{}", Uuid::new_v4()); @@ -276,8 +291,8 @@ fn create_default_chart_config() -> database::types::metric_yml::ChartConfig { // Test unauthorized access #[tokio::test] async fn test_update_metric_status_unauthorized() -> Result<()> { - // Get database pool - it's already initialized - let _ = get_pg_pool(); + // Initialize the database pool + initialize(); // Create a test ID for unique naming let test_id = format!("test-{}", Uuid::new_v4()); @@ -496,8 +511,8 @@ async fn test_update_metric_status_unauthorized() -> Result<()> { // Test edge cases for status updates #[tokio::test] async fn test_update_metric_status_null_value() -> Result<()> { - // Get database pool - it's already initialized - let _ = get_pg_pool(); + // Initialize the database pool + initialize(); // Create a test ID for unique naming let test_id = format!("test-{}", Uuid::new_v4());