//! Direct-SQL seeding for DB-layer contract tests. //! //! The `db_*_layer.rs` modules exercise `db::` functions against a bare //! [`TestDb`](super::db::TestDb), with no app router and no session, so they //! cannot reach for [`TestHarness::signup`](super::TestHarness::signup) to get //! a user row. These helpers insert the row the layer under test needs and //! nothing more. //! //! Prefer the harness methods (`signup`, `create_creator`, //! `create_creator_with_item`) wherever a session or an HTTP flow is in play: //! those go through the real handlers and so assert the production path. Seeding //! is for tests whose subject is a `db::` function, where the signup flow would //! be setup noise. use makenotwork::db::{ProjectId, UserId}; use sqlx::PgPool; /// Insert a verified user with password `password123` and email /// `{username}@test.com`. Returns the new id. pub(crate) async fn seed_user(pool: &PgPool, username: &str) -> UserId { let hash = makenotwork::auth::hash_password("password123").expect("hash password for seed user"); sqlx::query_scalar::<_, UserId>( "INSERT INTO users (username, email, password_hash, email_verified) VALUES ($1, $2, $3, true) RETURNING id", ) .bind(username) .bind(format!("{username}@test.com")) .bind(&hash) .fetch_one(pool) .await .expect("seed user") } /// Insert a project owned by `user` at `slug`, titled `P`. Returns the new id. pub(crate) async fn seed_project(pool: &PgPool, user: UserId, slug: &str) -> ProjectId { sqlx::query_scalar::<_, ProjectId>( "INSERT INTO projects (user_id, slug, title) VALUES ($1, $2, 'P') RETURNING id", ) .bind(user) .bind(slug) .fetch_one(pool) .await .expect("seed project") }