Skip to content

Commit

Permalink
test: 💍 sync seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
morintd committed Apr 27, 2024
1 parent 922bac6 commit 150b44d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 15 additions & 1 deletion testing/index.mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { exec } from 'child_process';

import { ObjectId } from 'bson';
import { Blog, Post, PrismaClient, Role, User } from '@prisma/client';
import { Blog, Post, PrismaClient, Role, User, Service } from '@prisma/client';
import dotenv from 'dotenv';
import { createId } from '@paralleldrive/cuid2';

dotenv.config();

export const seededUsers = [buildUser(1, { warnings: 0 }), buildUser(2, { warnings: 5 }), buildUser(3, { warnings: 10 })];
export const seededBlogs = [buildBlog(1, { title: 'blog-1' }), buildBlog(2, { title: 'blog-2', userId: seededUsers[0].id })];
export const seededServices = [buildService({ userId: seededUsers[0].id, name: 'facebook' })];

export const seededPosts = [
buildPost(1, { authorId: seededUsers[0].id, blogId: seededBlogs[0].id }),
Expand All @@ -31,6 +32,10 @@ export async function simulateSeed(prisma: PrismaClient) {
];

await prisma.post.createMany({ data: postsToSave.map(({ id, ...post }) => ({ ...post })) });

const servicesToSave = [buildService({ userId: savedUsers[0].id, name: 'facebook' })];

await prisma.service.createMany({ data: servicesToSave.map(({ id, ...service }) => ({ ...service })) });
}

export async function resetDb() {
Expand Down Expand Up @@ -84,6 +89,15 @@ export function buildBlog(id: number, blog: Partial<Blog>) {
};
}

export function buildService(service: Partial<Service> & { userId: number }) {
const { name = '', userId } = service;

return {
name,
userId,
};
}

export function isUUID(maybeUUID: string) {
const regexUUID = /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
return regexUUID.test(maybeUUID);
Expand Down
5 changes: 3 additions & 2 deletions testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ export function buildBlog(id: number, blog: Partial<Blog>) {
};
}

export function buildService(service: Partial<Service>) {
const { name = '', userId = 1 } = service;
export function buildService(service: Partial<Service> & { userId: number }) {
const { name = '', userId } = service;

return {
name,
userId,
Expand Down

0 comments on commit 150b44d

Please sign in to comment.