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 75bd973ae0
commit db29bcde57

View File

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