Skip to content

Commit

Permalink
test seeds isolated from each other
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Feb 27, 2024
1 parent e24fd1c commit 2c6dc31
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 350 deletions.
4 changes: 2 additions & 2 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const config: PlaywrightTestConfig = {
expect: {
timeout: 3000
},
timeout: 10000,
// timeout: 10000,
testDir: 'tests',
testMatch:'*spec.ts',
use: {
locale: 'en-US',
timezoneId: 'America/Los_Angeles'
}
},
};

export default config;
33 changes: 17 additions & 16 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import SeedUtils from './utils';
const prisma = new PrismaClient();

async function main() {
const utils = new SeedUtils(new Date(), prisma);
console.log("SEEDING")
// const utils = new SeedUtils(new Date(), prisma);

await utils.deleteAllHouseholds();
// await utils.deleteAllHouseholds();

await Promise.all([
utils.deleteAllFriendRequests(),
utils.createExpiredLink(1),
...USERS_WITH_NOTHING.map((userInd) => utils.createUserWithNothing(userInd)),
...USERS_WITH_EMPTY_HOUSEHOLD.map((userInd) => utils.createUserWithEmptyHousehold(userInd)),
...USERS_WITH_ACTIVE_SESSION.map((userInd) => utils.createActiveSession(userInd)),
utils.createUserWithEmptyHousehold(5),
utils.createUserWithKid(6),
]);
// await Promise.all([
// utils.deleteAllFriendRequests(),
// utils.createExpiredLink(1),
// ...USERS_WITH_NOTHING.map((userInd) => utils.createUserWithNothing(userInd)),
// ...USERS_WITH_EMPTY_HOUSEHOLD.map((userInd) => utils.createUserWithEmptyHousehold(userInd)),
// ...USERS_WITH_ACTIVE_SESSION.map((userInd) => utils.createActiveSession(userInd)),
// utils.createUserWithEmptyHousehold(5),
// utils.createUserWithKid(6),
// ]);

await Promise.all([
utils.createFriendRequest(4, 3),
utils.createHouseholdConnection(3, 5),
utils.createHouseholdInvite(5, 2)
]);
// await Promise.all([
// utils.createFriendRequest(4, 3),
// utils.createHouseholdConnection(3, 5),
// utils.createHouseholdInvite(5, 2)
// ]);
}

export async function run() {
Expand Down
213 changes: 69 additions & 144 deletions prisma/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ import { Prisma, PrismaClient, Pronoun } from '@prisma/client';
export default class SeedUtils {
#now: Date;
#prisma: PrismaClient;
PHONES = [
'+12015550121',
'+12015550122',
'+12015550123',
'+12015550124',
'+12015550125',
'+12015550126',
'+12015550127'
];

constructor(now: Date, prisma: PrismaClient) {
this.#now = now;
Expand Down Expand Up @@ -40,15 +31,8 @@ export default class SeedUtils {
emptyHousehold(ind: number) {
return {
household: {
connectOrCreate: {
where: {
id: ind
},
create: {
// phone: phones[ind - 1],
id: ind,
name: `Household ${ind}`
}
create: {
name: `Household ${ind}`
}
}
};
Expand All @@ -57,25 +41,13 @@ export default class SeedUtils {
householdWithKid(ind: number, kidInd: number) {
return {
household: {
connectOrCreate: {
where: {
id: ind
},
create: {
id: ind,
name: `Household ${ind}`,
children: {
connectOrCreate: [
{
where: {
id: kidInd
},
create: {
firstName: `User ${ind} Kid ${kidInd}`,
pronouns: Pronoun['HE_HIM_HIS']
}
}
]
create: {
id: ind,
name: `Household ${ind}`,
children: {
create: {
firstName: `User ${ind} Kid ${kidInd}`,
pronouns: Pronoun['HE_HIM_HIS']
}
}
}
Expand All @@ -95,26 +67,18 @@ export default class SeedUtils {
};
}

async deleteAllFriendRequests() {
await this.#prisma.friendRequest
.deleteMany()
.catch(() => console.log('No friend request table to delete'));
}

async deleteAllHouseholds() {
await this.#prisma.householdChild
.deleteMany()
.catch(() => console.log('No household child table to delete'));
await this.#prisma.household
.deleteMany()
.catch(() => console.log('No household table to delete'));
}

async deleteUserAndHousehold(phone: string) {
const user = await this.#prisma.user.findUnique({
where: { phone }
});
if (!user || !user.householdId) return;
if (!user) return;

if (!user.householdId) {
await this.#prisma.user.delete({
where: { phone }
});
return;
}
const { householdId } = user;
// delete all kids
const deleteKids = this.#prisma.householdChild.deleteMany({
Expand Down Expand Up @@ -167,47 +131,55 @@ export default class SeedUtils {
}
});

// delete all adults
const deleteAdults = this.#prisma.user.deleteMany({
where: {
householdId
}
});

// finally, delete the household
const deleteHousehold = this.#prisma.household.delete({
where: { id: householdId }
});

await this.#prisma.$transaction([
deleteKids,
deleteHouseholdInvites,
deleteFriendReqs1,
deleteFriendReqs2,
deleteFriends1,
deleteFriends2,
deleteAdults,
deleteHousehold
]);
try {
await this.#prisma.$transaction([
deleteKids,
deleteHouseholdInvites,
deleteFriendReqs1,
deleteFriendReqs2,
deleteFriends1,
deleteFriends2
]);
// delete all adults
await this.#prisma.user.deleteMany({
where: {
householdId
}
});

// finally, delete the household
await this.#prisma.household.delete({
where: { id: householdId }
});
} catch (err) {
console.error(err);
console.error('Failed to delete user and household for:', { phone });
console.error('Household Invites', await this.#prisma.joinHouseholdRequest.findMany());
throw new Error("Couldn't delete user and household");
}
}

async createExpiredLink(userInd: number) {
const expiredLink = {
token: '3e99472f1003794c',
phone: this.PHONES[userInd - 1],
expires: new Date('8/5/2020')
};
await this.#prisma.magicLink.upsert({
where: {
id: userInd
},
update: expiredLink,
create: expiredLink
let crypto;
try {
crypto = await import('node:crypto');
} catch (err) {
console.error('crypto support is disabled!');
return null;
}
const token = crypto.randomBytes(8).toString('hex');
await this.#prisma.magicLink.create({
data: {
token,
phone: this.userIndToPhone(userInd),
expires: new Date('8/5/2020')
}
});
return token;
}

async createUserWithNothing(userInd: number) {
const phone = this.PHONES[userInd - 1];
const phone = this.userIndToPhone(userInd);

await this.#prisma.user.upsert({
where: {
Expand All @@ -222,7 +194,7 @@ export default class SeedUtils {
}

async createActiveSession(userInd: number) {
const phone = this.PHONES[userInd - 1];
const phone = this.userIndToPhone(userInd);

const expires = new Date(this.#now);
expires.setHours(expires.getHours() + 1);
Expand All @@ -243,7 +215,7 @@ export default class SeedUtils {
}

async createUserWithEmptyHousehold(userInd: number) {
const phone = this.PHONES[userInd - 1];
const phone = this.userIndToPhone(userInd);
const user = {
...this.basicUser(userInd),
...this.emptyHousehold(userInd)
Expand All @@ -261,54 +233,13 @@ export default class SeedUtils {
});
}

async createFriendRequest(fromUserInd: number, toUserInd: number) {
const friendReq = {
id: toUserInd,
targetPhone: this.PHONES[toUserInd - 1],
fromHouseholdId: fromUserInd,
fromUserId: fromUserInd
};

await this.#prisma.friendRequest.upsert({
where: {
id: toUserInd
},
update: friendReq,
create: friendReq
});
}

async createHouseholdConnection(hId1: number, hId2: number) {
const householdConnection = {
id: hId1,
householdId: hId1,
friendHouseholdId: hId2
};
await this.#prisma.householdConnection.upsert({
where: {
id: hId1
},
update: householdConnection,
create: householdConnection
});
}

async createHouseholdInvite(fromUserInd: number, toUserInd: number) {
// household invite from User 5 to User 2
const householdInvite = {
id: toUserInd,
targetPhone: this.PHONES[toUserInd - 1],
householdId: fromUserInd,
fromUserId: fromUserInd
};

await this.#prisma.joinHouseholdRequest.upsert({
where: {
id: toUserInd
},
update: householdInvite,
create: householdInvite
});
userIndToPhone(userInd: number) {
const BASE = '+1201555';
let suffix = `${userInd}`;
while (suffix.length < 4) {
suffix = `0${suffix}`;
}
return `${BASE}${suffix}`;
}

async createUserWithKid(userInd: number) {
Expand All @@ -317,7 +248,7 @@ export default class SeedUtils {
...this.householdWithKid(userInd, 1)
};

const phone = this.PHONES[userInd - 1];
const phone = this.userIndToPhone(userInd);

await this.#prisma.user.upsert({
where: {
Expand All @@ -330,10 +261,4 @@ export default class SeedUtils {
}
});
}

async deleteUsers(where: Prisma.UserWhereUniqueInput) {
await this.#prisma.user.deleteMany({
where
});
}
}
2 changes: 1 addition & 1 deletion src/lib/server/repository/Household.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import prisma from '$lib/logics/_shared/prisma';
import type * as Prisma from '@prisma/client';
import type { Prisma } from '@prisma/client';

export default class HouseholdRepository {
static async create(
Expand Down
2 changes: 1 addition & 1 deletion src/routes/db/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function POST({
} catch (err) {
console.error(`${req.type} for user ${user.id} failed`);
console.error(err)
throw error(err.status, err.body.message);
throw error(err?.status || 500, err.body?.message || err);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
create profile
create kid
save basic info
*/
*/
Loading

0 comments on commit 2c6dc31

Please sign in to comment.