Files
payloadcms/test/fields-relationship/seed.ts
Alessio Gravili 545d870650 chore: fix various e2e test setup issues (#12670)
I noticed a few issues when running e2e tests that will be resolved by
this PR:

- Most important: for some test suites (fields, fields-relationship,
versions, queues, lexical), the database was cleared and seeded
**twice** in between each test run. This is because the onInit function
was running the clear and seed script, when it should only have been
running the seed script. Clearing the database / the snapshot workflow
is being done by the reInit endpoint, which then calls onInit to seed
the actual data.
- The slowest part of `clearAndSeedEverything` is recreating indexes on
mongodb. This PR slightly improves performance here by:
- Skipping this process for the built-in `['payload-migrations',
'payload-preferences', 'payload-locked-documents']` collections
- Previously we were calling both `createIndexes` and `ensureIndexes`.
This was unnecessary - `ensureIndexes` is a deprecated alias of
`createIndexes`. This PR changes it to only call `createIndexes`
- Makes the reinit endpoint accept GET requests instead of POST requests
- this makes it easier to debug right in the browser
- Some typescript fixes
- Adds a `dev:memorydb` script to the package.json. For some reason,
`dev` is super unreliable on mongodb locally when running e2e tests - it
frequently fails during index creation. Using the memorydb fixes this
issue, with the bonus of more closely resembling the CI environment
- Previously, you were unable to run test suites using turbopack +
postgres. This fixes it, by explicitly installing `pg` as devDependency
in our monorepo
- Fixes jest open handles warning
2025-06-04 17:34:37 -03:00

174 lines
3.8 KiB
TypeScript

import type { Payload } from 'payload'
import { devUser } from '../credentials.js'
import {
collection1Slug,
collection2Slug,
podcastCollectionSlug,
relationOneSlug,
relationRestrictedSlug,
relationTwoSlug,
relationWithTitleSlug,
slug,
videoCollectionSlug,
} from './slugs.js'
export const seed = async (_payload: Payload) => {
await _payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
depth: 0,
overrideAccess: true,
})
// Create docs to relate to
const { id: relationOneDocId } = await _payload.create({
collection: relationOneSlug,
data: {
name: relationOneSlug,
},
depth: 0,
overrideAccess: true,
})
const relationOneIDs: string[] = []
for (let i = 0; i < 11; i++) {
const doc = await _payload.create({
collection: relationOneSlug,
data: {
name: relationOneSlug,
},
depth: 0,
overrideAccess: true,
})
relationOneIDs.push(doc.id)
}
const relationTwoIDs: string[] = []
for (let i = 0; i < 11; i++) {
const doc = await _payload.create({
collection: relationTwoSlug,
data: {
name: relationTwoSlug,
},
depth: 0,
overrideAccess: true,
})
relationTwoIDs.push(doc.id)
}
// Existing relationships
const { id: restrictedDocId } = await _payload.create({
collection: relationRestrictedSlug,
data: {
name: 'relation-restricted',
},
depth: 0,
overrideAccess: true,
})
const relationsWithTitle: string[] = []
for (const title of ['relation-title', 'word boundary search']) {
const { id } = await _payload.create({
collection: relationWithTitleSlug,
depth: 0,
overrideAccess: true,
data: {
name: title,
meta: {
title,
},
},
})
relationsWithTitle.push(id)
}
await _payload.create({
collection: slug,
depth: 0,
overrideAccess: true,
data: {
relationship: relationOneDocId,
relationshipRestricted: restrictedDocId,
relationshipWithTitle: relationsWithTitle[0],
},
})
for (let i = 0; i < 11; i++) {
await _payload.create({
collection: slug,
depth: 0,
overrideAccess: true,
data: {
relationship: relationOneDocId,
relationshipHasManyMultiple: relationOneIDs.map((id) => ({
relationTo: relationOneSlug,
value: id,
})),
relationshipRestricted: restrictedDocId,
},
})
}
for (let i = 0; i < 15; i++) {
const relationOneID = relationOneIDs[Math.floor(Math.random() * 10)]
const relationTwoID = relationTwoIDs[Math.floor(Math.random() * 10)]
await _payload.create({
collection: slug,
depth: 0,
overrideAccess: true,
data: {
relationship: relationOneDocId,
relationshipHasMany: [relationOneID],
relationshipHasManyMultiple: [{ relationTo: relationTwoSlug, value: relationTwoID }],
relationshipReadOnly: relationOneID,
relationshipRestricted: restrictedDocId,
},
})
}
for (let i = 0; i < 15; i++) {
await _payload.create({
collection: collection1Slug,
depth: 0,
overrideAccess: true,
data: {
name: `relationship-test ${i}`,
},
})
await _payload.create({
collection: collection2Slug,
depth: 0,
overrideAccess: true,
data: {
name: `relationship-test ${i}`,
},
})
}
for (let i = 0; i < 2; i++) {
await _payload.create({
collection: videoCollectionSlug,
data: {
id: i,
title: `Video ${i}`,
},
})
await _payload.create({
collection: podcastCollectionSlug,
data: {
id: i,
title: `Podcast ${i}`,
},
})
}
}