Skip to content

Commit e271b7e

Browse files
committed
chore: moved e2e user and session creation to setup function
1 parent 2cb44b3 commit e271b7e

File tree

3 files changed

+103
-95
lines changed

3 files changed

+103
-95
lines changed

drizzle/seed.ts

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ import "dotenv/config";
77

88
import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
99
import postgres from "postgres";
10-
import {
11-
E2E_USER_ONE_EMAIL,
12-
E2E_USER_ONE_ID,
13-
E2E_USER_TWO_EMAIL,
14-
E2E_USER_TWO_ID,
15-
E2E_USER_ONE_SESSION_ID,
16-
E2E_USER_TWO_SESSION_ID,
17-
} from "../e2e/constants";
1810

1911
const DATABASE_URL = process.env.DATABASE_URL || "";
2012

@@ -118,62 +110,6 @@ ${chance.paragraph()}
118110
return users;
119111
};
120112

121-
const seedE2EUser = async (email: string, id: string, name: string) => {
122-
const [existingE2EUser] = await db
123-
.selectDistinct()
124-
.from(user)
125-
.where(eq(user.id, id));
126-
127-
if (existingE2EUser) {
128-
console.log("E2E Test user already exists. Skipping creation");
129-
return existingE2EUser;
130-
}
131-
132-
const userData = {
133-
id: id,
134-
username: `${name.split(" ").join("-").toLowerCase()}-${chance.integer({
135-
min: 0,
136-
max: 999,
137-
})}`,
138-
name,
139-
email,
140-
image: `https://robohash.org/${encodeURIComponent(name)}?bgset=bg1`,
141-
location: chance.country({ full: true }),
142-
bio: chance.sentence({ words: 10 }),
143-
websiteUrl: chance.url(),
144-
};
145-
const [createdUser] = await db.insert(user).values(userData).returning();
146-
return createdUser;
147-
};
148-
149-
const seedE2EUserSession = async (userId: string, sessionToken: string) => {
150-
const [existingE2EUserSession] = await db
151-
.selectDistinct()
152-
.from(session)
153-
.where(eq(session.sessionToken, sessionToken));
154-
155-
if (existingE2EUserSession) {
156-
console.log("E2E Test session already exists. Skipping creation");
157-
return existingE2EUserSession;
158-
}
159-
160-
try {
161-
const currentDate = new Date();
162-
163-
return await db
164-
.insert(session)
165-
.values({
166-
userId,
167-
sessionToken,
168-
// Set session to expire in 6 months.
169-
expires: new Date(currentDate.setMonth(currentDate.getMonth() + 6)),
170-
})
171-
.returning();
172-
} catch (err) {
173-
console.log(err);
174-
}
175-
};
176-
177113
const userData = generateUserData();
178114

179115
const addUserData = async () => {
@@ -249,19 +185,6 @@ ${chance.paragraph()}
249185

250186
try {
251187
await addUserData();
252-
const userOne = await seedE2EUser(
253-
E2E_USER_ONE_EMAIL,
254-
E2E_USER_ONE_ID,
255-
"E2E Test User One",
256-
);
257-
const userTwo = await seedE2EUser(
258-
E2E_USER_TWO_EMAIL,
259-
E2E_USER_TWO_ID,
260-
"E2E Test User Two",
261-
);
262-
263-
await seedE2EUserSession(userOne.id, E2E_USER_ONE_SESSION_ID);
264-
await seedE2EUserSession(userTwo.id, E2E_USER_TWO_SESSION_ID);
265188
} catch (error) {
266189
console.log("Error:", error);
267190
}

e2e/setup.ts

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import postgres from "postgres";
22
import { drizzle } from "drizzle-orm/postgres-js";
3-
import { post, comment } from "@/server/db/schema";
4-
import { E2E_USER_ONE_ID, E2E_USER_TWO_ID } from "./constants";
3+
import { post, comment, session, user } from "@/server/db/schema";
4+
import {
5+
E2E_USER_ONE_EMAIL,
6+
E2E_USER_ONE_ID,
7+
E2E_USER_ONE_SESSION_ID,
8+
E2E_USER_TWO_EMAIL,
9+
E2E_USER_TWO_ID,
10+
E2E_USER_TWO_SESSION_ID,
11+
} from "./constants";
12+
import { eq } from "drizzle-orm";
513

614
export const setup = async () => {
715
const db = drizzle(
@@ -42,10 +50,90 @@ export const setup = async () => {
4250
.returning();
4351
};
4452

53+
const seedE2EUser = async (
54+
email: string,
55+
id: string,
56+
name: string,
57+
username: string,
58+
) => {
59+
const [existingE2EUser] = await db
60+
.selectDistinct()
61+
.from(user)
62+
.where(eq(user.id, id));
63+
64+
if (existingE2EUser) {
65+
console.log("E2E Test user already exists. Skipping creation");
66+
return existingE2EUser;
67+
}
68+
69+
const userData = {
70+
id: id,
71+
username,
72+
name,
73+
email,
74+
image: `https://robohash.org/${encodeURIComponent(name)}?bgset=bg1`,
75+
location: "Ireland",
76+
bio: "Hi I am an robot",
77+
websiteUrl: "codu.co",
78+
};
79+
const [createdUser] = await db.insert(user).values(userData).returning();
80+
return createdUser;
81+
};
82+
83+
const seedE2EUserSession = async (userId: string, sessionToken: string) => {
84+
const [existingE2EUserSession] = await db
85+
.selectDistinct()
86+
.from(session)
87+
.where(eq(session.sessionToken, sessionToken));
88+
89+
if (existingE2EUserSession) {
90+
console.log("E2E Test session already exists. Skipping creation");
91+
return existingE2EUserSession;
92+
}
93+
94+
try {
95+
const currentDate = new Date();
96+
97+
return await db
98+
.insert(session)
99+
.values({
100+
userId,
101+
sessionToken,
102+
// Set session to expire in 6 months.
103+
expires: new Date(currentDate.setMonth(currentDate.getMonth() + 6)),
104+
})
105+
.returning();
106+
} catch (err) {
107+
console.log(err);
108+
}
109+
};
110+
45111
try {
46-
console.log("creating articles");
112+
console.log("Creating users");
113+
const [userOne, userTwo] = await Promise.all([
114+
seedE2EUser(
115+
E2E_USER_ONE_EMAIL,
116+
E2E_USER_ONE_ID,
117+
"E2E Test User One",
118+
"e2e-test-user-one-111",
119+
),
120+
seedE2EUser(
121+
E2E_USER_TWO_EMAIL,
122+
E2E_USER_TWO_ID,
123+
"E2E Test User Two",
124+
"e2e-test-user-two-222",
125+
),
126+
]);
127+
128+
console.log("Creating sessions");
129+
await Promise.all([
130+
seedE2EUserSession(userOne.id, E2E_USER_ONE_SESSION_ID),
131+
seedE2EUserSession(userTwo.id, E2E_USER_TWO_SESSION_ID),
132+
]);
47133

134+
console.log("Creating articles");
48135
await addE2EArticleAndComment(E2E_USER_ONE_ID, E2E_USER_TWO_ID);
136+
49137
console.log("DB setup successful");
50138
} catch (err) {
51139
console.log("Error while setting up DB before E2E test run", err);

e2e/teardown.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ export const teardown = async () => {
55
try {
66
const db = postgres("postgresql://postgres:secret@127.0.0.1:5432/postgres");
77

8-
// the test suit adds posts created by the E2E users. We want to remove them between test runs
9-
await db`
10-
DELETE FROM "Post" WHERE "userId" = ${E2E_USER_ONE_ID}
11-
`;
12-
await db`
13-
DELETE FROM "Post" WHERE "userId" = ${E2E_USER_TWO_ID}
14-
`;
15-
// the test suit adds comments created by the E2E user. We want to remove them between test runs
16-
await db`
17-
DELETE FROM "Comment" WHERE "userId" = ${E2E_USER_ONE_ID}
18-
`;
19-
await db`
20-
DELETE FROM "Comment" WHERE "userId" = ${E2E_USER_TWO_ID}
21-
`;
22-
8+
await Promise.all([
9+
// the test suit adds posts created by the E2E users. We want to remove them between test runs
10+
db`
11+
DELETE FROM "Post" WHERE "userId" IN(${E2E_USER_ONE_ID}, ${E2E_USER_TWO_ID})
12+
`,
13+
// the test suite adds comments created by the E2E user. We want to remove them between test runs
14+
db`
15+
DELETE FROM "Comment" WHERE "userId" IN(${E2E_USER_ONE_ID}, ${E2E_USER_TWO_ID})
16+
`,
17+
db`
18+
DELETE FROM "user" WHERE "id" IN(${E2E_USER_ONE_ID}, ${E2E_USER_TWO_ID})`,
19+
]);
2320
console.log("DB clean up successful");
2421
} catch (err) {
2522
console.log("Error while cleaning up DB after E2E test run", err);

0 commit comments

Comments
 (0)