Fix test setup for database connectivity

This commit is contained in:
dal 2025-04-07 16:27:51 -06:00
parent 72c376df7e
commit bac973e791
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 213 additions and 12 deletions

View File

@ -21,10 +21,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 database connection pool // Get database pool - it's already initialized
if let Err(e) = init_pools().await { let _ = get_pg_pool();
panic!("Failed to initialize test pools: {}", e);
}
// 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());
@ -66,6 +64,59 @@ async fn test_update_metric_status() -> Result<()> {
test_id, test_id,
}; };
// Insert the organization and user into the database
let mut conn = get_pg_pool().get().await?;
// Insert the organization
diesel::insert_into(database::schema::organizations::table)
.values(&setup.organization)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Insert the user
let user_model = database::models::User {
id: setup.user.id,
email: setup.user.email.clone(),
name: setup.user.name.clone(),
config: setup.user.config.clone(),
created_at: setup.user.created_at,
updated_at: setup.user.updated_at,
attributes: setup.user.attributes.clone(),
avatar_url: setup.user.avatar_url.clone(),
};
diesel::insert_into(database::schema::users::table)
.values(&user_model)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Create the relationship between user and organization
let user_org = database::models::UserToOrganization {
user_id: setup.user.id,
organization_id: setup.organization.id,
role: setup.user.organizations[0].role,
sharing_setting: database::enums::SharingSetting::None,
edit_sql: true,
upload_csv: true,
export_assets: true,
email_slack_enabled: true,
created_at: Utc::now(),
updated_at: Utc::now(),
deleted_at: None,
created_by: setup.user.id,
updated_by: setup.user.id,
deleted_by: None,
status: database::enums::UserOrganizationStatus::Active,
};
diesel::insert_into(database::schema::users_to_organizations::table)
.values(&user_org)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Create a test metric file // Create a test metric file
let metric_id = Uuid::new_v4(); let metric_id = Uuid::new_v4();
let current_time = Utc::now(); let current_time = Utc::now();
@ -164,6 +215,22 @@ async fn test_update_metric_status() -> Result<()> {
.filter(database::schema::asset_permissions::asset_id.eq(metric_id)) .filter(database::schema::asset_permissions::asset_id.eq(metric_id))
.execute(&mut conn) .execute(&mut conn)
.await?; .await?;
// Clean up user data - cascades to related tables
diesel::delete(database::schema::users_to_organizations::table)
.filter(database::schema::users_to_organizations::user_id.eq(setup.user.id))
.execute(&mut conn)
.await?;
diesel::delete(database::schema::users::table)
.filter(database::schema::users::id.eq(setup.user.id))
.execute(&mut conn)
.await?;
diesel::delete(database::schema::organizations::table)
.filter(database::schema::organizations::id.eq(setup.organization.id))
.execute(&mut conn)
.await?;
// No TestDb cleanup in our simplified setup - we manually clean up // No TestDb cleanup in our simplified setup - we manually clean up
// But in a real test, we would use a TestDb cleanup helper // But in a real test, we would use a TestDb cleanup helper
@ -209,10 +276,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 database connection pool // Get database pool - it's already initialized
if let Err(e) = init_pools().await { let _ = get_pg_pool();
panic!("Failed to initialize test pools: {}", e);
}
// 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());
@ -254,6 +319,59 @@ async fn test_update_metric_status_unauthorized() -> Result<()> {
test_id, test_id,
}; };
// Insert the organization and user into the database
let mut conn = get_pg_pool().get().await?;
// Insert the organization
diesel::insert_into(database::schema::organizations::table)
.values(&setup.organization)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Insert the user
let user_model = database::models::User {
id: setup.user.id,
email: setup.user.email.clone(),
name: setup.user.name.clone(),
config: setup.user.config.clone(),
created_at: setup.user.created_at,
updated_at: setup.user.updated_at,
attributes: setup.user.attributes.clone(),
avatar_url: setup.user.avatar_url.clone(),
};
diesel::insert_into(database::schema::users::table)
.values(&user_model)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Create the relationship between user and organization
let user_org = database::models::UserToOrganization {
user_id: setup.user.id,
organization_id: setup.organization.id,
role: setup.user.organizations[0].role,
sharing_setting: database::enums::SharingSetting::None,
edit_sql: true,
upload_csv: true,
export_assets: true,
email_slack_enabled: true,
created_at: Utc::now(),
updated_at: Utc::now(),
deleted_at: None,
created_by: setup.user.id,
updated_by: setup.user.id,
deleted_by: None,
status: database::enums::UserOrganizationStatus::Active,
};
diesel::insert_into(database::schema::users_to_organizations::table)
.values(&user_org)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Create a test metric file // Create a test metric file
let metric_id = Uuid::new_v4(); let metric_id = Uuid::new_v4();
let current_time = Utc::now(); let current_time = Utc::now();
@ -352,6 +470,22 @@ async fn test_update_metric_status_unauthorized() -> Result<()> {
.filter(database::schema::asset_permissions::asset_id.eq(metric_id)) .filter(database::schema::asset_permissions::asset_id.eq(metric_id))
.execute(&mut conn) .execute(&mut conn)
.await?; .await?;
// Clean up user data - cascades to related tables
diesel::delete(database::schema::users_to_organizations::table)
.filter(database::schema::users_to_organizations::user_id.eq(setup.user.id))
.execute(&mut conn)
.await?;
diesel::delete(database::schema::users::table)
.filter(database::schema::users::id.eq(setup.user.id))
.execute(&mut conn)
.await?;
diesel::delete(database::schema::organizations::table)
.filter(database::schema::organizations::id.eq(setup.organization.id))
.execute(&mut conn)
.await?;
// No TestDb cleanup in our simplified setup - we manually clean up // No TestDb cleanup in our simplified setup - we manually clean up
// But in a real test, we would use a TestDb cleanup helper // But in a real test, we would use a TestDb cleanup helper
@ -362,10 +496,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 database connection pool // Get database pool - it's already initialized
if let Err(e) = init_pools().await { let _ = get_pg_pool();
panic!("Failed to initialize test pools: {}", e);
}
// 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());
@ -407,6 +539,59 @@ async fn test_update_metric_status_null_value() -> Result<()> {
test_id, test_id,
}; };
// Insert the organization and user into the database
let mut conn = get_pg_pool().get().await?;
// Insert the organization
diesel::insert_into(database::schema::organizations::table)
.values(&setup.organization)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Insert the user
let user_model = database::models::User {
id: setup.user.id,
email: setup.user.email.clone(),
name: setup.user.name.clone(),
config: setup.user.config.clone(),
created_at: setup.user.created_at,
updated_at: setup.user.updated_at,
attributes: setup.user.attributes.clone(),
avatar_url: setup.user.avatar_url.clone(),
};
diesel::insert_into(database::schema::users::table)
.values(&user_model)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Create the relationship between user and organization
let user_org = database::models::UserToOrganization {
user_id: setup.user.id,
organization_id: setup.organization.id,
role: setup.user.organizations[0].role,
sharing_setting: database::enums::SharingSetting::None,
edit_sql: true,
upload_csv: true,
export_assets: true,
email_slack_enabled: true,
created_at: Utc::now(),
updated_at: Utc::now(),
deleted_at: None,
created_by: setup.user.id,
updated_by: setup.user.id,
deleted_by: None,
status: database::enums::UserOrganizationStatus::Active,
};
diesel::insert_into(database::schema::users_to_organizations::table)
.values(&user_org)
.on_conflict_do_nothing()
.execute(&mut conn)
.await?;
// Create a test metric file // Create a test metric file
let metric_id = Uuid::new_v4(); let metric_id = Uuid::new_v4();
let current_time = Utc::now(); let current_time = Utc::now();
@ -505,6 +690,22 @@ async fn test_update_metric_status_null_value() -> Result<()> {
.filter(database::schema::asset_permissions::asset_id.eq(metric_id)) .filter(database::schema::asset_permissions::asset_id.eq(metric_id))
.execute(&mut conn) .execute(&mut conn)
.await?; .await?;
// Clean up user data - cascades to related tables
diesel::delete(database::schema::users_to_organizations::table)
.filter(database::schema::users_to_organizations::user_id.eq(setup.user.id))
.execute(&mut conn)
.await?;
diesel::delete(database::schema::users::table)
.filter(database::schema::users::id.eq(setup.user.id))
.execute(&mut conn)
.await?;
diesel::delete(database::schema::organizations::table)
.filter(database::schema::organizations::id.eq(setup.organization.id))
.execute(&mut conn)
.await?;
// No TestDb cleanup in our simplified setup - we manually clean up // No TestDb cleanup in our simplified setup - we manually clean up
// But in a real test, we would use a TestDb cleanup helper // But in a real test, we would use a TestDb cleanup helper