feat(16-01): update test helper to seed user and return { db, userId }
- createTestDb uses PGlite with drizzle-pg migrations
- Seeds test user with logtoSub and per-user Uncategorized category
- Returns { db, userId } instead of just db
- Add createSecondTestUser helper for cross-user isolation tests
This commit is contained in:
@@ -1,21 +1,40 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { drizzle } from "drizzle-orm/bun-sqlite";
|
||||
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
||||
import { PGlite } from "@electric-sql/pglite";
|
||||
import { drizzle } from "drizzle-orm/pglite";
|
||||
import { migrate } from "drizzle-orm/pglite/migrator";
|
||||
import * as schema from "../../src/db/schema.ts";
|
||||
|
||||
export function createTestDb() {
|
||||
const sqlite = new Database(":memory:");
|
||||
sqlite.run("PRAGMA foreign_keys = ON");
|
||||
type Db = ReturnType<typeof drizzle<typeof schema>>;
|
||||
|
||||
const db = drizzle(sqlite, { schema });
|
||||
export async function createTestDb() {
|
||||
const client = new PGlite();
|
||||
const db = drizzle(client, { schema });
|
||||
|
||||
// Apply all migrations to create tables
|
||||
migrate(db, { migrationsFolder: "./drizzle" });
|
||||
await migrate(db, { migrationsFolder: "./drizzle-pg" });
|
||||
|
||||
// Seed default Uncategorized category
|
||||
db.insert(schema.categories)
|
||||
.values({ name: "Uncategorized", icon: "package" })
|
||||
.run();
|
||||
// Seed a test user
|
||||
const [user] = await db
|
||||
.insert(schema.users)
|
||||
.values({ logtoSub: "test-user-sub" })
|
||||
.returning();
|
||||
|
||||
return db;
|
||||
// Seed per-user Uncategorized category
|
||||
await db
|
||||
.insert(schema.categories)
|
||||
.values({ name: "Uncategorized", icon: "package", userId: user.id });
|
||||
|
||||
return { db, userId: user.id };
|
||||
}
|
||||
|
||||
export async function createSecondTestUser(db: Db) {
|
||||
const [user] = await db
|
||||
.insert(schema.users)
|
||||
.values({ logtoSub: "test-user-2-sub" })
|
||||
.returning();
|
||||
|
||||
await db
|
||||
.insert(schema.categories)
|
||||
.values({ name: "Uncategorized", icon: "package", userId: user.id });
|
||||
|
||||
return user.id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user