Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS API] Refactoring test utils #28663

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bindings/js/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"build": "npm run tsc",
"prepare": "npm run build",
"lint": "eslint .",
"test_setup": "node ./tests/unit/setup.js",
"test_setup": "node ./tests/setup.js",
"test": "npm run test_setup && node --test ./tests/unit/*.test.js",
"test:e2e": "mocha ./tests/e2e/electron-app.test.js",
"tsc": "tsc",
Expand Down
15 changes: 6 additions & 9 deletions src/bindings/js/node/scripts/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,15 @@ async function checkIfPathExists(path) {
*
* @function downloadFile
* @param {string} url - The file URL.
* @param {string} filename - The filename of result file.
* @param {string} destination - The destination path of result file.
* @param {string} filePath - Path to downloaded file.
* @param {string} [proxy=null] - (Optional) The proxy URL.
* @returns {Promise<string>} - Path to downloaded file.
*/
function downloadFile(url, destination, filename, proxy = null) {
console.log(`Downloading file by link: ${url} to ${destination}`
+ `with filename: ${filename}`);
function downloadFile(url, filePath, proxy = null) {
console.log(`Downloading file by link: ${url} to ${filePath}.`);

const timeout = 5000;
const fullPath = path.resolve(destination, filename);
const file = createWriteStream(fullPath);
const file = createWriteStream(filePath);

if (new URL(url).protocol === 'http:')
throw new Error('Http link doesn\'t support');
Expand Down Expand Up @@ -98,8 +95,8 @@ function downloadFile(url, destination, filename, proxy = null) {

file.on('finish', () => {
file.close();
console.log(`File was successfully downloaded to '${fullPath}'.`);
resolve(fullPath);
console.log(`File was successfully downloaded to '${filePath}'.`);
resolve(filePath);
});
});

Expand Down
9 changes: 5 additions & 4 deletions src/bindings/js/node/tests/e2e/demo-electron-app/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { app } = require('electron');
const { addon: ov } = require('openvino-node');
const { testModels, lengthFromShape } = require('../../utils.js');

const epsilon = 0.5; // To avoid very small numbers
const pathToModel = '../tests/unit/test_models/test_model_fp32.xml';
const testModelFP32 = testModels.testModelFP32;

main();

Expand All @@ -15,17 +16,17 @@ async function main() {
const core = new ov.Core();
console.log('Created OpenVINO Runtime Core');

const model = await core.readModel(pathToModel);
const model = await core.readModel(testModelFP32.xml);
console.log('Model read successfully:', model);
const compiledModel = await core.compileModel(model, 'CPU');
const inferRequest = compiledModel.createInferRequest();
console.log('Infer request created:', inferRequest);

const tensorData = Float32Array.from(
{ length: 3072 },
{ length: lengthFromShape(testModelFP32.inputShape) },
() => Math.random() + epsilon,
);
const tensor = new ov.Tensor(ov.element.f32, [1, 3, 32, 32], tensorData);
const tensor = new ov.Tensor(ov.element.f32, testModelFP32.inputShape, tensorData);
console.log('Tensor created:', tensor);

const result = await inferRequest.inferAsync([tensor]);
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/js/node/tests/e2e/electron-app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const util = require('node:util');
const assert = require('node:assert');
const { exec } = require('child_process');
const execPromise = util.promisify(exec);
const { testModels, downloadTestModel } = require('../unit/utils.js');
const { testModels, downloadTestModel } = require('../utils.js');

describe('E2E testing for OpenVINO as an Electron dependency.', function() {
this.timeout(50000);
Expand Down
16 changes: 8 additions & 8 deletions src/bindings/js/node/tests/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ const { after, describe, it, before, beforeEach } = require('node:test');
const {
testModels,
compareModels,
getModelPath,
isModelAvailable,
sleep,
} = require('./utils.js');
lengthFromShape,
} = require('../utils.js');
const epsilon = 0.5;

describe('ov basic tests.', () => {
let testXml = null;
const testModelFP32 = testModels.testModelFP32
const testXml = testModelFP32.xml;
let core = null;
let model = null;
let compiledModel = null;
Expand All @@ -27,8 +28,7 @@ describe('ov basic tests.', () => {

before(async () => {
outDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'ov_js_out_'));
await isModelAvailable(testModels.testModelFP32);
testXml = getModelPath().xml;
await isModelAvailable(testModelFP32);
});

beforeEach(() => {
Expand Down Expand Up @@ -277,8 +277,8 @@ describe('ov basic tests.', () => {
assert.strictEqual(obj.input().getAnyName(), 'data');
assert.strictEqual(obj.input().anyName, 'data');

assert.deepStrictEqual(obj.input(0).shape, [1, 3, 32, 32]);
assert.deepStrictEqual(obj.input(0).getShape(), [1, 3, 32, 32]);
assert.deepStrictEqual(obj.input(0).shape, testModelFP32.inputShape);
assert.deepStrictEqual(obj.input(0).getShape(), testModelFP32.inputShape);
});
});
});
Expand All @@ -290,7 +290,7 @@ describe('ov basic tests.', () => {

before(() => {
tensor = Float32Array.from(
{ length: 3072 },
{ length: lengthFromShape(testModelFP32.inputShape) },
() => Math.random() + epsilon,
);
const core = new ov.Core();
Expand Down
7 changes: 4 additions & 3 deletions src/bindings/js/node/tests/unit/compiled_model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
const { testModels, getModelPath, isModelAvailable } = require('./utils.js');
const { testModels, isModelAvailable } = require('../utils.js');

describe('ov.CompiledModel tests', () => {
let testXml = null;
let core = null;
let compiledModel = null;

before(async () => {
await isModelAvailable(testModels.testModelFP32);
testXml = getModelPath().xml;
const testModelFP32 = testModels.testModelFP32;
await isModelAvailable(testModelFP32);
testXml = testModelFP32.xml;
core = new ov.Core();
});

Expand Down
9 changes: 5 additions & 4 deletions src/bindings/js/node/tests/unit/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
const { testModels, isModelAvailable, getModelPath } = require('./utils.js');
const { testModels, isModelAvailable } = require('../utils.js');

describe('ov.Core tests', () => {
const testModelFP32 = testModels.testModelFP32;
let core = null;
before(async () => {
await isModelAvailable(testModels.testModelFP32);
await isModelAvailable(testModelFP32);
});

beforeEach(() => {
Expand Down Expand Up @@ -95,15 +96,15 @@ describe('ov.Core tests', () => {
});

it('Core.queryModel() with incorrect arguments should throw an error', () => {
const model = core.readModelSync(getModelPath().xml);
const model = core.readModelSync(testModelFP32.xml);
assert.throws(
() => core.queryModel(model, 'arg1', 'arg2').then(),
/'queryModel' method called with incorrect parameters./,
);
});

it('Core.queryModel() should have device in the result values', () => {
const model = core.readModelSync(getModelPath().xml);
const model = core.readModelSync(testModelFP32.xml);
const device = 'CPU';
const queryModel = core.queryModel(model, device);
assert(Object.values(queryModel).includes(device));
Expand Down
22 changes: 11 additions & 11 deletions src/bindings/js/node/tests/unit/infer_request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
const { testModels, isModelAvailable, getModelPath } = require('./utils.js');
const { testModels, isModelAvailable, lengthFromShape } = require('../utils.js');

const epsilon = 0.5; // To avoid very small numbers
const testXml = getModelPath().xml;

describe('ov.InferRequest tests', () => {
const testModelFP32 = testModels.testModelFP32;
let compiledModel = null;
let tensorData = null;
let tensor = null;
let resTensor = null;
let tensorLike = null;

before(async () => {
await isModelAvailable(testModels.testModelFP32);
await isModelAvailable(testModelFP32);

const core = new ov.Core();
const model = core.readModelSync(testXml);
const model = core.readModelSync(testModelFP32.xml);
compiledModel = core.compileModelSync(model, 'CPU');

tensorData = Float32Array.from(
{ length: 3072 },
{ length: lengthFromShape(testModelFP32.inputShape) },
() => Math.random() + epsilon,
);
tensor = new ov.Tensor(ov.element.f32, [1, 3, 32, 32], tensorData);
resTensor = new ov.Tensor(ov.element.f32, [1, 10], tensorData.slice(-10));
tensor = new ov.Tensor(ov.element.f32, testModelFP32.inputShape, tensorData);
resTensor = new ov.Tensor(ov.element.f32, testModelFP32.outputShape, tensorData.slice(-10));
tensorLike = [tensor, tensorData];
});

Expand All @@ -43,15 +43,15 @@ describe('ov.InferRequest tests', () => {
tensorLike.forEach((tl) => {
const result = inferRequest.infer({ data: tl });
assert.deepStrictEqual(Object.keys(result), ['fc_out']);
assert.deepStrictEqual(result['fc_out'].data.length, 10);
assert.deepStrictEqual(result['fc_out'].data.length, lengthFromShape(testModelFP32.outputShape));
});
});

it('Test infer(inputData: Tensor[]/TypedArray[])', () => {
tensorLike.forEach((tl) => {
const result = inferRequest.infer([tl]);
assert.deepStrictEqual(Object.keys(result), ['fc_out']);
assert.deepStrictEqual(result['fc_out'].data.length, 10);
assert.deepStrictEqual(result['fc_out'].data.length, lengthFromShape(testModelFP32.outputShape));
});
});

Expand Down Expand Up @@ -102,15 +102,15 @@ describe('ov.InferRequest tests', () => {
inferRequest.inferAsync({ data: tensor }).then((result) => {
assert.ok(result['fc_out'] instanceof ov.Tensor);
assert.deepStrictEqual(Object.keys(result), ['fc_out']);
assert.deepStrictEqual(result['fc_out'].data.length, 10);
assert.deepStrictEqual(result['fc_out'].data.length, lengthFromShape(testModelFP32.outputShape));
});
});

it('Test inferAsync(inputData: Tensor[])', () => {
inferRequest.inferAsync([tensor]).then((result) => {
assert.ok(result['fc_out'] instanceof ov.Tensor);
assert.deepStrictEqual(Object.keys(result), ['fc_out']);
assert.deepStrictEqual(result['fc_out'].data.length, 10);
assert.deepStrictEqual(result['fc_out'].data.length, lengthFromShape(testModelFP32.outputShape));
});
});

Expand Down
9 changes: 4 additions & 5 deletions src/bindings/js/node/tests/unit/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
const { testModels, getModelPath, isModelAvailable } = require('./utils.js');
const { testModels, isModelAvailable } = require('../utils.js');

describe('ov.Model tests', () => {
let testXml = null;
const testModelFP32 = testModels.testModelFP32;
let core = null;
let model = null;

before(async () => {
await isModelAvailable(testModels.testModelFP32);
testXml = getModelPath().xml;
await isModelAvailable(testModelFP32);
core = new ov.Core();
});

beforeEach(() => {
model = core.readModelSync(testXml);
model = core.readModelSync(testModelFP32.xml);
});

describe('Model.isDynamic()', () => {
Expand Down
9 changes: 4 additions & 5 deletions src/bindings/js/node/tests/unit/pre_post_processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
const { testModels, getModelPath, isModelAvailable } = require('./utils.js');
const { testModels, isModelAvailable } = require('../utils.js');

describe('ov.preprocess.PrePostProcessor tests', () => {
let testXml = null;
const testModelFP32 = testModels.testModelFP32;
let core = null;
let model = null;

before(async () => {
await isModelAvailable(testModels.testModelFP32);
testXml = getModelPath().xml;
await isModelAvailable(testModelFP32);
core = new ov.Core();
});

beforeEach(() => {
model = core.readModelSync(testXml);
model = core.readModelSync(testModelFP32.xml);
});

describe('PrePostProcess', () => {
Expand Down
21 changes: 10 additions & 11 deletions src/bindings/js/node/tests/unit/read_model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ const fs = require('node:fs');
const { addon: ov } = require('../..');
const assert = require('assert');
const { describe, it, before, beforeEach } = require('node:test');
const { testModels, isModelAvailable, getModelPath } = require('./utils.js');

const { xml: modelPath, bin: weightsPath } = getModelPath();
const { testModels, isModelAvailable } = require('../utils.js');

describe('Tests for reading model.', () => {
const testModelFP32 = testModels.testModelFP32;
let modelFile = null;
let modelStr = null;
let weightsFile = null;
let weightsTensor = null;
let core = null;

before(async () => {
await isModelAvailable(testModels.testModelFP32);
modelFile = fs.readFileSync(modelPath);
modelStr = fs.readFileSync(modelPath, 'utf8');
weightsFile = fs.readFileSync(weightsPath);
await isModelAvailable(testModelFP32);
modelFile = fs.readFileSync(testModelFP32.xml);
modelStr = fs.readFileSync(testModelFP32.xml, 'utf8');
weightsFile = fs.readFileSync(testModelFP32.bin);
});

beforeEach(() => {
Expand All @@ -35,13 +34,13 @@ describe('Tests for reading model.', () => {

describe('Core.readModeSync', () => {
it('readModeSync(xmlPath) ', () => {
const model = core.readModelSync(modelPath);
const model = core.readModelSync(testModelFP32.xml);
assert.ok(model instanceof ov.Model);
assert.equal(model.inputs.length, 1);
});

it('readModeSync(xmlPath, weightsPath) ', () => {
const model = core.readModelSync(modelPath, weightsPath);
const model = core.readModelSync(testModelFP32.xml, testModelFP32.bin);
assert.ok(model instanceof ov.Model);
assert.equal(model.inputs.length, 1);
});
Expand Down Expand Up @@ -71,12 +70,12 @@ describe('Tests for reading model.', () => {

describe('Core.readModel', () => {
it('readModel(xmlPath) ', async () => {
const model = await core.readModel(modelPath);
const model = await core.readModel(testModelFP32.xml);
assert.equal(model.inputs.length, 1);
});

it('readModel(xmlPath, weightsPath) ', async () => {
const model = await core.readModel(modelPath, weightsPath);
const model = await core.readModel(testModelFP32.xml, testModelFP32.bin);
assert.equal(model.inputs.length, 1);
});

Expand Down
3 changes: 2 additions & 1 deletion src/bindings/js/node/tests/unit/tensor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { addon: ov } = require('../..');
const assert = require('assert');
const { test, describe, it, before } = require('node:test');
const getRandomBigInt = require('random-bigint');
const { lengthFromShape } = require('../utils');

describe('ov.Tensor tests', () => {
let shape = null;
Expand Down Expand Up @@ -260,7 +261,7 @@ describe('ov.Tensor tests', () => {
describe('Tensor getSize', () => {
it('getSize returns the correct total number of elements', () => {
const tensor = new ov.Tensor(ov.element.f32, shape, data);
const expectedSize = shape.reduce((acc, dim) => acc * dim, 1);
const expectedSize = lengthFromShape(shape);
assert.strictEqual(tensor.getSize(), expectedSize);
});

Expand Down
Loading
Loading