Skip to content

Commit

Permalink
Fix return version tests (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangoulding authored Aug 30, 2024
1 parent e7fa989 commit aebcfb5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
16 changes: 8 additions & 8 deletions test/models/return-version.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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' })
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down
16 changes: 16 additions & 0 deletions test/support/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions test/support/helpers/mod-log.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions test/support/helpers/return-version.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit aebcfb5

Please sign in to comment.