Skip to content

Commit

Permalink
Create seeder for charge_categories (#1312)
Browse files Browse the repository at this point in the history
Currently, with the way data is being inserted into `charge_categories` using the helper to create records during unit testing. We are getting intermittent failures when running the unit tests when the charge `reference` gets duplicated due to its lack of randomness.

We have therefore decided that since the data in `charge_categories` is reference data we shouldn't be creating new charge category records each time a unit test requires one. Instead, we should just be seeding the data once and then writing the unit tests to use this seeded data. The bulk of this work has been done for other tables in this PR #1230

This PR will create the functions required to seed the `charge_categories` and then fix any code affected by this change.
  • Loading branch information
Jozzey authored Sep 11, 2024
1 parent cc39aae commit 5c07d46
Show file tree
Hide file tree
Showing 23 changed files with 2,723 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
ignorePatterns: ['docs/*'],
overrides: [
{
files: ['*.controller.js'],
files: ['*.controller.js', '*.seed.js'],
rules: {
'jsdoc/require-jsdoc': 'off'
}
Expand Down
2 changes: 0 additions & 2 deletions app/services/data/load/load.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const BillHelper = require('../../../../test/support/helpers/bill.helper.js')
const BillingAccountAddressHelper = require('../../../../test/support/helpers/billing-account-address.helper.js')
const BillingAccountHelper = require('../../../../test/support/helpers/billing-account.helper.js')
const ChangeReasonHelper = require('../../../../test/support/helpers/change-reason.helper.js')
const ChargeCategoryHelper = require('../../../../test/support/helpers/charge-category.helper.js')
const ChargeElementHelper = require('../../../../test/support/helpers/charge-element.helper.js')
const ChargeReferenceHelper = require('../../../../test/support/helpers/charge-reference.helper.js')
const ChargeVersionHelper = require('../../../../test/support/helpers/charge-version.helper.js')
Expand Down Expand Up @@ -78,7 +77,6 @@ const LOAD_HELPERS = {
billingAccountAddresses: { helper: BillingAccountAddressHelper, test: true, legacy: { schema: 'crm_v2', table: 'invoice_account_addresses', id: 'invoice_account_address_id' } },
billingAccounts: { helper: BillingAccountHelper, test: true, legacy: { schema: 'crm_v2', table: 'invoice_accounts', id: 'invoice_account_id' } },
changeReasons: { helper: ChangeReasonHelper, test: false },
chargeCategories: { helper: ChargeCategoryHelper, test: true, legacy: { schema: 'water', table: 'billing_charge_categories', id: 'billing_charge_category_id' } },
chargeElements: { helper: ChargeElementHelper, test: true, legacy: { schema: 'water', table: 'charge_purposes', id: 'charge_purpose_id' } },
chargeReferences: { helper: ChargeReferenceHelper, test: true, legacy: { schema: 'water', table: 'charge_elements', id: 'charge_element_id' } },
chargeVersions: { helper: ChargeVersionHelper, test: true, legacy: { schema: 'water', table: 'charge_versions', id: 'charge_version_id' } },
Expand Down
33 changes: 33 additions & 0 deletions db/seeds/13-charge-categories.seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const { timestampForPostgres } = require('../../app/lib/general.lib.js')
const { data: chargeCategories } = require('./data/charge-categories.js')
const ChargeCategoryModel = require('../../app/models/charge-category.model.js')

async function seed () {
for (const chargeCategory of chargeCategories) {
await _upsert(chargeCategory)
}
}

async function _upsert (chargeCategory) {
return ChargeCategoryModel.query()
.insert({ ...chargeCategory, createdAt: timestampForPostgres(), updatedAt: timestampForPostgres() })
.onConflict('reference')
.merge([
'description',
'lossFactor',
'maxVolume',
'minVolume',
'modelTier',
'restrictedSource',
'shortDescription',
'subsistenceCharge',
'tidal',
'updatedAt'
])
}

module.exports = {
seed
}
Loading

0 comments on commit 5c07d46

Please sign in to comment.