Skip to main content

max / goingson

1.9 KB · 63 lines History Blame Raw
1 //! Common test utilities for repository integration tests.
2
3 use chrono::{Duration, Utc};
4 use goingson_core::{TaskId, UserId};
5 use goingson_db_sqlite::{init_pool, run_migrations};
6 use sqlx::SqlitePool;
7 use uuid::Uuid;
8
9 /// Creates an in-memory SQLite database with migrations applied.
10 pub async fn setup_test_db() -> SqlitePool {
11 let pool = init_pool(Some(":memory:")).await.expect("Failed to create test pool");
12 run_migrations(&pool).await.expect("Failed to run migrations");
13 pool
14 }
15
16 /// Creates a test user and returns their ID.
17 pub async fn create_test_user(pool: &SqlitePool) -> UserId {
18 let user_id = Uuid::new_v4();
19 let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
20
21 sqlx::query(
22 "INSERT INTO users (id, email, password_hash, display_name, created_at) VALUES (?, ?, ?, ?, ?)"
23 )
24 .bind(user_id.to_string())
25 .bind(format!("test-{}@example.com", user_id))
26 .bind("test-password-hash")
27 .bind("Test User")
28 .bind(&now)
29 .execute(pool)
30 .await
31 .expect("Failed to create test user");
32
33 UserId::from(user_id)
34 }
35
36 /// Creates a test task and returns its ID.
37 #[allow(dead_code)]
38 pub async fn create_test_task(pool: &SqlitePool, user_id: UserId) -> TaskId {
39 let task_id = Uuid::new_v4();
40 let now = Utc::now();
41 let due = now + Duration::days(1);
42 let now_str = now.format("%Y-%m-%d %H:%M:%S").to_string();
43 let due_str = due.format("%Y-%m-%d %H:%M:%S").to_string();
44
45 sqlx::query(
46 r#"INSERT INTO tasks (id, user_id, description, status, priority, due, created_at, recurrence)
47 VALUES (?, ?, ?, ?, ?, ?, ?, ?)"#
48 )
49 .bind(task_id.to_string())
50 .bind(user_id.to_string())
51 .bind("Test task description")
52 .bind("Pending")
53 .bind("Medium")
54 .bind(&due_str)
55 .bind(&now_str)
56 .bind("None")
57 .execute(pool)
58 .await
59 .expect("Failed to create test task");
60
61 TaskId::from(task_id)
62 }
63