-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create migration script, model, helper and unit tests for `licence_ve…
…rsions` (#456) https://eaflood.atlassian.net/browse/WATER-3486 Whilst working on the unit tests for the enhancement in this PR #443 it was found that we would need the migration script, model, helper and unit tests for table `licence_versions`. These will be created in this PR.
- Loading branch information
Showing
6 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
/** | ||
* Model for licenceVersions | ||
* @module LicenceVersionModel | ||
*/ | ||
|
||
const { Model } = require('objection') | ||
|
||
const WaterBaseModel = require('./water-base.model.js') | ||
|
||
class LicenceVersionModel extends WaterBaseModel { | ||
static get tableName () { | ||
return 'licenceVersions' | ||
} | ||
|
||
static get idColumn () { | ||
return 'licenceVersionId' | ||
} | ||
|
||
static get translations () { | ||
return [ | ||
{ database: 'dateCreated', model: 'createdAt' }, | ||
{ database: 'dateUpdated', model: 'updatedAt' } | ||
] | ||
} | ||
|
||
static get relationMappings () { | ||
return { | ||
licence: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'licence.model', | ||
join: { | ||
from: 'licenceVersions.licenceId', | ||
to: 'licences.licenceId' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = LicenceVersionModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
db/migrations/20231009155523_create-water-licence-versions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
const tableName = 'licence_versions' | ||
|
||
exports.up = function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('water') | ||
.createTable(tableName, (table) => { | ||
// Primary Key | ||
table.uuid('licence_version_id').primary().defaultTo(knex.raw('gen_random_uuid()')) | ||
|
||
// Data | ||
table.uuid('licence_id').notNullable() | ||
table.integer('issue').notNullable() | ||
table.integer('increment').notNullable() | ||
table.string('status').notNullable() | ||
table.date('start_date').notNullable() | ||
table.date('end_date') | ||
table.string('external_id').notNullable() | ||
table.boolean('is_test').notNullable().defaultTo(false) | ||
|
||
// Legacy timestamps | ||
table.timestamp('date_created', { useTz: false }).notNullable() | ||
table.timestamp('date_updated', { useTz: false }).notNullable() | ||
}) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('water') | ||
.dropTableIfExists(tableName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict' | ||
|
||
// Test framework dependencies | ||
const Lab = require('@hapi/lab') | ||
const Code = require('@hapi/code') | ||
|
||
const { describe, it, beforeEach } = exports.lab = Lab.script() | ||
const { expect } = Code | ||
|
||
// Test helpers | ||
const DatabaseHelper = require('../../support/helpers/database.helper.js') | ||
const LicenceHelper = require('../../support/helpers/water/licence.helper.js') | ||
const LicenceModel = require('../../../app/models/water/licence.model.js') | ||
const LicenceVersionHelper = require('../../support/helpers/water/licence-version.helper.js') | ||
|
||
// Thing under test | ||
const LicenceVersionModel = require('../../../app/models/water/licence-version.model.js') | ||
|
||
describe('Licence Version model', () => { | ||
let testRecord | ||
|
||
beforeEach(async () => { | ||
await DatabaseHelper.clean() | ||
|
||
testRecord = await LicenceVersionHelper.add() | ||
}) | ||
|
||
describe('Basic query', () => { | ||
it('can successfully run a basic query', async () => { | ||
const result = await LicenceVersionModel.query().findById(testRecord.licenceVersionId) | ||
|
||
expect(result).to.be.an.instanceOf(LicenceVersionModel) | ||
expect(result.licenceVersionId).to.equal(testRecord.licenceVersionId) | ||
}) | ||
}) | ||
|
||
describe('Relationships', () => { | ||
describe('when linking to licence', () => { | ||
let testLicence | ||
|
||
beforeEach(async () => { | ||
testLicence = await LicenceHelper.add() | ||
|
||
const { licenceId } = testLicence | ||
testRecord = await LicenceVersionHelper.add({ licenceId }) | ||
}) | ||
|
||
it('can successfully run a related query', async () => { | ||
const query = await LicenceVersionModel.query() | ||
.innerJoinRelated('licence') | ||
|
||
expect(query).to.exist() | ||
}) | ||
|
||
it('can eager load the licence', async () => { | ||
const result = await LicenceVersionModel.query() | ||
.findById(testRecord.licenceVersionId) | ||
.withGraphFetched('licence') | ||
|
||
expect(result).to.be.instanceOf(LicenceVersionModel) | ||
expect(result.licenceVersionId).to.equal(testRecord.licenceVersionId) | ||
|
||
expect(result.licence).to.be.an.instanceOf(LicenceModel) | ||
expect(result.licence).to.equal(testLicence) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module LicenceVersionHelper | ||
*/ | ||
|
||
const { generateUUID, timestampForPostgres } = require('../../../../app/lib/general.lib.js') | ||
const { randomInteger } = require('../general.helper.js') | ||
const LicenceVersionModel = require('../../../../app/models/water/licence-version.model.js') | ||
|
||
/** | ||
* Add a new licence version | ||
* | ||
* If no `data` is provided, default values will be used. These are | ||
* | ||
* - `licenceId` - [random UUID] | ||
* - `issue` - 1 | ||
* - `increment` - 0 | ||
* - `status` - 'current' | ||
* - `startDate` - new Date('2022-01-01') | ||
* - `externalId` - [randomly generated - 9:99999:1:0] | ||
* - `createdAt` - new Date() | ||
* - `updatedAt` - new Date() | ||
* | ||
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database | ||
* | ||
* @returns {module:LicenceVersionModel} The instance of the newly created record | ||
*/ | ||
async function add (data = {}) { | ||
const insertData = defaults(data) | ||
|
||
return LicenceVersionModel.query() | ||
.insert({ ...insertData }) | ||
.returning('*') | ||
} | ||
|
||
/** | ||
* Returns the defaults used | ||
* | ||
* It will override or append to them any data provided. Mainly used by the `add()` method, we make it available | ||
* for use in tests to avoid having to duplicate values. | ||
* | ||
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database | ||
*/ | ||
function defaults (data = {}) { | ||
const timestamp = timestampForPostgres() | ||
|
||
const defaults = { | ||
licenceId: generateUUID(), | ||
issue: 1, | ||
increment: 0, | ||
status: 'current', | ||
startDate: new Date('2022-01-01'), | ||
externalId: `9:${randomInteger(10000, 99999)}:1:0`, | ||
createdAt: timestamp, | ||
updatedAt: timestamp | ||
} | ||
|
||
return { | ||
...defaults, | ||
...data | ||
} | ||
} | ||
|
||
module.exports = { | ||
add, | ||
defaults | ||
} |