Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create model for purposes_uses table #335

Merged
merged 14 commits into from
Aug 4, 2023
8 changes: 8 additions & 0 deletions app/models/water/charge-purpose.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class ChargePurposeModel extends WaterBaseModel {
from: 'chargePurposes.chargeElementId',
to: 'chargeElements.chargeElementId'
}
},
purposesUse: {
relation: Model.BelongsToOneRelation,
modelClass: 'purposes-use.model',
join: {
from: 'chargePurposes.purposeUseId',
to: 'purposesUses.purposeUseId'
}
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions app/models/water/purposes-use.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

/**
* Model for purposesUses
* @module PurposesUseModel
*/

const { Model } = require('objection')

const WaterBaseModel = require('./water-base.model.js')

class PurposesUseModel extends WaterBaseModel {
static get tableName () {
return 'purposesUses'
}

static get idColumn () {
return 'purposeUseId'
}

static get translations () {
return [
{ database: 'dateCreated', model: 'createdAt' },
{ database: 'dateUpdated', model: 'updatedAt' }
]
}

static get relationMappings () {
return {
chargePurpose: {
relation: Model.BelongsToOneRelation,
modelClass: 'charge-purpose.model',
join: {
from: 'purposesUses.purposeUseId',
to: 'chargePurposes.purposeUseId'
}
}
}
}
}

module.exports = PurposesUseModel
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ exports.up = function (knex) {
table.string('return_id').primary()

// Data
table.string('regime')
table.string('licence_type')
table.string('licence_ref')
table.date('start_date')
table.date('end_date')
table.string('returns_frequency')
table.string('status')
table.string('regime').notNullable()
table.string('licence_type').notNullable()
table.string('licence_ref').notNullable()
table.date('start_date').notNullable()
table.date('end_date').notNullable()
table.string('returns_frequency').notNullable()
table.string('status').notNullable()
table.string('source')
table.jsonb('metadata')
table.date('received_date')
table.string('return_requirement')
table.date('due_date')
table.boolean('under_query')
table.string('return_requirement').notNullable()
table.date('due_date').notNullable()
table.boolean('under_query').notNullable().defaultTo(false)
table.string('under_query_comment')
table.boolean('is_test')
table.boolean('is_test').notNullable().defaultTo(false)
table.uuid('return_cycle_id')

// Legacy timestamps
Expand Down
31 changes: 31 additions & 0 deletions db/migrations/20230803160405_create-water-purposes-uses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const tableName = 'purposes_uses'

exports.up = function (knex) {
return knex
.schema
.withSchema('water')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('purpose_use_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.string('legacy_id').notNullable().unique()
table.string('description').notNullable()
table.string('loss_factor').notNullable()
table.boolean('is_two_part_tariff').notNullable().defaultTo(false)
table.boolean('is_test').notNullable().defaultTo(false)

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
32 changes: 32 additions & 0 deletions test/models/water/charge-purpose.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const ChargeElementHelper = require('../../support/helpers/water/charge-element.
const ChargeElementModel = require('../../../app/models/water/charge-element.model.js')
const ChargePurposeHelper = require('../../support/helpers/water/charge-purpose.helper.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const PurposesUseModel = require('../../../app/models/water/purposes-use.model.js')
const PurposesUseHelper = require('../../support/helpers/water/purposes-use.helper.js')

// Thing under test
const ChargePurposeModel = require('../../../app/models/water/charge-purpose.model.js')
Expand Down Expand Up @@ -64,5 +66,35 @@ describe('Charge Purpose model', () => {
expect(result.chargeElement).to.equal(testChargeElement)
})
})

describe('when linking to purposes use', () => {
let testPurposesUse

beforeEach(async () => {
testPurposesUse = await PurposesUseHelper.add()

const { purposeUseId } = testPurposesUse
testRecord = await ChargePurposeHelper.add({ purposeUseId })
})

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

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

it('can eager load the purposes use', async () => {
const result = await ChargePurposeModel.query()
.findById(testRecord.chargePurposeId)
.withGraphFetched('purposesUse')

expect(result).to.be.instanceOf(ChargePurposeModel)
expect(result.chargePurposeId).to.equal(testRecord.chargePurposeId)

expect(result.purposesUse).to.be.an.instanceOf(PurposesUseModel)
expect(result.purposesUse).to.equal(testPurposesUse)
})
})
})
})
67 changes: 67 additions & 0 deletions test/models/water/purposes-use.model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'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 ChargePurposeHelper = require('../../support/helpers/water/charge-purpose.helper.js')
const ChargePurposeModel = require('../../../app/models/water/charge-purpose.model.js')
const DatabaseHelper = require('../../support/helpers/database.helper.js')
const PurposesUseHelper = require('../../support/helpers/water/purposes-use.helper.js')

// Thing under test
const PurposesUseModel = require('../../../app/models/water/purposes-use.model.js')

describe('Purposes Use model', () => {
let testRecord

beforeEach(async () => {
await DatabaseHelper.clean()

testRecord = await PurposesUseHelper.add()
})

describe('Basic query', () => {
it('can successfully run a basic query', async () => {
const result = await PurposesUseModel.query().findById(testRecord.purposeUseId)

expect(result).to.be.an.instanceOf(PurposesUseModel)
expect(result.purposeUseId).to.equal(testRecord.purposeUseId)
})
})

describe('Relationships', () => {
describe('when linking to charge purpose', () => {
let testChargePurpose

beforeEach(async () => {
const { purposeUseId } = testRecord

testChargePurpose = await ChargePurposeHelper.add({ purposeUseId })
})

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

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

it('can eager load the charge purpose', async () => {
const result = await PurposesUseModel.query()
.findById(testRecord.purposeUseId)
.withGraphFetched('chargePurpose')

expect(result).to.be.instanceOf(PurposesUseModel)
expect(result.purposeUseId).to.equal(testRecord.purposeUseId)

expect(result.chargePurpose).to.be.an.instanceOf(ChargePurposeModel)
expect(result.chargePurpose).to.equal(testChargePurpose)
})
})
})
})
1 change: 1 addition & 0 deletions test/support/helpers/returns/return.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function defaults (data = {}) {
returnId: 'v1:1:9/99/99/99/9999:10021668:2022-04-01:2023-03-31',
regime: 'water',
licenceType: 'abstraction',
licenceRef: '9/99/99/99/9999',
startDate: '2022-04-01',
endDate: '2023-03-31',
returnsFrequency: 'month',
Expand Down
56 changes: 56 additions & 0 deletions test/support/helpers/water/purposes-use.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'

/**
* @module PurposesUseHelper
*/

const PurposesUseModel = require('../../../../app/models/water/purposes-use.model.js')

/**
* Add a new purposes use
*
* If no `data` is provided, default values will be used. These are
*
* - `legacyId` - 420
* - `description` - Spray Irrigation - Storage
* - `lossFactor` - high
* - `isTwoPartTariff` - true
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
*
* @returns {module:PurposesUseModel} The instance of the newly created record
*/
function add (data = {}) {
const insertData = defaults(data)

return PurposesUseModel.query()
.insert({ ...insertData })
.returning('*')
}

/**
* Returns the defaults used when creating a new purposes use
*
* 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 defaults = {
legacyId: '420',
description: 'Spray Irrigation - Storage',
lossFactor: 'high',
isTwoPartTariff: true
}

return {
...defaults,
...data
}
}

module.exports = {
add,
defaults
}