diff --git a/drizzle/seed.ts b/drizzle/seed.ts index 87991d02..256941d7 100644 --- a/drizzle/seed.ts +++ b/drizzle/seed.ts @@ -7,14 +7,6 @@ import "dotenv/config"; import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js"; import postgres from "postgres"; -import { - E2E_USER_ONE_EMAIL, - E2E_USER_ONE_ID, - E2E_USER_TWO_EMAIL, - E2E_USER_TWO_ID, - E2E_USER_ONE_SESSION_ID, - E2E_USER_TWO_SESSION_ID, -} from "../e2e/constants"; const DATABASE_URL = process.env.DATABASE_URL || ""; @@ -118,62 +110,6 @@ ${chance.paragraph()} return users; }; - const seedE2EUser = async (email: string, id: string, name: string) => { - const [existingE2EUser] = await db - .selectDistinct() - .from(user) - .where(eq(user.id, id)); - - if (existingE2EUser) { - console.log("E2E Test user already exists. Skipping creation"); - return existingE2EUser; - } - - const userData = { - id: id, - username: `${name.split(" ").join("-").toLowerCase()}-${chance.integer({ - min: 0, - max: 999, - })}`, - name, - email, - image: `https://robohash.org/${encodeURIComponent(name)}?bgset=bg1`, - location: chance.country({ full: true }), - bio: chance.sentence({ words: 10 }), - websiteUrl: chance.url(), - }; - const [createdUser] = await db.insert(user).values(userData).returning(); - return createdUser; - }; - - const seedE2EUserSession = async (userId: string, sessionToken: string) => { - const [existingE2EUserSession] = await db - .selectDistinct() - .from(session) - .where(eq(session.sessionToken, sessionToken)); - - if (existingE2EUserSession) { - console.log("E2E Test session already exists. Skipping creation"); - return existingE2EUserSession; - } - - try { - const currentDate = new Date(); - - return await db - .insert(session) - .values({ - userId, - sessionToken, - // Set session to expire in 6 months. - expires: new Date(currentDate.setMonth(currentDate.getMonth() + 6)), - }) - .returning(); - } catch (err) { - console.log(err); - } - }; - const userData = generateUserData(); const addUserData = async () => { @@ -249,19 +185,6 @@ ${chance.paragraph()} try { await addUserData(); - const userOne = await seedE2EUser( - E2E_USER_ONE_EMAIL, - E2E_USER_ONE_ID, - "E2E Test User One", - ); - const userTwo = await seedE2EUser( - E2E_USER_TWO_EMAIL, - E2E_USER_TWO_ID, - "E2E Test User Two", - ); - - await seedE2EUserSession(userOne.id, E2E_USER_ONE_SESSION_ID); - await seedE2EUserSession(userTwo.id, E2E_USER_TWO_SESSION_ID); } catch (error) { console.log("Error:", error); } diff --git a/e2e/setup.ts b/e2e/setup.ts index df3a101a..8be1dcc9 100644 --- a/e2e/setup.ts +++ b/e2e/setup.ts @@ -1,7 +1,15 @@ import postgres from "postgres"; import { drizzle } from "drizzle-orm/postgres-js"; -import { post, comment } from "@/server/db/schema"; -import { E2E_USER_ONE_ID, E2E_USER_TWO_ID } from "./constants"; +import { post, comment, session, user } from "@/server/db/schema"; +import { + E2E_USER_ONE_EMAIL, + E2E_USER_ONE_ID, + E2E_USER_ONE_SESSION_ID, + E2E_USER_TWO_EMAIL, + E2E_USER_TWO_ID, + E2E_USER_TWO_SESSION_ID, +} from "./constants"; +import { eq } from "drizzle-orm"; export const setup = async () => { const db = drizzle( @@ -42,10 +50,90 @@ export const setup = async () => { .returning(); }; + const seedE2EUser = async ( + email: string, + id: string, + name: string, + username: string, + ) => { + const [existingE2EUser] = await db + .selectDistinct() + .from(user) + .where(eq(user.id, id)); + + if (existingE2EUser) { + console.log("E2E Test user already exists. Skipping creation"); + return existingE2EUser; + } + + const userData = { + id: id, + username, + name, + email, + image: `https://robohash.org/${encodeURIComponent(name)}?bgset=bg1`, + location: "Ireland", + bio: "Hi I am an robot", + websiteUrl: "codu.co", + }; + const [createdUser] = await db.insert(user).values(userData).returning(); + return createdUser; + }; + + const seedE2EUserSession = async (userId: string, sessionToken: string) => { + const [existingE2EUserSession] = await db + .selectDistinct() + .from(session) + .where(eq(session.sessionToken, sessionToken)); + + if (existingE2EUserSession) { + console.log("E2E Test session already exists. Skipping creation"); + return existingE2EUserSession; + } + + try { + const currentDate = new Date(); + + return await db + .insert(session) + .values({ + userId, + sessionToken, + // Set session to expire in 6 months. + expires: new Date(currentDate.setMonth(currentDate.getMonth() + 6)), + }) + .returning(); + } catch (err) { + console.log(err); + } + }; + try { - console.log("creating articles"); + console.log("Creating users"); + const [userOne, userTwo] = await Promise.all([ + seedE2EUser( + E2E_USER_ONE_EMAIL, + E2E_USER_ONE_ID, + "E2E Test User One", + "e2e-test-user-one-111", + ), + seedE2EUser( + E2E_USER_TWO_EMAIL, + E2E_USER_TWO_ID, + "E2E Test User Two", + "e2e-test-user-two-222", + ), + ]); + + console.log("Creating sessions"); + await Promise.all([ + seedE2EUserSession(userOne.id, E2E_USER_ONE_SESSION_ID), + seedE2EUserSession(userTwo.id, E2E_USER_TWO_SESSION_ID), + ]); + console.log("Creating articles"); await addE2EArticleAndComment(E2E_USER_ONE_ID, E2E_USER_TWO_ID); + console.log("DB setup successful"); } catch (err) { console.log("Error while setting up DB before E2E test run", err); diff --git a/e2e/teardown.ts b/e2e/teardown.ts index a9b223b8..bb06e6a0 100644 --- a/e2e/teardown.ts +++ b/e2e/teardown.ts @@ -5,21 +5,18 @@ export const teardown = async () => { try { const db = postgres("postgresql://postgres:secret@127.0.0.1:5432/postgres"); - // the test suit adds posts created by the E2E users. We want to remove them between test runs - await db` - DELETE FROM "Post" WHERE "userId" = ${E2E_USER_ONE_ID} - `; - await db` - DELETE FROM "Post" WHERE "userId" = ${E2E_USER_TWO_ID} - `; - // the test suit adds comments created by the E2E user. We want to remove them between test runs - await db` - DELETE FROM "Comment" WHERE "userId" = ${E2E_USER_ONE_ID} - `; - await db` - DELETE FROM "Comment" WHERE "userId" = ${E2E_USER_TWO_ID} - `; - + await Promise.all([ + // the test suit adds posts created by the E2E users. We want to remove them between test runs + db` + DELETE FROM "Post" WHERE "userId" IN(${E2E_USER_ONE_ID}, ${E2E_USER_TWO_ID}) + `, + // the test suite adds comments created by the E2E user. We want to remove them between test runs + db` + DELETE FROM "Comment" WHERE "userId" IN(${E2E_USER_ONE_ID}, ${E2E_USER_TWO_ID}) + `, + db` + DELETE FROM "user" WHERE "id" IN(${E2E_USER_ONE_ID}, ${E2E_USER_TWO_ID})`, + ]); console.log("DB clean up successful"); } catch (err) { console.log("Error while cleaning up DB after E2E test run", err);