nxtgauge-backend-rust/tests/db_test_helper.rs

68 lines
2.3 KiB
Rust
Raw Normal View History

use std::env;
use sqlx::{Pool, Postgres};
use nxtgauge_backend_rust::users::{AppState, router};
use std::sync::Arc;
use redis::AsyncCommands;
// Test database configuration
pub async fn setup_test_db() -> Pool<Postgres> {
// Use environment variable for test database URL
let database_url = env::var("TEST_DATABASE_URL")
.unwrap_or_else(|_| "postgres://postgres:password@localhost:5432/nxtgauge_test".to_string());
// In a real implementation, we would:
// 1. Create the test database if it doesn't exist
// 2. Run migrations against it
// 3. Return the connection pool
// For now, we'll return a placeholder that will fail gracefully
// indicating that test database setup needs to be implemented
match Pool::connect(&database_url).await {
Ok(pool) => pool,
Err(e) => {
eprintln!("Warning: Could not connect to test database: {}", e);
eprintln!("Tests will run in mock mode. Set up TEST_DATABASE_URL for real database tests.");
// Return a dummy pool - tests that actually need DB will fail appropriately
// This allows tests to compile and run basic assertions
panic!("Test database not configured. Please set TEST_DATABASE_URL environment variable.");
}
}
}
// Helper to create test Redis connection
pub async fn setup_test_redis() -> redis::aio::ConnectionManager {
let redis_url = env::var("TEST_REDIS_URL")
.unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
redis::Client::open(redis_url as &str)
.expect("Invalid Redis URL")
.get_connection_manager()
.await
.expect("Failed to connect to Redis")
}
// Helper to create test app state
pub async fn create_test_app_state() -> AppState {
let pool = setup_test_db().await;
let redis = setup_test_redis().await;
// In a real implementation, we would create a test mailer
// let mailer = Arc::new(TestMailer::new());
AppState {
pool,
mail: Arc::new(crate::email::Mailer::new()), // This will fail in test mode without SMTP config
redis: Arc::new(redis),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_helper_imports() {
// Just verify the module compiles
assert!(true);
}
}