Skip to content

Commit

Permalink
Add link to licence version purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruikshanks committed Aug 27, 2024
1 parent e0eacb7 commit ea61ffb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/licence-version-purpose.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class LicenceVersionPurposeModel extends BaseModel {
to: 'licenceVersionPurposeConditions.licenceVersionPurposeId'
}
},
licenceVersionPurposePoints: {
relation: Model.HasManyRelation,
modelClass: 'licence-version-purpose-point.model',
join: {
from: 'licenceVersionPurposes.id',
to: 'licenceVersionPurposePoints.licenceVersionPurposeId'
}
},
primaryPurpose: {
relation: Model.BelongsToOneRelation,
modelClass: 'primary-purpose.model.js',
Expand Down
40 changes: 40 additions & 0 deletions test/models/licence-version-purpose.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const LicenceVersionModel = require('../../app/models/licence-version.model.js')
const LicenceVersionPurposeConditionHelper = require('../support/helpers/licence-version-purpose-condition.helper.js')
const LicenceVersionPurposeConditionModel = require('../../app/models/licence-version-purpose-condition.model.js')
const LicenceVersionPurposeHelper = require('../support/helpers/licence-version-purpose.helper.js')
const LicenceVersionPurposePointModel = require('../../app/models/licence-version-purpose-point.model.js')
const LicenceVersionPurposePointHelper = require('../support/helpers/licence-version-purpose-point.helper.js')
const PrimaryPurposeHelper = require('../support/helpers/primary-purpose.helper.js')
const PrimaryPurposeModel = require('../../app/models/primary-purpose.model.js')
const PurposeHelper = require('../support/helpers/purpose.helper.js')
Expand Down Expand Up @@ -118,6 +120,44 @@ describe('Licence Version Purpose model', () => {
})
})

describe('when linking to licence version purpose points', () => {
let testLicenceVersionPurposePoints

beforeEach(async () => {
testRecord = await LicenceVersionPurposeHelper.add()

testLicenceVersionPurposePoints = []
for (let i = 0; i < 2; i++) {
const licenceVersionPurposePoint = await LicenceVersionPurposePointHelper.add({
licenceVersionPurposeId: testRecord.id
})

testLicenceVersionPurposePoints.push(licenceVersionPurposePoint)
}
})

it('can successfully run a related query', async () => {
const query = await LicenceVersionPurposeModel.query()
.innerJoinRelated('licenceVersionPurposePoints')

expect(query).to.exist()
})

it('can eager load the licence version purpose points', async () => {
const result = await LicenceVersionPurposeModel.query()
.findById(testRecord.id)
.withGraphFetched('licenceVersionPurposePoints')

expect(result).to.be.instanceOf(LicenceVersionPurposeModel)
expect(result.id).to.equal(testRecord.id)

expect(result.licenceVersionPurposePoints).to.be.an.array()
expect(result.licenceVersionPurposePoints[0]).to.be.an.instanceOf(LicenceVersionPurposePointModel)
expect(result.licenceVersionPurposePoints).to.include(testLicenceVersionPurposePoints[0])
expect(result.licenceVersionPurposePoints).to.include(testLicenceVersionPurposePoints[1])
})
})

describe('when linking to primary purpose', () => {
beforeEach(async () => {
testRecord = await LicenceVersionPurposeHelper.add({ primaryPurposeId })
Expand Down

0 comments on commit ea61ffb

Please sign in to comment.