Skip to content

Commit 844f99f

Browse files
test: 21x faster versions int suite (#14510)
Removes `clearAndSeedEverything` that was being run before every version test. The changes here make sure data is isolated per describe block/test. This means we can continue to use the same DB and there is no reason to clear and seed. Before: ~18 minutes After: ~52 seconds
1 parent d3b934c commit 844f99f

File tree

3 files changed

+991
-591
lines changed

3 files changed

+991
-591
lines changed

test/versions/clearAndSeedEverything.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/versions/helpers.ts

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import type { CollectionSlug, GlobalSlug, Payload } from 'payload'
2+
3+
import { toSnakeCase } from 'drizzle-orm/casing'
4+
5+
import type { DraftPost } from './payload-types.js'
6+
7+
import { isMongoose } from '../helpers/isMongoose.js'
8+
9+
/**
10+
* Creates multiple versions of a document by updating it sequentially
11+
*
12+
* @example
13+
* const doc = await createVersions({
14+
* payload,
15+
* collection: 'draft-posts',
16+
* data: { title: 'Test' },
17+
* total: 10
18+
* })
19+
*/
20+
21+
/**
22+
* Creates a draft document with standard blocksField structure
23+
*
24+
* @example
25+
* const draft = await createDraftDocument({
26+
* payload,
27+
* collection: 'draft-posts',
28+
* title: 'My Draft',
29+
* description: 'Test description'
30+
* })
31+
*/
32+
export async function createDraftDocument({
33+
additionalData = {},
34+
blocksField,
35+
collection,
36+
description = 'Description',
37+
payload,
38+
title,
39+
}: {
40+
additionalData?: Record<string, any>
41+
blocksField?: DraftPost['blocksField']
42+
collection: string
43+
description?: string
44+
payload: Payload
45+
title: string
46+
}) {
47+
const defaultBlocksField: DraftPost['blocksField'] = [
48+
{
49+
blockType: 'block',
50+
localized: null,
51+
text: 'Hello World',
52+
},
53+
]
54+
55+
return await payload.create({
56+
collection,
57+
data: {
58+
blocksField: blocksField || defaultBlocksField,
59+
description,
60+
radio: 'test',
61+
title,
62+
...additionalData,
63+
},
64+
depth: 0,
65+
draft: true,
66+
overrideAccess: true,
67+
})
68+
}
69+
70+
/**
71+
* Creates a document with many versions by incrementally updating a field
72+
*
73+
* @example
74+
* const { id, versions } = await createDocumentWithManyVersions({
75+
* payload,
76+
* collection: 'draft-posts',
77+
* initialData: { title: 'Initial Title' },
78+
* versionCount: 10,
79+
* updateField: 'title',
80+
* updateValue: (i) => `Title Version ${i}`
81+
* })
82+
*/
83+
export async function createDocumentWithManyVersions({
84+
collection,
85+
draft = false,
86+
initialData,
87+
payload,
88+
updateField,
89+
updateValue,
90+
versionCount = 10,
91+
}: {
92+
collection: string
93+
draft?: boolean
94+
initialData: Record<string, any>
95+
payload: Payload
96+
updateField: string
97+
updateValue: (index: number) => any
98+
versionCount?: number
99+
}) {
100+
const doc = await payload.create({
101+
collection,
102+
data: initialData,
103+
depth: 0,
104+
draft,
105+
overrideAccess: true,
106+
})
107+
108+
const versions = [doc]
109+
110+
for (let i = 1; i < versionCount; i++) {
111+
const updated = await payload.update({
112+
id: doc.id,
113+
collection,
114+
data: {
115+
[updateField]: updateValue(i),
116+
},
117+
depth: 0,
118+
overrideAccess: true,
119+
})
120+
versions.push(updated)
121+
}
122+
123+
return { id: doc.id, versions }
124+
}
125+
126+
export async function cleanupDocuments({
127+
payload,
128+
collectionSlugs,
129+
}: {
130+
collectionSlugs: CollectionSlug[]
131+
payload: Payload
132+
}) {
133+
for (const collectionSlug of collectionSlugs) {
134+
await payload.delete({
135+
collection: collectionSlug,
136+
where: {
137+
id: {
138+
exists: true,
139+
},
140+
},
141+
overrideAccess: true,
142+
})
143+
}
144+
}
145+
146+
export async function cleanupGlobal({
147+
payload,
148+
globalSlug,
149+
}: {
150+
globalSlug: GlobalSlug
151+
payload: Payload
152+
}) {
153+
if (isMongoose(payload)) {
154+
await payload.db.updateGlobal({
155+
slug: globalSlug,
156+
data: {
157+
title: {},
158+
content: {},
159+
},
160+
})
161+
} else {
162+
await payload.db.drizzle.delete(payload.db.tables[toSnakeCase(globalSlug)])
163+
}
164+
165+
await payload.db.deleteVersions({
166+
globalSlug,
167+
where: {},
168+
})
169+
}

0 commit comments

Comments
 (0)