From 18d40010f78b931103e1f06089657929a1c93447 Mon Sep 17 00:00:00 2001 From: Dayoung Lee Date: Fri, 4 Nov 2022 13:07:21 +0900 Subject: [PATCH 1/4] This commit clarifies the OneExplorer unit tests. (1) Elaborate `create a base model` test. (2) Add isDefined checker to easily notice undefined objects (3) Replace `?` with `!` to assert unexpected undefined objects ONE-vscode-DCO-1.0-Signed-off-by: Dayoung Lee --- src/Tests/OneExplorer/OneExplorer.test.ts | 11 +++++------ src/Tests/OneExplorer/OneStorage.test.ts | 13 +++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Tests/OneExplorer/OneExplorer.test.ts b/src/Tests/OneExplorer/OneExplorer.test.ts index bdbaa089..fdf1697c 100644 --- a/src/Tests/OneExplorer/OneExplorer.test.ts +++ b/src/Tests/OneExplorer/OneExplorer.test.ts @@ -77,23 +77,22 @@ suite('OneExplorer', function() { }); }); - test('create a base model node with cfg', function() { + test('create a base model node outside workspace', function() { const baseModelName = 'test.tflite'; const configName = `test.cfg`; - // Write a file inside temp directory - // and get file paths inside the temp directory testBuilder.writeFileSync(baseModelName, ''); - const baseModelPath = testBuilder.getPath(baseModelName); + const baseModelPath = testBuilder.getPath(baseModelName); // in /tmp + const configPath = testBuilder.getPath(configName, 'workspace'); // in workspace + // Find a base model outside workspace by locating its absolute path testBuilder.writeFileSync( configName, ` [one-import-tflite] -input_file=${baseModelPath} +input_path=${baseModelPath} `, 'workspace'); - const configPath = testBuilder.getPath(configName, 'workspace'); // Validation { diff --git a/src/Tests/OneExplorer/OneStorage.test.ts b/src/Tests/OneExplorer/OneStorage.test.ts index 7e8668ba..eb7bd423 100644 --- a/src/Tests/OneExplorer/OneStorage.test.ts +++ b/src/Tests/OneExplorer/OneStorage.test.ts @@ -51,12 +51,13 @@ input_path=${modelName} const configPath = testBuilder.getPath(configName, 'workspace'); const modelPath = testBuilder.getPath(modelName, 'workspace'); - // Validation - { - assert.strictEqual(OneStorage.getCfgs(modelPath)?.length, 1); - assert.strictEqual(OneStorage.getCfgs(modelPath)![0], configPath); - } - }); + // Validation + { + assert.isDefined(OneStorage.getCfgs(modelPath)); + assert.strictEqual(OneStorage.getCfgs(modelPath)?.length, 1); + assert.strictEqual(OneStorage.getCfgs(modelPath)![0], configPath); + } + }); test('NEG: Returns undefined for not existing path', function() { { assert.isUndefined(OneStorage.getCfgs('invalid/path')); } From a425c45f566bff7acf3bd00f5eaa09b1d6bf677b Mon Sep 17 00:00:00 2001 From: Dayoung Lee Date: Fri, 4 Nov 2022 13:15:13 +0900 Subject: [PATCH 2/4] Add isDefined --- src/Tests/OneExplorer/ConfigObject.test.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Tests/OneExplorer/ConfigObject.test.ts b/src/Tests/OneExplorer/ConfigObject.test.ts index 1f5ac110..80fe992a 100644 --- a/src/Tests/OneExplorer/ConfigObject.test.ts +++ b/src/Tests/OneExplorer/ConfigObject.test.ts @@ -99,6 +99,7 @@ input_path=${modelName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.strictEqual(configObj?.getProducts.length, 0); @@ -130,6 +131,7 @@ output_path=${productName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.strictEqual(configObj?.getProducts.length, 0); } @@ -158,6 +160,7 @@ input_path=${modelName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.strictEqual(configObj?.getProducts.length, 0); @@ -191,6 +194,7 @@ output_path=${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.strictEqual(configObj?.getProducts.length, 0); } @@ -226,6 +230,7 @@ output_path=${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -260,6 +265,7 @@ output_path=${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.strictEqual(configObj?.getProducts.length, 0); } @@ -297,6 +303,7 @@ output_path=${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -342,6 +349,7 @@ output_path=dummy/dummy/../..//${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -384,6 +392,7 @@ output_path=/dummy/dummy/../..//${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -424,6 +433,7 @@ output_path=/${productPath2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -468,6 +478,7 @@ output_path=/${productPath2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -509,6 +520,7 @@ output_path=/${productPath2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 1); assert.strictEqual(configObj?.getBaseModelsExists.length, 0); @@ -542,6 +554,7 @@ output_path=${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -574,6 +587,7 @@ output_path=${productName2} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.strictEqual(configObj?.getProducts.length, 0); } @@ -602,6 +616,7 @@ command=${productName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -631,6 +646,7 @@ command=${productName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.strictEqual(configObj?.getProducts.length, 0); } @@ -666,6 +682,7 @@ command=--save-temps --save-allocations ${productName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -704,6 +721,7 @@ command=--save-chrome-trace ${traceName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.notStrictEqual(configObj?.getProducts.length, 0); @@ -730,6 +748,7 @@ command=--save-chrome-trace ${traceName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.strictEqual(configObj?.getProducts.length, 0); } @@ -758,6 +777,7 @@ commands=--save-chrome-trace ${traceName} // Validation { + assert.isDefined(configObj); assert.strictEqual(configObj?.getBaseModels.length, 0); assert.strictEqual(configObj?.getProducts.length, 0); } From c087ac0dec6eef185ee5bf7adc7b0e3effb620c9 Mon Sep 17 00:00:00 2001 From: Dayoung Lee Date: Fri, 4 Nov 2022 13:44:15 +0900 Subject: [PATCH 3/4] Replace `?` --- src/Tests/OneExplorer/ConfigObject.test.ts | 175 +++++++++------------ src/Tests/OneExplorer/OneExplorer.test.ts | 38 ++--- src/Tests/OneExplorer/OneStorage.test.ts | 17 +- 3 files changed, 106 insertions(+), 124 deletions(-) diff --git a/src/Tests/OneExplorer/ConfigObject.test.ts b/src/Tests/OneExplorer/ConfigObject.test.ts index 80fe992a..5486d8b5 100644 --- a/src/Tests/OneExplorer/ConfigObject.test.ts +++ b/src/Tests/OneExplorer/ConfigObject.test.ts @@ -65,7 +65,7 @@ suite('OneExplorer', function() { assert.isObject(configObj!.rawObj); assert.isObject(configObj!.obj); assert.isArray(configObj!.obj.baseModels); - assert.isArray(configObj?.getProducts); + assert.isArray(configObj!.getProducts); assert.isArray(configObj!.getBaseModels); assert.isArray(configObj!.getProducts); assert.isArray(configObj!.getBaseModelsExists); @@ -100,14 +100,14 @@ input_path=${modelName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.strictEqual(configObj!.getProducts.length, 0); - assert.isTrue(configObj?.isChildOf(modelPath)); + assert.isTrue(configObj!.isChildOf(modelPath)); assert.isTrue( - configObj?.getBaseModels.map(baseModel => baseModel.path).includes(modelPath)); + configObj!.getBaseModels.map(baseModel => baseModel.path).includes(modelPath)); assert.isTrue( - configObj?.getBaseModelsExists.map(baseModel => baseModel.path).includes(modelPath)); + configObj!.getBaseModelsExists.map(baseModel => baseModel.path).includes(modelPath)); } }); @@ -132,8 +132,8 @@ output_path=${productName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.strictEqual(configObj!.getProducts.length, 0); } }); }); @@ -161,14 +161,14 @@ input_path=${modelName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.strictEqual(configObj!.getProducts.length, 0); - assert.isTrue(configObj?.isChildOf(modelPath)); + assert.isTrue(configObj!.isChildOf(modelPath)); assert.isTrue( - configObj?.getBaseModels.map(baseModel => baseModel.path).includes(modelPath)); + configObj!.getBaseModels.map(baseModel => baseModel.path).includes(modelPath)); assert.isTrue( - configObj?.getBaseModelsExists.map(baseModel => baseModel.path).includes(modelPath)); + configObj!.getBaseModelsExists.map(baseModel => baseModel.path).includes(modelPath)); } }); @@ -195,8 +195,8 @@ output_path=${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.strictEqual(configObj!.getProducts.length, 0); } }); }); @@ -231,15 +231,13 @@ output_path=${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.notStrictEqual(configObj!.getProducts.length, 0); assert.isTrue( - configObj?.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath1)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath2)); + configObj!.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath1)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath2)); } }); @@ -266,8 +264,8 @@ output_path=${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.strictEqual(configObj!.getProducts.length, 0); } }); @@ -304,19 +302,15 @@ output_path=${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.notStrictEqual(configObj!.getProducts.length, 0); assert.isTrue( - configObj?.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath1)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath2)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath3)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath4)); + configObj!.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath1)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath2)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath3)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath4)); } }); @@ -350,15 +344,13 @@ output_path=dummy/dummy/../..//${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.notStrictEqual(configObj!.getProducts.length, 0); assert.isTrue( - configObj?.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath1)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath2)); + configObj!.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath1)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath2)); } }); @@ -393,14 +385,14 @@ output_path=/dummy/dummy/../..//${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.notStrictEqual(configObj!.getProducts.length, 0); - assert.notStrictEqual(configObj?.getBaseModels[0].path, baseModelPath); + assert.notStrictEqual(configObj!.getBaseModels[0].path, baseModelPath); assert.isFalse( - configObj?.getProducts.map(product => product.path).includes(productPath1)); + configObj!.getProducts.map(product => product.path).includes(productPath1)); assert.isFalse( - configObj?.getProducts.map(product => product.path).includes(productPath2)); + configObj!.getProducts.map(product => product.path).includes(productPath2)); } }); @@ -434,15 +426,12 @@ output_path=/${productPath2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.notStrictEqual(configObj!.getProducts.length, 0); - assert.strictEqual( - configObj?.getBaseModels[0].path, baseModelPath); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath1)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath2)); + assert.strictEqual(configObj!.getBaseModels[0].path, baseModelPath); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath1)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath2)); } }); @@ -479,15 +468,15 @@ output_path=/${productPath2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.notStrictEqual(configObj!.getProducts.length, 0); assert.isTrue( - configObj?.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); + configObj!.getBaseModels.map(baseModel => baseModel.path).includes(baseModelPath)); assert.isTrue( - configObj?.getProductsExists.map(product => product.path).includes(productPath1)); + configObj!.getProductsExists.map(product => product.path).includes(productPath1)); assert.isTrue( - configObj?.getProductsExists.map(product => product.path).includes(productPath2)); + configObj!.getProductsExists.map(product => product.path).includes(productPath2)); } }); @@ -521,11 +510,11 @@ output_path=/${productPath2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 1); - assert.strictEqual(configObj?.getBaseModelsExists.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 1); + assert.strictEqual(configObj!.getBaseModelsExists.length, 0); - assert.notStrictEqual(configObj?.getProducts.length, 0); - assert.strictEqual(configObj?.getBaseModelsExists.length, 0); + assert.notStrictEqual(configObj!.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModelsExists.length, 0); } }); }); @@ -555,13 +544,12 @@ output_path=${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.notStrictEqual(configObj!.getProducts.length, 0); assert.isTrue( - configObj?.getProducts.map(baseModel => baseModel.path).includes(productPath1)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath2)); + configObj!.getProducts.map(baseModel => baseModel.path).includes(productPath1)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath2)); } }); @@ -588,8 +576,8 @@ output_path=${productName2} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.strictEqual(configObj!.getProducts.length, 0); } }); }); @@ -617,11 +605,10 @@ command=${productName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.notStrictEqual(configObj!.getProducts.length, 0); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath)); } }); @@ -647,8 +634,8 @@ command=${productName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.strictEqual(configObj!.getProducts.length, 0); } }); @@ -683,19 +670,14 @@ command=--save-temps --save-allocations ${productName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.notStrictEqual(configObj!.getProducts.length, 0); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(productPath)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(extra1Path)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(extra2Path)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(extra3Path)); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(extra4Path)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(productPath)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(extra1Path)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(extra2Path)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(extra3Path)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(extra4Path)); } }); }); @@ -722,11 +704,10 @@ command=--save-chrome-trace ${traceName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.notStrictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.notStrictEqual(configObj!.getProducts.length, 0); - assert.isTrue( - configObj?.getProducts.map(product => product.path).includes(tracePath)); + assert.isTrue(configObj!.getProducts.map(product => product.path).includes(tracePath)); } }); @@ -749,8 +730,8 @@ command=--save-chrome-trace ${traceName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.strictEqual(configObj!.getProducts.length, 0); } }); @@ -778,8 +759,8 @@ commands=--save-chrome-trace ${traceName} // Validation { assert.isDefined(configObj); - assert.strictEqual(configObj?.getBaseModels.length, 0); - assert.strictEqual(configObj?.getProducts.length, 0); + assert.strictEqual(configObj!.getBaseModels.length, 0); + assert.strictEqual(configObj!.getProducts.length, 0); } }); }); diff --git a/src/Tests/OneExplorer/OneExplorer.test.ts b/src/Tests/OneExplorer/OneExplorer.test.ts index fdf1697c..7ed59587 100644 --- a/src/Tests/OneExplorer/OneExplorer.test.ts +++ b/src/Tests/OneExplorer/OneExplorer.test.ts @@ -66,8 +66,8 @@ suite('OneExplorer', function() { // Validation { const dirNode = NodeFactory.create(NodeType.directory, dirPath, undefined); - assert.strictEqual(dirNode!.getChildren().length, 1); - assert.strictEqual(dirNode!.getChildren()[0].path, baseModelPath); + assert.strictEqual(dirNode.getChildren().length, 1); + assert.strictEqual(dirNode.getChildren()[0].path, baseModelPath); } }); @@ -98,13 +98,13 @@ input_path=${baseModelPath} { const baseModelNode = NodeFactory.create(NodeType.baseModel, baseModelPath, undefined); - assert.strictEqual(baseModelNode!.openViewType, BaseModelNode.defaultOpenViewType); - assert.strictEqual(baseModelNode!.icon, BaseModelNode.defaultIcon); - assert.strictEqual(baseModelNode!.canHide, BaseModelNode.defaultCanHide); + assert.strictEqual(baseModelNode.openViewType, BaseModelNode.defaultOpenViewType); + assert.strictEqual(baseModelNode.icon, BaseModelNode.defaultIcon); + assert.strictEqual(baseModelNode.canHide, BaseModelNode.defaultCanHide); - assert.strictEqual(baseModelNode!.getChildren().length, 1); - assert.strictEqual(baseModelNode!.getChildren()[0].type, NodeType.config); - assert.strictEqual(baseModelNode!.getChildren()[0].path, configPath); + assert.strictEqual(baseModelNode.getChildren().length, 1); + assert.strictEqual(baseModelNode.getChildren()[0].type, NodeType.config); + assert.strictEqual(baseModelNode.getChildren()[0].path, configPath); } }); @@ -126,10 +126,10 @@ input_path=${baseModelPath} // Validation { const configNode = NodeFactory.create(NodeType.config, configPath, undefined); - assert.strictEqual(configNode!.openViewType, ConfigNode.defaultOpenViewType); - assert.strictEqual(configNode!.icon, ConfigNode.defaultIcon); - assert.strictEqual(configNode!.canHide, ConfigNode.defaultCanHide); - assert.strictEqual(configNode!.getChildren().length, 0); + assert.strictEqual(configNode.openViewType, ConfigNode.defaultOpenViewType); + assert.strictEqual(configNode.icon, ConfigNode.defaultIcon); + assert.strictEqual(configNode.canHide, ConfigNode.defaultCanHide); + assert.strictEqual(configNode.getChildren().length, 0); } }); @@ -151,10 +151,10 @@ input_path=${baseModelPath} // Validation { const productNode = NodeFactory.create(NodeType.product, productPath, undefined); - assert.strictEqual(productNode!.openViewType, ProductNode.defaultOpenViewType); - assert.strictEqual(productNode!.icon, ProductNode.defaultIcon); - assert.strictEqual(productNode!.canHide, ProductNode.defaultCanHide); - assert.strictEqual(productNode!.getChildren().length, 0); + assert.strictEqual(productNode.openViewType, ProductNode.defaultOpenViewType); + assert.strictEqual(productNode.icon, ProductNode.defaultIcon); + assert.strictEqual(productNode.canHide, ProductNode.defaultCanHide); + assert.strictEqual(productNode.getChildren().length, 0); } }); @@ -173,7 +173,7 @@ input_path=${baseModelPath} const directoryNode = NodeFactory.create(NodeType.directory, directoryPath, undefined); const productNode = NodeFactory.create(NodeType.product, productPath, directoryNode); - assert.strictEqual(productNode?.parent, directoryNode); + assert.strictEqual(productNode.parent, directoryNode); }); test('NEG: get an empty parent', function() { @@ -187,7 +187,7 @@ input_path=${baseModelPath} const productNode = NodeFactory.create(NodeType.product, productPath, undefined); - assert.strictEqual(productNode?.parent, undefined); + assert.strictEqual(productNode.parent, undefined); }); }); @@ -195,7 +195,7 @@ input_path=${baseModelPath} test('constructor', function() { const directoryPath = testBuilder.getPath(''); const directoryNode = NodeFactory.create(NodeType.directory, directoryPath, undefined); - const oneNode = new OneNode(vscode.TreeItemCollapsibleState.Collapsed, directoryNode!); + const oneNode = new OneNode(vscode.TreeItemCollapsibleState.Collapsed, directoryNode); { assert.strictEqual(oneNode.contextValue, 'directory'); } }); }); diff --git a/src/Tests/OneExplorer/OneStorage.test.ts b/src/Tests/OneExplorer/OneStorage.test.ts index eb7bd423..fe324c82 100644 --- a/src/Tests/OneExplorer/OneStorage.test.ts +++ b/src/Tests/OneExplorer/OneStorage.test.ts @@ -51,13 +51,13 @@ input_path=${modelName} const configPath = testBuilder.getPath(configName, 'workspace'); const modelPath = testBuilder.getPath(modelName, 'workspace'); - // Validation - { - assert.isDefined(OneStorage.getCfgs(modelPath)); - assert.strictEqual(OneStorage.getCfgs(modelPath)?.length, 1); - assert.strictEqual(OneStorage.getCfgs(modelPath)![0], configPath); - } - }); + // Validation + { + assert.isDefined(OneStorage.getCfgs(modelPath)); + assert.strictEqual(OneStorage.getCfgs(modelPath)!.length, 1); + assert.strictEqual(OneStorage.getCfgs(modelPath)![0], configPath); + } + }); test('NEG: Returns undefined for not existing path', function() { { assert.isUndefined(OneStorage.getCfgs('invalid/path')); } @@ -102,7 +102,8 @@ input_path=${modelName} // Validation { assert.isDefined(OneStorage.getCfgObj(configPath)); - assert.strictEqual(OneStorage.getCfgObj(configPath)?.getBaseModelsExists[0].path, modelPath); + assert.strictEqual( + OneStorage.getCfgObj(configPath)!.getBaseModelsExists[0].path, modelPath); } }); From b29b63f153424304e7578d278a63db9b8f98c275 Mon Sep 17 00:00:00 2001 From: Dayoung Lee Date: Thu, 3 Nov 2022 10:51:55 +0900 Subject: [PATCH 4/4] [OneExplorer] Introduce BaseModelToCfgMap/CfgToCfgObjMap This commit refactors OneStorage class to divide its functionalities into CfgToCfgObjMap and BaseModelToCfgMap. It includes unit tests. ONE-vscode-DCO-1.0-Signed-off-by: Dayoung Lee --- src/OneExplorer/OneStorage.ts | 228 +++++++++++---- src/Tests/OneExplorer/OneStorage.test.ts | 352 ++++++++++++++++++++++- 2 files changed, 516 insertions(+), 64 deletions(-) diff --git a/src/OneExplorer/OneStorage.ts b/src/OneExplorer/OneStorage.ts index 2b37d72c..263a3f98 100644 --- a/src/OneExplorer/OneStorage.ts +++ b/src/OneExplorer/OneStorage.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import {assert} from 'chai'; import * as fs from 'fs'; import * as path from 'path'; import * as vscode from 'vscode'; @@ -24,12 +25,155 @@ import {Logger} from '../Utils/Logger'; import {ConfigObj} from './ConfigObject'; import {Node, NodeType} from './OneExplorer'; -interface StringListMap { - [key: string]: string[]; +export { + BaseModelToCfgMap as _unit_test_BaseModelToCfgMap, + CfgToCfgObjMap as _unit_test_CfgToCfgObjMap, +}; + +class CfgToCfgObjMap { + private _map: Map; + + constructor() { + this._map = new Map(); + } + + public init(cfgList: string[]) { + cfgList.forEach(cfg => { + const cfgObj = ConfigObj.createConfigObj(vscode.Uri.file(cfg)); + if (cfgObj) { + this._map.set(cfg, cfgObj); + } + }); + } + + get size() { + return this._map.size; + } + + public get(path: string) { + return this._map.get(path); + } + + public reset(type: NodeType, path: string) { + switch (type) { + case NodeType.config: + this._map.delete(path); + break; + case NodeType.baseModel: + case NodeType.product: + case NodeType.directory: + default: + assert.isOk(false, `Cannot reach here`); + break; + } + } + + /** + * Update the data when cfg file's path is changed or the content is changed. + * @param type NodeType + * @param path config path to update + * @param newpath (optional) if exists, change the file path + */ + public update(type: NodeType, path: string, newpath?: string) { + switch (type) { + case NodeType.config: { + this._map.delete(path); + + path = newpath ? ? path; + const cfgObj = ConfigObj.createConfigObj(vscode.Uri.file(path)); + if (cfgObj) { + this._map.set(path, cfgObj); + } + + break; + } + case NodeType.baseModel: + case NodeType.product: + case NodeType.directory: + default: + assert.isOk(false, `Cannot reach here`); + break; + } + } } -interface ConfigObjMap { - [key: string]: ConfigObj; +class BaseModelToCfgMap { + private _map: Map; + + constructor() { + this._map = new Map(); + } + + public init(cfgList: string[], cfgToCfgObjMap: CfgToCfgObjMap) { + cfgList.forEach(cfg => { + const cfgObj = cfgToCfgObjMap.get(cfg); + + (cfgObj ? cfgObj.getBaseModelsExists : []).forEach(artifact => { + this._map.get(artifact.path) ? + this._map.set(artifact.path, this._map.get(artifact.path)!.concat([cfg])) : + this._map.set(artifact.path, [cfg]); + }); + }); + } + + get size() { + return this._map.size; + } + + public get(path: string) { + return this._map.get(path); + } + + public reset(type: NodeType, path: string) { + switch (type) { + case NodeType.baseModel: + this._map.delete(path); + break; + case NodeType.config: + this._map.forEach((cfgs, key, map) => { + if (cfgs.includes(path)) { + cfgs = cfgs.filter(cfg => cfg !== path); + map.set(key, cfgs); + } + }); + break; + case NodeType.product: + case NodeType.directory: + default: + assert.isOk(false, `Cannot reach here`); + break; + } + } + + public update(type: NodeType, oldpath: string, newpath: string) { + switch (type) { + case NodeType.baseModel: { + const value = this._map.get(oldpath); + + if (!value) { + return; + } + + this._map.delete(oldpath); + this._map.set(newpath, value); + + break; + } + case NodeType.config: + this._map.forEach((cfgs, key, map) => { + if (cfgs.includes(oldpath)) { + cfgs = cfgs.map(cfg => (cfg === oldpath) ? newpath : cfg); + map.set(key, cfgs); + } + }); + break; + case NodeType.product: + case NodeType.directory: + default: + assert.isOk(false, `Cannot reach here`); + break; + } + } } /** @@ -62,11 +206,11 @@ export class OneStorage { /** * @brief A map of ConfigObj (key: cfg path) */ - private _cfgToCfgObjMap: ConfigObjMap; + private _cfgToCfgObjMap: CfgToCfgObjMap; /** * @brief A map of BaseModel path to Cfg path */ - private _baseModelToCfgsMap: StringListMap; + private _baseModelToCfgsMap: BaseModelToCfgMap; /** * Get the list of .cfg files within the workspace @@ -101,49 +245,17 @@ export class OneStorage { } } - private _initCfgToCfgObjMap(cfgList: string[]): ConfigObjMap { - let map: ConfigObjMap = {}; - - cfgList.forEach(cfg => { - const cfgObj = ConfigObj.createConfigObj(vscode.Uri.file(cfg)); - if (cfgObj) { - map[cfg] = cfgObj; - } - }); - - return map; - } - - private _initBaseModelToCfgsMap(cfgList: string[], cfgToCfgObjMap: ConfigObjMap): StringListMap { - let map: StringListMap = {}; - - cfgList.forEach(cfg => { - const cfgObj = cfgToCfgObjMap[cfg]; - if (cfgObj) { - cfgObj.getBaseModelsExists.forEach(baseModelArtifact => { - if (!map[baseModelArtifact.path]) { - map[baseModelArtifact.path] = []; - } - - if (!map[baseModelArtifact.path].includes(cfg)) { - map[baseModelArtifact.path].push(cfg); - } - }); - } - }); - - return map; - } - private static _delete(node: Node) { - OneStorage.get()._nodeMap.delete(node.path); + const instance = OneStorage.get(); + instance._nodeMap.delete(node.path); switch (node.type) { case NodeType.baseModel: - OneStorage.resetBaseModel(node.path); + instance._baseModelToCfgsMap.reset(node.type, node.path); break; case NodeType.config: - OneStorage.resetConfig(node.path); + instance._baseModelToCfgsMap.reset(node.type, node.path); + instance._cfgToCfgObjMap.reset(node.type, node.path); break; default: break; @@ -152,8 +264,12 @@ export class OneStorage { private constructor() { const cfgList = this._getCfgList(); - this._cfgToCfgObjMap = this._initCfgToCfgObjMap(cfgList); - this._baseModelToCfgsMap = this._initBaseModelToCfgsMap(cfgList, this._cfgToCfgObjMap); + + this._cfgToCfgObjMap = new CfgToCfgObjMap(); + this._cfgToCfgObjMap.init(cfgList); + + this._baseModelToCfgsMap = new BaseModelToCfgMap(); + this._baseModelToCfgsMap.init(cfgList, this._cfgToCfgObjMap); } private static _obj: OneStorage|undefined; @@ -162,20 +278,20 @@ export class OneStorage { * Get cfg lists which refers the base model path * @param baseModelPath * @return a list of cfg path or undefined - * 'undefined' is returned when + * An empty array is returned when * (1) the path not exists * (2) the path is not a base model file * (3) the path is a lonely base model file */ public static getCfgs(baseModelPath: string): string[]|undefined { - return OneStorage.get()._baseModelToCfgsMap[baseModelPath]; + return OneStorage.get()._baseModelToCfgsMap.get(baseModelPath); } /** * Get cfgObj from the map */ public static getCfgObj(cfgPath: string): ConfigObj|undefined { - return OneStorage.get()._cfgToCfgObjMap[cfgPath]; + return OneStorage.get()._cfgToCfgObjMap.get(cfgPath); } /** @@ -188,7 +304,7 @@ export class OneStorage { public static insert(node: Node) { // NOTE // Only _nodeMap is built by calling this function - // _baseModelToCfgsMap and _cfgToCfgObjMap are built at once by scanning the file system. + // _baseModelToCfgsMap and _cfgToCfgObjMap are built at constuctors OneStorage.get()._nodeMap.set(node.path, node); } @@ -226,18 +342,4 @@ export class OneStorage { public static reset(): void { OneStorage._obj = undefined; } - - public static resetBaseModel(path: string): void { - delete OneStorage.get()._baseModelToCfgsMap[path]; - Logger.debug('OneStorage', `Base Mode Path(${path}) is removed.`); - } - - public static resetConfig(path: string): void { - delete OneStorage.get()._cfgToCfgObjMap[path]; - Object.entries(OneStorage.get()._baseModelToCfgsMap).forEach(([modelpath]) => { - OneStorage.get()._baseModelToCfgsMap[modelpath] = - OneStorage.get()._baseModelToCfgsMap[modelpath].filter(cfg => cfg !== path); - }); - Logger.debug('OneStorage', `Config Path(${path}) is removed.`); - } } diff --git a/src/Tests/OneExplorer/OneStorage.test.ts b/src/Tests/OneExplorer/OneStorage.test.ts index fe324c82..8a3a7d7e 100644 --- a/src/Tests/OneExplorer/OneStorage.test.ts +++ b/src/Tests/OneExplorer/OneStorage.test.ts @@ -15,8 +15,10 @@ */ import {assert} from 'chai'; -import {OneStorage} from '../../OneExplorer/OneStorage'; +import {NodeType} from '../../OneExplorer/OneExplorer'; +import {OneStorage} from '../../OneExplorer/OneStorage'; +import {_unit_test_BaseModelToCfgMap as BaseModelToCfgMap, _unit_test_CfgToCfgObjMap as CfgToCfgObjMap} from '../../OneExplorer/OneStorage'; import {TestBuilder} from '../TestBuilder'; suite('OneExplorer', function() { @@ -136,5 +138,353 @@ input_path=${modelName} }); }); }); + + suite('CfgToCfgObjMap', function() { + suite('#constructor()', function() { + test('does not throw', function() { + assert.doesNotThrow(() => new CfgToCfgObjMap()); + }); + }); + + suite('#init()', function() { + test('NEG: with an empty cfglist', function() { + const cfgToCfgObjMap = new CfgToCfgObjMap(); + assert.doesNotThrow(() => { + cfgToCfgObjMap.init([]); + }); + assert.strictEqual(cfgToCfgObjMap.size, 0); + }); + test('NEG: a falsy cfg list (not existing)', function() { + const cfgToCfgObjMap = new CfgToCfgObjMap(); + assert.doesNotThrow(() => { + cfgToCfgObjMap.init(['not/existing/path']); + }); + assert.isUndefined(cfgToCfgObjMap.get('not/existing/path')); + assert.strictEqual(cfgToCfgObjMap.size, 0); + }); + }); + + suite('#get()', function() { + test('NEG: empty path', function() { + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([]); + assert.isUndefined(cfgToCfgObjMap.get('')); + assert.strictEqual(cfgToCfgObjMap.size, 0); + }); + + test('NEG: invalid path', function() { + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([]); + assert.isUndefined(cfgToCfgObjMap.get('invalid/path')); + assert.strictEqual(cfgToCfgObjMap.size, 0); + }); + + test('existing path', function() { + const configName = 'model.cfg'; + const configPath = testBuilder.getPath(configName, 'workspace'); + testBuilder.writeFileSync(configName, '', 'workspace'); + + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([configPath]); + + assert.strictEqual(cfgToCfgObjMap.size, 1); + assert.strictEqual(cfgToCfgObjMap.get(configPath)?.uri.fsPath, configPath); + }); + }); + + suite('#reset()', function() { + test('existing path', function() { + const configName = 'model.cfg'; + const configPath = testBuilder.getPath(configName, 'workspace'); + testBuilder.writeFileSync(configName, '', 'workspace'); + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([configPath]); + + assert.strictEqual(cfgToCfgObjMap.size, 1); + cfgToCfgObjMap.reset(NodeType.config, configPath); + assert.strictEqual(cfgToCfgObjMap.size, 0); + }); + test('NEG: not existing path', function() { + const configName = 'model.cfg'; + const configPath = testBuilder.getPath(configName, 'workspace'); + // commented out : testBuilder.writeFileSync(configName, '', 'workspace'); + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([configPath]); + + assert.strictEqual(cfgToCfgObjMap.size, 0); + assert.doesNotThrow(() => {cfgToCfgObjMap.reset(NodeType.config, configPath)}); + assert.strictEqual(cfgToCfgObjMap.size, 0); + }); + }); + + suite('#update()', function() { + test('existing path', function() { + const configName = 'model.cfg'; + const configPath = testBuilder.getPath(configName, 'workspace'); + testBuilder.writeFileSync(configName, '', 'workspace'); + + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([configPath]); + + const newConfigName = 'model.new.cfg'; + const newConfigPath = testBuilder.getPath(newConfigName, 'workspace'); + testBuilder.writeFileSync(newConfigName, '', 'workspace'); + + assert.strictEqual(cfgToCfgObjMap.size, 1); + cfgToCfgObjMap.update(NodeType.config, configPath, newConfigPath); + assert.strictEqual(cfgToCfgObjMap.size, 1); + assert.isUndefined(cfgToCfgObjMap.get(configPath)); + assert.isDefined(cfgToCfgObjMap.get(newConfigPath)); + assert.strictEqual(cfgToCfgObjMap.get(newConfigPath)?.uri.fsPath, newConfigPath); + }); + + test('NEG: not existing new path', function() { + const configName = 'model.cfg'; + const configPath = testBuilder.getPath(configName, 'workspace'); + testBuilder.writeFileSync(configName, '', 'workspace'); + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([configPath]); + + const newConfigName = 'model.new.cfg'; + const newConfigPath = testBuilder.getPath(newConfigName, 'workspace'); + // commented out : testBuilder.writeFileSync(newConfigPath, '', 'workspace'); + + assert.strictEqual(cfgToCfgObjMap.size, 1); + cfgToCfgObjMap.update(NodeType.config, configPath, newConfigPath); + assert.strictEqual(cfgToCfgObjMap.size, 0); + }); + + test('NEG: not existing path', function() { + const configName = 'model.cfg'; + const configPath = testBuilder.getPath(configName, 'workspace'); + // commented out : testBuilder.writeFileSync(configName, '', 'workspace'); + const cfgToCfgObjMap = new CfgToCfgObjMap(); + cfgToCfgObjMap.init([configPath]); + + const newConfigName = 'model.new.cfg'; + const newConfigPath = testBuilder.getPath(configName, 'workspace'); + + assert.strictEqual(cfgToCfgObjMap.size, 0); + assert.doesNotThrow(() => { + cfgToCfgObjMap.update(NodeType.config, configPath, newConfigName); + }); + assert.strictEqual(cfgToCfgObjMap.size, 0); + assert.isUndefined(cfgToCfgObjMap.get(configPath)); + assert.isUndefined(cfgToCfgObjMap.get(newConfigPath)); + }); + }); + }); + + suite('BaseModelToCfgMap', function() { + suite('#constructor()', function() { + test('does not throw', function() { + assert.doesNotThrow(() => new BaseModelToCfgMap()); + }); + }); + + suite('#init()', function() { + test('NEG: with an empty cfglist and cfgObjMap', function() { + const baseModelToCfgMap = new BaseModelToCfgMap(); + assert.doesNotThrow(() => { + baseModelToCfgMap.init([], new CfgToCfgObjMap()); + }); + assert.strictEqual(baseModelToCfgMap.size, 0); + }); + test('NEG: falsy cfg list (not existing)', function() { + const baseModelToCfgMap = new BaseModelToCfgMap(); + assert.doesNotThrow(() => { + baseModelToCfgMap.init(['not/existing/path'], new CfgToCfgObjMap()); + }); + assert.isUndefined(baseModelToCfgMap.get('not/existing/path')); + assert.strictEqual(baseModelToCfgMap.size, 0); + }); + }); + + suite('#get()', function() { + test('NEG: empty path', function() { + const baseModelToCfgMap = new BaseModelToCfgMap(); + baseModelToCfgMap.init([], new CfgToCfgObjMap()); + assert.isUndefined(baseModelToCfgMap.get('')); + assert.strictEqual(baseModelToCfgMap.size, 0); + }); + + test('NEG: invalid path', function() { + const baseModelToCfgMap = new BaseModelToCfgMap(); + baseModelToCfgMap.init([], new CfgToCfgObjMap()); + assert.isUndefined(baseModelToCfgMap.get('invalid/path')); + assert.strictEqual(baseModelToCfgMap.size, 0); + }); + + test('existing path', function() { + const model = testBuilder.getPath('model.tflite', 'workspace'); + const config = testBuilder.getPath('model.cfg', 'workspace'); + const content = ` + [one-import-onnx] + input_path='model.tflite' + `; + + testBuilder.writeFileSync('model.cfg', content, 'workspace'); + testBuilder.writeFileSync('model.tflite', '', 'workspace'); + + const cfgList = [config]; + const cfgToCfgObjMap = new CfgToCfgObjMap(); + const baseModelToCfgMap = new BaseModelToCfgMap(); + cfgToCfgObjMap.init(cfgList); + baseModelToCfgMap.init(cfgList, cfgToCfgObjMap); + + assert.isDefined(baseModelToCfgMap.get(model)); + assert.strictEqual(baseModelToCfgMap.get(model)!.length, 1); + assert.strictEqual(baseModelToCfgMap.get(model)![0], config); + assert.strictEqual(baseModelToCfgMap.size, 1); + }); + }); + + suite('#reset()', function() { + test('existing path', function() { + const model = testBuilder.getPath('model.tflite', 'workspace'); + const config = testBuilder.getPath('model.cfg', 'workspace'); + const content = ` + [one-import-onnx] + input_path='model.tflite' + `; + + testBuilder.writeFileSync('model.cfg', content, 'workspace'); + testBuilder.writeFileSync('model.tflite', '', 'workspace'); + + const cfgList = [config]; + const cfgToCfgObjMap = new CfgToCfgObjMap(); + const baseModelToCfgMap = new BaseModelToCfgMap(); + cfgToCfgObjMap.init(cfgList); + baseModelToCfgMap.init(cfgList, cfgToCfgObjMap); + + assert.isDefined(baseModelToCfgMap.get(model)); + assert.strictEqual(baseModelToCfgMap.get(model)!.length, 1); + assert.strictEqual(baseModelToCfgMap.get(model)![0], config); + assert.strictEqual(baseModelToCfgMap.size, 1); + + baseModelToCfgMap.reset(NodeType.config, config); + + assert.isDefined(baseModelToCfgMap.get(model)); + assert.strictEqual(baseModelToCfgMap.get(model)!.length, 0); + assert.strictEqual(baseModelToCfgMap.size, 1); + }); + test('NEG: not existing path', function() { + const config = testBuilder.getPath('model.cfg', 'workspace'); + + const cfgList = [config]; + const cfgToCfgObjMap = new CfgToCfgObjMap(); + const baseModelToCfgMap = new BaseModelToCfgMap(); + cfgToCfgObjMap.init(cfgList); + baseModelToCfgMap.init(cfgList, cfgToCfgObjMap); + + assert.strictEqual(baseModelToCfgMap.size, 0); + assert.doesNotThrow(() => baseModelToCfgMap.reset(NodeType.config, config)); + }); + }); + + suite('#update()', function() { + test('config path', function() { + const model = testBuilder.getPath('model.tflite', 'workspace'); + const config = testBuilder.getPath('model.cfg', 'workspace'); + + const content = ` + [one-import-onnx] + input_path='model.tflite' + `; + + testBuilder.writeFileSync('model.cfg', content, 'workspace'); + testBuilder.writeFileSync('model.tflite', '', 'workspace'); + + const cfgList = [config]; + const cfgToCfgObjMap = new CfgToCfgObjMap(); + const baseModelToCfgMap = new BaseModelToCfgMap(); + cfgToCfgObjMap.init(cfgList); + baseModelToCfgMap.init(cfgList, cfgToCfgObjMap); + + assert.isDefined(baseModelToCfgMap.get(model)); + assert.strictEqual(baseModelToCfgMap.get(model)!.length, 1); + assert.strictEqual(baseModelToCfgMap.get(model)![0], config); + assert.strictEqual(baseModelToCfgMap.size, 1); + + const newConfig = testBuilder.getPath('model.new.cfg', 'workspace'); + testBuilder.writeFileSync('model.new.cfg', content, 'workspace'); + + baseModelToCfgMap.update(NodeType.config, config, newConfig); + + assert.isDefined(baseModelToCfgMap.get(model)); + assert.strictEqual(baseModelToCfgMap.get(model)!.length, 1); + assert.strictEqual(baseModelToCfgMap.get(model)![0], newConfig); + assert.strictEqual(baseModelToCfgMap.size, 1); + }); + + test('model and config names', function() { + const content = ` + [one-import-onnx] + input_path='model.tflite' + `; + + const oldModel = testBuilder.getPath('model.tflite', 'workspace'); + const config = testBuilder.getPath('model.cfg', 'workspace'); + testBuilder.writeFileSync('model.cfg', content, 'workspace'); + testBuilder.writeFileSync('model.tflite', '', 'workspace'); + + const cfgList = [config]; + const cfgToCfgObjMap = new CfgToCfgObjMap(); + const baseModelToCfgMap = new BaseModelToCfgMap(); + cfgToCfgObjMap.init(cfgList); + baseModelToCfgMap.init(cfgList, cfgToCfgObjMap); + + assert.strictEqual(baseModelToCfgMap.size, 1); + assert.isDefined(baseModelToCfgMap.get(oldModel)); + assert.strictEqual(baseModelToCfgMap.get(oldModel)!.length, 1); + assert.strictEqual(baseModelToCfgMap.get(oldModel)![0], config); + + const newModel = testBuilder.getPath('model.new.tflite', 'workspace'); + const newContent = ` + [one-import-onnx] + input_path='model.new.tflite' + `; + + testBuilder.writeFileSync('model.cfg', newContent, 'workspace'); + testBuilder.writeFileSync('model.new.tflite', '', 'workspace'); + + baseModelToCfgMap.update(NodeType.baseModel, oldModel, newModel); + + assert.strictEqual(baseModelToCfgMap.size, 1); + assert.isUndefined(baseModelToCfgMap.get(oldModel)); + assert.isDefined(baseModelToCfgMap.get(newModel)); + assert.strictEqual(baseModelToCfgMap.get(newModel)!.length, 1); + assert.strictEqual(baseModelToCfgMap.get(newModel)![0], config); + }); + + test('NEG: not existing path', function() { + const content = ` + [one-import-onnx] + input_path='model.tflite' + `; + + const model = testBuilder.getPath('model.tflite', 'workspace'); + const config = testBuilder.getPath('model.cfg', 'workspace'); + testBuilder.writeFileSync('model.cfg', content, 'workspace'); + testBuilder.writeFileSync('model.tflite', '', 'workspace'); + + const cfgList = [config]; + const cfgToCfgObjMap = new CfgToCfgObjMap(); + const baseModelToCfgMap = new BaseModelToCfgMap(); + cfgToCfgObjMap.init(cfgList); + baseModelToCfgMap.init(cfgList, cfgToCfgObjMap); + + assert.strictEqual(baseModelToCfgMap.size, 1); + assert.isDefined(baseModelToCfgMap.get(model)); + assert.strictEqual(baseModelToCfgMap.get(model)!.length, 1); + assert.strictEqual(baseModelToCfgMap.get(model)![0], config); + + assert.doesNotThrow(() => { + baseModelToCfgMap.update(NodeType.config, config, '/invalid/path'); + }); + assert.strictEqual(cfgToCfgObjMap.size, 1); + }); + }); + }); }); });