Bug fixes for Script Unit Test user interface -- don't bug out on invalid json.

This commit is contained in:
Dan 2022-12-16 15:43:49 -05:00
parent 55bac8eb27
commit e78f2471df
1 changed files with 43 additions and 28 deletions

View File

@ -70,10 +70,10 @@ export default function ProcessModelEditDiagram() {
interface ScriptUnitTestResult {
result: boolean;
context: object;
error: string;
line_number: number;
offset: number;
context?: object;
error?: string;
line_number?: number;
offset?: number;
}
const [currentScriptUnitTest, setCurrentScriptUnitTest] =
@ -468,6 +468,16 @@ export default function ProcessModelEditDiagram() {
const runCurrentUnitTest = () => {
if (currentScriptUnitTest && scriptElement) {
let inputJson = '';
let expectedJson = '';
try {
inputJson = JSON.parse(currentScriptUnitTest.inputJson.value);
expectedJson = JSON.parse(currentScriptUnitTest.expectedOutputJson.value);
} catch (e) {
setScriptUnitTestResult({ result:false, error:"The JSON provided contains a formatting error."})
return;
}
resetUnitTextResult();
HttpService.makeCallToBackend({
path: `/process-models/${modifiedProcessModelId}/script-unit-tests/run`,
@ -476,10 +486,8 @@ export default function ProcessModelEditDiagram() {
postBody: {
bpmn_task_identifier: (scriptElement as any).id,
python_script: scriptText,
input_json: JSON.parse(currentScriptUnitTest.inputJson.value),
expected_output_json: JSON.parse(
currentScriptUnitTest.expectedOutputJson.value
),
input_json: inputJson,
expected_output_json: expectedJson
},
});
}
@ -488,19 +496,20 @@ export default function ProcessModelEditDiagram() {
const unitTestFailureElement = () => {
if (
scriptUnitTestResult &&
scriptUnitTestResult.result === false &&
!scriptUnitTestResult.line_number
scriptUnitTestResult.result === false
) {
let errorStringElement = null;
if (scriptUnitTestResult.error) {
errorStringElement = (
<span>
Received error when running script:{' '}
{JSON.stringify(scriptUnitTestResult.error)}
</span>
);
let errorMessage = '';
if (scriptUnitTestResult.context) {
errorMessage = 'Unexpected result. Please see the comparison below.';
} else if (scriptUnitTestResult.line_number) {
errorMessage = `Error encountered running the script. Please check the code around line ${ scriptUnitTestResult.line_number}`
} else {
errorMessage = `Error encountered running the script. ${JSON.stringify(scriptUnitTestResult.error)}`
}
let errorStringElement = <span>{ errorMessage }</span>;
let errorContextElement = null;
if (scriptUnitTestResult.context) {
errorStringElement = (
<span>Unexpected result. Please see the comparison below.</span>
@ -571,16 +580,22 @@ export default function ProcessModelEditDiagram() {
</Col>
);
}
const inputJson = JSON.stringify(
JSON.parse(currentScriptUnitTest.inputJson.value),
null,
' '
);
const outputJson = JSON.stringify(
JSON.parse(currentScriptUnitTest.expectedOutputJson.value),
null,
' '
);
let inputJson = currentScriptUnitTest.inputJson.value;
let outputJson = currentScriptUnitTest.expectedOutputJson.value;
try {
inputJson = JSON.stringify(
JSON.parse(currentScriptUnitTest.inputJson.value),
null,
' '
);
outputJson = JSON.stringify(
JSON.parse(currentScriptUnitTest.expectedOutputJson.value),
null,
' '
);
} catch (e) {
// Attemping to format the json failed -- it's invalid.
}
return (
<main>