diff --git a/api/libs/handlers/tests/metrics/update_metric_test.rs b/api/libs/handlers/tests/metrics/update_metric_test.rs index d815a66ae..25810648d 100644 --- a/api/libs/handlers/tests/metrics/update_metric_test.rs +++ b/api/libs/handlers/tests/metrics/update_metric_test.rs @@ -1,42 +1,80 @@ use anyhow::Result; use chrono::Utc; use database::enums::{AssetPermissionRole, AssetType, UserOrganizationRole, Verification}; -use database::models::MetricFile; +use database::models::{MetricFile, Organization}; +use database::pool::{get_pg_pool, init_pools}; use database::schema::metric_files; -use database::tests::common::db::TestSetup; use diesel::prelude::*; use diesel_async::RunQueryDsl; use handlers::metrics::update_metric_handler::{update_metric_handler, UpdateMetricRequest}; +use middleware::{AuthenticatedUser, OrganizationMembership}; +use serde_json::json; use uuid::Uuid; -// Force the initialization of database pools -lazy_static::lazy_static! { - static ref _INIT: () = { - let rt = tokio::runtime::Runtime::new().unwrap(); - if let Err(e) = rt.block_on(database::pool::init_pools()) { - panic!("Failed to initialize test pools: {}", e); - } - }; +// Create a simplified test setup for our test +struct TestSetup { + pub user: AuthenticatedUser, + pub organization: Organization, + pub test_id: String, } // Integration test that tests metric status update functionality #[tokio::test] async fn test_update_metric_status() -> Result<()> { - // Initialize lazy static to ensure pools are initialized - lazy_static::initialize(&_INIT); + // Initialize database connection pool + if let Err(e) = init_pools().await { + panic!("Failed to initialize test pools: {}", e); + } - // Set up test environment with admin user - let setup = TestSetup::new(Some(UserOrganizationRole::WorkspaceAdmin)).await?; + // Create a test ID for unique naming + let test_id = format!("test-{}", Uuid::new_v4()); + + // Create organization and user IDs + let organization_id = Uuid::new_v4(); + let user_id = Uuid::new_v4(); + + // Create mock user and organization + let user = AuthenticatedUser { + id: user_id, + email: format!("test-{}@example.com", test_id), + name: Some(format!("Test User {}", test_id)), + config: json!({"preferences": {"theme": "light"}}), + created_at: Utc::now(), + updated_at: Utc::now(), + attributes: json!({}), + avatar_url: None, + organizations: vec![OrganizationMembership { + id: organization_id, + role: UserOrganizationRole::WorkspaceAdmin, + }], + teams: vec![], + }; + + let organization = Organization { + id: organization_id, + name: format!("Test Organization {}", test_id), + domain: Some(format!("test-{}.org", test_id)), + created_at: Utc::now(), + updated_at: Utc::now(), + deleted_at: None, + }; + + // Create our simplified test setup + let setup = TestSetup { + user, + organization, + test_id, + }; // Create a test metric file let metric_id = Uuid::new_v4(); let current_time = Utc::now(); - let mut conn = setup.db.diesel_conn().await?; + let mut conn = get_pg_pool().get().await?; // Create a simple metric with test content let content = database::types::MetricYml { - name: format!("Test Metric {}", setup.db.test_id), - description: Some(format!("Test metric description for {}", setup.db.test_id)), + name: format!("Test Metric {}", setup.test_id), + description: Some(format!("Test metric description for {}", setup.test_id)), sql: "SELECT * FROM test".to_string(), time_frame: "last 30 days".to_string(), chart_config: create_default_chart_config(), @@ -49,8 +87,8 @@ async fn test_update_metric_status() -> Result<()> { // Create the test metric file let metric_file = MetricFile { id: metric_id, - name: format!("{}-Test Metric", setup.db.test_id), - file_name: format!("{}-test_metric.yml", setup.db.test_id), + name: format!("{}-Test Metric", setup.test_id), + file_name: format!("{}-test_metric.yml", setup.test_id), content: content.clone(), verification: initial_verification, evaluation_obj: None, @@ -127,8 +165,8 @@ async fn test_update_metric_status() -> Result<()> { .execute(&mut conn) .await?; - // Use TestDb cleanup for thorough cleanup - setup.db.cleanup().await?; + // No TestDb cleanup in our simplified setup - we manually clean up + // But in a real test, we would use a TestDb cleanup helper Ok(()) } @@ -180,12 +218,12 @@ async fn test_update_metric_status_unauthorized() -> Result<()> { // Create a test metric file let metric_id = Uuid::new_v4(); let current_time = Utc::now(); - let mut conn = setup.db.diesel_conn().await?; + let mut conn = get_pg_pool().get().await?; // Create a simple metric with test content let content = database::types::MetricYml { - name: format!("Test Metric {}", setup.db.test_id), - description: Some(format!("Test metric description for {}", setup.db.test_id)), + name: format!("Test Metric {}", setup.test_id), + description: Some(format!("Test metric description for {}", setup.test_id)), sql: "SELECT * FROM test".to_string(), time_frame: "last 30 days".to_string(), chart_config: create_default_chart_config(), @@ -198,8 +236,8 @@ async fn test_update_metric_status_unauthorized() -> Result<()> { // Create the test metric file let metric_file = MetricFile { id: metric_id, - name: format!("{}-Test Metric", setup.db.test_id), - file_name: format!("{}-test_metric.yml", setup.db.test_id), + name: format!("{}-Test Metric", setup.test_id), + file_name: format!("{}-test_metric.yml", setup.test_id), content: content.clone(), verification: initial_verification, evaluation_obj: None, @@ -276,8 +314,8 @@ async fn test_update_metric_status_unauthorized() -> Result<()> { .execute(&mut conn) .await?; - // Use TestDb cleanup for thorough cleanup - setup.db.cleanup().await?; + // No TestDb cleanup in our simplified setup - we manually clean up + // But in a real test, we would use a TestDb cleanup helper Ok(()) } @@ -294,12 +332,12 @@ async fn test_update_metric_status_null_value() -> Result<()> { // Create a test metric file let metric_id = Uuid::new_v4(); let current_time = Utc::now(); - let mut conn = setup.db.diesel_conn().await?; + let mut conn = get_pg_pool().get().await?; // Create a simple metric with test content let content = database::types::MetricYml { - name: format!("Test Metric {}", setup.db.test_id), - description: Some(format!("Test metric description for {}", setup.db.test_id)), + name: format!("Test Metric {}", setup.test_id), + description: Some(format!("Test metric description for {}", setup.test_id)), sql: "SELECT * FROM test".to_string(), time_frame: "last 30 days".to_string(), chart_config: create_default_chart_config(), @@ -312,8 +350,8 @@ async fn test_update_metric_status_null_value() -> Result<()> { // Create the test metric file let metric_file = MetricFile { id: metric_id, - name: format!("{}-Test Metric", setup.db.test_id), - file_name: format!("{}-test_metric.yml", setup.db.test_id), + name: format!("{}-Test Metric", setup.test_id), + file_name: format!("{}-test_metric.yml", setup.test_id), content: content.clone(), verification: initial_verification, evaluation_obj: None, @@ -390,8 +428,8 @@ async fn test_update_metric_status_null_value() -> Result<()> { .execute(&mut conn) .await?; - // Use TestDb cleanup for thorough cleanup - setup.db.cleanup().await?; + // No TestDb cleanup in our simplified setup - we manually clean up + // But in a real test, we would use a TestDb cleanup helper Ok(()) } \ No newline at end of file