Skip to content

Commit

Permalink
ISSUE #5163 vertical range is now converted to calibbration units
Browse files Browse the repository at this point in the history
test adjusted
  • Loading branch information
ChristosTsiotsias committed Sep 24, 2024
1 parent bd9c225 commit 8070467
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const { getDrawingById, updateModelSettings } = require('../../../../../models/m
const { getRevisionByIdOrTag, getRevisions, getRevisionsByQuery } = require('../../../../../models/revisions');
const { UUIDToString } = require('../../../../../utils/helper/uuids');
const { calibrationStatuses } = require('../../../../../models/calibrations.constants');
const { deleteIfUndefined } = require('../../../../../utils/helper/objects');
const { convertArrayUnits } = require('../../../../../utils/helper/units');
const { events } = require('../../../../../services/eventsManager/eventsManager.constants');
const { modelTypes } = require('../../../../../models/modelSettings.constants');
const { publish } = require('../../../../../services/eventsManager/eventsManager');
Expand All @@ -44,6 +44,7 @@ Calibrations.getCalibration = async (teamspace, project, drawing, revision) => {
horizontal: 1,
createdAt: 1,
createdBy: 1,
units: 1,
};

const [latestCalibration, { calibration: drawingData }] = await Promise.all([
Expand All @@ -52,7 +53,12 @@ Calibrations.getCalibration = async (teamspace, project, drawing, revision) => {
]);

if (latestCalibration) {
return { calibration: { ...latestCalibration, ...drawingData }, status: calibrationStatuses.CALIBRATED };
return {
calibration: {
...latestCalibration,
verticalRange: convertArrayUnits(drawingData.verticalRange, drawingData.units, latestCalibration.units),
},
status: calibrationStatuses.CALIBRATED };
}

const { timestamp } = await getRevisionByIdOrTag(teamspace, drawing,
Expand All @@ -71,7 +77,11 @@ Calibrations.getCalibration = async (teamspace, project, drawing, revision) => {
const data = revIdToCalib[UUIDToString(previousRevisions[i]._id)];
if (data) {
return {
calibration: { ...data.latestCalibration, ...drawingData },
calibration: {
...data.latestCalibration,
verticalRange: convertArrayUnits(drawingData.verticalRange, drawingData.units,
data.latestCalibration.units),
},
status: calibrationStatuses.UNCONFIRMED,
};
}
Expand Down
41 changes: 41 additions & 0 deletions backend/src/v5/utils/helper/units.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (C) 2024 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

const { isNumber } = require('./typeCheck');

const UnitsHelper = {};

const UNITS_CONVERSION_FACTORS_TO_METRES = {
m: 1,
dm: 10,
cm: 100,
mm: 1000,
ft: 3.28084,
};

UnitsHelper.convertArrayUnits = (array, fromUnit, toUnit) => {
const fromFactor = UNITS_CONVERSION_FACTORS_TO_METRES[fromUnit];
const toFactor = UNITS_CONVERSION_FACTORS_TO_METRES[toUnit];

if (!array.every(isNumber) || !fromFactor || !toFactor) {
return null;
}

return array.map((n) => (n / fromFactor) * toFactor);
};

module.exports = UnitsHelper;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { times } = require('lodash');

const { modelTypes } = require(`${src}/models/modelSettings.constants`);
const { templates } = require(`${src}/utils/responseCodes`);
const { convertArrayUnits } = require(`${src}/utils/helper/units`);

let server;
let agent;
Expand Down Expand Up @@ -156,9 +157,11 @@ const testGetCalibration = () => {
const latestCalibration = calibrations.reduce(
(max, cal) => (max.createdAt > cal.createdAt ? max : cal));

const { verticalRange, units: drawingUnits } = parameters.drawing.properties.calibration;

expect(res.body).toEqual({
verticalRange: parameters.drawing.properties.calibration.verticalRange,
units: parameters.drawing.properties.calibration.units,
verticalRange: convertArrayUnits(verticalRange, drawingUnits, latestCalibration.units),
units: latestCalibration.units,
horizontal: latestCalibration.horizontal,
createdAt: res.body.createdAt,
createdBy: res.body.createdBy });
Expand Down Expand Up @@ -190,7 +193,7 @@ const testAddCalibration = () => {
drawing: times(2, () => times(2, () => ServiceHelper.generateRandomNumber())),
},
verticalRange: [ServiceHelper.generateRandomNumber(0, 10), ServiceHelper.generateRandomNumber(11, 20)],
units: 'm',
units: 'mm',
};

beforeAll(async () => {
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/v5/helper/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ db.createCalibration = async (teamspace, project, drawing, revision, calibration
drawing,
rev_id: stringToUUID(revision),
verticalRange: undefined,
units: undefined,
});

await DbHandler.insertOne(teamspace, 'drawings.calibrations', formattedCalibration);
Expand Down Expand Up @@ -496,7 +495,7 @@ ServiceHelper.generateCalibration = () => ({
drawing: times(2, () => times(2, () => ServiceHelper.generateRandomNumber())),
},
verticalRange: [0, 10],
units: 'm',
units: 'mm',
createdAt: ServiceHelper.generateRandomDate(),
createdBy: ServiceHelper.generateRandomString(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const {
generateUUID,
} = require('../../../../../../helper/services');

const { deleteIfUndefined } = require(`${src}/utils/helper/objects`);

const { templates } = require(`${src}/utils/responseCodes`);
const { UUIDToString } = require(`${src}/utils/helper/uuids`);
const { modelTypes } = require(`${src}/models/modelSettings.constants`);
Expand All @@ -53,7 +51,7 @@ const testGetCalibration = () => {
const drawing = generateRandomString();
const revisionId = generateRandomString();

const calibration = generateRandomObject();
const calibration = { units: 'mm', ...generateRandomObject() };
const drawingData = {
calibration: { verticalRange: [0, 10], units: 'm' },
};
Expand All @@ -75,6 +73,7 @@ const testGetCalibration = () => {
horizontal: 1,
createdAt: 1,
createdBy: 1,
units: 1,
};

test('should return the latest revision calibration', async () => {
Expand All @@ -83,7 +82,7 @@ const testGetCalibration = () => {

await expect(Calibrations.getCalibration(teamspace, project, drawing, revisionId))
.resolves.toEqual({
calibration: { ...calibration, ...drawingData.calibration },
calibration: { ...calibration, verticalRange: [0, 10000] },
status: calibrationStatuses.CALIBRATED,
});

Expand Down Expand Up @@ -159,7 +158,7 @@ const testGetCalibration = () => {

await expect(Calibrations.getCalibration(teamspace, project, drawing, revisionId))
.resolves.toEqual({
calibration: { ...calibration, ...drawingData.calibration },
calibration: { ...calibration, verticalRange: [0, 10000] },
status: calibrationStatuses.UNCONFIRMED,
});

Expand Down
59 changes: 59 additions & 0 deletions backend/tests/v5/unit/utils/helper/units.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (C) 2024 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

const { src } = require('../../../helper/path');
const { generateRandomString } = require('../../../helper/services');

const UnitsHelper = require(`${src}/utils/helper/units`);

const testConvertArrayUnits = () => {
describe.each([
['invalid fromUnit', [1, 5], generateRandomString(), 'm'],
['invalid toUnit', [1, 5], 'm', generateRandomString()],
['array with non numbers', [generateRandomString(), 5], 'm', 'mm'],
['m to dm', [1, 5], 'm', 'dm', [10, 50]],
['m to cm', [1, 5], 'm', 'cm', [100, 500]],
['m to mm', [1, 5], 'm', 'mm', [1000, 5000]],
['m to ft', [1, 5], 'm', 'ft', [3.281, 16.404]],
['dm to m', [1, 5], 'dm', 'm', [0.1, 0.5]],
['dm to cm', [1, 5], 'dm', 'cm', [10, 50]],
['dm to mm', [1, 5], 'dm', 'mm', [100, 500]],
['dm to ft', [1, 5], 'dm', 'ft', [0.328, 1.640]],
['mm to dm', [1, 5], 'mm', 'dm', [0.01, 0.05]],
['mm to cm', [1, 5], 'mm', 'cm', [0.1, 0.5]],
['mm to m', [1, 5], 'mm', 'm', [0.001, 0.005]],
['mm to ft', [1, 5], 'mm', 'ft', [0.003, 0.016]],
['ft to dm', [1, 5], 'ft', 'dm', [3.048, 15.24]],
['ft to cm', [1, 5], 'ft', 'cm', [30.48, 152.4]],
['ft to m', [1, 5], 'ft', 'm', [0.305, 1.524]],
['ft to mm', [1, 5], 'ft', 'mm', [304.8, 1524]],
])('Convert array units', (description, array, fromUnit, toUnit, result = null) => {
test(`with ${description} should return ${result}`, () => {
let res = UnitsHelper.convertArrayUnits(array, fromUnit, toUnit);

if (res) {
res = res.map((r) => Math.round(r * 1000) / 1000);
}

expect(res).toEqual(result);
});
});
};

describe('utils/helper/units', () => {
testConvertArrayUnits();
});

0 comments on commit 8070467

Please sign in to comment.