Rewrite the data access layer onto Prisma Client
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
|
||||
import { getPool, closePool } from '../../src/db.js';
|
||||
import { getPrismaClient, closePrismaClient } from '../../src/db.js';
|
||||
import { loadConfig } from '../../src/config.js';
|
||||
import {
|
||||
createJob,
|
||||
@@ -13,18 +13,18 @@ import {
|
||||
markCleaned,
|
||||
} from '../../src/jobs/jobRepository.js';
|
||||
|
||||
let pool;
|
||||
let prisma;
|
||||
|
||||
beforeAll(() => {
|
||||
pool = getPool(loadConfig());
|
||||
prisma = getPrismaClient(loadConfig());
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await closePool();
|
||||
await closePrismaClient();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await pool.query('DELETE FROM conversion_jobs');
|
||||
await prisma.conversionJob.deleteMany();
|
||||
});
|
||||
|
||||
function baseJob(overrides = {}) {
|
||||
@@ -44,9 +44,9 @@ function baseJob(overrides = {}) {
|
||||
|
||||
describe('jobRepository', () => {
|
||||
it('creates and retrieves a pending job', async () => {
|
||||
await createJob(pool, baseJob());
|
||||
await createJob(prisma, baseJob());
|
||||
|
||||
const job = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
|
||||
const job = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
|
||||
|
||||
expect(job.id).toEqual(expect.any(Number));
|
||||
expect(job.uuid).toBe('11111111-1111-4111-8111-111111111111');
|
||||
@@ -66,31 +66,31 @@ describe('jobRepository', () => {
|
||||
});
|
||||
|
||||
it('stores and retrieves a numeric quality value', async () => {
|
||||
await createJob(pool, baseJob({ uuid: '66666666-6666-4666-8666-666666666666', quality: 82 }));
|
||||
await createJob(prisma, baseJob({ uuid: '66666666-6666-4666-8666-666666666666', quality: 82 }));
|
||||
|
||||
const job = await getJobByUuid(pool, '66666666-6666-4666-8666-666666666666');
|
||||
const job = await getJobByUuid(prisma, '66666666-6666-4666-8666-666666666666');
|
||||
expect(job.quality).toBe(82);
|
||||
});
|
||||
|
||||
it('returns null for an unknown uuid', async () => {
|
||||
const job = await getJobByUuid(pool, '22222222-2222-4222-8222-222222222222');
|
||||
const job = await getJobByUuid(prisma, '22222222-2222-4222-8222-222222222222');
|
||||
expect(job).toBeNull();
|
||||
});
|
||||
|
||||
it('transitions a job through processing to done', async () => {
|
||||
await createJob(pool, baseJob());
|
||||
await createJob(prisma, baseJob());
|
||||
|
||||
await markProcessing(pool, (await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111')).id);
|
||||
const processing = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
|
||||
await markProcessing(prisma, (await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111')).id);
|
||||
const processing = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
|
||||
expect(processing.status).toBe('processing');
|
||||
|
||||
await markDone(pool, processing.id, {
|
||||
await markDone(prisma, processing.id, {
|
||||
outputPath: '11111111-1111-4111-8111-111111111111.webp',
|
||||
outputMimeType: 'image/webp',
|
||||
outputSizeBytes: 2048,
|
||||
conversionDurationSeconds: 1.5,
|
||||
});
|
||||
const done = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
|
||||
const done = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
|
||||
expect(done.status).toBe('done');
|
||||
expect(done.outputPath).toBe('11111111-1111-4111-8111-111111111111.webp');
|
||||
expect(done.outputMimeType).toBe('image/webp');
|
||||
@@ -99,29 +99,29 @@ describe('jobRepository', () => {
|
||||
});
|
||||
|
||||
it('marks a job failed with a short message and a separate detailed log', async () => {
|
||||
await createJob(pool, baseJob());
|
||||
const created = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
|
||||
await createJob(prisma, baseJob());
|
||||
const created = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
|
||||
|
||||
await markFailed(pool, created.id, {
|
||||
await markFailed(prisma, created.id, {
|
||||
errorMessage: 'Conversion failed, please try again',
|
||||
errorLog: 'Error: sharp threw at line 42\n at convert (image.js:10:5)',
|
||||
});
|
||||
|
||||
const job = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
|
||||
const job = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
|
||||
expect(job.status).toBe('failed');
|
||||
expect(job.errorMessage).toBe('Conversion failed, please try again');
|
||||
expect(job.errorLog).toBeUndefined();
|
||||
|
||||
const errorLog = await getJobErrorLog(pool, created.id);
|
||||
const errorLog = await getJobErrorLog(prisma, created.id);
|
||||
expect(errorLog).toBe('Error: sharp threw at line 42\n at convert (image.js:10:5)');
|
||||
});
|
||||
|
||||
it('finds pending jobs oldest first, up to a limit', async () => {
|
||||
await createJob(pool, baseJob({ uuid: '33333333-3333-4333-8333-333333333333' }));
|
||||
await createJob(pool, baseJob({ uuid: '44444444-4444-4444-8444-444444444444' }));
|
||||
await createJob(pool, baseJob({ uuid: '55555555-5555-4555-8555-555555555555' }));
|
||||
await createJob(prisma, baseJob({ uuid: '33333333-3333-4333-8333-333333333333' }));
|
||||
await createJob(prisma, baseJob({ uuid: '44444444-4444-4444-8444-444444444444' }));
|
||||
await createJob(prisma, baseJob({ uuid: '55555555-5555-4555-8555-555555555555' }));
|
||||
|
||||
const jobs = await findPendingJobs(pool, 2);
|
||||
const jobs = await findPendingJobs(prisma, 2);
|
||||
|
||||
expect(jobs).toHaveLength(2);
|
||||
expect(jobs[0].uuid).toBe('33333333-3333-4333-8333-333333333333');
|
||||
@@ -129,19 +129,19 @@ describe('jobRepository', () => {
|
||||
});
|
||||
|
||||
it('finds expired jobs and allows marking them cleaned without deleting the row', async () => {
|
||||
await createJob(pool, baseJob({ expiresAt: new Date(Date.now() - 1000) }));
|
||||
await createJob(prisma, baseJob({ expiresAt: new Date(Date.now() - 1000) }));
|
||||
|
||||
const expired = await findExpiredJobs(pool);
|
||||
const expired = await findExpiredJobs(prisma);
|
||||
expect(expired).toHaveLength(1);
|
||||
expect(expired[0].uuid).toBe('11111111-1111-4111-8111-111111111111');
|
||||
|
||||
await markCleaned(pool, expired[0].id);
|
||||
await markCleaned(prisma, expired[0].id);
|
||||
|
||||
const stillPresent = await getJobByUuid(pool, '11111111-1111-4111-8111-111111111111');
|
||||
const stillPresent = await getJobByUuid(prisma, '11111111-1111-4111-8111-111111111111');
|
||||
expect(stillPresent).not.toBeNull();
|
||||
expect(stillPresent.cleanedAt).not.toBeNull();
|
||||
|
||||
const afterCleaning = await findExpiredJobs(pool);
|
||||
const afterCleaning = await findExpiredJobs(prisma);
|
||||
expect(afterCleaning).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user