From 56f9c88251fd35a4d34ee9ea0d5424a2ccd1d9e0 Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Mon, 25 Mar 2024 23:58:55 -0400 Subject: [PATCH] chore: optimize test seed payload.create calls and run them in parallel to reduce MongoDB errors --- test/admin/seed.ts | 154 +++++++++++-------- test/fields/seed.ts | 252 ++++++++++++++++---------------- test/helpers/executePromises.ts | 2 +- test/versions/seed.ts | 59 +++++--- 4 files changed, 258 insertions(+), 209 deletions(-) diff --git a/test/admin/seed.ts b/test/admin/seed.ts index f6fdb2fa39..b691b12a65 100644 --- a/test/admin/seed.ts +++ b/test/admin/seed.ts @@ -1,6 +1,7 @@ import type { Payload } from 'payload' import { devUser } from '../credentials.js' +import { executePromises } from '../helpers/executePromises.js' import { seedDB } from '../helpers/seed.js' import { collectionSlugs, @@ -13,73 +14,102 @@ import { usersCollectionSlug, } from './slugs.js' -export async function clearAndSeedEverything(_payload: Payload) { +export async function clearAndSeedEverything(_payload: Payload, parallel: boolean = false) { return await seedDB({ snapshotKey: 'adminTest', - shouldResetDB: true, collectionSlugs, _payload, seedFunction: async (_payload) => { - await Promise.all([ - _payload.create({ - collection: usersCollectionSlug, - data: { - email: devUser.email, - password: devUser.password, - }, - }), - ...[...Array(11)].map(() => { - void _payload.create({ - collection: postsCollectionSlug, - data: { - title: 'Title', - description: 'Description', - }, - }) - }), - _payload.create({ - collection: customViews1CollectionSlug, - data: { - title: 'Custom View', - }, - }), - _payload.create({ - collection: customViews2CollectionSlug, - data: { - title: 'Custom View', - }, - }), - _payload.create({ - collection: geoCollectionSlug, - data: { - point: [7, -7], - }, - }), - _payload.create({ - collection: geoCollectionSlug, - data: { - point: [5, -5], - }, - }), - _payload.create({ - collection: noApiViewCollectionSlug, - data: {}, - }), - _payload.create({ - collection: 'customIdTab', - data: { - id: customIdCollectionId, - title: 'Hello world title', - }, - }), - _payload.create({ - collection: 'customIdRow', - data: { - id: customIdCollectionId, - title: 'Hello world title', - }, - }), - ]) + await executePromises( + [ + () => + _payload.create({ + collection: usersCollectionSlug, + data: { + email: devUser.email, + password: devUser.password, + }, + depth: 0, + overrideAccess: true, + }), + ...[...Array(11)].map( + () => () => + _payload.create({ + collection: postsCollectionSlug, + data: { + title: 'Title', + description: 'Description', + }, + depth: 0, + overrideAccess: true, + }), + ), + () => + _payload.create({ + collection: customViews1CollectionSlug, + data: { + title: 'Custom View', + }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: customViews2CollectionSlug, + data: { + title: 'Custom View', + }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: geoCollectionSlug, + data: { + point: [7, -7], + }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: geoCollectionSlug, + data: { + point: [5, -5], + }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: noApiViewCollectionSlug, + data: {}, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: 'customIdTab', + data: { + id: customIdCollectionId, + title: 'Hello world title', + }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: 'customIdRow', + data: { + id: customIdCollectionId, + title: 'Hello world title', + }, + depth: 0, + overrideAccess: true, + }), + ], + parallel, + ) }, }) } diff --git a/test/fields/seed.ts b/test/fields/seed.ts index 57e09ec578..1438364500 100644 --- a/test/fields/seed.ts +++ b/test/fields/seed.ts @@ -185,135 +185,137 @@ export async function clearAndSeedEverything(_payload: Payload, parallel: boolea .replace(/"\{\{RICH_TEXT_DOC_ID\}\}"/g, `${formattedRichTextDocID}`), ) - await executePromises([ - () => - _payload.create({ - collection: usersSlug, - depth: 0, - data: { - email: devUser.email, - password: devUser.password, - }, - overrideAccess: true, - }), - () => - _payload.create({ - collection: collapsibleFieldsSlug, - data: collapsibleDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: conditionalLogicSlug, - data: conditionalLogicDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: groupFieldsSlug, - data: groupDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: selectFieldsSlug, - data: selectsDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: radioFieldsSlug, - data: radiosDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: tabsFieldsSlug, - data: tabsDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: pointFieldsSlug, - data: pointDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: dateFieldsSlug, - data: dateDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: codeFieldsSlug, - data: codeDoc, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: jsonFieldsSlug, - data: jsonDoc, - depth: 0, - overrideAccess: true, - }), + await executePromises( + [ + () => + _payload.create({ + collection: usersSlug, + depth: 0, + data: { + email: devUser.email, + password: devUser.password, + }, + overrideAccess: true, + }), + () => + _payload.create({ + collection: collapsibleFieldsSlug, + data: collapsibleDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: conditionalLogicSlug, + data: conditionalLogicDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: groupFieldsSlug, + data: groupDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: selectFieldsSlug, + data: selectsDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: radioFieldsSlug, + data: radiosDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: tabsFieldsSlug, + data: tabsDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: pointFieldsSlug, + data: pointDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: dateFieldsSlug, + data: dateDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: codeFieldsSlug, + data: codeDoc, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: jsonFieldsSlug, + data: jsonDoc, + depth: 0, + overrideAccess: true, + }), - () => - _payload.create({ - collection: blockFieldsSlug, - data: blocksDocWithRichText, - depth: 0, - overrideAccess: true, - }), + () => + _payload.create({ + collection: blockFieldsSlug, + data: blocksDocWithRichText, + depth: 0, + overrideAccess: true, + }), - () => - _payload.create({ - collection: lexicalFieldsSlug, - data: lexicalDocWithRelId, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: lexicalMigrateFieldsSlug, - data: lexicalMigrateDocWithRelId, - depth: 0, - overrideAccess: true, - }), + () => + _payload.create({ + collection: lexicalFieldsSlug, + data: lexicalDocWithRelId, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: lexicalMigrateFieldsSlug, + data: lexicalMigrateDocWithRelId, + depth: 0, + overrideAccess: true, + }), - () => - _payload.create({ - collection: numberFieldsSlug, - data: { number: 2 }, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: numberFieldsSlug, - data: { number: 3 }, - depth: 0, - overrideAccess: true, - }), - () => - _payload.create({ - collection: numberFieldsSlug, - data: numberDoc, - depth: 0, - overrideAccess: true, - }), - ]) + () => + _payload.create({ + collection: numberFieldsSlug, + data: { number: 2 }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: numberFieldsSlug, + data: { number: 3 }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: numberFieldsSlug, + data: numberDoc, + depth: 0, + overrideAccess: true, + }), + ], + parallel, + ) }, - shouldResetDB: true, snapshotKey: 'fieldsTest', uploadsDir: path.resolve(dirname, './collections/Upload/uploads'), }) diff --git a/test/helpers/executePromises.ts b/test/helpers/executePromises.ts index 14861b54fc..54d5727a20 100644 --- a/test/helpers/executePromises.ts +++ b/test/helpers/executePromises.ts @@ -3,7 +3,7 @@ */ export async function executePromises Promise>>( promiseFns: T, - parallel: boolean = false, + parallel: boolean, ): Promise<{ [K in keyof T]: Awaited> }> { if (parallel) { // Parallel execution with Promise.all and maintain proper typing diff --git a/test/versions/seed.ts b/test/versions/seed.ts index d59f4f5d97..82b59ac541 100644 --- a/test/versions/seed.ts +++ b/test/versions/seed.ts @@ -1,14 +1,14 @@ import { type Payload } from 'payload' import { devUser } from '../credentials.js' +import { executePromises } from '../helpers/executePromises.js' import { seedDB } from '../helpers/seed.js' import { titleToDelete } from './shared.js' import { collectionSlugs, draftCollectionSlug } from './slugs.js' -export async function clearAndSeedEverything(_payload: Payload) { +export async function clearAndSeedEverything(_payload: Payload, parallel: boolean = false) { return await seedDB({ snapshotKey: 'versionsTest', - shouldResetDB: true, collectionSlugs, _payload, seedFunction: async (_payload) => { @@ -20,25 +20,34 @@ export async function clearAndSeedEverything(_payload: Payload) { }, ] - await Promise.all([ - _payload.create({ - collection: 'users', - data: { - email: devUser.email, - password: devUser.password, - }, - }), - _payload.create({ - collection: draftCollectionSlug, - data: { - blocksField, - description: 'Description', - radio: 'test', - title: 'Draft Title', - }, - draft: true, - }), - ]) + await executePromises( + [ + () => + _payload.create({ + collection: 'users', + data: { + email: devUser.email, + password: devUser.password, + }, + depth: 0, + overrideAccess: true, + }), + () => + _payload.create({ + collection: draftCollectionSlug, + data: { + blocksField, + description: 'Description', + radio: 'test', + title: 'Draft Title', + }, + depth: 0, + overrideAccess: true, + draft: true, + }), + ], + parallel, + ) const { id: manyDraftsID } = await _payload.create({ collection: draftCollectionSlug, @@ -48,6 +57,8 @@ export async function clearAndSeedEverything(_payload: Payload) { radio: 'test', title: 'Title With Many Versions', }, + depth: 0, + overrideAccess: true, draft: true, }) @@ -58,6 +69,8 @@ export async function clearAndSeedEverything(_payload: Payload) { data: { title: `Title With Many Versions ${i + 2}`, }, + depth: 0, + overrideAccess: true, }) } @@ -70,6 +83,8 @@ export async function clearAndSeedEverything(_payload: Payload) { radio: 'test', title: 'Published Title', }, + depth: 0, + overrideAccess: true, draft: false, }) @@ -80,6 +95,8 @@ export async function clearAndSeedEverything(_payload: Payload) { description: 'Description', title: titleToDelete, }, + depth: 0, + overrideAccess: true, draft: true, }) },