Files
komp_ac/server/tests/common/mod.rs

52 lines
1.4 KiB
Rust

// tests/common/mod.rs
use dotenvy;
use sqlx::{postgres::PgPoolOptions, PgPool};
use std::env;
use std::path::Path;
pub async fn setup_test_db() -> PgPool {
// Get path to server directory
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set");
let env_path = Path::new(&manifest_dir).join(".env_test");
// Load environment variables
dotenvy::from_path(env_path).ok();
// Create connection pool
let database_url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set");
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&database_url)
.await
.expect("Failed to create pool");
// Run migrations
sqlx::migrate!()
.run(&pool)
.await
.expect("Migrations failed");
pool
}
// Add this to tests/common/mod.rs
pub async fn clear_all_tables(pool: &PgPool) {
// Clear tables in the correct order to respect foreign key constraints
clear_uctovnictvo_table(pool).await;
clear_adresar_table(pool).await;
}
pub async fn clear_uctovnictvo_table(pool: &PgPool) {
sqlx::query!("DELETE FROM uctovnictvo")
.execute(pool)
.await
.expect("Failed to clear uctovnictvo table");
}
pub async fn clear_adresar_table(pool: &PgPool) {
sqlx::query!("DELETE FROM adresar")
.execute(pool)
.await
.expect("Failed to clear adresar table");
}