Skip to content
Open
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
72 changes: 72 additions & 0 deletions __tests__/db/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, it, beforeAll, afterAll } from 'vitest'
import { PrismaClient } from '@prisma/client'
import { execSync } from 'child_process'

const prisma = new PrismaClient()

beforeAll(async () => {
// Run database migrations before tests
try {
execSync('npx prisma migrate dev --name init_test_schema', { stdio: 'inherit' })
} catch (error) {
console.error('Migration failed:', error)
throw error
}
})

afterAll(async () => {
// Clean up after tests
await prisma.$disconnect()
})

describe('Authentication Database Schema', () => {
it('should create a user with required fields', async () => {
const user = await prisma.user.create({
data: {
email: 'test@example.com',
passwordHash: 'hashed_password_placeholder',
}
})

expect(user).toBeDefined()
expect(user.email).toBe('test@example.com')
expect(user.createdAt).toBeDefined()
})

it('should prevent duplicate email addresses', async () => {
await prisma.user.create({
data: {
email: 'unique@example.com',
passwordHash: 'hashed_password_placeholder',
}
})

await expect(prisma.user.create({
data: {
email: 'unique@example.com',
passwordHash: 'another_hashed_password',
}
})).rejects.toThrow()
})

it('should create a saved job for a user', async () => {
const user = await prisma.user.create({
data: {
email: 'saveduser@example.com',
passwordHash: 'hashed_password_placeholder',
}
})

const savedJob = await prisma.savedJob.create({
data: {
userId: user.id,
jobId: 'job123',
jobTitle: 'Software Engineer',
companyName: 'Tech Corp'
}
})

expect(savedJob).toBeDefined()
expect(savedJob.jobTitle).toBe('Software Engineer')
})
})
13 changes: 13 additions & 0 deletions lib/db/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Singleton Prisma Client to prevent multiple instances

import { PrismaClient } from '@prisma/client'

const globalForPrisma = global as unknown as { prisma: PrismaClient }

export const prisma =
globalForPrisma.prisma ||
new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
})

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
Loading