-
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.
Build models for
returns
based on new Views (#533)
DEFRA/water-abstraction-team#106 As part of the work we have been doing to get migrations working in [water-abstraction-system](https://github.com/DEFRA/water-abstraction-system) we are going to be creating all our new tables in a single schema `public`. We have also decided that when there is a legacy table that we are still going to need for SROC. We are going to create a View of that table in the `public` schema and correct any issues with naming conventions, unused fields etc in the view. The first of these views that represent the old `returns` tables have been created in #531 This PR will create the models, helpers and unit tests required to be able to start to use these new views.
- Loading branch information
Showing
13 changed files
with
660 additions
and
2 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,38 @@ | ||
'use strict' | ||
|
||
/** | ||
* Model for return_logs | ||
* @module ReturnLogModel | ||
*/ | ||
|
||
const { Model } = require('objection') | ||
|
||
const BaseModel = require('./base.model.js') | ||
|
||
class ReturnLogModel extends BaseModel { | ||
static get tableName () { | ||
return 'returnLogs' | ||
} | ||
|
||
// Defining which fields contain json allows us to insert an object without needing to stringify it first | ||
static get jsonAttributes () { | ||
return [ | ||
'metadata' | ||
] | ||
} | ||
|
||
static get relationMappings () { | ||
return { | ||
returnSubmissions: { | ||
relation: Model.HasManyRelation, | ||
modelClass: 'return-submission.model', | ||
join: { | ||
from: 'returnLogs.id', | ||
to: 'returnSubmissions.returnLogId' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = ReturnLogModel |
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,31 @@ | ||
'use strict' | ||
|
||
/** | ||
* Model for return_submission_lines | ||
* @module ReturnSubmissionLineModel | ||
*/ | ||
|
||
const { Model } = require('objection') | ||
|
||
const BaseModel = require('./base.model.js') | ||
|
||
class ReturnSubmissionLineModel extends BaseModel { | ||
static get tableName () { | ||
return 'returnSubmissionLines' | ||
} | ||
|
||
static get relationMappings () { | ||
return { | ||
returnSubmission: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'return-submission.model', | ||
join: { | ||
from: 'returnSubmissionLines.returnSubmissionId', | ||
to: 'returnSubmissions.id' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = ReturnSubmissionLineModel |
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,46 @@ | ||
'use strict' | ||
|
||
/** | ||
* Model for return_submissions | ||
* @module ReturnSubmissionModel | ||
*/ | ||
|
||
const { Model } = require('objection') | ||
|
||
const BaseModel = require('./base.model.js') | ||
|
||
class ReturnSubmissionModel extends BaseModel { | ||
static get tableName () { | ||
return 'returnSubmissions' | ||
} | ||
|
||
// Defining which fields contain json allows us to insert an object without needing to stringify it first | ||
static get jsonAttributes () { | ||
return [ | ||
'metadata' | ||
] | ||
} | ||
|
||
static get relationMappings () { | ||
return { | ||
returnLog: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: 'return-log.model', | ||
join: { | ||
from: 'returnSubmissions.returnLogId', | ||
to: 'returnLogs.id' | ||
} | ||
}, | ||
returnSubmissionLines: { | ||
relation: Model.HasManyRelation, | ||
modelClass: 'return-submission-line.model', | ||
join: { | ||
from: 'returnSubmissions.id', | ||
to: 'returnSubmissionLines.returnSubmissionId' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = ReturnSubmissionModel |
23 changes: 23 additions & 0 deletions
23
db/migrations/legacy/20231122181918_add-defaults-to-returns-table.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,23 @@ | ||
'use strict' | ||
|
||
const tableName = 'returns' | ||
|
||
exports.up = function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('returns') | ||
.alterTable(tableName, (table) => { | ||
table.string('regime').notNullable().defaultTo('water').alter() | ||
table.string('licence_type').notNullable().defaultTo('abstraction').alter() | ||
}) | ||
} | ||
|
||
exports.down = async function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('returns') | ||
.alterTable(tableName, (table) => { | ||
table.string('regime').notNullable().alter() | ||
table.string('licence_type').notNullable().alter() | ||
}) | ||
} |
23 changes: 23 additions & 0 deletions
23
db/migrations/legacy/20231122183613_add-defaults-to-lines-table.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,23 @@ | ||
'use strict' | ||
|
||
const tableName = 'lines' | ||
|
||
exports.up = function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('returns') | ||
.alterTable(tableName, (table) => { | ||
table.string('substance').notNullable().defaultTo('water').alter() | ||
table.string('unit').notNullable().defaultTo('m³').alter() | ||
}) | ||
} | ||
|
||
exports.down = async function (knex) { | ||
return knex | ||
.schema | ||
.withSchema('returns') | ||
.alterTable(tableName, (table) => { | ||
table.string('substance').notNullable().alter() | ||
table.string('unit').notNullable().alter() | ||
}) | ||
} |
2 changes: 1 addition & 1 deletion
2
...c/20231120122556_create-form-logs-view.js → ...20231120122556_create-return-logs-view.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
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,74 @@ | ||
'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 ReturnLogHelper = require('../support/helpers/return-log.helper.js') | ||
const ReturnSubmissionHelper = require('../support/helpers/return-submission.helper.js') | ||
const ReturnSubmissionModel = require('../../app/models/return-submission.model.js') | ||
|
||
// Thing under test | ||
const ReturnLogModel = require('../../app/models/return-log.model.js') | ||
|
||
describe('Return Log model', () => { | ||
let testRecord | ||
|
||
beforeEach(async () => { | ||
await DatabaseHelper.clean() | ||
|
||
testRecord = await ReturnLogHelper.add() | ||
}) | ||
|
||
describe('Basic query', () => { | ||
it('can successfully run a basic query', async () => { | ||
const result = await ReturnLogModel.query().findById(testRecord.id) | ||
|
||
expect(result).to.be.an.instanceOf(ReturnLogModel) | ||
expect(result.id).to.equal(testRecord.id) | ||
}) | ||
}) | ||
|
||
describe('Relationships', () => { | ||
describe('when linking to return submissions', () => { | ||
let returnSubmissions | ||
|
||
beforeEach(async () => { | ||
const { id: returnLogId } = testRecord | ||
|
||
returnSubmissions = [] | ||
for (let i = 0; i < 2; i++) { | ||
const version = i | ||
const returnSubmission = await ReturnSubmissionHelper.add({ returnLogId, version }) | ||
returnSubmissions.push(returnSubmission) | ||
} | ||
}) | ||
|
||
it('can successfully run a related query', async () => { | ||
const query = await ReturnLogModel.query() | ||
.innerJoinRelated('returnSubmissions') | ||
|
||
expect(query).to.exist() | ||
}) | ||
|
||
it('can eager load the return submissions', async () => { | ||
const result = await ReturnLogModel.query() | ||
.findById(testRecord.id) | ||
.withGraphFetched('returnSubmissions') | ||
|
||
expect(result).to.be.instanceOf(ReturnLogModel) | ||
expect(result.id).to.equal(testRecord.id) | ||
|
||
expect(result.returnSubmissions).to.be.an.array() | ||
expect(result.returnSubmissions[0]).to.be.an.instanceOf(ReturnSubmissionModel) | ||
expect(result.returnSubmissions).to.include(returnSubmissions[0]) | ||
expect(result.returnSubmissions).to.include(returnSubmissions[1]) | ||
}) | ||
}) | ||
}) | ||
}) |
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 ReturnSubmissionLineHelper = require('../support/helpers/return-submission-line.helper.js') | ||
const ReturnSubmissionHelper = require('../support/helpers/return-submission.helper.js') | ||
const ReturnSubmissionModel = require('../../app/models/return-submission.model.js') | ||
|
||
// Thing under test | ||
const ReturnSubmissionLineModel = require('../../app/models/return-submission-line.model.js') | ||
|
||
describe('Return Submission Line model', () => { | ||
let testRecord | ||
|
||
beforeEach(async () => { | ||
await DatabaseHelper.clean() | ||
}) | ||
|
||
describe('Basic query', () => { | ||
beforeEach(async () => { | ||
testRecord = await ReturnSubmissionLineHelper.add() | ||
}) | ||
|
||
it('can successfully run a basic query', async () => { | ||
const result = await ReturnSubmissionLineModel.query().findById(testRecord.id) | ||
|
||
expect(result).to.be.an.instanceOf(ReturnSubmissionLineModel) | ||
expect(result.id).to.equal(testRecord.id) | ||
}) | ||
}) | ||
|
||
describe('Relationships', () => { | ||
describe('when linking to return submission', () => { | ||
let testReturnSubmission | ||
|
||
beforeEach(async () => { | ||
testReturnSubmission = await ReturnSubmissionHelper.add() | ||
testRecord = await ReturnSubmissionLineHelper.add({ returnSubmissionId: testReturnSubmission.id }) | ||
}) | ||
|
||
it('can successfully run a related query', async () => { | ||
const query = await ReturnSubmissionLineModel.query() | ||
.innerJoinRelated('returnSubmission') | ||
|
||
expect(query).to.exist() | ||
}) | ||
|
||
it('can eager load the return submission', async () => { | ||
const result = await ReturnSubmissionLineModel.query() | ||
.findById(testRecord.id) | ||
.withGraphFetched('returnSubmission') | ||
|
||
expect(result).to.be.instanceOf(ReturnSubmissionLineModel) | ||
expect(result.id).to.equal(testRecord.id) | ||
|
||
expect(result.returnSubmission).to.be.an.instanceOf(ReturnSubmissionModel) | ||
expect(result.returnSubmission).to.equal(testReturnSubmission) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.