directus

Форк
0
/
validate-diff.test.ts 
323 строки · 7.4 Кб
1
import { describe, expect, test } from 'vitest';
2
import type { Collection } from '../types/collection.js';
3
import type {
4
	Snapshot,
5
	SnapshotDiff,
6
	SnapshotDiffWithHash,
7
	SnapshotField,
8
	SnapshotRelation,
9
	SnapshotWithHash,
10
} from '../types/snapshot.js';
11
import { validateApplyDiff } from './validate-diff.js';
12

13
test('should fail on invalid diff schema', () => {
14
	const diff = {} as SnapshotDiffWithHash;
15
	const snapshot = {} as SnapshotWithHash;
16

17
	expect(() => validateApplyDiff(diff, snapshot)).toThrowError('"hash" is required');
18
});
19

20
test('should fail on invalid hash', () => {
21
	const diff = {
22
		hash: 'abc',
23
		diff: { collections: [{ collection: 'test', diff: [] }], fields: [], relations: [] },
24
	} as SnapshotDiffWithHash;
25

26
	const snapshot = { hash: 'xyz' } as SnapshotWithHash;
27

28
	expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
29
		"Provided hash does not match the current instance's schema hash",
30
	);
31
});
32

33
describe('should throw accurate error', () => {
34
	const baseDiff = (partialDiff: Partial<SnapshotDiff>): SnapshotDiffWithHash => {
35
		return {
36
			hash: 'abc',
37
			diff: {
38
				fields: [],
39
				collections: [],
40
				relations: [],
41
				...partialDiff,
42
			},
43
		};
44
	};
45

46
	const baseSnapshot = (partialSnapshot?: Partial<Snapshot>) => {
47
		return {
48
			hash: 'xyz',
49
			collections: [] as Collection[],
50
			fields: [] as SnapshotField[],
51
			relations: [] as SnapshotRelation[],
52
			...partialSnapshot,
53
		} as SnapshotWithHash;
54
	};
55

56
	test('creating collection which already exists', () => {
57
		const diff = baseDiff({
58
			collections: [{ collection: 'test', diff: [{ kind: 'N', rhs: {} as Collection }] }],
59
		});
60

61
		const snapshot = baseSnapshot({ collections: [{ collection: 'test' } as Collection] });
62

63
		expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
64
			'Provided diff is trying to create collection "test" but it already exists',
65
		);
66
	});
67

68
	test('deleting collection which does not exist', () => {
69
		const diff = baseDiff({
70
			collections: [{ collection: 'test', diff: [{ kind: 'D', lhs: {} as Collection }] }],
71
		});
72

73
		expect(() => validateApplyDiff(diff, baseSnapshot())).toThrowError(
74
			'Provided diff is trying to delete collection "test" but it does not exist',
75
		);
76
	});
77

78
	test('creating field which already exists', () => {
79
		const diff = baseDiff({
80
			fields: [{ collection: 'test', field: 'test', diff: [{ kind: 'N', rhs: {} as SnapshotField }] }],
81
		});
82

83
		const snapshot = baseSnapshot({ fields: [{ collection: 'test', field: 'test' } as SnapshotField] });
84

85
		expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
86
			'Provided diff is trying to create field "test.test" but it already exists',
87
		);
88
	});
89

90
	test('deleting field which does not exist', () => {
91
		const diff = baseDiff({
92
			fields: [{ collection: 'test', field: 'test', diff: [{ kind: 'D', lhs: {} as SnapshotField }] }],
93
		});
94

95
		expect(() => validateApplyDiff(diff, baseSnapshot())).toThrowError(
96
			'Provided diff is trying to delete field "test.test" but it does not exist',
97
		);
98
	});
99

100
	test('creating relation which already exists', () => {
101
		const diff = baseDiff({
102
			relations: [
103
				{
104
					collection: 'test',
105
					field: 'test',
106
					related_collection: 'relation',
107
					diff: [{ kind: 'N', rhs: {} as SnapshotRelation }],
108
				},
109
			],
110
		});
111

112
		const snapshot = baseSnapshot({
113
			relations: [{ collection: 'test', field: 'test', related_collection: 'relation' } as SnapshotRelation],
114
		});
115

116
		expect(() => validateApplyDiff(diff, snapshot)).toThrowError(
117
			'Provided diff is trying to create relation "test.test-> relation" but it already exists',
118
		);
119
	});
120

121
	test('deleting relation which does not exist', () => {
122
		const diff = baseDiff({
123
			relations: [
124
				{
125
					collection: 'test',
126
					field: 'test',
127
					related_collection: 'relation',
128
					diff: [{ kind: 'D', lhs: {} as SnapshotRelation }],
129
				},
130
			],
131
		});
132

133
		expect(() => validateApplyDiff(diff, baseSnapshot())).toThrowError(
134
			'Provided diff is trying to delete relation "test.test-> relation" but it does not exist',
135
		);
136
	});
137
});
138

139
test('should not throw error for diffs with varying types of lhs/rhs', () => {
140
	const diff: any = {
141
		hash: 'abc',
142
		diff: {
143
			collections: [
144
				{
145
					collection: 'a',
146
					diff: [
147
						{
148
							kind: 'E',
149
							path: ['meta', 'color'],
150
							lhs: null,
151
							rhs: '#6644FF',
152
						},
153
					],
154
				},
155
				{
156
					collection: 'a',
157
					diff: [
158
						{
159
							kind: 'A',
160
							path: ['meta', 'translations'],
161
							index: 1,
162
							item: {
163
								kind: 'N',
164
								rhs: {
165
									language: 'de-DE',
166
									translation: 'Collection A de-DE',
167
								},
168
							},
169
						},
170
					],
171
				},
172
				{
173
					collection: 'b',
174
					diff: [
175
						{
176
							kind: 'E',
177
							path: ['meta', 'translations', 1, 'language'],
178
							lhs: 'es-ES',
179
							rhs: 'nl-NL',
180
						},
181
						{
182
							kind: 'E',
183
							path: ['meta', 'translations', 1, 'translation'],
184
							lhs: 'nombre',
185
							rhs: 'naam',
186
						},
187
					],
188
				},
189
			],
190
			fields: [
191
				{
192
					collection: 'a',
193
					field: 'new_field',
194
					diff: [
195
						{
196
							kind: 'N',
197
							rhs: {
198
								collection: 'a',
199
								field: 'new_field',
200
								type: 'string',
201
								meta: {},
202
								schema: {},
203
							},
204
						},
205
					],
206
				},
207
				{
208
					collection: 'a',
209
					field: 'update_field',
210
					diff: [
211
						{
212
							kind: 'E',
213
							path: ['meta', 'options'],
214
							lhs: {
215
								iconLeft: 'check_circle',
216
							},
217
							rhs: null,
218
						},
219
					],
220
				},
221
				{
222
					collection: 'a',
223
					field: 'delete_field',
224
					diff: [
225
						{
226
							kind: 'D',
227
							lhs: {
228
								collection: 'a',
229
								field: 'delete_field',
230
								type: 'string',
231
								meta: {},
232
								schema: {},
233
							},
234
						},
235
					],
236
				},
237
			],
238
			relations: [
239
				{
240
					collection: 'a',
241
					field: 'm2o',
242
					related_collection: 'b',
243
					diff: [
244
						{
245
							kind: 'E',
246
							path: ['schema', 'on_delete'],
247
							lhs: 'SET NULL',
248
							rhs: 'CASCADE',
249
						},
250
					],
251
				},
252
			],
253
		},
254
	};
255

256
	const snapshot = { hash: 'abc' } as SnapshotWithHash;
257

258
	expect(() => validateApplyDiff(diff, snapshot)).not.toThrow();
259
});
260

261
test('should not throw error for relation diff with null related_collection (applicable for M2A junction tables)', () => {
262
	const diff: any = {
263
		hash: 'abc',
264
		diff: {
265
			collections: [],
266
			fields: [],
267
			relations: [
268
				{
269
					collection: 'pages_blocks',
270
					field: 'item',
271
					related_collection: null,
272
					diff: [
273
						{
274
							kind: 'N',
275
							rhs: {
276
								collection: 'pages_blocks',
277
								field: 'item',
278
								related_collection: null,
279
								meta: {
280
									junction_field: 'pages_id',
281
									many_collection: 'pages_blocks',
282
									many_field: 'item',
283
									one_allowed_collections: ['a', 'b'],
284
									one_collection: null,
285
									one_collection_field: 'collection',
286
									one_deselect_action: 'nullify',
287
									one_field: null,
288
									sort_field: null,
289
								},
290
							},
291
						},
292
					],
293
				},
294
			],
295
		},
296
	};
297

298
	const snapshot = { hash: 'abc' } as SnapshotWithHash;
299

300
	expect(() => validateApplyDiff(diff, snapshot)).not.toThrow();
301
});
302

303
test('should detect empty diff', () => {
304
	const diff = {
305
		hash: 'abc',
306
		diff: { collections: [], fields: [], relations: [] },
307
	};
308

309
	const snapshot = {} as SnapshotWithHash;
310

311
	expect(validateApplyDiff(diff, snapshot)).toBe(false);
312
});
313

314
test('should pass on valid diff', () => {
315
	const diff = {
316
		hash: 'abc',
317
		diff: { collections: [{ collection: 'test', diff: [] }], fields: [], relations: [] },
318
	};
319

320
	const snapshot = { hash: 'abc' } as SnapshotWithHash;
321

322
	expect(validateApplyDiff(diff, snapshot)).toBe(true);
323
});
324

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.