Files
directus/api/src/utils/apply-snapshot.test.ts
Pascal Jufer 17afb9a3bc Patch Tuesday 🐸 (#18260)
* Update `@directus/api` deps (minor)

 @aws-sdk/client-ses          3.292.0  →   3.316.0
 @godaddy/terminus             4.11.2  →    4.12.0
 @rollup/plugin-alias           4.0.3  →     4.0.4
 @rollup/plugin-node-resolve   15.0.1  →    15.0.2
 @types/node                  18.15.3  →  18.15.11
 @vitest/coverage-c8           0.29.3  →    0.30.1
 axios                          1.3.4  →     1.3.6
 fs-extra                      11.1.0  →    11.1.1
 helmet                         6.0.1  →     6.1.5
 ioredis                        5.3.1  →     5.3.2
 joi                           17.8.4  →    17.9.1
 liquidjs                      10.6.1  →    10.7.0
 marked                        4.2.12  →     4.3.0
 nanoid                         3.3.4  →     3.3.6
 rollup                        3.19.1  →    3.20.6
 sharp                         0.31.3  →    0.32.0
 vitest                        0.29.3  →    0.30.1
 vm2                           3.9.16  →    3.9.17

* Update `@directus/api` deps (major)

 @rollup/plugin-alias     4.0.4  →     5.0.0
 @types/inquirer          8.2.6  →     9.0.3
 @types/node           18.15.11  →  18.15.12
 camelcase                6.3.0  →     7.0.1
 chalk                    4.1.2  →     5.2.0
 commander                9.5.0  →    10.0.1
 execa                    5.1.1  →     7.1.1
 icc                      2.0.0  →     3.0.0
 inquirer                 8.2.5  →     9.1.5
 ~ldapjs                   2.3.3  →     3.0.2~
 nanoid                   3.3.6  →     4.0.2
 ~openapi3-ts              3.2.0  →     4.1.1~
 ora                      5.4.1  →     6.3.0
 strip-bom-stream         4.0.0  →     5.0.0
 tedious                 15.1.3  →    16.0.0
 typescript               4.9.5  →     5.0.4

* Update `@directus/app` deps

 @babel/core                             7.21.3  →    7.21.4
 @babel/preset-env                       7.20.2  →    7.21.4
 @fortawesome/fontawesome-svg-core        6.3.0  →     6.4.0
 @fortawesome/free-brands-svg-icons       6.3.0  →     6.4.0
 @fullcalendar/core                       6.1.4  →     6.1.5
 @fullcalendar/daygrid                    6.1.4  →     6.1.5
 @fullcalendar/interaction                6.1.4  →     6.1.5
 @fullcalendar/list                       6.1.4  →     6.1.5
 @fullcalendar/timegrid                   6.1.4  →     6.1.5
 @pinia/testing                          0.0.15  →    0.0.16
 @popperjs/core                          2.11.6  →    2.11.7
 @storybook/addon-actions            7.0.0-rc.4  →     7.0.6
 @storybook/addon-backgrounds        7.0.0-rc.4  →     7.0.6
 @storybook/addon-docs               7.0.0-rc.4  →     7.0.6
 @storybook/addon-essentials         7.0.0-rc.4  →     7.0.6
 @storybook/addon-links              7.0.0-rc.4  →     7.0.6
 @storybook/addon-mdx-gfm            7.0.0-rc.4  →     7.0.6
 @storybook/addon-measure            7.0.0-rc.4  →     7.0.6
 @storybook/addon-outline            7.0.0-rc.4  →     7.0.6
 @storybook/client-api               7.0.0-rc.4  →     7.0.6
 @storybook/client-logger            7.0.0-rc.4  →     7.0.6
 @storybook/vue3                     7.0.0-rc.4  →     7.0.6
 @storybook/vue3-vite                7.0.0-rc.4  →     7.0.6
 @types/diff                              5.0.2  →     5.0.3
 @types/dompurify                         3.0.0  →     3.0.2
 @types/lodash                         4.14.191  →  4.14.194
 @vitejs/plugin-vue                       4.0.0  →     4.1.0
 @vue/test-utils                          2.3.1  →     2.3.2
 apexcharts                              3.37.1  →    3.39.0
 axios                                    1.3.4  →     1.3.6
 dompurify                                3.0.1  →     3.0.2
 happy-dom                                8.9.0  →     9.8.4
 marked                                  4.2.12  →     4.3.0
 nanoid                                   4.0.1  →     4.0.2
 pinia                                   2.0.33  →    2.0.34
 sass                                    1.59.3  →    1.62.0
 storybook                           7.0.0-rc.4  →     7.0.6
 typescript                               4.9.5  →     5.0.4
 vite                                     4.1.4  →     4.2.2
 vitest                                  0.29.3  →    0.30.1
 webpack                                 5.76.2  →    5.80.0

* Update root deps

* Update `@directus/composables` deps

* Update `@directus/constant` deps

* Update `create-directus-extension` deps

* Update `@directus/exceptions` deps

* tsconfig workaround: ignoreDeprecations

* Update `@directus/extensions-sdk` deps

* Update `@directus/schema` deps

* Update `@directus/storage` deps

* Update `@directus/storage-driver-azure` deps

* Update `@directus/storage-driver-cloudinary` deps

* Update `@directus/storage-driver-gcs` deps

* Update `@directus/storage-driver-local` deps

* Update `@directus/storage-driver-s3` deps

* Update `@directus/types` deps

* Update `@directus/update-check` deps

* Update `@directus/utils` deps

* tsconfig workaround in schema: ignoreDeprecations

* tsconfig workaround in remaining packages: ignoreDeprecations

* Update `tests-blackbox` deps

* Revert "tsconfig workaround: ignoreDeprecations"

This reverts commit 5d97da55e3.

* Revert tsconfig override

* Update tsconfig

* Fix imports in @directus/utils

* Fix imports in composables

* Fix imports in extensions-sdk

* Fix imports in @directus/api

* Move RateLimiterAbstract to types import as well

* Bump pnpm to 8.3.1

* Update `docs` deps

> [...] the @vueuse/head package will be deprecated. If you're setting up this package on a new project, you should use the @unhead/vue package directly [...]

* Remove obselete dep `concurrently` from extensions-sdk

Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>

* New day, new updates

Also forgot to include minor updates of deps in `tests-blackbox` in previous
commit

* Fix `api` tests

---------

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>
2023-04-21 12:59:27 -04:00

449 lines
12 KiB
TypeScript

import type { Knex } from 'knex';
import knex from 'knex';
import { createTracker, MockClient, Tracker } from 'knex-mock-client';
import type { MockedFunction } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { snapshotApplyTestSchema } from '../__utils__/schemas.js';
import {
snapshotBeforeCreateCollection,
snapshotBeforeDeleteCollection,
snapshotCreateCollection,
snapshotCreateCollectionNotNested,
} from '../__utils__/snapshots.js';
import { CollectionsService, FieldsService } from '../services/index.js';
import type { Snapshot, SnapshotField } from '../types/index.js';
import { applySnapshot } from './apply-snapshot.js';
import * as getSchema from './get-schema.js';
class Client_PG extends MockClient {}
describe('applySnapshot', () => {
let db: MockedFunction<Knex>;
let tracker: Tracker;
const mutationOptions = {
autoPurgeSystemCache: false,
bypassEmitAction: expect.any(Function),
bypassLimits: true,
};
beforeEach(() => {
db = vi.mocked(knex.default({ client: Client_PG }));
tracker = createTracker(db);
});
afterEach(() => {
tracker.reset();
vi.clearAllMocks();
});
describe('Creating new collection(s)', () => {
it('Creates new top-level collection(s)', async () => {
const expected = {
collection: 'test_table_2',
meta: {
accountability: 'all',
collection: 'test_table_2',
group: null,
hidden: true,
icon: 'import_export',
item_duplication_fields: null,
note: null,
singleton: false,
translations: {},
},
schema: { name: 'test_table_2' },
fields: [
{
collection: 'test_table_2',
field: 'id',
meta: {
collection: 'test_table_2',
conditions: null,
display: null,
display_options: null,
field: 'id',
group: null,
hidden: true,
interface: null,
note: null,
options: null,
readonly: false,
required: false,
sort: null,
special: null,
translations: {},
validation: null,
validation_message: null,
width: 'full',
},
schema: {
data_type: 'uuid',
default_value: null,
foreign_key_column: null,
foreign_key_table: null,
generation_expression: null,
has_auto_increment: false,
is_generated: false,
is_nullable: false,
is_primary_key: true,
is_unique: true,
max_length: null,
name: 'id',
numeric_precision: null,
numeric_scale: null,
table: 'test_table_2',
},
type: 'uuid',
},
],
};
// Stop call to db later on in apply-snapshot
vi.spyOn(getSchema, 'getSchema').mockReturnValue(Promise.resolve(snapshotApplyTestSchema));
// We are not actually testing that createOne works, just that is is called correctly
const createOneCollectionSpy = vi.spyOn(CollectionsService.prototype, 'createOne').mockResolvedValue('test');
const createFieldSpy = vi.spyOn(FieldsService.prototype, 'createField').mockResolvedValue();
await applySnapshot(snapshotCreateCollectionNotNested, {
database: db,
current: snapshotBeforeCreateCollection,
schema: snapshotApplyTestSchema,
});
expect(createOneCollectionSpy).toHaveBeenCalledTimes(1);
expect(createOneCollectionSpy).toHaveBeenCalledWith(expected, mutationOptions);
// There should be no fields left to create
// they will get filtered in createCollections
expect(createFieldSpy).toHaveBeenCalledTimes(0);
});
it('Creates the highest-level nested collection(s) with existing parents and any children', async () => {
const expected = {
collection: 'test_table_2',
meta: {
accountability: 'all',
collection: 'test_table_2',
group: 'test_table',
hidden: true,
icon: 'import_export',
item_duplication_fields: null,
note: null,
singleton: false,
translations: {},
},
schema: { name: 'test_table_2' },
fields: [
{
collection: 'test_table_2',
field: 'id',
meta: {
collection: 'test_table_2',
conditions: null,
display: null,
display_options: null,
field: 'id',
group: null,
hidden: true,
interface: null,
note: null,
options: null,
readonly: false,
required: false,
sort: null,
special: null,
translations: {},
validation: null,
validation_message: null,
width: 'full',
},
schema: {
data_type: 'uuid',
default_value: null,
foreign_key_column: null,
foreign_key_table: null,
generation_expression: null,
has_auto_increment: false,
is_generated: false,
is_nullable: false,
is_primary_key: true,
is_unique: true,
max_length: null,
name: 'id',
numeric_precision: null,
numeric_scale: null,
table: 'test_table_2',
},
type: 'uuid',
},
],
};
const expected2 = {
collection: 'test_table_3',
fields: [
{
collection: 'test_table_3',
field: 'id',
meta: {
collection: 'test_table_3',
conditions: null,
display: null,
display_options: null,
field: 'id',
group: null,
hidden: true,
interface: null,
note: null,
options: null,
readonly: false,
required: false,
sort: null,
special: null,
translations: {},
validation: null,
validation_message: null,
width: 'full',
},
schema: {
data_type: 'uuid',
default_value: null,
foreign_key_column: null,
foreign_key_table: null,
generation_expression: null,
has_auto_increment: false,
is_generated: false,
is_nullable: false,
is_primary_key: true,
is_unique: true,
max_length: null,
name: 'id',
numeric_precision: null,
numeric_scale: null,
table: 'test_table_3',
},
type: 'uuid',
},
],
meta: {
accountability: 'all',
collection: 'test_table_3',
group: 'test_table_2',
hidden: true,
icon: 'import_export',
item_duplication_fields: null,
note: null,
singleton: false,
translations: {},
},
schema: { name: 'test_table_3' },
};
// Stop call to db later on in apply-snapshot
vi.spyOn(getSchema, 'getSchema').mockReturnValue(Promise.resolve(snapshotApplyTestSchema));
// We are not actually testing that createOne works, just that is is called correctly
const createOneCollectionSpy = vi.spyOn(CollectionsService.prototype, 'createOne').mockResolvedValue('test');
const createFieldSpy = vi.spyOn(FieldsService.prototype, 'createField').mockResolvedValue();
await applySnapshot(snapshotCreateCollection, {
database: db,
current: snapshotBeforeCreateCollection,
schema: snapshotApplyTestSchema,
});
expect(createOneCollectionSpy).toHaveBeenCalledTimes(2);
expect(createOneCollectionSpy).toHaveBeenCalledWith(expected, mutationOptions);
expect(createOneCollectionSpy).toHaveBeenCalledWith(expected2, mutationOptions);
// There should be no fields left to create
// they will get filtered in createCollections
expect(createFieldSpy).toHaveBeenCalledTimes(0);
});
});
describe('Creating new collection with UUID primary key field', () => {
const fieldSchemaMaxLength = 36;
it.each(['char', 'varchar'])(
'casts non-postgres schema snapshots of UUID fields as %s(36) to UUID type',
async (fieldSchemaDataType) => {
const snapshotToApply: Snapshot = {
version: 1,
directus: '0.0.0',
collections: [
{
collection: 'test_uuid_table',
meta: {
accountability: 'all',
collection: 'test_uuid_table',
group: null,
hidden: true,
icon: 'box',
item_duplication_fields: null,
note: null,
singleton: false,
translations: {},
},
schema: { name: 'test_uuid_table' },
},
],
fields: [
{
collection: 'test_uuid_table',
field: 'id',
meta: {
collection: 'test_uuid_table',
conditions: null,
display: null,
display_options: null,
field: 'id',
group: null,
hidden: true,
interface: null,
note: null,
options: null,
readonly: false,
required: false,
sort: null,
special: null,
translations: {},
validation: null,
validation_message: null,
width: 'full',
},
schema: {
comment: null,
data_type: fieldSchemaDataType,
default_value: null,
foreign_key_column: null,
foreign_key_schema: null,
foreign_key_table: null,
generation_expression: null,
has_auto_increment: false,
is_generated: false,
is_nullable: false,
is_primary_key: true,
is_unique: true,
max_length: fieldSchemaMaxLength,
name: 'id',
numeric_precision: null,
numeric_scale: null,
table: 'test_uuid_table',
},
type: 'uuid',
} as SnapshotField,
],
relations: [],
};
const expected = {
collection: 'test_uuid_table',
meta: {
accountability: 'all',
collection: 'test_uuid_table',
group: null,
hidden: true,
icon: 'box',
item_duplication_fields: null,
note: null,
singleton: false,
translations: {},
},
schema: { name: 'test_uuid_table' },
fields: [
{
collection: 'test_uuid_table',
field: 'id',
meta: {
collection: 'test_uuid_table',
conditions: null,
display: null,
display_options: null,
field: 'id',
group: null,
hidden: true,
interface: null,
note: null,
options: null,
readonly: false,
required: false,
sort: null,
special: null,
translations: {},
validation: null,
validation_message: null,
width: 'full',
},
schema: {
data_type: 'uuid',
default_value: null,
foreign_key_column: null,
foreign_key_table: null,
generation_expression: null,
has_auto_increment: false,
is_generated: false,
is_nullable: false,
is_primary_key: true,
is_unique: true,
max_length: null,
name: 'id',
numeric_precision: null,
numeric_scale: null,
table: 'test_uuid_table',
},
type: 'uuid',
},
],
};
// Stop call to db later on in apply-snapshot
vi.spyOn(getSchema, 'getSchema').mockReturnValue(Promise.resolve(snapshotApplyTestSchema));
// We are not actually testing that createOne works, just that is is called with the right data type
const createOneCollectionSpy = vi.spyOn(CollectionsService.prototype, 'createOne').mockResolvedValue('test');
vi.spyOn(FieldsService.prototype, 'createField').mockResolvedValue();
await applySnapshot(snapshotToApply, {
database: db,
current: {
version: 1,
directus: '0.0.0',
collections: [],
fields: [],
relations: [],
},
schema: snapshotApplyTestSchema,
});
expect(createOneCollectionSpy).toHaveBeenCalledOnce();
expect(createOneCollectionSpy).toHaveBeenCalledWith(expected, mutationOptions);
}
);
});
describe('Delete collections', () => {
it('Deletes interrelated collections', async () => {
const snapshotToApply: Snapshot = {
version: 1,
directus: '0.0.0',
collections: [],
fields: [],
relations: [],
};
// Stop call to db later on in apply-snapshot
vi.spyOn(getSchema, 'getSchema').mockReturnValue(Promise.resolve(snapshotApplyTestSchema));
// We are not actually testing that deleteOne works, just that is is called correctly
const deleteOneCollectionSpy = vi.spyOn(CollectionsService.prototype, 'deleteOne').mockResolvedValue('test');
await applySnapshot(snapshotToApply, {
database: db,
current: snapshotBeforeDeleteCollection,
schema: snapshotApplyTestSchema,
});
expect(deleteOneCollectionSpy).toHaveBeenCalledTimes(3);
});
});
});