UNPKG

@finos/legend-studio

Version:
230 lines 8.8 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 { AssertFail, TestPassed, TestError, TestFailed, EqualToJson, ExternalFormatData, EqualToJsonAssertFail, MultiExecutionServiceTestResult, AssertPass, } from '@finos/legend-graph'; import { ActionState, assertErrorThrown, ContentType, } from '@finos/legend-shared'; import { action, flow, makeObservable, observable } from 'mobx'; import { externalFormatData_setData } from '../../../graphModifier/DSLData_GraphModifierHelper.js'; import { getTestableResultFromAssertionStatus, TESTABLE_RESULT, } from '../../../sidebar-state/testable/GlobalTestRunnerState.js'; export var TEST_ASSERTION_TAB; (function (TEST_ASSERTION_TAB) { TEST_ASSERTION_TAB["ASSERTION_SETUP"] = "ASSERTION_SETUP"; TEST_ASSERTION_TAB["ASSERTION_RESULT"] = "ASSERTION_RESULT"; })(TEST_ASSERTION_TAB = TEST_ASSERTION_TAB || (TEST_ASSERTION_TAB = {})); export class TestAssertionStatusState { resultState; status; constructor(resultState, status) { this.resultState = resultState; this.status = status; } } export class AssertFailState extends TestAssertionStatusState { constructor(resultState, status) { super(resultState, status); this.status = status; makeObservable(this, { status: observable, }); } } export class EqualToJsonAssertFailState extends AssertFailState { diffModal = false; constructor(resultState, status) { super(resultState, status); this.status = status; makeObservable(this, { diffModal: observable, setDiffModal: action, }); } setDiffModal(val) { this.diffModal = val; } } export class UnsupportedAssertionStatusState extends TestAssertionStatusState { } export class TestAssertionResultState { testResult; statusState; editorStore; assertionState; constructor(editorStore, assertionState) { makeObservable(this, { testResult: observable, setTestResult: action, statusState: observable, }); this.editorStore = editorStore; this.assertionState = assertionState; } setTestResult(val) { this.testResult = val; this.statusState = undefined; if (val instanceof TestFailed) { const status = val.assertStatuses.find((_status) => _status.assertion === this.assertionState.assertion); this.statusState = this.buildStatus(status); } else if (val instanceof MultiExecutionServiceTestResult) { const statusMap = new Map(); Array.from(val.keyIndexedTestResults.entries()).forEach((keyedResult) => { const resultState = new TestAssertionResultState(this.editorStore, this.assertionState); resultState.setTestResult(keyedResult[1]); statusMap.set(keyedResult[0], resultState); }); this.statusState = statusMap; } } buildStatus(val) { if (val) { if (val instanceof EqualToJsonAssertFail) { return new EqualToJsonAssertFailState(this, val); } if (val instanceof AssertFail) { return new AssertFailState(this, val); } return new UnsupportedAssertionStatusState(this, val); } return undefined; } get result() { if (this.assertionState.testState.runningTestAction.isInProgress) { return TESTABLE_RESULT.IN_PROGRESS; } if (this.testResult instanceof TestError) { return TESTABLE_RESULT.ERROR; } else if (this.testResult instanceof TestPassed) { return TESTABLE_RESULT.PASSED; } else if (this.testResult instanceof TestFailed && this.statusState instanceof TestAssertionStatusState) { return getTestableResultFromAssertionStatus(this.statusState.status); } else if (this.testResult instanceof MultiExecutionServiceTestResult) { const passed = Array.from(this.testResult.keyIndexedTestResults.entries()).every((keyResult) => { const result = keyResult[1]; if (result instanceof TestPassed) { return true; } if (result instanceof TestFailed) { const status = result.assertStatuses.find((_status) => _status.assertion === this.assertionState.assertion); if (status instanceof AssertPass) { return true; } } return false; }); if (passed) { return TESTABLE_RESULT.PASSED; } const assertionErrors = Array.from(this.testResult.keyIndexedTestResults.values()).find((t) => t instanceof TestError); if (assertionErrors) { return TESTABLE_RESULT.ERROR; } return TESTABLE_RESULT.FAILED; } return TESTABLE_RESULT.DID_NOT_RUN; } } export class TestAssertionState { editorStore; assertion; result; constructor(editorStore, assertionState) { this.editorStore = editorStore; this.assertion = assertionState.assertion; this.result = new TestAssertionResultState(editorStore, assertionState); } } export class EqualToJsonAssertionState extends TestAssertionState { setExpectedValue(val) { externalFormatData_setData(this.assertion.expected, val); } generateExpected(status) { if (status instanceof EqualToJsonAssertFail) { const expected = status.actual; this.setExpectedValue(expected); } } generateBare() { const bareAssertion = new EqualToJson(); bareAssertion.expected = new ExternalFormatData(); bareAssertion.expected.contentType = ContentType.APPLICATION_JSON; bareAssertion.expected.data = ''; return bareAssertion; } label() { return 'EqualToJSON'; } } export class UnsupportedAssertionState extends TestAssertionState { generateBare() { throw new Error('Method not implemented.'); } generateExpected(status) { return; } label() { return 'Unsupported'; } } export class TestAssertionEditorState { editorStore; testState; assertionState; assertionResultState; assertion; selectedTab = TEST_ASSERTION_TAB.ASSERTION_SETUP; generatingExpectedAction = ActionState.create(); constructor(editorStore, assertion, testState) { makeObservable(this, { selectedTab: observable, assertionResultState: observable, setSelectedTab: action, generateExpected: flow, }); this.editorStore = editorStore; this.assertion = assertion; this.testState = testState; this.assertionState = this.buildAssertionState(assertion); this.assertionResultState = new TestAssertionResultState(editorStore, this); } setSelectedTab(val) { this.selectedTab = val; } *generateExpected() { try { this.generatingExpectedAction.inProgress(); const bare = this.assertionState.generateBare(); bare.parentTest = this.assertion.parentTest; const status = (yield this.editorStore.graphManagerState.graphManager.generateExpectedResult(this.testState.testable, this.testState.test, bare, this.editorStore.graphManagerState.graph)); this.assertionState.generateExpected(status); this.generatingExpectedAction.complete(); } catch (error) { assertErrorThrown(error); this.editorStore.applicationStore.notifyError(`Error generating expected result: ${error.message}`); this.generatingExpectedAction.fail(); } } buildAssertionState(assertion) { if (assertion instanceof EqualToJson) { return new EqualToJsonAssertionState(this.editorStore, this); } return new UnsupportedAssertionState(this.editorStore, this); } } //# sourceMappingURL=TestAssertionState.js.map