Skip to main content

max / makenotwork

1.7 KB · 46 lines History Blame Raw
1 //! Direct-SQL seeding for DB-layer contract tests.
2 //!
3 //! The `db_*_layer.rs` modules exercise `db::` functions against a bare
4 //! [`TestDb`](super::db::TestDb), with no app router and no session, so they
5 //! cannot reach for [`TestHarness::signup`](super::TestHarness::signup) to get
6 //! a user row. These helpers insert the row the layer under test needs and
7 //! nothing more.
8 //!
9 //! Prefer the harness methods (`signup`, `create_creator`,
10 //! `create_creator_with_item`) wherever a session or an HTTP flow is in play:
11 //! those go through the real handlers and so assert the production path. Seeding
12 //! is for tests whose subject is a `db::` function, where the signup flow would
13 //! be setup noise.
14
15 use makenotwork::db::{ProjectId, UserId};
16 use sqlx::PgPool;
17
18 /// Insert a verified user with password `password123` and email
19 /// `{username}@test.com`. Returns the new id.
20 pub(crate) async fn seed_user(pool: &PgPool, username: &str) -> UserId {
21 let hash =
22 makenotwork::auth::hash_password("password123").expect("hash password for seed user");
23 sqlx::query_scalar::<_, UserId>(
24 "INSERT INTO users (username, email, password_hash, email_verified)
25 VALUES ($1, $2, $3, true) RETURNING id",
26 )
27 .bind(username)
28 .bind(format!("{username}@test.com"))
29 .bind(&hash)
30 .fetch_one(pool)
31 .await
32 .expect("seed user")
33 }
34
35 /// Insert a project owned by `user` at `slug`, titled `P`. Returns the new id.
36 pub(crate) async fn seed_project(pool: &PgPool, user: UserId, slug: &str) -> ProjectId {
37 sqlx::query_scalar::<_, ProjectId>(
38 "INSERT INTO projects (user_id, slug, title) VALUES ($1, $2, 'P') RETURNING id",
39 )
40 .bind(user)
41 .bind(slug)
42 .fetch_one(pool)
43 .await
44 .expect("seed project")
45 }
46