UNPKG

@dossierhq/integration-test

Version:

Integration test to ensure that different Dossier database adapters work as expected.

56 lines 2.81 kB
/// <reference types="./InvalidEntityUtils.d.ts" /> import { copyEntity, notOk, ok, } from '@dossierhq/core'; import { ChangeValidationsComponentWithoutValidationsUpdate, ChangeValidationsWithoutValidationsUpdate, IntegrationTestSchema, } from '../IntegrationTestSchema.js'; import { CHANGE_VALIDATIONS_CREATE, VALUE_ITEMS_CREATE } from './Fixtures.js'; import { withSchemaAdvisoryLock } from './SchemaTestUtils.js'; export async function createInvalidEntity(client, fields, options) { return doCreateInvalidEntity(client, ChangeValidationsWithoutValidationsUpdate, copyEntity(CHANGE_VALIDATIONS_CREATE, { fields }), options); } export async function createEntityWithInvalidComponent(client, options) { return doCreateInvalidEntity(client, ChangeValidationsComponentWithoutValidationsUpdate, copyEntity(VALUE_ITEMS_CREATE, { fields: { any: { type: 'ChangeValidationsComponent', matchPattern: 'no match' } }, }), options); } async function doCreateInvalidEntity(client, schemaUpdate, entity, options) { let result = notOk.Generic('not set'); const schemaResult = await withTemporarySchemaChange(client, schemaUpdate, async () => { const createResult = await client.createEntity(entity, { publish: options?.publish, }); result = createResult.isError() ? createResult : ok({ entity: createResult.value.entity, validations: [] }); return createResult.isOk() ? { id: createResult.value.entity.id } : undefined; }, (processed) => { if (result.isOk() && result.value.entity.id === processed.id) { result.value.validations.push(processed); } }); if (schemaResult.isError()) return schemaResult; return result; } async function withTemporarySchemaChange(client, schemaUpdate, onChangedSchema, onProcessed) { return await withSchemaAdvisoryLock(client, async () => { // remove validations from the schema const removeValidationsResult = await client.updateSchemaSpecification(schemaUpdate); if (removeValidationsResult.isError()) return removeValidationsResult; const filter = await onChangedSchema(); // restore validations to the schema const restoreSchemaResult = await client.updateSchemaSpecification(IntegrationTestSchema); if (restoreSchemaResult.isError()) return restoreSchemaResult; // process dirty if (filter) { const processResult = await client.processDirtyEntity(filter); if (processResult.isError()) return processResult; if (processResult.value) { onProcessed(processResult.value); } } return ok(undefined); }); } //# sourceMappingURL=InvalidEntityUtils.js.map