|
| 1 | +/* eslint vitest/no-standalone-expect: ["error", { "additionalTestBlockFunctions": ["test", "test.options"] }] -- Tests use the shared fixture wrapper. */ |
| 2 | + |
| 3 | +import type { PayloadRequest } from 'payload' |
| 4 | + |
| 5 | +import { initTransaction, killTransaction } from 'payload' |
| 6 | +import { expect } from 'vitest' |
| 7 | + |
| 8 | +import { test } from '../__helpers/int/vitest.js' |
| 9 | +import { customIDsSlug, postsSlug } from './shared.js' |
| 10 | + |
| 11 | +type CopyTestPost = { |
| 12 | + arrayWithIDs?: { id?: string; text?: string }[] |
| 13 | + blocksWithIDs?: { blockType: string; id?: string; text?: string }[] |
| 14 | + id: number | string |
| 15 | + title: string |
| 16 | +} |
| 17 | + |
| 18 | +test.suite('Database copy', { config: './config.ts' }, () => { |
| 19 | + test('should copy stored data with overrides and new row IDs', async ({ payload }) => { |
| 20 | + const source = (await payload.create({ |
| 21 | + collection: postsSlug, |
| 22 | + data: { |
| 23 | + arrayWithIDs: [{ text: 'source array row' }], |
| 24 | + blocksWithIDs: [{ blockType: 'block-first', text: 'source block row' }], |
| 25 | + title: 'source title', |
| 26 | + }, |
| 27 | + overrideAccess: true, |
| 28 | + })) as CopyTestPost |
| 29 | + |
| 30 | + const copied = (await payload.db.copy({ |
| 31 | + collection: postsSlug, |
| 32 | + data: { title: 'copied title' }, |
| 33 | + req: { payload }, |
| 34 | + where: { id: { equals: source.id } }, |
| 35 | + })) as CopyTestPost |
| 36 | + |
| 37 | + expect(copied.id).not.toBe(source.id) |
| 38 | + expect(copied.title).toBe('copied title') |
| 39 | + expect(copied.arrayWithIDs?.[0]?.text).toBe('source array row') |
| 40 | + expect(copied.arrayWithIDs?.[0]?.id).not.toBe(source.arrayWithIDs?.[0]?.id) |
| 41 | + expect(copied.blocksWithIDs?.[0]?.text).toBe('source block row') |
| 42 | + expect(copied.blocksWithIDs?.[0]?.id).not.toBe(source.blocksWithIDs?.[0]?.id) |
| 43 | + }) |
| 44 | + |
| 45 | + test('should reject a missing source document', async ({ payload }) => { |
| 46 | + await expect( |
| 47 | + payload.db.copy({ |
| 48 | + collection: postsSlug, |
| 49 | + req: { payload }, |
| 50 | + where: { id: { equals: 'missing-document' } }, |
| 51 | + }), |
| 52 | + ).rejects.toMatchObject({ name: 'NotFound' }) |
| 53 | + }) |
| 54 | + |
| 55 | + test('should require a destination ID for a custom-ID collection', async ({ payload }) => { |
| 56 | + const source = await payload.create({ |
| 57 | + collection: customIDsSlug, |
| 58 | + data: { title: 'custom ID source' }, |
| 59 | + overrideAccess: true, |
| 60 | + }) |
| 61 | + |
| 62 | + await expect( |
| 63 | + payload.db.copy({ |
| 64 | + collection: customIDsSlug, |
| 65 | + req: { payload }, |
| 66 | + where: { id: { equals: source.id } }, |
| 67 | + }), |
| 68 | + ).rejects.toThrow('requires data.id') |
| 69 | + }) |
| 70 | + |
| 71 | + test('should copy a custom-ID document when given a destination ID', async ({ payload }) => { |
| 72 | + const source = await payload.create({ |
| 73 | + collection: customIDsSlug, |
| 74 | + data: { title: 'custom ID source' }, |
| 75 | + overrideAccess: true, |
| 76 | + }) |
| 77 | + |
| 78 | + const copied = await payload.db.copy({ |
| 79 | + collection: customIDsSlug, |
| 80 | + data: { |
| 81 | + id: 'copied-custom-id', |
| 82 | + title: 'custom ID copy', |
| 83 | + }, |
| 84 | + req: { payload }, |
| 85 | + where: { id: { equals: source.id } }, |
| 86 | + }) |
| 87 | + |
| 88 | + expect(copied.id).toBe('copied-custom-id') |
| 89 | + expect(copied.title).toBe('custom ID copy') |
| 90 | + }) |
| 91 | + |
| 92 | + test.options( |
| 93 | + 'should leave rollback to the caller transaction', |
| 94 | + { db: (adapter) => adapter === 'mongodb' || adapter === 'postgres' }, |
| 95 | + async ({ payload }) => { |
| 96 | + const source = await payload.create({ |
| 97 | + collection: postsSlug, |
| 98 | + data: { title: 'transaction source' }, |
| 99 | + overrideAccess: true, |
| 100 | + }) |
| 101 | + const req = { payload } as PayloadRequest |
| 102 | + const didStartTransaction = await initTransaction(req) |
| 103 | + const callerTransactionID = await req.transactionID |
| 104 | + let copiedID: number | string | undefined |
| 105 | + |
| 106 | + expect(didStartTransaction).toBe(true) |
| 107 | + |
| 108 | + try { |
| 109 | + const copied = await payload.db.copy({ |
| 110 | + collection: postsSlug, |
| 111 | + req, |
| 112 | + where: { id: { equals: source.id } }, |
| 113 | + }) |
| 114 | + |
| 115 | + copiedID = copied.id |
| 116 | + expect(req.transactionID).toBe(callerTransactionID) |
| 117 | + } finally { |
| 118 | + if (req.transactionID) { |
| 119 | + await killTransaction(req) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + const copyAfterRollback = await payload.db.findOne({ |
| 124 | + collection: postsSlug, |
| 125 | + where: { id: { equals: copiedID } }, |
| 126 | + }) |
| 127 | + |
| 128 | + expect(copyAfterRollback).toBeNull() |
| 129 | + }, |
| 130 | + ) |
| 131 | +}) |
0 commit comments