chore: optimize test seed payload.create calls and run them in parallel to reduce MongoDB errors

This commit is contained in:
Alessio Gravili
2024-03-25 23:58:55 -04:00
parent 8a5a08cbe1
commit 56f9c88251
4 changed files with 258 additions and 209 deletions

View File

@@ -1,6 +1,7 @@
import type { Payload } from 'payload' import type { Payload } from 'payload'
import { devUser } from '../credentials.js' import { devUser } from '../credentials.js'
import { executePromises } from '../helpers/executePromises.js'
import { seedDB } from '../helpers/seed.js' import { seedDB } from '../helpers/seed.js'
import { import {
collectionSlugs, collectionSlugs,
@@ -13,73 +14,102 @@ import {
usersCollectionSlug, usersCollectionSlug,
} from './slugs.js' } from './slugs.js'
export async function clearAndSeedEverything(_payload: Payload) { export async function clearAndSeedEverything(_payload: Payload, parallel: boolean = false) {
return await seedDB({ return await seedDB({
snapshotKey: 'adminTest', snapshotKey: 'adminTest',
shouldResetDB: true,
collectionSlugs, collectionSlugs,
_payload, _payload,
seedFunction: async (_payload) => { seedFunction: async (_payload) => {
await Promise.all([ await executePromises(
_payload.create({ [
collection: usersCollectionSlug, () =>
data: { _payload.create({
email: devUser.email, collection: usersCollectionSlug,
password: devUser.password, data: {
}, email: devUser.email,
}), password: devUser.password,
...[...Array(11)].map(() => { },
void _payload.create({ depth: 0,
collection: postsCollectionSlug, overrideAccess: true,
data: { }),
title: 'Title', ...[...Array(11)].map(
description: 'Description', () => () =>
}, _payload.create({
}) collection: postsCollectionSlug,
}), data: {
_payload.create({ title: 'Title',
collection: customViews1CollectionSlug, description: 'Description',
data: { },
title: 'Custom View', depth: 0,
}, overrideAccess: true,
}), }),
_payload.create({ ),
collection: customViews2CollectionSlug, () =>
data: { _payload.create({
title: 'Custom View', collection: customViews1CollectionSlug,
}, data: {
}), title: 'Custom View',
_payload.create({ },
collection: geoCollectionSlug, depth: 0,
data: { overrideAccess: true,
point: [7, -7], }),
}, () =>
}), _payload.create({
_payload.create({ collection: customViews2CollectionSlug,
collection: geoCollectionSlug, data: {
data: { title: 'Custom View',
point: [5, -5], },
}, depth: 0,
}), overrideAccess: true,
_payload.create({ }),
collection: noApiViewCollectionSlug, () =>
data: {}, _payload.create({
}), collection: geoCollectionSlug,
_payload.create({ data: {
collection: 'customIdTab', point: [7, -7],
data: { },
id: customIdCollectionId, depth: 0,
title: 'Hello world title', overrideAccess: true,
}, }),
}), () =>
_payload.create({ _payload.create({
collection: 'customIdRow', collection: geoCollectionSlug,
data: { data: {
id: customIdCollectionId, point: [5, -5],
title: 'Hello world title', },
}, 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,
)
}, },
}) })
} }

View File

@@ -185,135 +185,137 @@ export async function clearAndSeedEverything(_payload: Payload, parallel: boolea
.replace(/"\{\{RICH_TEXT_DOC_ID\}\}"/g, `${formattedRichTextDocID}`), .replace(/"\{\{RICH_TEXT_DOC_ID\}\}"/g, `${formattedRichTextDocID}`),
) )
await executePromises([ await executePromises(
() => [
_payload.create({ () =>
collection: usersSlug, _payload.create({
depth: 0, collection: usersSlug,
data: { depth: 0,
email: devUser.email, data: {
password: devUser.password, email: devUser.email,
}, password: devUser.password,
overrideAccess: true, },
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: collapsibleFieldsSlug, _payload.create({
data: collapsibleDoc, collection: collapsibleFieldsSlug,
depth: 0, data: collapsibleDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: conditionalLogicSlug, _payload.create({
data: conditionalLogicDoc, collection: conditionalLogicSlug,
depth: 0, data: conditionalLogicDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: groupFieldsSlug, _payload.create({
data: groupDoc, collection: groupFieldsSlug,
depth: 0, data: groupDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: selectFieldsSlug, _payload.create({
data: selectsDoc, collection: selectFieldsSlug,
depth: 0, data: selectsDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: radioFieldsSlug, _payload.create({
data: radiosDoc, collection: radioFieldsSlug,
depth: 0, data: radiosDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: tabsFieldsSlug, _payload.create({
data: tabsDoc, collection: tabsFieldsSlug,
depth: 0, data: tabsDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: pointFieldsSlug, _payload.create({
data: pointDoc, collection: pointFieldsSlug,
depth: 0, data: pointDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: dateFieldsSlug, _payload.create({
data: dateDoc, collection: dateFieldsSlug,
depth: 0, data: dateDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: codeFieldsSlug, _payload.create({
data: codeDoc, collection: codeFieldsSlug,
depth: 0, data: codeDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
() => }),
_payload.create({ () =>
collection: jsonFieldsSlug, _payload.create({
data: jsonDoc, collection: jsonFieldsSlug,
depth: 0, data: jsonDoc,
overrideAccess: true, depth: 0,
}), overrideAccess: true,
}),
() => () =>
_payload.create({ _payload.create({
collection: blockFieldsSlug, collection: blockFieldsSlug,
data: blocksDocWithRichText, data: blocksDocWithRichText,
depth: 0, depth: 0,
overrideAccess: true, overrideAccess: true,
}), }),
() => () =>
_payload.create({ _payload.create({
collection: lexicalFieldsSlug, collection: lexicalFieldsSlug,
data: lexicalDocWithRelId, data: lexicalDocWithRelId,
depth: 0, depth: 0,
overrideAccess: true, overrideAccess: true,
}), }),
() => () =>
_payload.create({ _payload.create({
collection: lexicalMigrateFieldsSlug, collection: lexicalMigrateFieldsSlug,
data: lexicalMigrateDocWithRelId, data: lexicalMigrateDocWithRelId,
depth: 0, depth: 0,
overrideAccess: true, overrideAccess: true,
}), }),
() => () =>
_payload.create({ _payload.create({
collection: numberFieldsSlug, collection: numberFieldsSlug,
data: { number: 2 }, data: { number: 2 },
depth: 0, depth: 0,
overrideAccess: true, overrideAccess: true,
}), }),
() => () =>
_payload.create({ _payload.create({
collection: numberFieldsSlug, collection: numberFieldsSlug,
data: { number: 3 }, data: { number: 3 },
depth: 0, depth: 0,
overrideAccess: true, overrideAccess: true,
}), }),
() => () =>
_payload.create({ _payload.create({
collection: numberFieldsSlug, collection: numberFieldsSlug,
data: numberDoc, data: numberDoc,
depth: 0, depth: 0,
overrideAccess: true, overrideAccess: true,
}), }),
]) ],
parallel,
)
}, },
shouldResetDB: true,
snapshotKey: 'fieldsTest', snapshotKey: 'fieldsTest',
uploadsDir: path.resolve(dirname, './collections/Upload/uploads'), uploadsDir: path.resolve(dirname, './collections/Upload/uploads'),
}) })

View File

@@ -3,7 +3,7 @@
*/ */
export async function executePromises<T extends Array<() => Promise<any>>>( export async function executePromises<T extends Array<() => Promise<any>>>(
promiseFns: T, promiseFns: T,
parallel: boolean = false, parallel: boolean,
): Promise<{ [K in keyof T]: Awaited<ReturnType<T[K]>> }> { ): Promise<{ [K in keyof T]: Awaited<ReturnType<T[K]>> }> {
if (parallel) { if (parallel) {
// Parallel execution with Promise.all and maintain proper typing // Parallel execution with Promise.all and maintain proper typing

View File

@@ -1,14 +1,14 @@
import { type Payload } from 'payload' import { type Payload } from 'payload'
import { devUser } from '../credentials.js' import { devUser } from '../credentials.js'
import { executePromises } from '../helpers/executePromises.js'
import { seedDB } from '../helpers/seed.js' import { seedDB } from '../helpers/seed.js'
import { titleToDelete } from './shared.js' import { titleToDelete } from './shared.js'
import { collectionSlugs, draftCollectionSlug } from './slugs.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({ return await seedDB({
snapshotKey: 'versionsTest', snapshotKey: 'versionsTest',
shouldResetDB: true,
collectionSlugs, collectionSlugs,
_payload, _payload,
seedFunction: async (_payload) => { seedFunction: async (_payload) => {
@@ -20,25 +20,34 @@ export async function clearAndSeedEverything(_payload: Payload) {
}, },
] ]
await Promise.all([ await executePromises(
_payload.create({ [
collection: 'users', () =>
data: { _payload.create({
email: devUser.email, collection: 'users',
password: devUser.password, data: {
}, email: devUser.email,
}), password: devUser.password,
_payload.create({ },
collection: draftCollectionSlug, depth: 0,
data: { overrideAccess: true,
blocksField, }),
description: 'Description', () =>
radio: 'test', _payload.create({
title: 'Draft Title', collection: draftCollectionSlug,
}, data: {
draft: true, blocksField,
}), description: 'Description',
]) radio: 'test',
title: 'Draft Title',
},
depth: 0,
overrideAccess: true,
draft: true,
}),
],
parallel,
)
const { id: manyDraftsID } = await _payload.create({ const { id: manyDraftsID } = await _payload.create({
collection: draftCollectionSlug, collection: draftCollectionSlug,
@@ -48,6 +57,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
radio: 'test', radio: 'test',
title: 'Title With Many Versions', title: 'Title With Many Versions',
}, },
depth: 0,
overrideAccess: true,
draft: true, draft: true,
}) })
@@ -58,6 +69,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
data: { data: {
title: `Title With Many Versions ${i + 2}`, title: `Title With Many Versions ${i + 2}`,
}, },
depth: 0,
overrideAccess: true,
}) })
} }
@@ -70,6 +83,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
radio: 'test', radio: 'test',
title: 'Published Title', title: 'Published Title',
}, },
depth: 0,
overrideAccess: true,
draft: false, draft: false,
}) })
@@ -80,6 +95,8 @@ export async function clearAndSeedEverything(_payload: Payload) {
description: 'Description', description: 'Description',
title: titleToDelete, title: titleToDelete,
}, },
depth: 0,
overrideAccess: true,
draft: true, draft: true,
}) })
}, },