-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathsetup.ts
More file actions
189 lines (170 loc) · 4.73 KB
/
setup.ts
File metadata and controls
189 lines (170 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import postgres from "postgres";
import { drizzle } from "drizzle-orm/postgres-js";
import { post, comment, session, user } from "@/server/db/schema";
import {
articleContent,
articleExcerpt,
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 () => {
// Dynamically import nanoid
const { nanoid } = await import("nanoid");
const db = drizzle(
postgres("postgresql://postgres:secret@127.0.0.1:5432/postgres"),
);
const addE2EArticleAndComment = async (
authorId: string,
commenterId: string,
) => {
const publishedPostId = nanoid(8);
const scheduledPostId = nanoid(8);
const draftPostId = nanoid(8);
const now = new Date().toISOString();
const oneYearFromToday = new Date(now);
oneYearFromToday.setFullYear(oneYearFromToday.getFullYear() + 1);
await Promise.all([
db
.insert(post)
.values({
id: publishedPostId,
published: now,
excerpt: articleExcerpt,
updatedAt: now,
slug: "e2e-test-slug-published",
likes: 10,
readTimeMins: 3,
title: "Published Article",
body: articleContent,
userId: authorId,
})
.onConflictDoNothing()
.returning(),
db
.insert(post)
.values({
id: draftPostId,
published: null,
excerpt: articleExcerpt,
updatedAt: now,
slug: "e2e-test-slug-draft",
likes: 10,
readTimeMins: 3,
title: "Draft Article",
body: articleContent,
userId: authorId,
})
.onConflictDoNothing()
.returning(),
db
.insert(post)
.values({
id: scheduledPostId,
published: oneYearFromToday.toISOString(),
excerpt: articleExcerpt,
updatedAt: now,
slug: "e2e-test-slug-scheduled",
likes: 10,
readTimeMins: 3,
title: "Scheduled Article",
body: articleContent,
userId: authorId,
})
.onConflictDoNothing()
.returning(),
]);
await db
.insert(comment)
.values({
postId: publishedPostId,
body: "What a great article! Thanks for sharing",
userId: commenterId,
})
.onConflictDoNothing()
.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: "https://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 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);
}
};
export default setup;