//! Common test utilities for repository integration tests. use chrono::{Duration, Utc}; use goingson_core::{TaskId, UserId}; use goingson_db_sqlite::{init_pool, run_migrations}; use sqlx::SqlitePool; use uuid::Uuid; /// Creates an in-memory SQLite database with migrations applied. pub async fn setup_test_db() -> SqlitePool { let pool = init_pool(Some(":memory:")).await.expect("Failed to create test pool"); run_migrations(&pool).await.expect("Failed to run migrations"); pool } /// Creates a test user and returns their ID. pub async fn create_test_user(pool: &SqlitePool) -> UserId { let user_id = Uuid::new_v4(); let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(); sqlx::query( "INSERT INTO users (id, email, password_hash, display_name, created_at) VALUES (?, ?, ?, ?, ?)" ) .bind(user_id.to_string()) .bind(format!("test-{}@example.com", user_id)) .bind("test-password-hash") .bind("Test User") .bind(&now) .execute(pool) .await .expect("Failed to create test user"); UserId::from(user_id) } /// Creates a test task and returns its ID. #[allow(dead_code)] pub async fn create_test_task(pool: &SqlitePool, user_id: UserId) -> TaskId { let task_id = Uuid::new_v4(); let now = Utc::now(); let due = now + Duration::days(1); let now_str = now.format("%Y-%m-%d %H:%M:%S").to_string(); let due_str = due.format("%Y-%m-%d %H:%M:%S").to_string(); sqlx::query( r#"INSERT INTO tasks (id, user_id, description, status, priority, due, created_at, recurrence) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"# ) .bind(task_id.to_string()) .bind(user_id.to_string()) .bind("Test task description") .bind("Pending") .bind("Medium") .bind(&due_str) .bind(&now_str) .bind("None") .execute(pool) .await .expect("Failed to create test task"); TaskId::from(task_id) }