From ae6957ec9ea3750c9ed6c4b61417f1b5e4241ea7 Mon Sep 17 00:00:00 2001 From: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com> Date: Fri, 23 Aug 2024 03:39:39 +0100 Subject: [PATCH] switch to scripts to get db data --- tasks/e2e-background-jobs/fixtures.mts | 23 +++++++++++++++++ tasks/e2e-background-jobs/run.mts | 34 +++++++++++++++++--------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/tasks/e2e-background-jobs/fixtures.mts b/tasks/e2e-background-jobs/fixtures.mts index 2342c026b17c..309cb22fc4ab 100644 --- a/tasks/e2e-background-jobs/fixtures.mts +++ b/tasks/e2e-background-jobs/fixtures.mts @@ -28,3 +28,26 @@ export const handler = async (event: APIGatewayEvent, _context: Context) => { } } ` + +export const JOBS_SCRIPT = ` +import { db } from 'api/src/lib/db' + +export default async () => { + const jobs = await db.backgroundJob.findMany() + console.log(JSON.stringify(jobs)) +} +` + +export const PRISMA_SCRIPT = ` +import { db } from 'api/src/lib/db' + +export default async () => { + const model = db.backgroundJob + console.log( + JSON.stringify({ + name: model.name, + }) + ) +} + +` diff --git a/tasks/e2e-background-jobs/run.mts b/tasks/e2e-background-jobs/run.mts index cacc58ee0577..675cacb2f74c 100644 --- a/tasks/e2e-background-jobs/run.mts +++ b/tasks/e2e-background-jobs/run.mts @@ -3,6 +3,8 @@ import process from 'node:process' import { $, cd, path, ProcessOutput, fs } from 'zx' import { + JOBS_SCRIPT, + PRISMA_SCRIPT, SAMPLE_FUNCTION, SAMPLE_JOB_PERFORM_ARGS, SAMPLE_JOB_PERFORM_BODY, @@ -94,13 +96,17 @@ async function main() { } // Confirm the prisma model exists - await $`yarn rw build api` - const { db } = await import( - makeFilePath(path.join(projectPath, 'api/dist/lib/db.js')) - ) - const model = db['BackgroundJob'] - if (model?.name !== 'BackgroundJob') { - console.error("Expected database model 'BackgroundJob' not found") + console.log('Action: Adding scripts to get information from the database') + const jobsScriptPath = path.join(projectPath, 'scripts/jobs.ts') + fs.writeFileSync(jobsScriptPath, JOBS_SCRIPT) + const prismaScriptPath = path.join(projectPath, 'scripts/prisma.ts') + fs.writeFileSync(prismaScriptPath, PRISMA_SCRIPT) + + console.log('Testing: the prisma model exists in the database') + const prismaData = (await $`yarn rw exec prisma --silent`).toString() + const { name } = JSON.parse(prismaData) + if (name !== 'BackgroundJob') { + console.error('Expected model not found in the database') process.exit(1) } console.log('Confirmed: prisma model exists') @@ -200,13 +206,15 @@ async function main() { // Step 10: Confirm the job was scheduled into the database console.log('Testing: Confirming the job was scheduled into the database') - const job = await db.backgroundJob.findFirst() - if (!job) { + const rawJobs = (await $`yarn rw exec jobs --silent`).toString() + const jobs = JSON.parse(rawJobs) + if (!jobs?.length) { console.error('Expected job not found in the database') process.exit(1) } - const handler = JSON.parse(job.handler ?? {}) - const args = handler?.args ?? [] + const job = jobs[0] + const handler = JSON.parse(job?.handler ?? '{}') + const args = handler.args ?? [] if (args[0] !== location || args[1] !== data) { console.error('Expected job arguments do not match') process.exit(1) @@ -247,7 +255,9 @@ async function main() { // Step 13: Confirm the job was removed from the database console.log('Testing: Confirming the job was removed from the database') - const jobAfter = await db.backgroundJob.findFirst() + const rawJobsAfter = (await $`yarn rw exec jobs --silent`).toString() + const jobsAfter = JSON.parse(rawJobsAfter) + const jobAfter = jobsAfter.find((j: any) => j.id === job.id) if (jobAfter) { console.error('Expected job found in the database') process.exit(1)