From aebcfb516790e547522a92e2f19fd35fd2338d5a Mon Sep 17 00:00:00 2001 From: Jonathan Goulding <58443816+jonathangoulding@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:41:55 +0100 Subject: [PATCH] Fix return version tests (#1295) --- test/models/return-version.model.test.js | 16 ++++++++-------- test/support/general.js | 16 ++++++++++++++++ test/support/helpers/mod-log.helper.js | 4 ++-- test/support/helpers/return-version.helper.js | 4 ++-- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/test/models/return-version.model.test.js b/test/models/return-version.model.test.js index 7ef83ceeb6..3ae629e85c 100644 --- a/test/models/return-version.model.test.js +++ b/test/models/return-version.model.test.js @@ -8,7 +8,7 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { randomInteger } = require('../support/general.js') +const { randomInteger, randomRegionCode } = require('../support/general.js') const LicenceHelper = require('../support/helpers/licence.helper.js') const LicenceModel = require('../../app/models/licence.model.js') const ModLogHelper = require('../support/helpers/mod-log.helper.js') @@ -198,7 +198,7 @@ describe('Return Version model', () => { describe('when a return version has mod log history', () => { beforeEach(async () => { - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const firstNaldId = randomInteger(100, 99998) await ModLogHelper.add({ @@ -279,7 +279,7 @@ describe('Return Version model', () => { describe('and has mod log history', () => { beforeEach(async () => { - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const firstNaldId = randomInteger(100, 99998) await ModLogHelper.add({ externalId: `${regionCode}:${firstNaldId}`, returnVersionId, userId: 'FIRST' }) @@ -343,7 +343,7 @@ describe('Return Version model', () => { describe('and none of the mod log history has notes', () => { beforeEach(async () => { - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const firstNaldId = randomInteger(100, 99998) await ModLogHelper.add({ @@ -366,7 +366,7 @@ describe('Return Version model', () => { describe('and some of the mod log history has notes', () => { beforeEach(async () => { - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const firstNaldId = randomInteger(100, 99998) await ModLogHelper.add({ @@ -394,7 +394,7 @@ describe('Return Version model', () => { returnVersionId = id - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const firstNaldId = randomInteger(100, 99998) await ModLogHelper.add({ @@ -462,7 +462,7 @@ describe('Return Version model', () => { describe('but the mod log history has no reason description recorded in the first entry', () => { beforeEach(async () => { - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const firstNaldId = randomInteger(100, 99998) await ModLogHelper.add({ @@ -484,7 +484,7 @@ describe('Return Version model', () => { describe('and the mod log history has a reason description recorded in the first entry', () => { beforeEach(async () => { - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const firstNaldId = randomInteger(100, 99998) await ModLogHelper.add({ diff --git a/test/support/general.js b/test/support/general.js index 07a808a0ae..12f197ce62 100644 --- a/test/support/general.js +++ b/test/support/general.js @@ -95,8 +95,24 @@ function selectRandomEntry (data) { return data[randomIndex] } +/** + * Generates a random region code + * + * Region codes should be between 1 and 9 based on the fixed region reference data. + * + * We see issues with this small range when tables have unique constraints when building external id's. + * + * This function is here to encapsulate this issue and remove any need to explain the issue else where in the tests. + * + * @returns a random number + */ +function randomRegionCode () { + return randomInteger(1, 999999) +} + module.exports = { postRequestOptions, randomInteger, + randomRegionCode, selectRandomEntry } diff --git a/test/support/helpers/mod-log.helper.js b/test/support/helpers/mod-log.helper.js index 6a60097f7b..e0a06c4db3 100644 --- a/test/support/helpers/mod-log.helper.js +++ b/test/support/helpers/mod-log.helper.js @@ -5,7 +5,7 @@ */ const ModLogModel = require('../../../app/models/mod-log.model.js') -const { randomInteger } = require('../general.js') +const { randomInteger, randomRegionCode } = require('../general.js') const { generateLicenceRef } = require('./licence.helper.js') /** @@ -44,7 +44,7 @@ function add (data = {}) { * @returns {object} - Returns the set defaults with the override data spread */ function defaults (data = {}) { - const regionCode = randomInteger(1, 9) + const regionCode = randomRegionCode() const defaults = { externalId: generateRegionNaldPatternExternalId(regionCode), diff --git a/test/support/helpers/return-version.helper.js b/test/support/helpers/return-version.helper.js index ddc930f841..0ef157b3b3 100644 --- a/test/support/helpers/return-version.helper.js +++ b/test/support/helpers/return-version.helper.js @@ -5,7 +5,7 @@ */ const { generateUUID } = require('../../../app/lib/general.lib.js') -const { randomInteger } = require('../general.js') +const { randomInteger, randomRegionCode } = require('../general.js') const ReturnVersionModel = require('../../../app/models/return-version.model.js') /** @@ -46,7 +46,7 @@ function defaults (data = {}) { const version = data.version ? data.version : 100 const defaults = { - externalId: `9:${randomInteger(100, 99999)}:${version}`, + externalId: `${randomRegionCode()}:${randomInteger(100, 99999)}:${version}`, licenceId: generateUUID(), reason: 'new-licence', startDate: new Date('2022-04-01'),