Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions drizzle/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "";

Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
}
Expand Down
94 changes: 91 additions & 3 deletions e2e/setup.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 12 additions & 15 deletions e2e/teardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ export const teardown = async () => {
try {
const db = postgres("postgresql://postgres:[email protected]: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);
Expand Down