directus

Форк
0
/
sanitize-schema.test.ts 
346 строк · 7.6 Кб
1
import type { Field, Relation } from '@directus/types';
2
import { expect, test, describe } from 'vitest';
3
import type { Collection } from '../types/index.js';
4
import { sanitizeCollection, sanitizeField, sanitizeRelation } from './sanitize-schema.js';
5

6
describe('sanitizeCollection', () => {
7
	test.each([
8
		// Not supported in SQLite + comment in MSSQL
9
		{
10
			collection: 'test',
11
			meta: {
12
				accountability: 'all',
13
				collection: 'test',
14
				group: null,
15
				hidden: false,
16
				icon: null,
17
				item_duplication_fields: null,
18
				note: null,
19
				singleton: false,
20
				translations: {},
21
			},
22
			schema: { comment: null, name: 'test', schema: 'public' },
23
		},
24
		// MySQL Only
25
		{
26
			collection: 'test',
27
			meta: {
28
				accountability: 'all',
29
				collection: 'test',
30
				group: null,
31
				hidden: false,
32
				icon: null,
33
				item_duplication_fields: null,
34
				note: null,
35
				singleton: false,
36
				translations: {},
37
			},
38
			schema: { collation: 'latin1_swedish_ci', name: 'test', engine: 'InnoDB' },
39
		},
40
		// Postgres Only
41
		{
42
			collection: 'test',
43
			meta: {
44
				accountability: 'all',
45
				collection: 'test',
46
				group: null,
47
				hidden: false,
48
				icon: null,
49
				item_duplication_fields: null,
50
				note: null,
51
				singleton: false,
52
				translations: {},
53
			},
54
			schema: { name: 'test', owner: 'postgres' },
55
		},
56
		// SQLite Only
57
		{
58
			collection: 'test',
59
			meta: {
60
				accountability: 'all',
61
				collection: 'test',
62
				group: null,
63
				hidden: false,
64
				icon: null,
65
				item_duplication_fields: null,
66
				note: null,
67
				singleton: false,
68
				translations: {},
69
			},
70
			schema: { name: 'test', sql: 'CREATE TABLE `test` (`id` integer not null primary key autoincrement)' },
71
		},
72
		// MSSQL only
73
		{
74
			collection: 'test',
75
			meta: {
76
				accountability: 'all',
77
				collection: 'test',
78
				group: null,
79
				hidden: false,
80
				icon: null,
81
				item_duplication_fields: null,
82
				note: null,
83
				singleton: false,
84
				translations: {},
85
			},
86
			schema: { name: 'test', catalog: 'test-db' },
87
		},
88
	] satisfies Collection[])('should only contain name property in collection schema', (testCollection) => {
89
		const result = sanitizeCollection(testCollection);
90

91
		expect(result).toEqual({
92
			collection: 'test',
93
			meta: {
94
				accountability: 'all',
95
				collection: 'test',
96
				group: null,
97
				hidden: false,
98
				icon: null,
99
				item_duplication_fields: null,
100
				note: null,
101
				singleton: false,
102
				translations: {},
103
			},
104
			schema: { name: 'test' },
105
		});
106
	});
107
});
108

109
describe('sanitizeField', () => {
110
	test('should only contain certain properties in field schema when sanitizeAllSchema is false', () => {
111
		const testField = {
112
			collection: 'test',
113
			field: 'id',
114
			name: 'id',
115
			meta: {
116
				id: 1,
117
				collection: 'test',
118
				conditions: null,
119
				display: null,
120
				display_options: null,
121
				field: 'id',
122
				group: null,
123
				hidden: true,
124
				interface: 'input',
125
				note: null,
126
				options: null,
127
				readonly: true,
128
				required: false,
129
				sort: null,
130
				special: null,
131
				translations: null,
132
				validation: null,
133
				validation_message: null,
134
				width: 'full',
135
			},
136
			schema: {
137
				comment: null,
138
				data_type: 'integer',
139
				default_value: "nextval('test_id_seq'::regclass)",
140
				foreign_key_column: null,
141
				foreign_key_schema: null,
142
				foreign_key_table: null,
143
				generation_expression: null,
144
				has_auto_increment: true,
145
				is_generated: false,
146
				is_nullable: false,
147
				is_primary_key: true,
148
				is_unique: true,
149
				max_length: null,
150
				name: 'id',
151
				numeric_precision: 32,
152
				numeric_scale: 0,
153
				schema: 'public',
154
				table: 'test',
155
			},
156
			type: 'integer',
157
		} satisfies Field;
158

159
		const result = sanitizeField(testField);
160

161
		expect(result).toEqual({
162
			collection: 'test',
163
			field: 'id',
164
			name: 'id',
165
			meta: {
166
				id: 1,
167
				collection: 'test',
168
				conditions: null,
169
				display: null,
170
				display_options: null,
171
				field: 'id',
172
				group: null,
173
				hidden: true,
174
				interface: 'input',
175
				note: null,
176
				options: null,
177
				readonly: true,
178
				required: false,
179
				sort: null,
180
				special: null,
181
				translations: null,
182
				validation: null,
183
				validation_message: null,
184
				width: 'full',
185
			},
186
			schema: {
187
				data_type: 'integer',
188
				default_value: "nextval('test_id_seq'::regclass)",
189
				foreign_key_column: null,
190
				foreign_key_table: null,
191
				generation_expression: null,
192
				has_auto_increment: true,
193
				is_generated: false,
194
				is_nullable: false,
195
				is_primary_key: true,
196
				is_unique: true,
197
				max_length: null,
198
				name: 'id',
199
				numeric_precision: 32,
200
				numeric_scale: 0,
201

202
				table: 'test',
203
			},
204
			type: 'integer',
205
		});
206
	});
207

208
	test('should not contain field schema when sanitizeAllSchema is true', () => {
209
		const testField = {
210
			collection: 'test',
211
			field: 'id',
212
			name: 'id',
213
			meta: {
214
				id: 1,
215
				collection: 'test',
216
				conditions: null,
217
				display: null,
218
				display_options: null,
219
				field: 'id',
220
				group: null,
221
				hidden: true,
222
				interface: 'input',
223
				note: null,
224
				options: null,
225
				readonly: true,
226
				required: false,
227
				sort: null,
228
				special: null,
229
				translations: null,
230
				validation: null,
231
				validation_message: null,
232
				width: 'full',
233
			},
234
			schema: {
235
				data_type: 'integer',
236
				default_value: "nextval('test_id_seq'::regclass)",
237
				foreign_key_column: null,
238
				foreign_key_table: null,
239
				generation_expression: null,
240
				has_auto_increment: true,
241
				is_generated: false,
242
				is_nullable: false,
243
				is_primary_key: true,
244
				is_unique: true,
245
				max_length: null,
246
				name: 'id',
247
				numeric_precision: 32,
248
				numeric_scale: 0,
249
				table: 'test',
250
			},
251
			type: 'integer',
252
		} satisfies Field;
253

254
		const result = sanitizeField(testField, true);
255

256
		expect(result).toEqual({
257
			collection: 'test',
258
			field: 'id',
259
			name: 'id',
260
			meta: {
261
				id: 1,
262
				collection: 'test',
263
				conditions: null,
264
				display: null,
265
				display_options: null,
266
				field: 'id',
267
				group: null,
268
				hidden: true,
269
				interface: 'input',
270
				note: null,
271
				options: null,
272
				readonly: true,
273
				required: false,
274
				sort: null,
275
				special: null,
276
				translations: null,
277
				validation: null,
278
				validation_message: null,
279
				width: 'full',
280
			},
281
			type: 'integer',
282
		});
283
	});
284
});
285

286
describe('sanitizeRelation', () => {
287
	test.each([
288
		// Postgres + MSSSQL
289
		{
290
			collection: 'test_example',
291
			field: 'm2m',
292
			related_collection: 'test',
293
			meta: {
294
				id: 1,
295
				junction_field: 'example_id',
296
				many_collection: 'test_example',
297
				many_field: 'test_id',
298
				one_allowed_collections: null,
299
				one_collection: 'test',
300
				one_collection_field: null,
301
				one_deselect_action: 'nullify',
302
				one_field: 'm2m',
303
				sort_field: null,
304
			},
305
			schema: {
306
				table: 'test_example',
307
				column: 'test_id',
308
				foreign_key_table: 'test',
309
				foreign_key_column: 'id',
310
				foreign_key_schema: 'public',
311
				constraint_name: 'test_example_test_id_foreign',
312
				on_update: 'NO ACTION',
313
				on_delete: 'SET NULL',
314
			},
315
		},
316
	] satisfies Relation[])('should only contain certain properties in relation schema', (testRelation) => {
317
		const result = sanitizeRelation(testRelation);
318

319
		expect(result).toEqual({
320
			collection: 'test_example',
321
			field: 'm2m',
322
			related_collection: 'test',
323
			meta: {
324
				id: 1,
325
				junction_field: 'example_id',
326
				many_collection: 'test_example',
327
				many_field: 'test_id',
328
				one_allowed_collections: null,
329
				one_collection: 'test',
330
				one_collection_field: null,
331
				one_deselect_action: 'nullify',
332
				one_field: 'm2m',
333
				sort_field: null,
334
			},
335
			schema: {
336
				table: 'test_example',
337
				column: 'test_id',
338
				foreign_key_table: 'test',
339
				foreign_key_column: 'id',
340
				constraint_name: 'test_example_test_id_foreign',
341
				on_update: 'NO ACTION',
342
				on_delete: 'SET NULL',
343
			},
344
		});
345
	});
346
});
347

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

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

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

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