UNPKG

@finos/legend-studio

Version:
142 lines 5.97 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ExternalFormatData, RelationalCSVData, ConnectionTestData, EngineRuntime, RuntimePointer, EqualToJson, DEFAULT_TEST_ASSERTION_PREFIX, RelationalCSVDataTable, } from '@finos/legend-graph'; import { assertTrue, ContentType, generateEnumerableNameFromToken, guaranteeNonEmptyString, isNonNullable, returnUndefOnError, } from '@finos/legend-shared'; import { EmbeddedDataType } from '../../editor-state/ExternalFormatState.js'; import { createMockDataForMappingElementSource } from '../MockDataUtil.js'; export const createBareExternalFormat = (contentType) => { const data = new ExternalFormatData(); data.contentType = contentType ?? ContentType.APPLICATION_JSON; data.data = ''; return data; }; export const getAllIdentifiedConnectionsFromRuntime = (runtime) => { const resolvedRuntimes = []; if (runtime instanceof RuntimePointer) { const engineRuntime = runtime.packageableRuntime.value.runtimeValue; resolvedRuntimes.push(engineRuntime); } else if (runtime instanceof EngineRuntime) { resolvedRuntimes.push(runtime); } return resolvedRuntimes .flatMap((e) => e.connections.map((l) => l.storeConnections)) .flat(); }; // NOTE: this will all move to `engine` once engine support generating test data for all connections // Throws if unable to generate test data export class TEMPORARY_EmbeddedDataConnectionVisitor { editorStore; constructor(editorStore) { this.editorStore = editorStore; } visit_Connection(connection) { throw new Error('Method not implemented.'); } visit_ConnectionPointer(connection) { const packageableConnection = connection.packageableConnection.value.connectionValue; return packageableConnection.accept_ConnectionVisitor(this); } visit_ModelChainConnection(connection) { throw new Error('Method not implemented.'); } visit_JsonModelConnection(connection) { const _class = connection.class.value; const _json = createMockDataForMappingElementSource(_class, this.editorStore); const data = createBareExternalFormat(); data.data = _json; return data; } visit_XmlModelConnection(connection) { throw new Error('Method not implemented.'); } visit_FlatDataConnection(connection) { throw new Error('Method not implemented.'); } visit_RelationalDatabaseConnection(connection) { throw new Error('Method not implemented.'); } } export class EmbeddedDataConnectionTypeVisitor { editorStore; constructor(editorStore) { this.editorStore = editorStore; } visit_Connection(connection) { return EmbeddedDataType.EXTERNAL_FORMAT_DATA; } visit_ConnectionPointer(connection) { const packageableConnection = connection.packageableConnection.value.connectionValue; return packageableConnection.accept_ConnectionVisitor(this); } visit_ModelChainConnection(connection) { return EmbeddedDataType.EXTERNAL_FORMAT_DATA; } visit_JsonModelConnection(connection) { return EmbeddedDataType.EXTERNAL_FORMAT_DATA; } visit_XmlModelConnection(connection) { return EmbeddedDataType.EXTERNAL_FORMAT_DATA; } visit_FlatDataConnection(connection) { return EmbeddedDataType.EXTERNAL_FORMAT_DATA; } visit_RelationalDatabaseConnection(connection) { return EmbeddedDataType.RELATIONAL_CSV; } } export const initializeConnectionDataFromRuntime = (runtime, editorStore) => { const identifiedConnections = getAllIdentifiedConnectionsFromRuntime(runtime); return identifiedConnections .map((identifiedConnection) => { const connection = identifiedConnection.connection; const embeddedData = returnUndefOnError(() => connection.accept_ConnectionVisitor(new TEMPORARY_EmbeddedDataConnectionVisitor(editorStore))); if (embeddedData) { const connectionTestData = new ConnectionTestData(); connectionTestData.connectionId = identifiedConnection.id; connectionTestData.testData = embeddedData; return connectionTestData; } return undefined; }) .filter(isNonNullable); }; export const createEmptyEqualToJsonAssertion = (test) => { const assert = new EqualToJson(); assert.id = generateEnumerableNameFromToken(test.assertions.map((a) => a.id), DEFAULT_TEST_ASSERTION_PREFIX); assert.expected = createBareExternalFormat(); assert.parentTest = test; return assert; }; export const createRelationalDataFromCSV = (val) => { const data = new RelationalCSVData(); const separator = '\n-----\n'; const lineBreak = /\r?\n/; const tables = val .split(separator) .filter((e) => !(e === '\n' || e === '\r' || e === '')); tables.forEach((tableData) => { const tableInfo = tableData.split(lineBreak); assertTrue(tableInfo.length >= 2, 'Table and Schema Name required from test data'); const table = new RelationalCSVDataTable(); table.schema = guaranteeNonEmptyString(tableInfo.shift()); table.table = guaranteeNonEmptyString(tableInfo.shift()); table.values = tableInfo.join('\n'); data.tables.push(table); }); return data; }; //# sourceMappingURL=TestableUtils.js.map