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

Make test helpers consistent #198

Merged
merged 7 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/models/water/billing-batch.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Billing Batch model', () => {

testBillingInvoices = []
for (let i = 0; i < 2; i++) {
const billingInvoice = await BillingInvoiceHelper.add({ financialYearEnding: 2023 }, { billingBatchId })
const billingInvoice = await BillingInvoiceHelper.add({ financialYearEnding: 2023, billingBatchId })
testBillingInvoices.push(billingInvoice)
}
})
Expand Down
4 changes: 2 additions & 2 deletions test/models/water/billing-invoice-licence.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Billing Invoice Licence model', () => {
testBillingInvoice = await BillingInvoiceHelper.add()

const { billingInvoiceId } = testBillingInvoice
testRecord = await BillingInvoiceLicenceHelper.add({}, {}, { billingInvoiceId })
testRecord = await BillingInvoiceLicenceHelper.add({ billingInvoiceId })
})

it('can successfully run a related query', async () => {
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Billing Invoice Licence model', () => {
testLicence = await LicenceHelper.add()

const { licenceId } = testLicence
testRecord = await BillingInvoiceLicenceHelper.add({}, { licenceId })
testRecord = await BillingInvoiceLicenceHelper.add({ licenceId })
})

it('can successfully run a related query', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/models/water/billing-invoice.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Billing Invoice model', () => {

beforeEach(async () => {
testBillingBatch = await BillingBatchHelper.add()
testRecord = await BillingInvoiceHelper.add({}, { billingBatchId: testBillingBatch.billingBatchId })
testRecord = await BillingInvoiceHelper.add({ billingBatchId: testBillingBatch.billingBatchId })
})

it('can successfully run a related query', async () => {
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('Billing Invoice model', () => {

testBillingInvoiceLicences = []
for (let i = 0; i < 2; i++) {
const billingInvoiceLicence = await BillingInvoiceLicenceHelper.add({}, {}, { billingInvoiceId })
const billingInvoiceLicence = await BillingInvoiceLicenceHelper.add({ billingInvoiceId })
testBillingInvoiceLicences.push(billingInvoiceLicence)
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/models/water/charge-version.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('ChargeVersion model', () => {
testLicence = await LicenceHelper.add()

const { licenceId } = testLicence
testRecord = await ChargeVersionHelper.add({}, { licenceId })
testRecord = await ChargeVersionHelper.add({ licenceId })
})

it('can successfully run a related query', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/models/water/licence.model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Licence model', () => {

testChargeVersions = []
for (let i = 0; i < 2; i++) {
const chargeVersion = await ChargeVersionHelper.add({ licenceRef }, { licenceId })
const chargeVersion = await ChargeVersionHelper.add({ licenceRef, licenceId })
testChargeVersions.push(chargeVersion)
}
})
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('Licence model', () => {

testBillingInvoiceLicences = []
for (let i = 0; i < 2; i++) {
const billingInvoiceLicence = await BillingInvoiceLicenceHelper.add({ licenceRef }, { licenceId })
const billingInvoiceLicence = await BillingInvoiceLicenceHelper.add({ licenceRef, licenceId })
testBillingInvoiceLicences.push(billingInvoiceLicence)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,17 @@ describe('Fetch Charge Versions service', () => {

// This creates a 'current' SROC charge version
const srocChargeVersion = await ChargeVersionHelper.add(
{ changeReasonId: changeReason.changeReasonId },
{ licenceId }
{ changeReasonId: changeReason.changeReasonId, licenceId }
)

// This creates a 'superseded' SROC charge version
const srocSupersededChargeVersion = await ChargeVersionHelper.add(
{ changeReasonId: changeReason.changeReasonId, status: 'superseded' },
{ licenceId }
{ changeReasonId: changeReason.changeReasonId, status: 'superseded', licenceId }
)

// This creates an ALCS (presroc) charge version
const alcsChargeVersion = await ChargeVersionHelper.add(
{ scheme: 'alcs' },
{ licenceId }
{ scheme: 'alcs', licenceId }
)

testRecords = [srocChargeVersion, srocSupersededChargeVersion, alcsChargeVersion]
Expand Down Expand Up @@ -171,10 +168,13 @@ describe('Fetch Charge Versions service', () => {
endDate: new Date('2023-03-31')
}

const srocDraftChargeVersion = await ChargeVersionHelper.add(
{ status: 'draft' },
{ regionId, isWaterUndertaker: true, includeInSrocSupplementaryBilling: true }
)
const { licenceId } = await LicenceHelper.add({
regionId,
isWaterUndertaker: true,
includeInSrocSupplementaryBilling: true
})

const srocDraftChargeVersion = await ChargeVersionHelper.add({ status: 'draft', licenceId })
testRecords = [srocDraftChargeVersion]
})

Expand All @@ -192,11 +192,13 @@ describe('Fetch Charge Versions service', () => {
endDate: new Date('2023-03-31')
}

const { licenceId } = await LicenceHelper.add({
regionId,
includeInSupplementaryBilling: true
})

// This creates an ALCS (presroc) charge version linked to a licence marked for supplementary billing
const alcsChargeVersion = await ChargeVersionHelper.add(
{ scheme: 'alcs' },
{ includeInSupplementaryBilling: 'yes' }
)
const alcsChargeVersion = await ChargeVersionHelper.add({ scheme: 'alcs', licenceId })
testRecords = [alcsChargeVersion]
})

Expand All @@ -214,11 +216,13 @@ describe('Fetch Charge Versions service', () => {
endDate: new Date('2023-03-31')
}

const { licenceId } = await LicenceHelper.add({
regionId,
includeInSrocSupplementaryBilling: true
})

// This creates a charge version with no `invoiceAccountId`
const nullInvoiceAccountIdChargeVersion = await ChargeVersionHelper.add(
{ invoiceAccountId: null },
{ regionId, includeInSrocSupplementaryBilling: true }
)
const nullInvoiceAccountIdChargeVersion = await ChargeVersionHelper.add({ invoiceAccountId: null, licenceId })
testRecords = [nullInvoiceAccountIdChargeVersion]
})

Expand All @@ -237,11 +241,15 @@ describe('Fetch Charge Versions service', () => {
endDate: new Date('2023-03-31')
}

const { licenceId } = await LicenceHelper.add({
regionId,
includeInSrocSupplementaryBilling: true
})

// This creates an SROC charge version with a start date before the billing period. This would have been
// picked up by a previous bill run
const alcsChargeVersion = await ChargeVersionHelper.add(
{ startDate: new Date(2022, 2, 31) }, // 2022-03-01 - Months are zero indexed :-)
{ includeInSrocSupplementaryBilling: true }
{ startDate: new Date(2022, 2, 31), licenceId } // 2022-03-01 - Months are zero indexed :-)
)
testRecords = [alcsChargeVersion]
})
Expand All @@ -260,11 +268,15 @@ describe('Fetch Charge Versions service', () => {
endDate: new Date('2023-03-31')
}

const { licenceId } = await LicenceHelper.add({
regionId,
includeInSrocSupplementaryBilling: true
})

// This creates an SROC charge version with a start date after the billing period. This will be picked in
// next years bill runs
const alcsChargeVersion = await ChargeVersionHelper.add(
{ startDate: new Date(2023, 3, 1) }, // 2023-04-01 - Months are zero indexed :-)
{ includeInSrocSupplementaryBilling: true }
{ startDate: new Date(2023, 3, 1), licenceId } // 2023-04-01 - Months are zero indexed :-)
)
testRecords = [alcsChargeVersion]
})
Expand All @@ -284,14 +296,13 @@ describe('Fetch Charge Versions service', () => {
endDate: new Date('2023-03-31')
}

const { licenceId } = await LicenceHelper.add({
regionId: 'e117b501-e3c1-4337-ad35-21c60ed9ad73',
includeInSrocSupplementaryBilling: true
})

// This creates an SROC charge version linked to a licence with an different region than selected
const otherRegionChargeVersion = await ChargeVersionHelper.add(
{},
{
includeInSrocSupplementaryBilling: true,
regionId: 'e117b501-e3c1-4337-ad35-21c60ed9ad73'
}
)
const otherRegionChargeVersion = await ChargeVersionHelper.add({ licenceId })
testRecords = [otherRegionChargeVersion]
})

Expand All @@ -309,14 +320,13 @@ describe('Fetch Charge Versions service', () => {
endDate: new Date('2023-03-31')
}

const chargeVersion = await ChargeVersionHelper.add(
{},
{
includeInSrocSupplementaryBilling: true,
regionId
}
)
await ChargeVersionWorkflowHelper.add({ licenceId: chargeVersion.licenceId })
const { licenceId } = await LicenceHelper.add({
regionId,
includeInSrocSupplementaryBilling: true
})

const chargeVersion = await ChargeVersionHelper.add({ licenceId })
await ChargeVersionWorkflowHelper.add({ licenceId })

testRecords = [chargeVersion]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,8 @@ describe('Fetch Previous Billing Transactions service', () => {

async function _createBillingBatchInvoiceAndLicence (invoiceAccountId, licenceId) {
const { billingBatchId } = await BillingBatchHelper.add({ status: 'sent' })
const { billingInvoiceId } = await BillingInvoiceHelper.add({ invoiceAccountId }, { billingBatchId })
const { billingInvoiceLicenceId } = await BillingInvoiceLicenceHelper.add(
{},
{ licenceId },
{ billingInvoiceId }
)
const { billingInvoiceId } = await BillingInvoiceHelper.add({ invoiceAccountId, billingBatchId })
const { billingInvoiceLicenceId } = await BillingInvoiceLicenceHelper.add({ billingInvoiceId, licenceId })

return billingInvoiceLicenceId
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ describe('Process billing batch service', () => {
{
changeReasonId: changeReason.changeReasonId,
invoiceAccountId: invoiceAccount.invoiceAccountId,
startDate: new Date(2022, 7, 1, 9)
},
{
startDate: new Date(2022, 7, 1, 9),
licenceId: licence.licenceId
}
)
Expand Down Expand Up @@ -171,10 +169,11 @@ describe('Process billing batch service', () => {

describe('because generating the calculated transactions fails', () => {
beforeEach(async () => {
const { chargeVersionId } = await ChargeVersionHelper.add(
{ changeReasonId: changeReason.changeReasonId, invoiceAccountId: invoiceAccount.invoiceAccountId },
{ licenceId: licence.licenceId }
)
const { chargeVersionId } = await ChargeVersionHelper.add({
changeReasonId: changeReason.changeReasonId,
invoiceAccountId: invoiceAccount.invoiceAccountId,
licenceId: licence.licenceId
})
const { chargeElementId } = await ChargeElementHelper.add(
{ billingChargeCategoryId: billingChargeCategory.billingChargeCategoryId, chargeVersionId }
)
Expand All @@ -194,10 +193,11 @@ describe('Process billing batch service', () => {

describe('because creating the billing transactions', () => {
beforeEach(async () => {
const { chargeVersionId } = await ChargeVersionHelper.add(
{ changeReasonId: changeReason.changeReasonId, invoiceAccountId: invoiceAccount.invoiceAccountId },
{ licenceId: licence.licenceId }
)
const { chargeVersionId } = await ChargeVersionHelper.add({
changeReasonId: changeReason.changeReasonId,
invoiceAccountId: invoiceAccount.invoiceAccountId,
licenceId: licence.licenceId
})
const { chargeElementId } = await ChargeElementHelper.add(
{ billingChargeCategoryId: billingChargeCategory.billingChargeCategoryId, chargeVersionId }
)
Expand Down
43 changes: 7 additions & 36 deletions test/support/helpers/water/billing-invoice-licence.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,29 @@
* @module BillingInvoiceLicenceHelper
*/

const BillingInvoiceHelper = require('./billing-invoice.helper.js')
const BillingInvoiceLicenceModel = require('../../../../app/models/water/billing-invoice-licence.model.js')
const LicenceHelper = require('./licence.helper.js')

/**
* Add a new billing invoice licence
*
* A billing invoice licence is always linked to a licence and a billing invoice. So, creating a billing invoice licence will automatically
* create a new licence, a new billing invoice and handle linking them together by `licenceId` & `billingInvoiceId`.
*
* If no `data` is provided, default values will be used. These are
*
* - `billingInvoiceId` - 45f08fe4-5b8b-4cf7-aaf0-5a07534e1357
* - `licenceRef` - 01/123
*
* See `LicenceHelper` for the licence defaults
* See `BillingInvoiceHelper` for the billing invoice defaults
* - `licenceId` - 2eb0623e-30c6-4bf4-9598-2d60a8366c7d
*
* @param {Object} [data] Any data you want to use instead of the defaults used here or in the database
* @param {Object} [licence] Any licence data you want to use instead of the defaults used here or in the database
* @param {Object} [billingInvoice] Any billing invoice data you want to use instead of the defaults used here or in the database
*
* @returns {module:BillingInvoiceLicenceModel} The instance of the newly created record
*/
async function add (data = {}, licence = {}, billingInvoice = {}) {
const licenceId = await _licenceId(licence)
const billingInvoiceId = await _billingInvoiceId(billingInvoice)

const insertData = defaults({ ...data, licenceId, billingInvoiceId })
async function add (data = {}) {
const insertData = defaults(data)

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

async function _licenceId (providedLicence) {
if (providedLicence?.licenceId) {
return providedLicence.licenceId
}

const licence = await LicenceHelper.add(providedLicence)

return licence.licenceId
}

async function _billingInvoiceId (providedBillingInvoice) {
if (providedBillingInvoice?.billingInvoiceId) {
return providedBillingInvoice.billingInvoiceId
}

const billingInvoice = await BillingInvoiceHelper.add(providedBillingInvoice)

return billingInvoice.billingInvoiceId
}

/**
* Returns the defaults used when creating a new billing invoice licence
*
Expand All @@ -68,7 +37,9 @@ async function _billingInvoiceId (providedBillingInvoice) {
*/
function defaults (data = {}) {
const defaults = {
licenceRef: '01/123'
billingInvoiceId: '45f08fe4-5b8b-4cf7-aaf0-5a07534e1357',
licenceRef: '01/123',
licenceId: '2eb0623e-30c6-4bf4-9598-2d60a8366c7d'
}

return {
Expand Down
Loading