From e8619c5e7dc5bd6429ee68fd4475f4b7cdfb4a89 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Thu, 8 Aug 2024 00:04:40 +0100 Subject: [PATCH] Restructure 'seeding' to be Knex based (#1230) https://eaflood.atlassian.net/browse/WATER-4575 We have been working on replicating how the [water-abstraction-import](https://github.com/DEFRA/water-abstraction-import) service imports a licence. See [Import Licence versions](https://github.com/DEFRA/water-abstraction-system/pull/1195) for example. This is in readiness for moving to ReSP managing licences rather than NALD. What it exposed was that our test data setup needed expanding. The import relies on 'real' reference data being present. So, it makes testing easier if this is the case in our test database when writing unit tests. We started with [create wrapper for licence-version-purpose-condition table](https://github.com/DEFRA/water-abstraction-system/pull/1181) which added the ability to seed licence-purpose condition types. This was then expanded by [Add new test reference data seeders](https://github.com/DEFRA/water-abstraction-system/pull/1206) which added some more reference data, for example, purposes. The issue we've had is that the existing changes are straddling two different mechanisms. We've included them as a seed file in our [Knex seeding capabilities](https://knexjs.org/guide/migrations.html#seed-files). This is reference data, so why not seed it?! And we are seeding it whenever we perform a database clean. Our intent is to have real reference data when unit testing, and until we've removed calls to `DatabaseSupport.clean()` from all our tests, we need to do this each time. That has led to the logic and data itself living in the `test/` folder. There is a conceptual problem with this: something we depend on 'for real' lives in our `test/` folder. Plus, the functionality is split into 2 places. It also means our `test/support/seeders` have a mix of types. Original 'unit test seeders' that were intended to help move bulky test setup away from within the test itself, with 'knex seeders', things we'll also call when we run `npm run seed` against the non-test DB. So, on reflection, we felt the need to carry out a little restructuring. _All_ the 'seeding' functionality is moved to `db/seeds` but with the ability to still call it from `clean()` until such time as we have updated all tests and can drop the command. While we're doing this housekeeping, we also update our existing user seed to match the conventions that have been introduced while also re-implementing some existing ones, for example, preferring **Objection.js** to raw SQL queries. The end result is that we can seed both the real and test databases with reference data, even when a database already has existing records, and this functionality will live in one place and be built on **Knex**. But there's more! Having made these changes to ensure both the real and test databases are correctly populated with reference data, it no longer made any sense to be adding new reference records within our tests. So, we updated the relevant test helpers to provide a `select()` in place of the `add()` method. This means you can select the details of a reference record without having to first create or query for it. We then went through the affected tests and 'test-seeders' and swapped calls to `Helper.add()` with `Helper.select()`. We also performed some housekeeping on some of the tests we touched. --- .github/workflows/ci.yml | 1 + app/services/data/load/load.service.js | 14 - db/seeds/01-regions.seed.js | 66 +++ db/seeds/01-users.js | 159 ------- db/seeds/02-purposes.seed.js | 22 + db/seeds/03-primary-purposes.seed.js | 22 + db/seeds/04-secondary-purposes.seed.js | 22 + ...ce-version-purpose-condition-types.seed.js | 22 + db/seeds/06-groups.seed.js | 36 ++ db/seeds/07-roles.seed.js | 36 ++ db/seeds/08-group-roles.seed.js | 58 +++ db/seeds/09-users.seed.js | 52 ++ db/seeds/10-user-groups.seed.js | 65 +++ db/seeds/11-financial-agreements.seed.js | 22 + db/seeds/README.md | 29 ++ db/seeds/data/financial-agreements.js | 68 +++ db/seeds/data/group-roles.js | 201 ++++++++ db/seeds/data/groups.js | 38 ++ ...licence-version-purpose-condition-types.js | 222 +++------ db/seeds/data/primary-purposes.js | 48 ++ .../seeds/data/purposes.js | 238 +++------- .../seeds/data/regions.js | 44 +- db/seeds/data/roles.js | 88 ++++ db/seeds/data/secondary-purposes.js | 323 +++++++++++++ db/seeds/data/user-groups.js | 46 ++ db/seeds/data/users.js | 128 +++++ db/seeds/reference-data.js | 19 - test/models/bill-run.model.test.js | 4 +- test/models/charge-element.model.test.js | 6 +- test/models/charge-reference.model.test.js | 6 +- test/models/financial-agreement.model.test.js | 16 +- test/models/group-role.model.test.js | 37 +- test/models/group.model.test.js | 64 +-- test/models/licence-agreement.model.test.js | 10 +- ...rsion-purpose-condition-type.model.test.js | 4 +- ...ce-version-purpose-condition.model.test.js | 4 +- .../licence-version-purpose.model.test.js | 26 +- test/models/licence-version.model.test.js | 6 +- test/models/licence.model.test.js | 4 +- test/models/primary-purpose.model.test.js | 8 +- test/models/purpose.model.test.js | 12 +- test/models/region.model.test.js | 4 +- .../return-requirement-purpose.model.test.js | 16 +- test/models/return-version.model.test.js | 5 +- test/models/role.model.test.js | 61 +-- test/models/secondary-purpose.model.test.js | 4 +- test/models/user-group.model.test.js | 38 +- test/models/user-role.model.test.js | 36 +- test/models/user.model.test.js | 64 +-- .../create-bill-run-event.presenter.test.js | 2 +- .../create-transaction.presenter.test.js | 3 +- .../create-bill-run.request.test.js | 2 +- ...fetch-bill-licence-summary.service.test.js | 6 +- .../fetch-bill-licence.service.test.js | 6 +- .../fetch-billing-accounts.service.test.js | 4 +- .../bill-runs/cancel-bill-run.service.test.js | 7 +- .../create-bill-run-event.service.test.js | 2 +- .../bill-runs/create-bill-run.service.test.js | 2 +- ...etermine-blocking-bill-run.service.test.js | 218 +++++---- .../bill-runs/fetch-bill-run.service.test.js | 2 +- .../bill-runs/fetch-bill-runs.service.test.js | 19 +- .../fetch-live-bill-runs.service.test.js | 32 +- .../fetch-matching-bill-run.service.test.js | 6 +- .../bill-runs/fetch-region.service.test.js | 6 +- .../initiate-bill-run.service.test.js | 3 +- .../bill-runs/send-bill-run.service.test.js | 15 +- ...termine-financial-year-end.service.test.js | 3 +- .../setup/fetch-regions.service.test.js | 8 +- .../fetch-charge-versions.service.test.js | 10 +- .../process-billing-period.service.test.js | 12 +- .../fetch-bill-run-licences.service.test.js | 2 +- .../fetch-billing-accounts.service.test.js | 2 +- .../fetch-charge-versions.service.test.js | 6 +- ...tch-review-licence-results.service.test.js | 6 +- .../process-billing-period.service.test.js | 10 +- .../remove-bill-run-licence.service.test.js | 9 +- .../bills/fetch-bill-summary.service.test.js | 15 +- .../services/bills/fetch-bill.service.test.js | 2 +- test/services/data/load/load.service.test.js | 46 +- ...etch-user-roles-and-groups.service.test.js | 96 ++-- .../import/legacy-licence.service.test.js | 12 +- .../import/persist-licence.service.test.js | 8 +- ...etch-time-limited-licences.service.test.js | 6 +- .../licences/fetch-agreements.service.test.js | 4 +- .../fetch-licence-summary.service.test.js | 12 +- .../fetch-points.service.test.js | 4 +- .../fetch-purposes.service.test.js | 8 +- ...eturn-version-requirements.service.test.js | 10 +- test/support/database.js | 33 +- test/support/general.js | 21 +- .../helpers/financial-agreement.helper.js | 57 +-- test/support/helpers/group-role.helper.js | 50 +- test/support/helpers/group.helper.js | 52 +- ...e-version-purpose-condition-type.helper.js | 62 +-- .../support/helpers/primary-purpose.helper.js | 71 +-- test/support/helpers/purpose.helper.js | 61 +-- test/support/helpers/region.helper.js | 69 +-- .../return-requirement-purpose.helper.js | 41 +- test/support/helpers/role.helper.js | 49 +- .../helpers/secondary-purpose.helper.js | 83 +--- test/support/helpers/user-group.helper.js | 31 +- test/support/helpers/user.helper.js | 40 +- .../seeders/data/primary-purposes.data.js | 60 --- .../seeders/data/secondary-purposes.data.js | 445 ------------------ .../licence-abstraction-data.seeder.js | 44 +- ...-version-purpose-condition-types.seeder.js | 34 -- .../support/seeders/primary-purpose.seeder.js | 32 -- test/support/seeders/purposes.seeder.js | 34 -- test/support/seeders/regions.seeder.js | 52 -- .../requirements-for-returns.seeder.js | 9 +- .../seeders/secondary-purpose.seeder.js | 32 -- test/support/seeders/seed-builder.js | 36 -- 112 files changed, 2359 insertions(+), 2289 deletions(-) create mode 100644 db/seeds/01-regions.seed.js delete mode 100644 db/seeds/01-users.js create mode 100644 db/seeds/02-purposes.seed.js create mode 100644 db/seeds/03-primary-purposes.seed.js create mode 100644 db/seeds/04-secondary-purposes.seed.js create mode 100644 db/seeds/05-licence-version-purpose-condition-types.seed.js create mode 100644 db/seeds/06-groups.seed.js create mode 100644 db/seeds/07-roles.seed.js create mode 100644 db/seeds/08-group-roles.seed.js create mode 100644 db/seeds/09-users.seed.js create mode 100644 db/seeds/10-user-groups.seed.js create mode 100644 db/seeds/11-financial-agreements.seed.js create mode 100644 db/seeds/README.md create mode 100644 db/seeds/data/financial-agreements.js create mode 100644 db/seeds/data/group-roles.js create mode 100644 db/seeds/data/groups.js rename test/support/seeders/data/licence-versions-purposes-condition-types.data.js => db/seeds/data/licence-version-purpose-condition-types.js (62%) create mode 100644 db/seeds/data/primary-purposes.js rename test/support/seeders/data/purposes.data.js => db/seeds/data/purposes.js (60%) rename test/support/seeders/data/region.data.js => db/seeds/data/regions.js (52%) create mode 100644 db/seeds/data/roles.js create mode 100644 db/seeds/data/secondary-purposes.js create mode 100644 db/seeds/data/user-groups.js create mode 100644 db/seeds/data/users.js delete mode 100644 db/seeds/reference-data.js delete mode 100644 test/support/seeders/data/primary-purposes.data.js delete mode 100644 test/support/seeders/data/secondary-purposes.data.js delete mode 100644 test/support/seeders/licence-version-purpose-condition-types.seeder.js delete mode 100644 test/support/seeders/primary-purpose.seeder.js delete mode 100644 test/support/seeders/purposes.seeder.js delete mode 100644 test/support/seeders/regions.seeder.js delete mode 100644 test/support/seeders/secondary-purpose.seeder.js delete mode 100644 test/support/seeders/seed-builder.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c5cc2503f..1c1e522d02 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,7 @@ jobs: POSTGRES_DB_TEST: wabs_system_test ENVIRONMENT: dev COOKIE_SECRET: 1a1028df-a2af-468a-929b-e3a274be1208 + DEFAULT_USER_PASSWORD: P@55word # Service containers to run with `runner-job` services: diff --git a/app/services/data/load/load.service.js b/app/services/data/load/load.service.js index 6ae43f970e..f3f72ee38e 100644 --- a/app/services/data/load/load.service.js +++ b/app/services/data/load/load.service.js @@ -27,10 +27,7 @@ const CompanyContactHelper = require('../../../../test/support/helpers/company-c const CompanyHelper = require('../../../../test/support/helpers/company.helper.js') const ContactHelper = require('../../../../test/support/helpers/contact.helper.js') const EventHelper = require('../../../../test/support/helpers/event.helper.js') -const FinancialAgreementHelper = require('../../../../test/support/helpers/financial-agreement.helper.js') const GaugingStationHelper = require('../../../../test/support/helpers/gauging-station.helper.js') -const GroupRoleHelper = require('../../../../test/support/helpers/group-role.helper.js') -const GroupHelper = require('../../../../test/support/helpers/group.helper.js') const LicenceAgreementHelper = require('../../../../test/support/helpers/licence-agreement.helper.js') const LicenceDocumentHeaderHelper = require('../../../../test/support/helpers/licence-document-header.helper.js') const LicenceDocumentRoleHelper = require('../../../../test/support/helpers/licence-document-role.helper.js') @@ -40,14 +37,11 @@ const LicenceEntityHelper = require('../../../../test/support/helpers/licence-en const LicenceGaugingStationHelper = require('../../../../test/support/helpers/licence-gauging-station.helper.js') const LicenceRoleHelper = require('../../../../test/support/helpers/licence-role.helper.js') const LicenceSupplementaryYearHelper = require('../../../../test/support/helpers/licence-supplementary-year.helper.js') -const LicenceVersionPurposeConditionTypeHelper = require('../../../../test/support/helpers/licence-version-purpose-condition-type.helper.js') const LicenceVersionPurposeConditionHelper = require('../../../../test/support/helpers/licence-version-purpose-condition.helper.js') const LicenceVersionPurposeHelper = require('../../../../test/support/helpers/licence-version-purpose.helper.js') const LicenceVersionHelper = require('../../../../test/support/helpers/licence-version.helper.js') const LicenceHelper = require('../../../../test/support/helpers/licence.helper.js') const PermitLicenceHelper = require('../../../../test/support/helpers/permit-licence.helper.js') -const PurposeHelper = require('../../../../test/support/helpers/purpose.helper.js') -const RegionHelper = require('../../../../test/support/helpers/region.helper.js') const ReturnLogHelper = require('../../../../test/support/helpers/return-log.helper.js') const ReturnRequirementPointHelper = require('../../../../test/support/helpers/return-requirement-point.helper.js') const ReturnRequirementPurposeHelper = require('../../../../test/support/helpers/return-requirement-purpose.helper.js') @@ -61,7 +55,6 @@ const ReviewChargeReferenceHelper = require('../../../../test/support/helpers/re const ReviewChargeVersionHelper = require('../../../../test/support/helpers/review-charge-version.helper.js') const ReviewLicenceHelper = require('../../../../test/support/helpers/review-licence.helper.js') const ReviewReturnHelper = require('../../../../test/support/helpers/review-return.helper.js') -const RoleHelper = require('../../../../test/support/helpers/role.helper.js') const ScheduledNotificationHelper = require('../../../../test/support/helpers/scheduled-notification.helper.js') const SessionHelper = require('../../../../test/support/helpers/session.helper.js') const TransactionHelper = require('../../../../test/support/helpers/transaction.helper.js') @@ -92,10 +85,7 @@ const LOAD_HELPERS = { companies: { helper: CompanyHelper, test: true, legacy: { schema: 'crm_v2', table: 'companies', id: 'company_id' } }, contacts: { helper: ContactHelper, test: true, legacy: { schema: 'crm_v2', table: 'contacts', id: 'contact_id' } }, events: { helper: EventHelper, test: false }, - financialAgreements: { helper: FinancialAgreementHelper, test: true, legacy: { schema: 'water', table: 'financial_agreement_types', id: 'financial_agreement_type_id' } }, gaugingStations: { helper: GaugingStationHelper, test: true, legacy: { schema: 'water', table: 'gauging_stations', id: 'gauging_station_id' } }, - groupRoles: { helper: GroupRoleHelper, test: false }, - groups: { helper: GroupHelper, test: false }, licenceAgreements: { helper: LicenceAgreementHelper, test: true, legacy: { schema: 'water', table: 'licence_agreements', id: 'licence_agreement_id' } }, licenceDocumentHeaders: { helper: LicenceDocumentHeaderHelper, test: false }, licenceDocumentRoles: { helper: LicenceDocumentRoleHelper, test: true, legacy: { schema: 'crm_v2', table: 'document_roles', id: 'document_role_id' } }, @@ -105,14 +95,11 @@ const LOAD_HELPERS = { licenceGaugingStations: { helper: LicenceGaugingStationHelper, test: true, legacy: { schema: 'water', table: 'licence_gauging_stations', id: 'licence_gauging_station_id' } }, licenceRoles: { helper: LicenceRoleHelper, test: false }, LicenceSupplementaryYears: { helper: LicenceSupplementaryYearHelper, test: false }, - licenceVersionPurposeConditionTypes: { helper: LicenceVersionPurposeConditionTypeHelper, test: false }, licenceVersionPurposeConditions: { helper: LicenceVersionPurposeConditionHelper, test: false }, licenceVersionPurposes: { helper: LicenceVersionPurposeHelper, test: true, legacy: { schema: 'water', table: 'licence_version_purposes', id: 'licence_version_purpose_id' } }, licenceVersions: { helper: LicenceVersionHelper, test: true, legacy: { schema: 'water', table: 'licence_versions', id: 'licence_version_id' } }, licences: { helper: LicenceHelper, test: true, legacy: { schema: 'water', table: 'licences', id: 'licence_id' } }, permitLicences: { helper: PermitLicenceHelper, test: false }, - purposes: { helper: PurposeHelper, test: true, legacy: { schema: 'water', table: 'purposes_uses', id: 'purpose_use_id' } }, - regions: { helper: RegionHelper, test: true, legacy: { schema: 'water', table: 'regions', id: 'region_id' } }, returnLogs: { helper: ReturnLogHelper, test: true, legacy: { schema: 'returns', table: 'returns', id: 'return_id' } }, returnRequirementPoints: { helper: ReturnRequirementPointHelper, test: false }, returnRequirementPurposes: { helper: ReturnRequirementPurposeHelper, test: false }, @@ -126,7 +113,6 @@ const LOAD_HELPERS = { reviewChargeVersions: { helper: ReviewChargeVersionHelper, test: false }, reviewLicences: { helper: ReviewLicenceHelper, test: false }, reviewReturns: { helper: ReviewReturnHelper, test: false }, - roles: { helper: RoleHelper, test: false }, scheduledNotifications: { helper: ScheduledNotificationHelper }, sessions: { helper: SessionHelper, test: false }, transactions: { helper: TransactionHelper, test: false }, diff --git a/db/seeds/01-regions.seed.js b/db/seeds/01-regions.seed.js new file mode 100644 index 0000000000..6d54382ac4 --- /dev/null +++ b/db/seeds/01-regions.seed.js @@ -0,0 +1,66 @@ +'use strict' + +const { db } = require('../db.js') + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { data: regions } = require('./data/regions.js') +const RegionModel = require('../../app/models/region.model.js') + +const ServerConfig = require('../../config/server.config.js') + +async function seed () { + for (const region of regions) { + const exists = await _exists(region) + + if (exists) { + await _update(region) + } else { + await _insert(region) + } + } +} + +async function _applyTestFlag (region, id) { + if (region.name !== 'Test') { + return null + } + + return db('regions').withSchema('water').update('isTest', true).where('regionId', id) +} + +async function _exists (region) { + const { chargeRegionId, naldRegionId } = region + + const result = await RegionModel.query() + .select('id') + .where('chargeRegionId', chargeRegionId) + .andWhere('naldRegionId', naldRegionId) + .limit(1) + .first() + + return !!result +} + +async function _insert (region) { + // The Test region is only intended to be seeded in our non-production environments + if (region.name === 'Test' && ServerConfig.environment === 'production') { + return + } + + const result = await RegionModel.query().insert(region) + + return _applyTestFlag(region, result.id) +} + +async function _update (region) { + const { chargeRegionId, displayName, naldRegionId, name } = region + + return RegionModel.query() + .patch({ displayName, name, updatedAt: timestampForPostgres() }) + .where('chargeRegionId', chargeRegionId) + .andWhere('naldRegionId', naldRegionId) +} + +module.exports = { + seed +} diff --git a/db/seeds/01-users.js b/db/seeds/01-users.js deleted file mode 100644 index b197e38b1f..0000000000 --- a/db/seeds/01-users.js +++ /dev/null @@ -1,159 +0,0 @@ -'use strict' - -const bcrypt = require('bcryptjs') - -const { generateUUID } = require('../../app/lib/general.lib.js') - -const DatabaseConfig = require('../../config/database.config.js') - -const seedUsers = [ - { - userName: 'admin-internal@wrls.gov.uk', - application: 'water_admin', - group: 'super' - }, - { - userName: 'super.user@wrls.gov.uk', - application: 'water_admin', - group: 'super' - }, - { - userName: 'environment.officer@wrls.gov.uk', - application: 'water_admin', - group: 'environment_officer' - }, - { - userName: 'waster.industry.regulatory.services@wrls.gov.uk', - application: 'water_admin', - group: 'wirs' - }, - { - userName: 'billing.data@wrls.gov.uk', - application: 'water_admin', - group: 'billing_and_data' - }, - { - userName: 'permitting.support.centre@wrls.gov.uk', - application: 'water_admin', - group: 'psc' - }, - { - userName: 'external@example.co.uk', - application: 'water_vml' - }, - { - userName: 'jon.lee@example.co.uk', - application: 'water_vml' - }, - { - userName: 'rachel.stevens@example.co.uk', - application: 'water_vml' - } -] - -async function seed (knex) { - // The following applies to all the seed users listed above. - // - // First create the user record if it doesn't exist - await _insertUsersWhereNotExists(knex) - - // Then if the seed user has a group property check if a `user_group` record exists. (This is only expected to be the - // case if we have just created the user) - await _updateSeedUsersWithUserIdAndGroupId(knex) - - // Finally, if the seed user has a group property and we've not found a `user_group` record, insert one. It's this - // that gives, for example, the billing.data@wrls.gov.uk user the permissions it needs to access billing features - await _insertUserGroupsWhereNotExists(knex) -} - -function _generateHashedPassword () { - // 10 is the number of salt rounds to perform to generate the salt. The legacy code uses - // const salt = bcrypt.genSaltSync(10) to pre-generate the salt before passing it to hashSync(). But this is - // intended for operations where you need to hash a large number of values. If you just pass in a number bcrypt will - // autogenerate the salt for you. - // https://github.com/kelektiv/node.bcrypt.js#usage - return bcrypt.hashSync(DatabaseConfig.defaultUserPassword, 10) -} - -async function _groups (knex) { - return knex('groups') - .withSchema('idm') - .select('groupId', 'group') -} - -async function _insertUsersWhereNotExists (knex) { - const password = _generateHashedPassword() - - for (const seedUser of seedUsers) { - const existingUser = await knex('users') - .withSchema('idm') - .first('userId') - .where('userName', seedUser.userName) - .andWhere('application', seedUser.application) - - if (!existingUser) { - await knex('users') - .withSchema('idm') - .insert({ - userName: seedUser.userName, - application: seedUser.application, - password, - userData: '{ "source": "Seeded" }', - resetRequired: 0, - badLogins: 0 - }) - } - } -} - -async function _insertUserGroupsWhereNotExists (knex) { - const seedUsersWithGroups = seedUsers.filter((seedData) => { - return seedData.group - }) - - for (const seedUser of seedUsersWithGroups) { - const existingUserGroup = await knex('userGroups') - .withSchema('idm') - .first('userGroupId') - .where('userId', seedUser.userId) - .andWhere('groupId', seedUser.groupId) - - if (!existingUserGroup) { - await knex('idm.userGroups') - .insert({ - userGroupId: generateUUID(), - userId: seedUser.userId, - groupId: seedUser.groupId - }) - } - } -} - -async function _updateSeedUsersWithUserIdAndGroupId (knex) { - const users = await _users(knex) - const groups = await _groups(knex) - - seedUsers.forEach((seedUser) => { - const user = users.find(({ userName }) => { - return userName === seedUser.userName - }) - seedUser.userId = user.userId - - if (seedUser.group) { - const userGroup = groups.find(({ group }) => { - return group === seedUser.group - }) - seedUser.groupId = userGroup.groupId - } - }) -} - -async function _users (knex) { - return knex('users') - .withSchema('idm') - .select('userId', 'userName') -} - -module.exports = { - seed -} diff --git a/db/seeds/02-purposes.seed.js b/db/seeds/02-purposes.seed.js new file mode 100644 index 0000000000..83d52fe027 --- /dev/null +++ b/db/seeds/02-purposes.seed.js @@ -0,0 +1,22 @@ +'use strict' + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { data: purposes } = require('./data/purposes.js') +const PurposeModel = require('../../app/models/purpose.model.js') + +async function seed () { + for (const purpose of purposes) { + await _upsert(purpose) + } +} + +async function _upsert (purpose) { + return PurposeModel.query() + .insert({ ...purpose, updatedAt: timestampForPostgres() }) + .onConflict('legacyId') + .merge(['description', 'lossFactor', 'twoPartTariff', 'updatedAt']) +} + +module.exports = { + seed +} diff --git a/db/seeds/03-primary-purposes.seed.js b/db/seeds/03-primary-purposes.seed.js new file mode 100644 index 0000000000..1968132cf5 --- /dev/null +++ b/db/seeds/03-primary-purposes.seed.js @@ -0,0 +1,22 @@ +'use strict' + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { data: primaryPurposes } = require('./data/primary-purposes.js') +const PrimaryPurposeModel = require('../../app/models/primary-purpose.model.js') + +async function seed () { + for (const purpose of primaryPurposes) { + await _upsert(purpose) + } +} + +async function _upsert (primaryPurpose) { + return PrimaryPurposeModel.query() + .insert({ ...primaryPurpose, updatedAt: timestampForPostgres() }) + .onConflict('legacyId') + .merge(['description', 'updatedAt']) +} + +module.exports = { + seed +} diff --git a/db/seeds/04-secondary-purposes.seed.js b/db/seeds/04-secondary-purposes.seed.js new file mode 100644 index 0000000000..0f6849691a --- /dev/null +++ b/db/seeds/04-secondary-purposes.seed.js @@ -0,0 +1,22 @@ +'use strict' + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { data: secondaryPurposes } = require('./data/secondary-purposes.js') +const SecondaryPurposeModel = require('../../app/models/secondary-purpose.model.js') + +async function seed () { + for (const purpose of secondaryPurposes) { + await _upsert(purpose) + } +} + +async function _upsert (secondaryPurpose) { + return SecondaryPurposeModel.query() + .insert({ ...secondaryPurpose, updatedAt: timestampForPostgres() }) + .onConflict('legacyId') + .merge(['description', 'updatedAt']) +} + +module.exports = { + seed +} diff --git a/db/seeds/05-licence-version-purpose-condition-types.seed.js b/db/seeds/05-licence-version-purpose-condition-types.seed.js new file mode 100644 index 0000000000..915bd023df --- /dev/null +++ b/db/seeds/05-licence-version-purpose-condition-types.seed.js @@ -0,0 +1,22 @@ +'use strict' + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { data: licenceVersionPurposeConditionTypes } = require('./data/licence-version-purpose-condition-types.js') +const LicenceVersionPurposeConditionTypeModel = require('../../app/models/licence-version-purpose-condition-type.model.js') + +async function seed () { + for (const licenceVersionPurposeConditionType of licenceVersionPurposeConditionTypes) { + await _upsert(licenceVersionPurposeConditionType) + } +} + +async function _upsert (licenceVersionPurposeConditionType) { + return LicenceVersionPurposeConditionTypeModel.query() + .insert({ ...licenceVersionPurposeConditionType, updatedAt: timestampForPostgres() }) + .onConflict(['code', 'subcode']) + .merge(['description', 'displayTitle', 'subcodeDescription', 'updatedAt']) +} + +module.exports = { + seed +} diff --git a/db/seeds/06-groups.seed.js b/db/seeds/06-groups.seed.js new file mode 100644 index 0000000000..9bdbfb4051 --- /dev/null +++ b/db/seeds/06-groups.seed.js @@ -0,0 +1,36 @@ +'use strict' + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { db } = require('../db.js') +const { data: groups } = require('./data/groups.js') + +async function seed () { + for (const group of groups) { + await _upsert(group) + } +} + +async function _upsert (group) { + // Note: We store the data in a format that matches what we get through Objection.js and our view model GroupModel. + // But because of the reason detailed below, the payload we send in the insert has to match how the underlying table + // is structured. Hence this transformation. + const payload = { + groupId: group.id, + application: 'water_admin', + group: group.group, + description: group.description, + dateUpdated: timestampForPostgres() + } + + // NOTE: We've had to drop back to Knex rather than Objection.js because the view model doesn't expose a field + // (application) that we rely on to make the onConflict work. This was by design, `application` never differs across + // records so is redundant to us when fetching instances of `GroupModel`. + return db('groups').withSchema('idm') + .insert(payload) + .onConflict(['application', 'group']) + .merge(['description', 'dateUpdated']) +} + +module.exports = { + seed +} diff --git a/db/seeds/07-roles.seed.js b/db/seeds/07-roles.seed.js new file mode 100644 index 0000000000..a5aca99ceb --- /dev/null +++ b/db/seeds/07-roles.seed.js @@ -0,0 +1,36 @@ +'use strict' + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { db } = require('../db.js') +const { data: roles } = require('./data/roles.js') + +async function seed () { + for (const role of roles) { + await _upsert(role) + } +} + +async function _upsert (role) { + // Note: We store the data in a format that matches what we get through Objection.js and our view model RoleModel. + // But because of the reason detailed below, the payload we send in the insert has to match how the underlying table + // is structured. Hence this transformation. + const payload = { + roleId: role.id, + application: 'water_admin', + role: role.role, + description: role.description, + dateUpdated: timestampForPostgres() + } + + // NOTE: We've had to drop back to Knex rather than Objection.js because the view model doesn't expose a field + // (application) that we rely on to make the onConflict work. This was by design, `application` never differs across + // records so is redundant to us when fetching instances of `RoleModel`. + return db('roles').withSchema('idm') + .insert(payload) + .onConflict(['application', 'role']) + .merge(['description', 'dateUpdated']) +} + +module.exports = { + seed +} diff --git a/db/seeds/08-group-roles.seed.js b/db/seeds/08-group-roles.seed.js new file mode 100644 index 0000000000..8430f4b5f9 --- /dev/null +++ b/db/seeds/08-group-roles.seed.js @@ -0,0 +1,58 @@ +'use strict' + +const { db } = require('../db.js') +const GroupRoleModel = require('../../app/models/group-role.model.js') +const { data: groupRoles } = require('./data/group-roles.js') +const { data: groups } = require('./data/groups.js') +const { data: roles } = require('./data/roles.js') + +async function seed () { + for (const groupRole of groupRoles) { + const { group, role } = _names(groupRole) + + const exists = await _exists(group, role) + + if (!exists) { + await _insert(groupRole.id, group, role) + } + } +} + +async function _exists (group, role) { + const result = await GroupRoleModel.query() + .select('groupRoles.id') + .innerJoinRelated('group') + .innerJoinRelated('role') + .where('group.group', group) + .andWhere('role.role', role) + .limit(1) + .first() + + return !!result +} + +async function _insert (id, group, role) { + return db.raw(` + INSERT INTO public.group_roles (id, group_id, role_id) + SELECT + (?) AS id, + (SELECT id FROM public."groups" g WHERE g."group" = ?) AS group_id, + (SELECT id FROM public.roles r WHERE r.role = ?) AS role_id; + `, [id, group, role]) +} + +function _names (groupRole) { + const { group } = groups.find((group) => { + return group.id === groupRole.groupId + }) + + const { role } = roles.find((role) => { + return role.id === groupRole.roleId + }) + + return { group, role } +} + +module.exports = { + seed +} diff --git a/db/seeds/09-users.seed.js b/db/seeds/09-users.seed.js new file mode 100644 index 0000000000..71037796aa --- /dev/null +++ b/db/seeds/09-users.seed.js @@ -0,0 +1,52 @@ +'use strict' + +const bcrypt = require('bcryptjs') + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { data: users } = require('./data/users.js') +const UserModel = require('../../app/models/user.model.js') + +const DatabaseConfig = require('../../config/database.config.js') +const ServerConfig = require('../../config/server.config.js') + +async function seed () { + // These users are for use in our non-production environments only + if (ServerConfig.environment === 'production') { + return + } + + const password = _generateHashedPassword() + + for (const user of users) { + await _upsert(user, password) + } +} + +function _generateHashedPassword () { + // 10 is the number of salt rounds to perform to generate the salt. The legacy code uses + // const salt = bcrypt.genSaltSync(10) to pre-generate the salt before passing it to hashSync(). But this is + // intended for operations where you need to hash a large number of values. If you just pass in a number bcrypt will + // autogenerate the salt for you. + // https://github.com/kelektiv/node.bcrypt.js#usage + return bcrypt.hashSync(DatabaseConfig.defaultUserPassword, 10) +} + +async function _upsert (user, password) { + return UserModel.query() + .insert({ ...user, password, updatedAt: timestampForPostgres() }) + .onConflict(['application', 'username']) + .merge([ + 'badLogins', + 'enabled', + 'lastLogin', + 'password', + 'resetGuid', + 'resetGuidCreatedAt', + 'resetRequired', + 'updatedAt' + ]) +} + +module.exports = { + seed +} diff --git a/db/seeds/10-user-groups.seed.js b/db/seeds/10-user-groups.seed.js new file mode 100644 index 0000000000..e4acb3cb49 --- /dev/null +++ b/db/seeds/10-user-groups.seed.js @@ -0,0 +1,65 @@ +'use strict' + +const { db } = require('../db.js') +const { data: groups } = require('./data/groups.js') +const UserGroupModel = require('../../app/models/user-group.model.js') +const { data: userGroups } = require('./data/user-groups.js') +const { data: users } = require('./data/users.js') + +const ServerConfig = require('../../config/server.config.js') + +async function seed () { + // These user groups relate to users that are only for use in our non-production environments + if (ServerConfig.environment === 'production') { + return + } + + for (const userGroup of userGroups) { + const { group, username } = _names(userGroup) + + const exists = await _exists(group, username) + + if (!exists) { + await _insert(userGroup.id, group, username) + } + } +} + +async function _exists (group, username) { + const result = await UserGroupModel.query() + .select('userGroups.id') + .innerJoinRelated('group') + .innerJoinRelated('user') + .where('group.group', group) + .andWhere('user.username', username) + .limit(1) + .first() + + return !!result +} + +async function _insert (id, group, username) { + return db.raw(` + INSERT INTO public.user_groups (id, group_id, user_id) + SELECT + (?) AS id, + (SELECT id FROM public."groups" g WHERE g."group" = ?) AS group_id, + (SELECT id FROM public.users u WHERE u.username = ?) AS user_id; + `, [id, group, username]) +} + +function _names (userGroup) { + const { group } = groups.find((group) => { + return group.id === userGroup.groupId + }) + + const { username } = users.find((user) => { + return user.id === userGroup.userId + }) + + return { group, username } +} + +module.exports = { + seed +} diff --git a/db/seeds/11-financial-agreements.seed.js b/db/seeds/11-financial-agreements.seed.js new file mode 100644 index 0000000000..cdd20d2aab --- /dev/null +++ b/db/seeds/11-financial-agreements.seed.js @@ -0,0 +1,22 @@ +'use strict' + +const { timestampForPostgres } = require('../../app/lib/general.lib.js') +const { data: financialAgreements } = require('./data/financial-agreements.js') +const FinancialAgreementModel = require('../../app/models/financial-agreement.model.js') + +async function seed () { + for (const financialAgreement of financialAgreements) { + await _upsert(financialAgreement) + } +} + +async function _upsert (financialAgreement) { + return FinancialAgreementModel.query() + .insert({ ...financialAgreement, updatedAt: timestampForPostgres() }) + .onConflict('code') + .merge(['description', 'disabled', 'updatedAt']) +} + +module.exports = { + seed +} diff --git a/db/seeds/README.md b/db/seeds/README.md new file mode 100644 index 0000000000..f0ac11992f --- /dev/null +++ b/db/seeds/README.md @@ -0,0 +1,29 @@ +# Seeds + +> Seed files allow you to populate your database with test or seed data independent of your migration files. + +Because we use [Objection.js](https://vincit.github.io/objection.js/) as our chosen [ORM](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping), it depends on [Knex.js](https://knexjs.org/) for actually generating the queries and connecting to the database. + +**Knex.js** also handles managing our [migrations](https://knexjs.org/guide/migrations.html). Not only that, it can handle [seeding](https://knexjs.org/guide/migrations.html#seed-files) a database. + +We use the seed files to ensure a database has the necessary reference data and records need to allow us to interact with it. Being a legacy service, for the most part of the legacy apps' migrations currently handle seeding a DB with, for example, primary purposes i.e. the reference data. + +But having our own version as a seed file has benefits. For example, we can add an additional test region to support our acceptance tests. We also extend the seeding to ensure there are users to allow us to access a freshly installed environment. + +The seed files are also referenced by the unit tests. At the start of a test run the test database is automatically cleaned then seeded. This means the tests have access to the same reference data as the real service. It also save some time by ensuring, again, users exist for use within the tests. + +## Adding a seed file + +Tips and guidance for working with the seed files. + +### Seeded as read from the folder + +**KNex.js** will seed the files in the alphabetical order it finds them in the folder. There is no config to change this and all files are always processed. This is why we make an exception to our file naming convention and prefix the seed files with a number. Bear this in mind if you are adding something that relies on other reference data, for example, `UserGroups` relies on `Users` and `Groups` so they need to be seeded first. + +### Write for production + +Though you may be adding a seed file just to ensure some reference data exists for the unit tests, they should always be written with the intent of seeding a new 'production' environment. So any 'test' or non-production entities should have a check against `ServerConfig.environment === 'production` to prevent them being seeded in our `production` environment. + +### Repeated use + +As mentioned before, unlike migrations where **Knex.js** keeps track of what migrations have been run, _all_ seed files are run every time `nom run seed` is called. Because of this you should write your seed files with the expectation that the reference data you are seeding already exists. diff --git a/db/seeds/data/financial-agreements.js b/db/seeds/data/financial-agreements.js new file mode 100644 index 0000000000..ee8f7f7950 --- /dev/null +++ b/db/seeds/data/financial-agreements.js @@ -0,0 +1,68 @@ +'use strict' + +const data = [ + { + id: 'c9b9424f-d111-4dac-b1a0-d29e2c5ec628', + code: 'S130S', + description: 'Section 130 BW Supported', + disabled: false + }, + { + id: 'a58807bd-2de6-4053-8586-68fa0653e7aa', + code: 'INST', + description: 'Installment', + disabled: false + }, + { + id: '2260b4eb-2a5a-4f9e-b0d4-c285d59e866e', + code: 'S126', + description: 'Section 126', + disabled: false + }, + { + id: '7d2c7675-6f7a-4443-9aa4-ccca1442cc56', + code: 'S127', + description: 'Section 127 (Two Part Tariff)', + disabled: false + }, + { + id: '56f695e3-af93-4a8b-8b84-b3ed09dc6099', + code: 'S130T', + description: 'Section 130 BW Third Party', + disabled: false + }, + { + id: '44ac0473-f6ea-4d16-9270-1a7d4d98cf95', + code: 'S130U', + description: 'Section 130 BW Unsupported', + disabled: false + }, + { + id: '78085113-e2d0-422d-9761-616d229868e9', + code: 'MCHG', + description: 'Minimum Charge Value', + disabled: false + }, + { + id: '794598cd-0374-4ba2-b2c9-7a1203b3b90e', + code: 'MVAL', + description: 'Minimum Invoice Value', + disabled: false + }, + { + id: '892268a1-7fde-4740-acc5-6d60d5cc394f', + code: 'S130W', + description: 'Section 130 BW Joint Venture Watergrid', + disabled: true + }, + { + id: 'd5aa8fc8-e2f0-4ee4-9d5a-8f5ee1d90aaa', + code: 'S126B', + description: 'Section 126 Bury M B C', + disabled: true + } +] + +module.exports = { + data +} diff --git a/db/seeds/data/group-roles.js b/db/seeds/data/group-roles.js new file mode 100644 index 0000000000..015b40ff05 --- /dev/null +++ b/db/seeds/data/group-roles.js @@ -0,0 +1,201 @@ +'use strict' + +const { data: groups } = require('./groups.js') +const { data: roles } = require('./roles.js') + +const data = [ + { + id: 'e6fec455-1c2f-4cea-8ce6-7f8a8a52f384', + groupId: groups.find((group) => { return group.group === 'environment_officer' }).id, + roleId: roles.find((role) => { return role.role === 'hof_notifications' }).id + }, + { + id: '521867f4-5816-4fa5-8b01-3433ca304e18', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'returns' }).id + }, + { + id: '90b975b3-6aa6-406d-bfef-bc9867811dea', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'bulk_return_notifications' }).id + }, + { + id: '4f3014cb-3a42-4eb3-a555-076eb5c96969', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'manage_accounts' }).id + }, + { + id: 'a4a66587-190c-4f68-bf2c-bae051754bb8', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'unlink_licences' }).id + }, + { + id: '9aa6163c-faf1-4167-b2ec-3346203f9365', + groupId: groups.find((group) => { return group.group === 'wirs' }).id, + roleId: roles.find((role) => { return role.role === 'returns' }).id + }, + { + id: '2724687a-4f16-47c7-94c4-db9c89522a68', + groupId: groups.find((group) => { return group.group === 'nps' }).id, + roleId: roles.find((role) => { return role.role === 'unlink_licences' }).id + }, + { + id: 'fbf0bc10-14fa-4d69-9f2a-75ebc49aba9b', + groupId: groups.find((group) => { return group.group === 'nps' }).id, + roleId: roles.find((role) => { return role.role === 'renewal_notifications' }).id + }, + { + id: 'c4bb2367-9491-42c3-844e-2717e20e1050', + groupId: groups.find((group) => { return group.group === 'psc' }).id, + roleId: roles.find((role) => { return role.role === 'unlink_licences' }).id + }, + { + id: '1cef304f-734c-4632-9330-872092979281', + groupId: groups.find((group) => { return group.group === 'psc' }).id, + roleId: roles.find((role) => { return role.role === 'renewal_notifications' }).id + }, + { + id: 'dc6bc4ad-444f-4ab0-9cda-335c58f14bdd', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'returns' }).id + }, + { + id: '34765744-b2df-4b48-a542-c4a4a4051b7b', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'hof_notifications' }).id + }, + { + id: '72c9f37f-535d-4bb6-8184-2de8ef64f75b', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'bulk_return_notifications' }).id + }, + { + id: '10af67ab-b23b-4ed4-a670-f866ba958c7e', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'manage_accounts' }).id + }, + { + id: 'a8713481-9af2-41b4-88e3-69a59b8daf5e', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'unlink_licences' }).id + }, + { + id: '04d51975-13e3-4346-97f2-37c367f1d6cc', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'renewal_notifications' }).id + }, + { + id: '227243f5-606f-4f5d-aa8b-9fb8104a466a', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'ar_user' }).id + }, + { + id: 'd56a2f9e-3272-4f80-aadf-effdda53e14d', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'ar_approver' }).id + }, + { + id: '88ba3607-f0fc-4272-9bb3-ec8792d3ad8a', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'billing' }).id + }, + { + id: '708256ad-2ebf-4871-b58a-7d1db7cac994', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'billing' }).id + }, + { + id: '0e0d76d0-1e3a-4147-8044-e0aecc4346da', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'charge_version_workflow_editor' }).id + }, + { + id: '027be72e-e13f-44bb-9d8d-a2952cc6577d', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'charge_version_workflow_reviewer' }).id + }, + { + id: '1830bb37-5e7e-451c-8ae6-3189355287e0', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'charge_version_workflow_reviewer' }).id + }, + { + id: 'f740c44f-393b-4f6b-89cb-d8fc57ef5fdc', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'manage_agreements' }).id + }, + { + id: '460ff8da-4466-4475-83a4-f046d25c8679', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'manage_agreements' }).id + }, + { + id: '38f2c87f-0885-4dca-b749-247bbe2215b1', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'delete_agreements' }).id + }, + { + id: 'd063e5d0-9c53-4e42-9348-1862f0f38f1d', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'delete_agreements' }).id + }, + { + id: '2f2dd7d0-c88f-45a4-9420-e5235105e2b5', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'charge_version_workflow_editor' }).id + }, + { + id: 'febe1594-a854-4289-a06c-a61d21bfcdad', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'manage_billing_accounts' }).id + }, + { + id: '4abb0505-c88e-43d6-af63-657e1ce2a4b9', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'manage_billing_accounts' }).id + }, + { + id: '50664eac-4ac1-43e5-9c9b-ac38d2a2ace7', + groupId: groups.find((group) => { return group.group === 'environment_officer' }).id, + roleId: roles.find((role) => { return role.role === 'manage_gauging_station_licence_links' }).id + }, + { + id: '5bdf10a6-76f8-44aa-a53e-4b05b76a0a25', + groupId: groups.find((group) => { return group.group === 'nps' }).id, + roleId: roles.find((role) => { return role.role === 'manage_gauging_station_licence_links' }).id + }, + { + id: '19c3c925-bfd6-46fd-a6cf-964e348067f1', + groupId: groups.find((group) => { return group.group === 'psc' }).id, + roleId: roles.find((role) => { return role.role === 'manage_gauging_station_licence_links' }).id + }, + { + id: '0b8df4ec-47bb-424e-9eb9-ed44a151c3e0', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'manage_gauging_station_licence_links' }).id + }, + { + id: '530e62e6-42ff-45d0-b1c3-97082ea82472', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + roleId: roles.find((role) => { return role.role === 'view_charge_versions' }).id + }, + { + id: 'f75a60f4-9219-4b18-8f5e-9dbf771b05c1', + groupId: groups.find((group) => { return group.group === 'nps' }).id, + roleId: roles.find((role) => { return role.role === 'view_charge_versions' }).id + }, + { + id: '1cd4ee5c-89b7-48b7-b08b-d7fe8eef9ee0', + groupId: groups.find((group) => { return group.group === 'psc' }).id, + roleId: roles.find((role) => { return role.role === 'view_charge_versions' }).id + }, + { + id: '882e3c15-f30d-4e25-b771-184065cc5f26', + groupId: groups.find((group) => { return group.group === 'super' }).id, + roleId: roles.find((role) => { return role.role === 'view_charge_versions' }).id + } +] + +module.exports = { + data +} diff --git a/db/seeds/data/groups.js b/db/seeds/data/groups.js new file mode 100644 index 0000000000..fdfd18de33 --- /dev/null +++ b/db/seeds/data/groups.js @@ -0,0 +1,38 @@ +'use strict' + +const data = [ + { + id: 'a83fbaca-e964-4bfc-9ac6-f5f675d757af', + group: 'environment_officer', + description: 'Environment officer' + }, + { + id: '4192c86c-f05b-4ca9-9f72-ced63075eac4', + group: 'billing_and_data', + description: 'Water Resources Billing & Data' + }, + { + id: '3731a013-5c2d-4305-8c20-c3ea1f108728', + group: 'wirs', + description: 'Waste Industry Regulatory Services' + }, + { + id: 'e7dbf2fa-881a-44d3-b9c7-f4fb73e3a029', + group: 'nps', + description: 'National Permitting Service' + }, + { + id: '1a340087-b45d-42bc-85a8-8164614b5a4c', + group: 'psc', + description: 'Permitting & Support Centre' + }, + { + id: 'd0b8e943-210d-4eb6-8004-d2ef5c1e4611', + group: 'super', + description: 'Super user' + } +] + +module.exports = { + data +} diff --git a/test/support/seeders/data/licence-versions-purposes-condition-types.data.js b/db/seeds/data/licence-version-purpose-condition-types.js similarity index 62% rename from test/support/seeders/data/licence-versions-purposes-condition-types.data.js rename to db/seeds/data/licence-version-purpose-condition-types.js index c45df55362..5fdedd713c 100644 --- a/test/support/seeders/data/licence-versions-purposes-condition-types.data.js +++ b/db/seeds/data/licence-version-purpose-condition-types.js @@ -1,15 +1,13 @@ 'use strict' -module.exports = [ +const data = [ { id: 'a71c352b-04a8-4f32-b349-929ec1607704', code: 'NSQ', subcode: 'M3FN', description: 'Non Standard Quantities', subcodeDescription: 'Cubic Metres Per Fortnight', - displayTitle: 'Non standard quantities cubic metres per fortnight', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Non standard quantities cubic metres per fortnight' }, { id: '56729fac-cd3c-4136-8763-c9c6f8f7a85d', @@ -17,9 +15,7 @@ module.exports = [ subcode: 'M3WK', description: 'Non Standard Quantities', subcodeDescription: 'Cubic Metres Per Week', - displayTitle: 'Non standard quantities cubic metres per week', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Non standard quantities cubic metres per week' }, { id: 'f9dd5078-d365-45e3-be57-d9859c6d4599', @@ -27,9 +23,7 @@ module.exports = [ subcode: 'PER', description: 'Non Standard Quantities', subcodeDescription: 'Text Defined Periodicity', - displayTitle: 'Non standard quantities', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Non standard quantities' }, { id: '875e9fde-67c2-42bb-8e0c-2033c78b0bbe', @@ -37,9 +31,7 @@ module.exports = [ subcode: 'GEN', description: 'Other Standard Conditions', subcodeDescription: 'General', - displayTitle: 'General conditions', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'General conditions' }, { id: '692bc351-5d50-427a-ad96-cd4328eb21d2', @@ -47,9 +39,7 @@ module.exports = [ subcode: 'COMP', description: 'Proportional Take', subcodeDescription: 'Complex', - displayTitle: 'Complex proportional take', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Complex proportional take' }, { id: '047ae10c-85a9-4d92-890a-6e7c4c1d4153', @@ -57,9 +47,7 @@ module.exports = [ subcode: 'PCENT', description: 'Proportional Take', subcodeDescription: 'Percentage Take', - displayTitle: 'Proportional percentage take', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Proportional percentage take' }, { id: 'e25b9fa3-5038-4b83-8ed0-58648358a143', @@ -67,9 +55,7 @@ module.exports = [ subcode: 'LPS', description: 'Rates', subcodeDescription: 'Litres Per Second', - displayTitle: 'Rates litres per second', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Rates litres per second' }, { id: '4a142b01-5588-4dfc-9330-920c996babe0', @@ -77,9 +63,7 @@ module.exports = [ subcode: 'M3D', description: 'Rates', subcodeDescription: 'M3 Per Day', - displayTitle: 'Rates m3 per day', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Rates m3 per day' }, { id: '98a1ad74-f753-4900-add8-e12779078f4a', @@ -87,9 +71,7 @@ module.exports = [ subcode: 'M3M', description: 'Rates', subcodeDescription: 'M3 Per Month', - displayTitle: 'Rates m3 per month', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Rates m3 per month' }, { id: 'fe52f2df-8536-4865-b364-ff8b7e146928', @@ -97,9 +79,7 @@ module.exports = [ subcode: 'M3S', description: 'Rates', subcodeDescription: 'M3 Per Second', - displayTitle: 'Rates m3 per second', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Rates m3 per second' }, { id: '60f41084-c014-4a0e-8036-f748390b7791', @@ -107,9 +87,7 @@ module.exports = [ subcode: 'BANI', description: 'Spray Irrigation', subcodeDescription: 'Ban Imposed', - displayTitle: 'Spray irrigation ban imposed', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Spray irrigation ban imposed' }, { id: '97f2634f-7357-4a43-b126-6a0d45eaf02c', @@ -117,9 +95,7 @@ module.exports = [ subcode: 'BANP', description: 'Spray Irrigation', subcodeDescription: 'Ban Proposed', - displayTitle: 'Spray irrigation ban proposed', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Spray irrigation ban proposed' }, { id: '03a6f321-f726-4835-9625-6aa593b06cda', @@ -127,9 +103,7 @@ module.exports = [ subcode: 'PTS', description: 'Cross Regional Point', subcodeDescription: 'Licensed Point Falls Within Another Region', - displayTitle: 'Licensed point falls within another region', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Licensed point falls within another region' }, { id: '8187812c-0533-4b0f-8433-3f5b2f463f86', @@ -137,9 +111,7 @@ module.exports = [ subcode: 'LLX', description: 'Aggregate', subcodeDescription: 'Licence To Licence; Cross Regional Condition Link Between Licences', - displayTitle: 'Aggregate condition cross regional link between licences', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Aggregate condition cross regional link between licences' }, { id: '265497c2-d3fb-41ab-9167-c3204fce3e3c', @@ -147,9 +119,7 @@ module.exports = [ subcode: 'WSL', description: 'Public Water Supply', subcodeDescription: 'Water Supply Licensee', - displayTitle: 'Public water supply licensee', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Public water supply licensee' }, { id: '54df1201-b6a1-4909-ae29-cc1a3d93e469', @@ -157,9 +127,7 @@ module.exports = [ subcode: 'REGS', description: 'Eel Regulations', subcodeDescription: 'Pass/Screen - Eel Regs SI3344', - displayTitle: 'Fish pass/screen - eel regs SI3344', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Fish pass/screen - eel regs SI3344' }, { id: '31dde8db-de9a-4b41-abd8-2efc88d6db68', @@ -167,9 +135,7 @@ module.exports = [ subcode: 'CLAUS', description: 'Derogation', subcodeDescription: 'Derogation Clause', - displayTitle: 'Derogation clause', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Derogation clause' }, { id: '03abbecd-2445-4209-a96c-fe4c9a857065', @@ -177,9 +143,7 @@ module.exports = [ subcode: 'LEVFL', description: 'Time Limited', subcodeDescription: 'Limited Extension of Validity - Full Licence Expiry', - displayTitle: 'Time Limited Extension of Validity - Full Licence Expiry', - createdAt: new Date('2024-01-12'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Time Limited Extension of Validity - Full Licence Expiry' }, { id: '0afdae8f-4fc9-4623-83a9-246ca862047f', @@ -187,9 +151,7 @@ module.exports = [ subcode: 'LEVTL', description: 'Time Limited', subcodeDescription: 'Limited Extension of Validity - Time Limited Variation', - displayTitle: 'Time Limited Extension of Validity - Time Limited Variation', - createdAt: new Date('2024-01-12'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Time Limited Extension of Validity - Time Limited Variation' }, { id: '478dc963-46d6-4464-862c-283d3991fd63', @@ -197,9 +159,7 @@ module.exports = [ subcode: 'LLL', description: 'Aggregate', subcodeDescription: 'Licence To Licence; Link Between Licences', - displayTitle: 'Aggregate condition link between licences', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Aggregate condition link between licences' }, { id: '1617d5f0-8ad0-443f-a897-318260db88f1', @@ -207,9 +167,7 @@ module.exports = [ subcode: 'CHE', description: 'Cessation Condition', subcodeDescription: 'Chemical', - displayTitle: 'Chemical cessation condition', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Chemical cessation condition' }, { id: '7f53fb27-9002-43b1-8b0f-04a338ab7333', @@ -217,9 +175,7 @@ module.exports = [ subcode: 'DEP', description: 'Cessation Condition', subcodeDescription: 'Dependent On Release From Schemes/Other Licences', - displayTitle: 'Cessation dependant on releases from schemes / other licences', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Cessation dependant on releases from schemes / other licences' }, { id: 'f2c1072f-6654-44c9-80c5-683c446ac87f', @@ -227,9 +183,7 @@ module.exports = [ subcode: 'FLOW', description: 'Cessation Condition', subcodeDescription: 'Flow', - displayTitle: 'Flow cessation condition', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Flow cessation condition' }, { id: '259db7c3-00cd-4f42-86a8-996d470f700c', @@ -237,9 +191,7 @@ module.exports = [ subcode: 'GWL', description: 'Cessation Condition', subcodeDescription: 'Groundwater Level', - displayTitle: 'Groundwater level cessation condition', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Groundwater level cessation condition' }, { id: 'f5dc9b11-3a7b-4d91-9113-bdb41289a43d', @@ -247,9 +199,7 @@ module.exports = [ subcode: 'LEV', description: 'Cessation Condition', subcodeDescription: 'Level', - displayTitle: 'Level cessation condition', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Level cessation condition' }, { id: '3bf80bb2-cbba-4897-a1f1-f1b3055930f7', @@ -257,9 +207,7 @@ module.exports = [ subcode: 'POL', description: 'Cessation Condition', subcodeDescription: 'Political - Hosepipe Ban', - displayTitle: 'Political cessation condition', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Political cessation condition' }, { id: 'b4d445c9-1ea0-4918-b97b-48e8c4496fb0', @@ -267,9 +215,7 @@ module.exports = [ subcode: 'LPL', description: 'Aggregate', subcodeDescription: 'Licence To Purpose; Link Between Different Licences', - displayTitle: 'Aggregate condition licence to purpose between different licences', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Aggregate condition licence to purpose between different licences' }, { id: '8f118be4-c132-4d96-8c0a-f2918cdd371b', @@ -277,9 +223,7 @@ module.exports = [ subcode: 'FLOW', description: 'By-Pass Flow', subcodeDescription: 'Flow', - displayTitle: 'By-pass flow', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'By-pass flow' }, { id: 'b942a057-5da1-4382-926f-ae3fa98cb484', @@ -287,9 +231,7 @@ module.exports = [ subcode: 'LINK', description: 'Condition To Indicate Licence Split On Nald', subcodeDescription: 'Link Between Split Licences', - displayTitle: 'Link between split licences', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Link between split licences' }, { id: '558d51fa-e84b-4a49-b85c-3cd1f017abe1', @@ -297,9 +239,7 @@ module.exports = [ subcode: 'GEN', description: 'Complex Condition', subcodeDescription: 'General', - displayTitle: 'Complex condition', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Complex condition' }, { id: 'a111a541-b3b8-48e3-a405-1fe0efa5f9a9', @@ -307,9 +247,7 @@ module.exports = [ subcode: 'FLOW', description: 'Compensation Release', subcodeDescription: 'Flow', - displayTitle: 'Flow compensation release', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Flow compensation release' }, { id: '8c059046-2a0b-498a-b9a6-053159cd14c5', @@ -317,9 +255,7 @@ module.exports = [ subcode: 'FILL', description: 'Filling/Emptying Clause', subcodeDescription: 'Filling/Emptying To Be Notified To Agency', - displayTitle: 'Filling/emptying to be notified to Agency', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Filling/emptying to be notified to Agency' }, { id: '32618825-4fda-474e-b6e3-33b9ab4ea708', @@ -327,9 +263,7 @@ module.exports = [ subcode: 'SEAS', description: 'Filling/Emptying Clause', subcodeDescription: 'Filling/Emptying Season Limited', - displayTitle: 'Filling/emptying season limited', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Filling/emptying season limited' }, { id: '965bbf07-06c9-4b25-8f0c-755a83dfb27a', @@ -337,9 +271,7 @@ module.exports = [ subcode: 'TYPE', description: 'Inflow Control', subcodeDescription: 'Control Type', - displayTitle: 'Inflow control type', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Inflow control type' }, { id: '7e2da099-3a5f-45b6-94f4-8e9f677fa095', @@ -347,9 +279,7 @@ module.exports = [ subcode: 'TYPE', description: 'Type Of Lining', subcodeDescription: 'Type', - displayTitle: 'Type of lining', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Type of lining' }, { id: '2410d74c-5f7b-4c1f-bd1c-1f67d63a5cd0', @@ -357,9 +287,7 @@ module.exports = [ subcode: 'OFF', description: 'Lockable Structures Required', subcodeDescription: 'Locked', - displayTitle: 'Lockable structures required', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Lockable structures required' }, { id: 'edcf5946-6e1d-41e4-97b2-39e7ee3e705d', @@ -367,9 +295,7 @@ module.exports = [ subcode: 'FLOW', description: 'Maintained Flow', subcodeDescription: 'Flow', - displayTitle: 'Maintained flow', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Maintained flow' }, { id: '769d0915-04c8-4838-9da4-9986f4677f4f', @@ -377,9 +303,7 @@ module.exports = [ subcode: 'MEANS', description: 'Means Of Compensation Flow Release', subcodeDescription: 'Means', - displayTitle: 'Means of compensation flow release', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Means of compensation flow release' }, { id: '4339796c-fc0e-41a8-9ea8-f3502529cf92', @@ -387,9 +311,7 @@ module.exports = [ subcode: 'M3MO', description: 'Non Standard Quantities', subcodeDescription: 'Cubic Metres Per Month', - displayTitle: 'Non standard quantities cubic metres per month', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Non standard quantities cubic metres per month' }, { id: 'e8807cab-124d-47c9-9b19-4efb2c35e213', @@ -397,9 +319,7 @@ module.exports = [ subcode: 'TRAN', description: 'Transfer', subcodeDescription: 'Transfer', - displayTitle: 'Transfer', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Transfer' }, { id: '8501db0e-8227-43a4-bd21-a5d4857e2506', @@ -407,9 +327,7 @@ module.exports = [ subcode: 'GW', description: 'Water Level Monitoring', subcodeDescription: 'Ground Water', - displayTitle: 'Water level monitoring groundwater', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Water level monitoring groundwater' }, { id: '91777b9d-3df0-40a8-a5e6-115f543c0908', @@ -417,9 +335,7 @@ module.exports = [ subcode: 'SW', description: 'Water Level Monitoring', subcodeDescription: 'Surface Water', - displayTitle: 'Water level monitoring surface water', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Water level monitoring surface water' }, { id: '629e6598-40a5-4d62-8229-cf1264c35bff', @@ -427,9 +343,7 @@ module.exports = [ subcode: 'GW', description: 'Water Quality Monitoring', subcodeDescription: 'Groundwater', - displayTitle: 'Water quality monitoring groundwater', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Water quality monitoring groundwater' }, { id: '6fd1ad95-6755-430a-b32b-d9adbb0ba476', @@ -437,9 +351,7 @@ module.exports = [ subcode: 'SW', description: 'Water Quality Monitoring', subcodeDescription: 'Surface Water', - displayTitle: 'Water quality monitoring surface water', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Water quality monitoring surface water' }, { id: '3b2c530b-0d81-44b7-9b12-752e458a0ae6', @@ -447,9 +359,7 @@ module.exports = [ subcode: 'VAR', description: 'Time Limited', subcodeDescription: 'Variation', - displayTitle: 'Time limited variation', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Time limited variation' }, { id: '3888f275-6281-4685-90a8-65451ef01ca4', @@ -457,9 +367,7 @@ module.exports = [ subcode: 'QTY', description: 'Water Rights Trade', subcodeDescription: 'Traded Quantity', - displayTitle: 'Water rights trade quantity', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Water rights trade quantity' }, { id: 'c3f5648f-efbd-4c46-9b86-4ddd6bd3ce61', @@ -467,9 +375,7 @@ module.exports = [ subcode: 'PUR', description: 'Water Rights Trade', subcodeDescription: 'Traded Purpose', - displayTitle: 'Water rights trade purpose', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Water rights trade purpose' }, { id: '7f036649-4ef1-48c9-9bdc-7f2415604801', @@ -477,9 +383,7 @@ module.exports = [ subcode: 'PNT', description: 'Water Rights Trade', subcodeDescription: 'Traded Point', - displayTitle: 'Water rights trade point', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Water rights trade point' }, { id: 'cddc2c57-7036-4557-953d-13440b90fa8e', @@ -487,9 +391,7 @@ module.exports = [ subcode: 'MINQ', description: 'Time Limited', subcodeDescription: 'Minimum Quantity', - displayTitle: 'Time limited minimum quantity', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Time limited minimum quantity' }, { id: '6f031296-bb36-4d19-980c-21545d7eeee8', @@ -497,9 +399,7 @@ module.exports = [ subcode: 'SD', description: 'Time Limited', subcodeDescription: 'Self Destruct', - displayTitle: 'Time limited self destruct', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Time limited self destruct' }, { id: 'bf53e6ad-af27-4e67-b100-d86001d21f1c', @@ -507,9 +407,7 @@ module.exports = [ subcode: 'PP', description: 'Aggregate', subcodeDescription: 'Purpose To Purpose; Link Within A Licence', - displayTitle: 'Aggregate condition purpose to purpose within a licence', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Aggregate condition purpose to purpose within a licence' }, { id: 'a5ddff18-f9b1-4b7a-9c20-fad7677aa7f2', @@ -517,9 +415,7 @@ module.exports = [ subcode: 'PPL', description: 'Aggregate', subcodeDescription: 'Purpose To Purpose ; Link Between Different Licences', - displayTitle: 'Aggregate condition purpose to purpose between different licences', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Aggregate condition purpose to purpose between different licences' }, { id: '18ade96c-75ae-4bc3-9580-b8a574f3f9e5', @@ -527,9 +423,7 @@ module.exports = [ subcode: 'DIM', description: 'Additional Groundwater Information', subcodeDescription: 'Dimensions', - displayTitle: 'Additional groundwater information dimensions', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Additional groundwater information dimensions' }, { id: '9aced507-2135-4804-9490-46c84607a08d', @@ -537,8 +431,10 @@ module.exports = [ subcode: 'RATE', description: 'Inflow Rate', subcodeDescription: 'Rate', - displayTitle: 'Inflow rate', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2024-07-30') + displayTitle: 'Inflow rate' } ] + +module.exports = { + data +} diff --git a/db/seeds/data/primary-purposes.js b/db/seeds/data/primary-purposes.js new file mode 100644 index 0000000000..840416ce24 --- /dev/null +++ b/db/seeds/data/primary-purposes.js @@ -0,0 +1,48 @@ +'use strict' + +const data = [ + { + id: 'b6bb3b77-cfe8-4f22-8dc9-e92713ca3156', + legacyId: 'A', + description: 'Agriculture' + }, + { + id: '3019b676-e8c6-4bc1-8336-b7862489af70', + legacyId: 'E', + description: 'Environmental' + }, + { + id: 'd6259e5e-9bb4-4743-b565-e61ec05afc0a', + legacyId: 'I', + description: 'Industrial, Commercial And Public Services' + }, + { + id: 'c096d17b-7369-4c6b-a230-fb0bc5109b9c', + legacyId: 'M', + description: 'Amenity' + }, + { + id: '4bbc388a-38cf-425c-bf8f-d609e6a5f57a', + legacyId: 'P', + description: 'Production Of Energy' + }, + { + id: '7cc6fa87-2e69-4cb3-9abb-a5700ec49b36', + legacyId: 'W', + description: 'Water Supply' + }, + { + id: '6ad72bf8-ab8e-4fb6-99b0-f54a7acc3a53', + legacyId: 'X', + description: 'Impounding' + }, + { + id: 'a38014a7-b8e3-4c3d-87b4-32ca1616d29b', + legacyId: 'C', + description: 'Crown And Government' + } +] + +module.exports = { + data +} diff --git a/test/support/seeders/data/purposes.data.js b/db/seeds/data/purposes.js similarity index 60% rename from test/support/seeders/data/purposes.data.js rename to db/seeds/data/purposes.js index 6a018c9a89..5dbf27eab7 100644 --- a/test/support/seeders/data/purposes.data.js +++ b/db/seeds/data/purposes.js @@ -1,526 +1,414 @@ 'use strict' -module.exports = [ +const data = [ { id: '64b8e40e-0d13-401e-90e1-0e498cda9fcb', legacyId: '10', description: 'Animal Watering & General Use In Non Farming Situations', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '59492ab7-a1a2-4cb3-b483-0dd6f9e45aec', legacyId: '20', description: 'Boiler Feed', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '464c1270-aa9a-48d9-9e96-2744d374f56c', legacyId: '30', description: 'Conveying Materials', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '7fb06487-8abc-4642-9ce5-80116b98ae12', legacyId: '40', description: 'Drinking, Cooking, Sanitary, Washing, (Small Garden) - Commercial/Industrial/Public Services', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'dc0a1059-f6a9-4432-8408-fa32df710196', legacyId: '50', description: 'Drinking, Cooking, Sanitary, Washing, (Small Garden) - Household', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '8b17770a-ced1-475e-b0e2-ba4f3ebe262e', legacyId: '60', description: 'Dust Suppression', lossFactor: 'high', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '0b65dc1a-8a40-453e-9a20-9a18acd19dc3', legacyId: '70', description: 'Effluent/Slurry Dilution', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '4e6d0ad8-7203-46f2-8d77-12110c85c3f8', legacyId: '80', description: 'Evaporative Cooling', lossFactor: 'high', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'a6dcf24c-7751-4f88-b772-af8c485b7f27', legacyId: '90', description: 'Fish Farm/Cress Pond Throughflow', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '92b555f8-ae9e-4257-bda2-87776a81c207', legacyId: '100', description: 'Fish Pass/Canoe Pass', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '9d9d9528-a992-424f-bdf4-f867d5639ea0', legacyId: '110', description: 'Gas Suppression/Scrubbing', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '06615efb-b13b-4d16-957c-6ab6cb779417', legacyId: '120', description: 'General Cooling (Existing Licences Only) (High Loss)', lossFactor: 'high', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '6506f380-ca07-4fe0-aa10-bf17252cb065', legacyId: '130', description: 'General Cooling (Existing Licences Only) (Low Loss)', lossFactor: 'low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '289d1644-5215-4a20-af9e-5664fa9a18c7', legacyId: '140', description: 'General Farming & Domestic', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '019f30ab-ea80-48cf-a5fc-0b0642cef4d5', legacyId: '150', description: 'General Use Relating To Secondary Category (High Loss)', lossFactor: 'high', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'f48f5c29-8231-4552-bb98-3f04234ca6cb', legacyId: '160', description: 'General Use Relating To Secondary Category (Medium Loss)', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '8eff7b9a-7f62-40bc-8a86-91d38ed61b9a', legacyId: '170', description: 'General Use Relating To Secondary Category (Low Loss)', lossFactor: 'low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '73e8a003-279b-49a2-839b-adf9daae5e9a', legacyId: '180', description: 'General Use Relating To Secondary Category (Very Low Loss)', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'e08d935b-2870-42ad-84c0-68103671d617', legacyId: '190', description: 'General Washing/Process Washing', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'b18e9132-0efd-43e4-a318-b2e060f8bfb3', legacyId: '200', description: 'Heat Pump', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '3cd7606f-ffd1-4ae6-82a1-d76ea5261321', legacyId: '210', description: 'Horticultural Watering', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '60ab5147-577a-4722-b77f-0c6558b16817', legacyId: '220', description: 'Hydraulic Rams', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '7fa8eadc-ccfe-4a9d-b91f-0fecd8209140', legacyId: '230', description: 'Hydraulic Testing', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '54fc8048-733a-4f50-bd12-cc9f66da2c9b', legacyId: '240', description: 'Hydroelectric Power Generation', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '8a9f62ab-781f-4003-846a-b47f1b189fe7', legacyId: '250', description: 'Lake & Pond Throughflow', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '8780c9d2-cb49-4ffa-b846-09380adf278e', legacyId: '260', description: 'Large Garden Watering', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'aa510d6e-f145-494b-918f-13b8b5500788', legacyId: '270', description: 'Laundry Use', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '681795bb-cf19-4b02-9fb0-e86b69b721e9', legacyId: '280', description: 'Make-Up Or Top Up Water', lossFactor: 'high', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'c0392edc-31aa-4f02-817d-4bde8c8d22a2', legacyId: '290', description: 'Milling & Water Power Other Than Electricity Generation', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'ccb4cf71-028d-4567-80ec-e860cab7cd47', legacyId: '300', description: 'Mineral Washing', lossFactor: 'low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '09af974e-5162-41fd-b963-b97eb30fd084', legacyId: '310', description: 'Non-Evaporative Cooling', lossFactor: 'low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'bd6d01a8-0d3a-4cc8-a56e-150a524e2956', legacyId: '320', description: 'Pollution Remediation', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '772136d1-9184-417b-90cd-91053287d1df', legacyId: '330', description: 'Potable Water Supply - Direct', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'd45eb987-5f6d-48a7-9130-c2f0de0573c0', legacyId: '340', description: 'Potable Water Supply - Storage', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '2688807a-ee41-4f17-bcf2-816f3afa72dd', legacyId: '350', description: 'Process Water', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '286c222c-cf21-4103-9135-a22c1cf0cead', legacyId: '360', description: 'Raw Water Supply', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '7c4681d6-61e8-478f-97e0-90e0b22c0edd', legacyId: '370', description: 'River Recirculation', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'a52dc94a-964a-47ac-b55a-2ef07aca7087', legacyId: '380', description: 'Spray Irrigation - Anti Frost', lossFactor: 'medium', - twoPartTariff: true, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: true }, { id: '23f1626c-0afa-4a1d-86ae-3bc985049e03', legacyId: '390', description: 'Spray Irrigation - Anti Frost Storage', lossFactor: 'medium', - twoPartTariff: true, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: true }, { id: '4ad11971-be6a-4da5-af04-563c76205b0e', legacyId: '400', description: 'Spray Irrigation - Direct', lossFactor: 'high', - twoPartTariff: true, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: true }, { id: 'a3424107-ef96-47ac-be76-a8b86de3271a', legacyId: '410', description: 'Spray Irrigation - Spray Irrigation Definition Order', lossFactor: 'high', - twoPartTariff: true, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: true }, { id: 'da576426-70bc-4f76-b05b-849bba48a8e8', legacyId: '420', description: 'Spray Irrigation - Storage', lossFactor: 'high', - twoPartTariff: true, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: true }, { id: '68952835-592a-48e8-a6ea-b5ab0c71716d', legacyId: '430', description: 'Supply To A Canal For Throughflow', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'bba3436e-3af9-40e4-9a56-aad81ae046ef', legacyId: '440', description: 'Supply To A Leat For Throughflow', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'cb5273f5-1962-477c-9c23-bec96e1409ef', legacyId: '450', description: 'Transfer Between Sources (Pre Water Act 2003)', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '96218b0a-07cf-4a4c-85e7-204ccbe5a7e6', legacyId: '460', description: 'Vegetable Washing', lossFactor: 'low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '722b0f7b-4258-4b39-999c-8942a77371b4', legacyId: '470', description: 'Water Bottling', lossFactor: 'medium', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'e1d58b9b-cd4a-43a9-950e-c9244ab9ab0e', legacyId: '480', description: 'Water Wheels Not Used For Power', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'a7bb7cbc-d417-4eaa-961b-4d8a75a5e1f9', legacyId: '490', description: 'Impounding (for any purpose excluding impounding for HEP)', lossFactor: 'non-chargeable', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'e4cabe35-e4f6-4d7d-855d-6d126f77b4fd', legacyId: '600', description: 'Trickle Irrigation - Direct', lossFactor: 'high', - twoPartTariff: true, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: true }, { id: 'b6e13514-81da-4cfe-b6de-6a13a22e010e', legacyId: '610', description: 'Trickle Irrigation - Under Cover/Containers', lossFactor: 'high', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '80c45e3e-8487-40e3-b661-834efedb3d15', legacyId: '620', description: 'Trickle Irrigation - Storage', lossFactor: 'high', - twoPartTariff: true, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: true }, { id: '443e7f1a-6d1d-4193-989d-2c94fb4c9656', legacyId: '630', description: 'Flood Irrigation, Including Water Meadows, Warping And Pest Control', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'ddcf9fe3-26b7-4ef2-8bf1-1226f11652be', legacyId: '640', description: 'Wet Fencing And Nature Conservation', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '96655751-b02c-42d8-a305-1859f9a17287', legacyId: '650', description: 'Transfer Between Sources (Post Water Act 2003)', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: 'a7eb6005-fca6-416d-878a-52ac0ead9b6f', legacyId: '660', description: 'Dewatering', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '10e3bd38-15dc-41d7-a77b-153d6e3c7d1b', legacyId: '670', description: 'Hydraulic Fracturing (Fracking)', lossFactor: 'high', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false }, { id: '6e4807d8-7d9d-4ff6-bdb2-b3b121077873', legacyId: '645', description: 'Wet Fencing and Agriculture', lossFactor: 'very low', - twoPartTariff: false, - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') + twoPartTariff: false } ] + +module.exports = { + data +} diff --git a/test/support/seeders/data/region.data.js b/db/seeds/data/regions.js similarity index 52% rename from test/support/seeders/data/region.data.js rename to db/seeds/data/regions.js index 59c1709088..6a57218b0e 100644 --- a/test/support/seeders/data/region.data.js +++ b/db/seeds/data/regions.js @@ -1,85 +1,71 @@ 'use strict' -module.exports = [ +const data = [ { id: 'a5f868ec-f51c-478d-924c-37852626b7c1', chargeRegionId: 'A', naldRegionId: 1, name: 'Anglian', - displayName: 'Anglian', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'Anglian' }, { id: '1acb3cc9-ee16-4276-b0f4-37603d791698', chargeRegionId: 'B', naldRegionId: 2, name: 'Midlands', - displayName: 'Midlands', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'Midlands' }, { id: '36706540-0985-4cef-b7e5-ab4345049b22', chargeRegionId: 'Y', naldRegionId: 3, name: 'North East', - displayName: 'North East', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'North East' }, { id: 'eb57737f-b309-49c2-9ab6-f701e3a6fd96', chargeRegionId: 'N', naldRegionId: 4, name: 'North West', - displayName: 'North West', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'North West' }, { id: '4ccf3c5b-ab4e-48e1-afa8-3b18b5d07fab', chargeRegionId: 'E', naldRegionId: 5, name: 'South West', - displayName: 'South West', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'South West' }, { id: 'd34d9f4f-11ed-46f5-b4fb-15d5988c2870', chargeRegionId: 'S', naldRegionId: 6, name: 'Southern', - displayName: 'Southern', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'Southern' }, { id: '7af8fb71-e197-4f85-bee4-23b62ef711c6', chargeRegionId: 'T', naldRegionId: 7, name: 'Thames', - displayName: 'Thames', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'Thames' }, { id: '77d44d65-6f33-425e-9075-ae5ac43a0c36', chargeRegionId: 'W', naldRegionId: 8, name: 'EA Wales', - displayName: 'Wales', - createdAt: new Date('2023-12-14'), - updatedAt: new Date('2023-12-14') + displayName: 'Wales' }, { id: 'd0a4123d-1e19-480d-9dd4-f70f3387c4b9', chargeRegionId: 'S', naldRegionId: 9, - name: 'Test Region', - displayName: 'Test Region', - createdAt: new Date('2024-07-18'), - updatedAt: new Date('2024-07-18') + name: 'Test', + displayName: 'Test Region' } ] + +module.exports = { + data +} diff --git a/db/seeds/data/roles.js b/db/seeds/data/roles.js new file mode 100644 index 0000000000..390869ea49 --- /dev/null +++ b/db/seeds/data/roles.js @@ -0,0 +1,88 @@ +'use strict' + +const data = [ + { + id: '0d1afc01-b2f0-4e6a-9e49-30955e139c7a', + role: 'returns', + description: 'Submit and edit returns' + }, + { + id: 'e486f477-6bca-46b2-8af7-ed1046ab50d4', + role: 'hof_notifications', + description: 'Send HoF notifications' + }, + { + id: '63d896d4-aa5d-44b1-9e76-435c3ddbbe4c', + role: 'bulk_return_notifications', + description: 'Send bulk return invitations and reminder notifications' + }, + { + id: 'e9125e71-b3dd-45ab-afab-50628f42d226', + role: 'manage_accounts', + description: 'Create and manage internal user accounts' + }, + { + id: '4dfa27f5-d99f-43d8-b272-17200c8e5981', + role: 'unlink_licences', + description: 'Remove licences registered to a company' + }, + { + id: 'e5c306cb-7b74-4d53-a934-930d969286f3', + role: 'renewal_notifications', + description: 'Send renewal notifications' + }, + { + id: 'fde3e071-5745-4cb9-aae7-c10146b07a72', + role: 'ar_user', + description: 'Edit licence data in Digitise! tool' + }, + { + id: 'c99ec5c7-2daf-4c30-b319-6b1c01c6969d', + role: 'ar_approver', + description: 'Approve licence data in Digitise! tool' + }, + { + id: 'd3151065-b100-4025-b027-5bfb3099f2af', + role: 'billing', + description: 'Administer billing' + }, + { + id: 'eabb9bcb-81aa-4e50-9b2d-e4d290235047', + role: 'charge_version_workflow_editor', + description: 'Create and edit charge information workflow data' + }, + { + id: '8be52f8c-4870-4d04-8314-5b7781133fcf', + role: 'charge_version_workflow_reviewer', + description: 'Approve charge information workflow data' + }, + { + id: '4d572dbd-8289-435b-a0fc-08d4a9358394', + role: 'manage_agreements', + description: 'Create and edit licence agreements' + }, + { + id: '3c281e02-1768-4ee9-8304-9c28c7d18cb5', + role: 'delete_agreements', + description: 'Delete licence agreements' + }, + { + id: 'b98a799b-84da-4e80-a9f6-144b90df86f5', + role: 'manage_billing_accounts', + description: 'View invoice accounts and change invoice account address' + }, + { + id: '311ab6d0-9c8e-44df-b472-ad8929f0db98', + role: 'manage_gauging_station_licence_links', + description: 'Manage linkages between gauging stations and licences' + }, + { + id: '30730887-ea74-4b71-895b-c809e65421dc', + role: 'view_charge_versions', + description: 'View charge information' + } +] + +module.exports = { + data +} diff --git a/db/seeds/data/secondary-purposes.js b/db/seeds/data/secondary-purposes.js new file mode 100644 index 0000000000..7cb082a81c --- /dev/null +++ b/db/seeds/data/secondary-purposes.js @@ -0,0 +1,323 @@ +'use strict' + +const data = [ + { + id: '2457bfeb-a120-4b57-802a-46494bd22f82', + legacyId: 'AGR', + description: 'General Agriculture' + }, + { + id: 'd0708921-b5c1-4a5b-b63b-31aab46016d8', + legacyId: 'AQF', + description: 'Aquaculture Fish' + }, + { + id: '3dce7cf6-0ed2-48bd-ad6c-bf6c71f79db4', + legacyId: 'AQP', + description: 'Aquaculture Plant' + }, + { + id: 'c05b93dd-a48f-49a8-a5e6-782252c18f9d', + legacyId: 'BRW', + description: 'Breweries/Wine' + }, + { + id: '2d7ba5e2-b6e7-4ed2-a2d1-a1ad3f97229f', + legacyId: 'BUS', + description: 'Business Parks' + }, + { + id: '7ea553b0-03df-4081-bcaa-e1f704aa3291', + legacyId: 'CHE', + description: 'Chemicals' + }, + { + id: 'ceb6ae1d-fc8c-4cf9-ae01-269122c24c8d', + legacyId: 'CON', + description: 'Construction' + }, + { + id: 'd83bc811-b48f-4e87-9916-4c47943d224c', + legacyId: 'CRN', + description: 'Crown And Government' + }, + { + id: 'a50fd6db-96ab-46c6-804e-ddd035a4f1b4', + legacyId: 'DAR', + description: 'Dairies' + }, + { + id: 'c8d3f626-a4d5-42ea-9f72-6e22c524048e', + legacyId: 'ELC', + description: 'Electricity' + }, + { + id: 'e35d4bf3-9c23-48fc-881c-a258b78bb2d1', + legacyId: 'EXT', + description: 'Extractive' + }, + { + id: 'eb7c97fd-a718-4136-a7a2-4f0151e0188c', + legacyId: 'FAD', + description: 'Food & Drink' + }, + { + id: '9ca665af-5356-4a6d-b509-b96ee64de27a', + legacyId: 'FOR', + description: 'Forestry' + }, + { + id: 'a76d6e29-88d1-4fa3-9136-d961a9cdb1bf', + legacyId: 'GOF', + description: 'Golf Courses' + }, + { + id: 'ce8e913d-55fb-4f21-b2b2-755084661662', + legacyId: 'HOL', + description: 'Holiday Sites, Camp Sites & Tourist Attractions' + }, + { + id: 'c7be8e7a-37a0-459a-bf26-97af4f120834', + legacyId: 'HOR', + description: 'Horticulture And Nurseries' + }, + { + id: '2c397566-4cde-41c0-8cca-d152b554f528', + legacyId: 'HOS', + description: 'Hospitals' + }, + { + id: 'c4165b99-f4f1-4124-acad-e512371d1bf7', + legacyId: 'HTL', + description: 'Hotels, Public Houses And Conference Centres' + }, + { + id: '70abbcfd-b7b9-4eef-9f39-29468b32ce0e', + legacyId: 'IND', + description: 'Industrial/Commercial/Energy/Public Services' + }, + { + id: 'ecd25cff-3749-49fd-ba30-9c86483b4a12', + legacyId: 'LAU', + description: 'Laundry' + }, + { + id: 'ad036953-0e1b-43ce-a359-f452f344c4da', + legacyId: 'MCH', + description: 'Machinery And Electronics' + }, + { + id: 'd5fb3110-0dde-46f7-b7a3-94d06052d8fc', + legacyId: 'MEC', + description: 'Mechanical Non Electrical' + }, + { + id: '86708863-167a-4865-8383-cde90e7d1b43', + legacyId: 'MIN', + description: 'Mineral Products' + }, + { + id: 'af1eec87-602c-41de-a982-2417bbc320a2', + legacyId: 'MTL', + description: 'Metal' + }, + { + id: '7cff0b52-da01-4a29-b29c-a4bba4036324', + legacyId: 'MUN', + description: 'Municipal Grounds' + }, + { + id: '4bbdf3ff-9efa-4303-bea8-f7d419022922', + legacyId: 'NAV', + description: 'Navigation' + }, + { + id: '00237355-5b18-44d9-a5c4-785e387739fb', + legacyId: 'NRE', + description: 'Non-Remedial River/Wetland Support' + }, + { + id: '789824ee-140e-4871-b453-b17ac12ab095', + legacyId: 'ORC', + description: 'Orchards' + }, + { + id: '303cc214-6118-417b-b6ee-343257af3a3b', + legacyId: 'OTE', + description: 'Other Environmental Improvements' + }, + { + id: 'f85ab791-e943-4492-a3bb-c0b3ad3f0712', + legacyId: 'OTI', + description: 'Other Industrial/Commercial/Public Services' + }, + { + id: 'a58ef991-ea9b-4443-862d-811327ccd3dc', + legacyId: 'PAD', + description: 'Public Administration' + }, + { + id: 'e882c4a6-53d0-4b82-b921-499d25428134', + legacyId: 'PAP', + description: 'Paper And Printing' + }, + { + id: 'f47814e0-a7a9-45be-af75-4d2b58784be0', + legacyId: 'PET', + description: 'Petrochemicals' + }, + { + id: '6cbb6f30-64bd-47e5-b852-5089e7088568', + legacyId: 'PRI', + description: 'Private Non-Industrial' + }, + { + id: '8b324ba7-7d82-46d8-ae61-bc343e388745', + legacyId: 'PRV', + description: 'Private Water Supply' + }, + { + id: '2362c06f-bb1e-4649-8342-29cc94017774', + legacyId: 'PUM', + description: 'Pump & Treat' + }, + { + id: '866fe96a-1536-4396-85b3-de073ac2f92f', + legacyId: 'PWS', + description: 'Public Water Supply' + }, + { + id: '1d3877a3-c5cf-405a-9e07-4fe94689f4ea', + legacyId: 'PWU', + description: 'Private Water Undertaking' + }, + { + id: 'b9a4c156-77e8-4891-95f4-6df745e58d42', + legacyId: 'RAC', + description: 'Racecourses' + }, + { + id: '04d711bb-607c-4814-aca9-0c6bd39e0ea4', + legacyId: 'REF', + description: 'Refuse And Recycling' + }, + { + id: '3c2a4aab-c2a7-4fea-aa06-bcdf2d9e499f', + legacyId: 'REM', + description: 'Remedial River/Wetland Support' + }, + { + id: 'c0691f62-1022-4cef-8f9b-575099f62c20', + legacyId: 'RES', + description: 'Research Non- University/College' + }, + { + id: 'b6d3c0c8-4018-4553-9393-0e87a3c91a8f', + legacyId: 'RET', + description: 'Retail' + }, + { + id: 'bf32cb71-8f5a-4055-913a-3c16942abb8c', + legacyId: 'RUB', + description: 'Rubber' + }, + { + id: 'f06c3e2e-0a0b-4cd5-81bd-04d527bd8533', + legacyId: 'SCH', + description: 'Schools And Colleges' + }, + { + id: 'b6fbd08e-a4ad-4a9b-871e-30ac51efee78', + legacyId: 'SLA', + description: 'Slaughtering' + }, + { + id: '59bd435f-5651-4cc5-bdf8-fb8e3697f95a', + legacyId: 'SPO', + description: 'Sports Grounds/Facilities' + }, + { + id: '9c16f3e8-4ea0-4292-8802-8427bec0571a', + legacyId: 'TRA', + description: 'Transport' + }, + { + id: 'c778de53-0fd4-445e-af86-3c441f79e07b', + legacyId: 'TXT', + description: 'Textiles & Leather' + }, + { + id: '9b1828d1-d319-4d6c-a620-d9828c455fcb', + legacyId: 'UNK', + description: 'Unknown (for impounding exluding impoundments for HEP)' + }, + { + id: 'eecd8280-9b2f-4279-b300-d4b3441bd87c', + legacyId: 'WAT', + description: 'Water Supply Related' + }, + { + id: 'f6f7b6bf-6447-4092-8cd6-27cae2d8956e', + legacyId: 'ZOO', + description: 'Zoos/Kennels/Stables' + }, + { + id: '490db793-eddf-4fed-93ce-78260b8d0584', + legacyId: 'CRW', + description: 'Crown - Other' + }, + { + id: '09545099-a184-4e98-be2a-e749d24392e9', + legacyId: 'GOV', + description: 'Government Departments - Other' + }, + { + id: '6bb0f9c0-7ef0-4da4-9c0f-2a60fc6b4785', + legacyId: 'DET', + description: 'Detention And Immigration Centres' + }, + { + id: '3fd3cf82-47ff-4f01-b523-96959f2d635b', + legacyId: 'VIF', + description: 'Visiting Armed Forces' + }, + { + id: '8b836771-6d7e-4277-b03f-473c0504d02b', + legacyId: 'FRT', + description: 'Government Or Crown Forestry' + }, + { + id: '489930ca-cef9-4604-86be-31ef01954cc7', + legacyId: 'CVY', + description: 'Statutory Water Conservancy Operation' + }, + { + id: '607f230d-e96b-4383-a588-f7976cbf3e6b', + legacyId: 'HRB', + description: 'Ports And Harbour Authority Operation' + }, + { + id: '075d0549-4075-47f4-9e1a-944e96a63eec', + legacyId: 'DRG', + description: 'Drainage Operations' + }, + { + id: '678fa94b-6320-44d2-a21d-5d772040de6c', + legacyId: 'POW', + description: 'Domestic Premises Related' + }, + { + id: 'd4f9f23b-1602-4846-9b05-07a49d2bd9c5', + legacyId: 'MOD', + description: 'Mod (Ministry Of Defence)' + }, + { + id: '7f75f247-5b3c-4785-954c-207456a4d6c4', + legacyId: 'OOG', + description: 'Onshore Oil and Gas Extraction' + } +] + +module.exports = { + data +} diff --git a/db/seeds/data/user-groups.js b/db/seeds/data/user-groups.js new file mode 100644 index 0000000000..ae2dc8f113 --- /dev/null +++ b/db/seeds/data/user-groups.js @@ -0,0 +1,46 @@ +'use strict' + +const { data: groups } = require('./groups.js') +const { data: users } = require('./users.js') + +const data = [ + { + id: '950706f1-31f7-4af6-aba0-40bfd00a06e0', + groupId: groups.find((group) => { return group.group === 'super' }).id, + userId: users.find((user) => { return user.username === 'admin-internal@wrls.gov.uk' }).id + }, + { + id: '9d2aa1e8-6f36-491d-9e7f-5536b3100d4d', + groupId: groups.find((group) => { return group.group === 'super' }).id, + userId: users.find((user) => { return user.username === 'super.user@wrls.gov.uk' }).id + }, + { + id: '9d9a7b55-4a31-4561-a556-f71cd367becb', + groupId: groups.find((group) => { return group.group === 'environment_officer' }).id, + userId: users.find((user) => { return user.username === 'environment.officer@wrls.gov.uk' }).id + }, + { + id: 'b90c6892-6113-4a84-b5a4-fffafba26ae5', + groupId: groups.find((group) => { return group.group === 'wirs' }).id, + userId: users.find((user) => { return user.username === 'waste.industry.regulatory.services@wrls.gov.uk' }).id + }, + { + id: '9d8fd2df-f5da-4450-8151-95f2c68c0232', + groupId: groups.find((group) => { return group.group === 'billing_and_data' }).id, + userId: users.find((user) => { return user.username === 'billing.data@wrls.gov.uk' }).id + }, + { + id: 'c5d7cbe2-591c-4a77-a3da-1a2acf5cdbd3', + groupId: groups.find((group) => { return group.group === 'psc' }).id, + userId: users.find((user) => { return user.username === 'permitting.support.centre@wrls.gov.uk' }).id + }, + { + id: 'da306f92-87e6-48f8-a521-411b19c0693f', + groupId: groups.find((group) => { return group.group === 'nps' }).id, + userId: users.find((user) => { return user.username === 'national.permitting.service@wrls.gov.uk' }).id + } +] + +module.exports = { + data +} diff --git a/db/seeds/data/users.js b/db/seeds/data/users.js new file mode 100644 index 0000000000..a0cadcbc9b --- /dev/null +++ b/db/seeds/data/users.js @@ -0,0 +1,128 @@ +'use strict' + +const data = [ + { + id: 100000, + username: 'admin-internal@wrls.gov.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_admin', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100001, + username: 'super.user@wrls.gov.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_admin', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100002, + username: 'environment.officer@wrls.gov.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_admin', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100003, + username: 'waste.industry.regulatory.services@wrls.gov.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_admin', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100004, + username: 'billing.data@wrls.gov.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_admin', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100005, + username: 'permitting.support.centre@wrls.gov.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_admin', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100006, + username: 'national.permitting.service@wrls.gov.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_admin', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100007, + username: 'external@example.co.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_vml', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100008, + username: 'jon.lee@example.co.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_vml', + resetGuidCreatedAt: null, + enabled: true + }, + { + id: 100009, + username: 'rachel.stevens@example.co.uk', + password: 'P@55word', + resetGuid: null, + resetRequired: 0, + lastLogin: null, + badLogins: 0, + application: 'water_vml', + resetGuidCreatedAt: null, + enabled: true + } +] + +module.exports = { + data +} diff --git a/db/seeds/reference-data.js b/db/seeds/reference-data.js deleted file mode 100644 index 614154574e..0000000000 --- a/db/seeds/reference-data.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict' - -const LicenceVersionPurposeConditionTypeSeeder = require('../../test/support/seeders/licence-version-purpose-condition-types.seeder.js') -const RegionsSeeder = require('../../test/support/seeders/regions.seeder.js') -const PurposesSeeder = require('../../test/support/seeders/purposes.seeder.js') -const PrimaryPurposesSeeder = require('../../test/support/seeders/primary-purpose.seeder.js') -const SecondaryPurposesSeeder = require('../../test/support/seeders/secondary-purpose.seeder.js') - -async function seed () { - await LicenceVersionPurposeConditionTypeSeeder.seed() - await RegionsSeeder.seed() - await PurposesSeeder.seed() - await PrimaryPurposesSeeder.seed() - await SecondaryPurposesSeeder.seed() -} - -module.exports = { - seed -} diff --git a/test/models/bill-run.model.test.js b/test/models/bill-run.model.test.js index d9dd9bcc37..c491844efe 100644 --- a/test/models/bill-run.model.test.js +++ b/test/models/bill-run.model.test.js @@ -42,7 +42,7 @@ describe('Bill Run model', () => { let testRegion beforeEach(async () => { - testRegion = await RegionHelper.add() + testRegion = RegionHelper.select() testRecord = await BillRunHelper.add({ regionId: testRegion.id }) }) @@ -62,7 +62,7 @@ describe('Bill Run model', () => { expect(result.id).to.equal(testRecord.id) expect(result.region).to.be.an.instanceOf(RegionModel) - expect(result.region).to.equal(testRegion) + expect(result.region).to.equal(testRegion, { skip: ['createdAt', 'updatedAt'] }) }) }) diff --git a/test/models/charge-element.model.test.js b/test/models/charge-element.model.test.js index 42bb52d6ae..358bd53a5b 100644 --- a/test/models/charge-element.model.test.js +++ b/test/models/charge-element.model.test.js @@ -11,8 +11,8 @@ const { expect } = Code const ChargeElementHelper = require('../support/helpers/charge-element.helper.js') const ChargeReferenceHelper = require('../support/helpers/charge-reference.helper.js') const ChargeReferenceModel = require('../../app/models/charge-reference.model.js') +const PurposeHelper = require('../support/helpers/purpose.helper.js') const PurposeModel = require('../../app/models/purpose.model.js') -const PurposesSeeder = require('../support/seeders/purposes.seeder.js') const ReviewChargeElementHelper = require('../support/helpers/review-charge-element.helper.js') const ReviewChargeElementModel = require('../../app/models/review-charge-element.model.js') @@ -71,7 +71,7 @@ describe('Charge Element model', () => { let testPurpose beforeEach(async () => { - testPurpose = PurposesSeeder.data[0] + testPurpose = PurposeHelper.select() const { id: purposeId } = testPurpose @@ -94,7 +94,7 @@ describe('Charge Element model', () => { expect(result.id).to.equal(testRecord.id) expect(result.purpose).to.be.an.instanceOf(PurposeModel) - expect(result.purpose).to.equal(testPurpose) + expect(result.purpose).to.equal(testPurpose, { skip: ['createdAt', 'updatedAt'] }) }) }) diff --git a/test/models/charge-reference.model.test.js b/test/models/charge-reference.model.test.js index c9eee113b7..a3c823ebfb 100644 --- a/test/models/charge-reference.model.test.js +++ b/test/models/charge-reference.model.test.js @@ -17,8 +17,8 @@ const ChargeElementModel = require('../../app/models/charge-element.model.js') const ChargeReferenceHelper = require('../support/helpers/charge-reference.helper.js') const ChargeVersionHelper = require('../support/helpers/charge-version.helper.js') const ChargeVersionModel = require('../../app/models/charge-version.model.js') +const PurposeHelper = require('../support/helpers/purpose.helper.js') const PurposeModel = require('../../app/models/purpose.model.js') -const PurposesSeeder = require('../support/seeders/purposes.seeder.js') const ReviewChargeReferenceHelper = require('../support/helpers/review-charge-reference.helper.js') const ReviewChargeReferenceModel = require('../../app/models/review-charge-reference.model.js') const TransactionHelper = require('../support/helpers/transaction.helper.js') @@ -183,7 +183,7 @@ describe('Charge Reference model', () => { let testPurpose beforeEach(async () => { - testPurpose = PurposesSeeder.data[0] + testPurpose = PurposeHelper.select() const { id: purposeId } = testPurpose @@ -206,7 +206,7 @@ describe('Charge Reference model', () => { expect(result.chargePurposeId).to.equal(testRecord.chargePurposeId) expect(result.purpose).to.be.an.instanceOf(PurposeModel) - expect(result.purpose).to.equal(testPurpose) + expect(result.purpose).to.equal(testPurpose, { skip: ['createdAt', 'updatedAt'] }) }) }) diff --git a/test/models/financial-agreement.model.test.js b/test/models/financial-agreement.model.test.js index c2f0bf2236..3124bde97d 100644 --- a/test/models/financial-agreement.model.test.js +++ b/test/models/financial-agreement.model.test.js @@ -4,7 +4,7 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Test helpers @@ -15,14 +15,16 @@ const LicenceAgreementModel = require('../../app/models/licence-agreement.model. // Thing under test const FinancialAgreementModel = require('../../app/models/financial-agreement.model.js') +const FINANCIAL_AGREEMENT_MVAL_INDEX = 7 + describe('Financial Agreement model', () => { let testRecord - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await FinancialAgreementHelper.add() - }) + before(async () => { + testRecord = FinancialAgreementHelper.select(FINANCIAL_AGREEMENT_MVAL_INDEX) + }) + describe('Basic query', () => { it('can successfully run a basic query', async () => { const result = await FinancialAgreementModel.query().findById(testRecord.id) @@ -35,9 +37,7 @@ describe('Financial Agreement model', () => { describe('when linking to licence agreements', () => { let testLicenceAgreements - beforeEach(async () => { - testRecord = await FinancialAgreementHelper.add() - + before(async () => { testLicenceAgreements = [] for (let i = 0; i < 2; i++) { const licenceAgreement = await LicenceAgreementHelper.add({ financialAgreementId: testRecord.id }) diff --git a/test/models/group-role.model.test.js b/test/models/group-role.model.test.js index ffeb84cf08..710d0d90f0 100644 --- a/test/models/group-role.model.test.js +++ b/test/models/group-role.model.test.js @@ -4,7 +4,7 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Test helpers @@ -17,14 +17,23 @@ const RoleModel = require('../../app/models/role.model.js') // Thing under test const GroupRoleModel = require('../../app/models/group-role.model.js') +const GROUP_BILLING_DATA_INDEX = 1 +const GROUP_ROLE_BILLING_DATA_INDEX = 18 +const ROLE_BILLING_INDEX = 8 + describe('Group Role model', () => { + let testGroup let testRecord + let testRole - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await GroupRoleHelper.add() - }) + before(async () => { + testRecord = GroupRoleHelper.select(GROUP_ROLE_BILLING_DATA_INDEX) + testGroup = GroupHelper.select(GROUP_BILLING_DATA_INDEX) + testRole = RoleHelper.select(ROLE_BILLING_INDEX) + }) + + describe('Basic query', () => { it('can successfully run a basic query', async () => { const result = await GroupRoleModel.query() .findById(testRecord.id) @@ -36,13 +45,6 @@ describe('Group Role model', () => { describe('Relationships', () => { describe('when linking to role', () => { - let testRole - - beforeEach(async () => { - testRole = await RoleHelper.add() - testRecord = await GroupRoleHelper.add({ roleId: testRole.id }) - }) - it('can successfully run a related query', async () => { const query = await GroupRoleModel.query() .innerJoinRelated('role') @@ -59,18 +61,11 @@ describe('Group Role model', () => { expect(result.id).to.equal(testRecord.id) expect(result.role).to.be.an.instanceOf(RoleModel) - expect(result.role).to.equal(testRole) + expect(result.role).to.equal(testRole, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking to group', () => { - let testGroup - - beforeEach(async () => { - testGroup = await GroupHelper.add() - testRecord = await GroupRoleHelper.add({ groupId: testGroup.id }) - }) - it('can successfully run a related query', async () => { const query = await GroupRoleModel.query() .innerJoinRelated('group') @@ -87,7 +82,7 @@ describe('Group Role model', () => { expect(result.id).to.equal(testRecord.id) expect(result.group).to.be.an.instanceOf(GroupModel) - expect(result.group).to.equal(testGroup) + expect(result.group).to.equal(testGroup, { skip: ['createdAt', 'updatedAt'] }) }) }) }) diff --git a/test/models/group.model.test.js b/test/models/group.model.test.js index f81a53eea3..ae031a3914 100644 --- a/test/models/group.model.test.js +++ b/test/models/group.model.test.js @@ -4,11 +4,10 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach, before } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { generateUUID } = require('../../app/lib/general.lib.js') const GroupHelper = require('../support/helpers/group.helper.js') const GroupRoleHelper = require('../support/helpers/group-role.helper.js') const GroupRoleModel = require('../../app/models/group-role.model.js') @@ -22,14 +21,29 @@ const UserModel = require('../../app/models/user.model.js') // Thing under test const GroupModel = require('../../app/models/group.model.js') +const GROUP_ROLE_WIRS_RTNS_INDEX = 5 +const GROUP_WIRS_INDEX = 2 +const ROLE_RTNS_INDEX = 0 +const USER_GROUP_WIRS_INDEX = 3 +const USER_WIRS_INDEX = 3 + describe('Group model', () => { + let testGroupRole let testRecord + let testRole + let testUser + let testUserGroup - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await GroupHelper.add() - }) + before(async () => { + testRecord = GroupHelper.select(GROUP_WIRS_INDEX) + + testGroupRole = GroupRoleHelper.select(GROUP_ROLE_WIRS_RTNS_INDEX) + testRole = RoleHelper.select(ROLE_RTNS_INDEX) + testUser = UserHelper.select(USER_WIRS_INDEX) + testUserGroup = UserGroupHelper.select(USER_GROUP_WIRS_INDEX) + }) + describe('Basic query', () => { it('can successfully run a basic query', async () => { const result = await GroupModel.query().findById(testRecord.id) @@ -40,13 +54,6 @@ describe('Group model', () => { describe('Relationships', () => { describe('when linking to group roles', () => { - let testGroupRole - - beforeEach(async () => { - testRecord = await GroupHelper.add() - testGroupRole = await GroupRoleHelper.add({ groupId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await GroupModel.query() .innerJoinRelated('groupRoles') @@ -65,19 +72,11 @@ describe('Group model', () => { expect(result.groupRoles).to.be.an.array() expect(result.groupRoles).to.have.length(1) expect(result.groupRoles[0]).to.be.an.instanceOf(GroupRoleModel) - expect(result.groupRoles[0]).to.equal(testGroupRole) + expect(result.groupRoles[0]).to.equal(testGroupRole, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking through group roles to roles', () => { - let testRole - - beforeEach(async () => { - testRecord = await GroupHelper.add() - testRole = await RoleHelper.add() - await GroupRoleHelper.add({ groupId: testRecord.id, roleId: testRole.id }) - }) - it('can successfully run a related query', async () => { const query = await GroupModel.query() .innerJoinRelated('roles') @@ -96,18 +95,11 @@ describe('Group model', () => { expect(result.roles).to.be.an.array() expect(result.roles).to.have.length(1) expect(result.roles[0]).to.be.an.instanceOf(RoleModel) - expect(result.roles[0]).to.equal(testRole) + expect(result.roles[0]).to.equal(testRole, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking to user groups', () => { - let testUserGroup - - beforeEach(async () => { - testRecord = await GroupHelper.add() - testUserGroup = await UserGroupHelper.add({ groupId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await GroupModel.query() .innerJoinRelated('userGroups') @@ -126,19 +118,11 @@ describe('Group model', () => { expect(result.userGroups).to.be.an.array() expect(result.userGroups).to.have.length(1) expect(result.userGroups[0]).to.be.an.instanceOf(UserGroupModel) - expect(result.userGroups[0]).to.equal(testUserGroup) + expect(result.userGroups[0]).to.equal(testUserGroup, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking through user groups to users', () => { - let testUser - - before(async () => { - testRecord = await GroupHelper.add() - testUser = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - await UserGroupHelper.add({ userId: testUser.id, groupId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await GroupModel.query() .innerJoinRelated('users') @@ -157,7 +141,7 @@ describe('Group model', () => { expect(result.users).to.be.an.array() expect(result.users).to.have.length(1) expect(result.users[0]).to.be.an.instanceOf(UserModel) - expect(result.users[0]).to.equal(testUser) + expect(result.users[0]).to.equal(testUser, { skip: ['createdAt', 'password', 'updatedAt'] }) }) }) }) diff --git a/test/models/licence-agreement.model.test.js b/test/models/licence-agreement.model.test.js index 1ac6800168..95be0ee1ce 100644 --- a/test/models/licence-agreement.model.test.js +++ b/test/models/licence-agreement.model.test.js @@ -4,7 +4,7 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers @@ -17,6 +17,8 @@ const LicenceModel = require('../../app/models/licence.model.js') // Thing under test const LicenceAgreementModel = require('../../app/models/licence-agreement.model.js') +const FINANCIAL_AGREEMENT_MCHG_INDEX = 6 + describe('Licence Agreement model', () => { let testRecord @@ -37,8 +39,8 @@ describe('Licence Agreement model', () => { describe('when linking to financial agreement', () => { let testFinancialAgreement - beforeEach(async () => { - testFinancialAgreement = await FinancialAgreementHelper.add() + before(async () => { + testFinancialAgreement = FinancialAgreementHelper.select(FINANCIAL_AGREEMENT_MCHG_INDEX) const { id: financialAgreementId } = testFinancialAgreement @@ -61,7 +63,7 @@ describe('Licence Agreement model', () => { expect(result.id).to.equal(testRecord.id) expect(result.financialAgreement).to.be.an.instanceOf(FinancialAgreementModel) - expect(result.financialAgreement).to.equal(testFinancialAgreement) + expect(result.financialAgreement).to.equal(testFinancialAgreement, { skip: ['createdAt', 'updatedAt'] }) }) }) diff --git a/test/models/licence-version-purpose-condition-type.model.test.js b/test/models/licence-version-purpose-condition-type.model.test.js index 7ccd29eea1..928edd639c 100644 --- a/test/models/licence-version-purpose-condition-type.model.test.js +++ b/test/models/licence-version-purpose-condition-type.model.test.js @@ -10,13 +10,13 @@ const { expect } = Code // Test helpers const LicenceVersionPurposeConditionHelper = require('../support/helpers/licence-version-purpose-condition.helper.js') const LicenceVersionPurposeConditionModel = require('../../app/models/licence-version-purpose-condition.model.js') -const LicenceVersionPurposesConditionsTypeSeeder = require('../support/seeders/licence-version-purpose-condition-types.seeder.js') +const LicenceVersionPurposeConditionTypeHelper = require('../support/helpers/licence-version-purpose-condition-type.helper.js') // Thing under test const LicenceVersionPurposeConditionTypeModel = require('../../app/models/licence-version-purpose-condition-type.model.js') describe('Licence Version Purposes Condition Type model', () => { - const licenceVersionPurposeConditionType = LicenceVersionPurposesConditionsTypeSeeder.data[0] + const licenceVersionPurposeConditionType = LicenceVersionPurposeConditionTypeHelper.select() describe('Basic query', () => { it('can successfully run a basic query', async () => { diff --git a/test/models/licence-version-purpose-condition.model.test.js b/test/models/licence-version-purpose-condition.model.test.js index 1db7d35d05..7a63c89325 100644 --- a/test/models/licence-version-purpose-condition.model.test.js +++ b/test/models/licence-version-purpose-condition.model.test.js @@ -9,10 +9,10 @@ const { expect } = Code // Test helpers const LicenceVersionPurposeConditionHelper = require('../support/helpers/licence-version-purpose-condition.helper.js') +const LicenceVersionPurposeConditionTypeHelper = require('../support/helpers/licence-version-purpose-condition-type.helper.js') const LicenceVersionPurposeConditionTypeModel = require('../../app/models/licence-version-purpose-condition-type.model.js') const LicenceVersionPurposeHelper = require('../support/helpers/licence-version-purpose.helper.js') const LicenceVersionPurposeModel = require('../../app/models/licence-version-purpose.model.js') -const LicenceVersionPurposesConditionsTypeSeeder = require('../support/seeders/licence-version-purpose-condition-types.seeder.js') // Thing under test const LicenceVersionPurposeConditionModel = require('../../app/models/licence-version-purpose-condition.model.js') @@ -66,7 +66,7 @@ describe('Licence Version Purpose Condition model', () => { }) describe('when linking to licence version purpose condition type', () => { - const licenceVersionPurposeConditionType = LicenceVersionPurposesConditionsTypeSeeder.data[0] + const licenceVersionPurposeConditionType = LicenceVersionPurposeConditionTypeHelper.select() beforeEach(async () => { testRecord = await LicenceVersionPurposeConditionHelper.add({ diff --git a/test/models/licence-version-purpose.model.test.js b/test/models/licence-version-purpose.model.test.js index a899c7a23b..49532b6447 100644 --- a/test/models/licence-version-purpose.model.test.js +++ b/test/models/licence-version-purpose.model.test.js @@ -13,26 +13,26 @@ const LicenceVersionModel = require('../../app/models/licence-version.model.js') const LicenceVersionPurposeConditionHelper = require('../support/helpers/licence-version-purpose-condition.helper.js') const LicenceVersionPurposeConditionModel = require('../../app/models/licence-version-purpose-condition.model.js') const LicenceVersionPurposeHelper = require('../support/helpers/licence-version-purpose.helper.js') +const PrimaryPurposeHelper = require('../support/helpers/primary-purpose.helper.js') const PrimaryPurposeModel = require('../../app/models/primary-purpose.model.js') -const PrimaryPurposesSeeder = require('../support/seeders/primary-purpose.seeder.js') +const PurposeHelper = require('../support/helpers/purpose.helper.js') const PurposeModel = require('../../app/models/purpose.model.js') -const PurposesSeeder = require('../support/seeders/purposes.seeder.js') +const SecondaryPurposeHelper = require('../support/helpers/secondary-purpose.helper.js') const SecondaryPurposeModel = require('../../app/models/secondary-purpose.model.js') -const SecondaryPurposeSeeder = require('../support/seeders/secondary-purpose.seeder.js') // Thing under test const LicenceVersionPurposeModel = require('../../app/models/licence-version-purpose.model.js') -describe('Licence Version Purposes model', () => { +describe('Licence Version Purpose model', () => { let testRecord let secondaryPurposeId let primaryPurposeId let purposeId beforeEach(() => { - primaryPurposeId = PrimaryPurposesSeeder.data[0].id - secondaryPurposeId = SecondaryPurposeSeeder.data[0].id - purposeId = PurposesSeeder.data[0].id + primaryPurposeId = PrimaryPurposeHelper.select().id + secondaryPurposeId = SecondaryPurposeHelper.select().id + purposeId = PurposeHelper.select().id }) describe('Basic query', () => { @@ -218,19 +218,19 @@ describe('Licence Version Purposes model', () => { let validSecondaryPurpose beforeEach(() => { - invalidPrimaryPurpose = PrimaryPurposesSeeder.data[0] - invalidSecondaryPurpose = SecondaryPurposeSeeder.data[0] - invalidPurpose = PurposesSeeder.data.find((purpose) => { + invalidPrimaryPurpose = PrimaryPurposeHelper.select(0) + invalidSecondaryPurpose = SecondaryPurposeHelper.select() + invalidPurpose = PurposeHelper.data.find((purpose) => { return purpose.legacyId === '400' }) - validPrimaryPurpose = PrimaryPurposesSeeder.data.find((primaryPurpose) => { + validPrimaryPurpose = PrimaryPurposeHelper.data.find((primaryPurpose) => { return primaryPurpose.legacyId === 'P' }) - validSecondaryPurpose = SecondaryPurposeSeeder.data.find((secondaryPurpose) => { + validSecondaryPurpose = SecondaryPurposeHelper.data.find((secondaryPurpose) => { return secondaryPurpose.legacyId === 'ELC' }) - validPurpose = PurposesSeeder.data.find((purpose) => { + validPurpose = PurposeHelper.data.find((purpose) => { return purpose.legacyId === '200' }) }) diff --git a/test/models/licence-version.model.test.js b/test/models/licence-version.model.test.js index 79e709658f..74de5a9992 100644 --- a/test/models/licence-version.model.test.js +++ b/test/models/licence-version.model.test.js @@ -12,8 +12,8 @@ const LicenceHelper = require('../support/helpers/licence.helper.js') const LicenceModel = require('../../app/models/licence.model.js') const LicenceVersionHelper = require('../support/helpers/licence-version.helper.js') const LicenceVersionPurposesHelper = require('../support/helpers/licence-version-purpose.helper.js') +const PurposeHelper = require('../support/helpers/purpose.helper.js') const PurposeModel = require('../../app/models/purpose.model.js') -const PurposesSeeder = require('../support/seeders/purposes.seeder.js') // Thing under test const LicenceVersionModel = require('../../app/models/licence-version.model.js') @@ -71,7 +71,7 @@ describe('Licence Version model', () => { beforeEach(async () => { testRecord = await LicenceVersionHelper.add() - purpose = PurposesSeeder.data[0] + purpose = PurposeHelper.select() const { id } = testRecord @@ -97,7 +97,7 @@ describe('Licence Version model', () => { expect(result.id).to.equal(testRecord.id) expect(result.purposes[0]).to.be.an.instanceOf(PurposeModel) - expect(result.purposes).to.equal([purpose]) + expect(result.purposes).to.equal([purpose], { skip: ['createdAt', 'updatedAt'] }) }) }) }) diff --git a/test/models/licence.model.test.js b/test/models/licence.model.test.js index 2fb25e0e4c..125c6e442a 100644 --- a/test/models/licence.model.test.js +++ b/test/models/licence.model.test.js @@ -309,7 +309,7 @@ describe('Licence model', () => { let testRegion beforeEach(async () => { - testRegion = await RegionHelper.add() + testRegion = RegionHelper.select() const { id: regionId } = testRegion @@ -332,7 +332,7 @@ describe('Licence model', () => { expect(result.id).to.equal(testRecord.id) expect(result.region).to.be.an.instanceOf(RegionModel) - expect(result.region).to.equal(testRegion) + expect(result.region).to.equal(testRegion, { skip: ['createdAt', 'updatedAt'] }) }) }) diff --git a/test/models/primary-purpose.model.test.js b/test/models/primary-purpose.model.test.js index f8d552c982..2763431d08 100644 --- a/test/models/primary-purpose.model.test.js +++ b/test/models/primary-purpose.model.test.js @@ -10,7 +10,7 @@ const { expect } = Code // Test helpers const LicenceVersionPurposeHelper = require('../support/helpers/licence-version-purpose.helper.js') const LicenceVersionPurposeModel = require('../../app/models/licence-version-purpose.model.js') -const PrimaryPurposeHelper = require('../support/seeders/primary-purpose.seeder.js') +const PrimaryPurposeHelper = require('../support/helpers/primary-purpose.helper.js') const ReturnRequirementPurposeHelper = require('../support/helpers/return-requirement-purpose.helper.js') const ReturnRequirementPurposeModel = require('../../app/models/return-requirement-purpose.model.js') @@ -22,7 +22,7 @@ describe('Primary Purpose model', () => { describe('Basic query', () => { beforeEach(async () => { - testRecord = PrimaryPurposeHelper.data[0] + testRecord = PrimaryPurposeHelper.select() }) it('can successfully run a basic query', async () => { @@ -38,7 +38,7 @@ describe('Primary Purpose model', () => { let testLicenceVersionPurposes beforeEach(async () => { - testRecord = PrimaryPurposeHelper.data[0] + testRecord = PrimaryPurposeHelper.select() testLicenceVersionPurposes = [] for (let i = 0; i < 2; i++) { @@ -76,7 +76,7 @@ describe('Primary Purpose model', () => { let testReturnRequirementPurposes beforeEach(async () => { - testRecord = PrimaryPurposeHelper.data[0] + testRecord = PrimaryPurposeHelper.select() testReturnRequirementPurposes = [] for (let i = 0; i < 2; i++) { diff --git a/test/models/purpose.model.test.js b/test/models/purpose.model.test.js index 3679f1acee..a7b3a78b79 100644 --- a/test/models/purpose.model.test.js +++ b/test/models/purpose.model.test.js @@ -14,7 +14,7 @@ const ChargeReferenceHelper = require('../support/helpers/charge-reference.helpe const ChargeReferenceModel = require('../../app/models/charge-reference.model.js') const LicenceVersionPurposeHelper = require('../support/helpers/licence-version-purpose.helper.js') const LicenceVersionPurposeModel = require('../../app/models/licence-version-purpose.model.js') -const PurposeSeeder = require('../support/seeders/purposes.seeder.js') +const PurposeHelper = require('../support/helpers/purpose.helper.js') const ReturnRequirementPurposeHelper = require('../support/helpers/return-requirement-purpose.helper.js') const ReturnRequirementPurposeModel = require('../../app/models/return-requirement-purpose.model.js') @@ -26,7 +26,7 @@ describe('Purpose model', () => { describe('Basic query', () => { beforeEach(async () => { - testRecord = PurposeSeeder.data[0] + testRecord = PurposeHelper.select() }) it('can successfully run a basic query', async () => { @@ -42,7 +42,7 @@ describe('Purpose model', () => { let testChargeElements beforeEach(async () => { - testRecord = PurposeSeeder.data[0] + testRecord = PurposeHelper.select() testChargeElements = [] for (let i = 0; i < 2; i++) { @@ -78,7 +78,7 @@ describe('Purpose model', () => { let testChargeReferences beforeEach(async () => { - testRecord = PurposeSeeder.data[0] + testRecord = PurposeHelper.select() testChargeReferences = [] for (let i = 0; i < 2; i++) { @@ -114,7 +114,7 @@ describe('Purpose model', () => { let testLicenceVersionPurposes beforeEach(async () => { - testRecord = PurposeSeeder.data[0] + testRecord = PurposeHelper.select() testLicenceVersionPurposes = [] for (let i = 0; i < 2; i++) { @@ -150,7 +150,7 @@ describe('Purpose model', () => { let testReturnRequirementPurposes beforeEach(async () => { - testRecord = PurposeSeeder.data[0] + testRecord = PurposeHelper.select() testReturnRequirementPurposes = [] for (let i = 0; i < 2; i++) { diff --git a/test/models/region.model.test.js b/test/models/region.model.test.js index 15138ed104..b96fad7027 100644 --- a/test/models/region.model.test.js +++ b/test/models/region.model.test.js @@ -20,8 +20,8 @@ const RegionModel = require('../../app/models/region.model.js') describe('Region model', () => { let testRecord - beforeEach(async () => { - testRecord = await RegionHelper.add() + beforeEach(() => { + testRecord = RegionHelper.select() }) describe('Basic query', () => { diff --git a/test/models/return-requirement-purpose.model.test.js b/test/models/return-requirement-purpose.model.test.js index 4056c9fe6d..0fb80fe7dd 100644 --- a/test/models/return-requirement-purpose.model.test.js +++ b/test/models/return-requirement-purpose.model.test.js @@ -8,15 +8,15 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers +const PrimaryPurposeHelper = require('../support/helpers/primary-purpose.helper.js') const PrimaryPurposeModel = require('../../app/models/primary-purpose.model.js') -const PrimaryPurposesSeeder = require('../support/seeders/primary-purpose.seeder.js') +const PurposeHelper = require('../support/helpers/purpose.helper.js') const PurposeModel = require('../../app/models/purpose.model.js') -const PurposesSeeder = require('../support/seeders/purposes.seeder.js') const ReturnRequirementHelper = require('../support/helpers/return-requirement.helper.js') const ReturnRequirementModel = require('../../app/models/return-requirement.model.js') const ReturnRequirementPurposeHelper = require('../support/helpers/return-requirement-purpose.helper.js') +const SecondaryPurposeHelper = require('../support/helpers/secondary-purpose.helper.js') const SecondaryPurposeModel = require('../../app/models/secondary-purpose.model.js') -const SecondaryPurposesSeeder = require('../support/seeders/secondary-purpose.seeder.js') // Thing under test const ReturnRequirementPurposeModel = require('../../app/models/return-requirement-purpose.model.js') @@ -42,7 +42,7 @@ describe('Return Requirement Purpose model', () => { let testPrimaryPurpose beforeEach(async () => { - testPrimaryPurpose = PrimaryPurposesSeeder.data[0] + testPrimaryPurpose = PrimaryPurposeHelper.select() testRecord = await ReturnRequirementPurposeHelper.add({ primaryPurposeId: testPrimaryPurpose.id }) }) @@ -63,7 +63,7 @@ describe('Return Requirement Purpose model', () => { expect(result.id).to.equal(testRecord.id) expect(result.primaryPurpose).to.be.an.instanceOf(PrimaryPurposeModel) - expect(result.primaryPurpose.id).to.equal(testPrimaryPurpose.id) + expect(result.primaryPurpose).to.equal(testPrimaryPurpose, { skip: ['createdAt', 'updatedAt'] }) }) }) @@ -71,7 +71,7 @@ describe('Return Requirement Purpose model', () => { let testPurpose beforeEach(async () => { - testPurpose = PurposesSeeder.data[0] + testPurpose = PurposeHelper.select() const { id: purposeId } = testPurpose @@ -94,7 +94,7 @@ describe('Return Requirement Purpose model', () => { expect(result.id).to.equal(testRecord.id) expect(result.purpose).to.be.an.instanceOf(PurposeModel) - expect(result.purpose).to.equal(testPurpose) + expect(result.purpose).to.equal(testPurpose, { skip: ['createdAt', 'updatedAt'] }) }) }) @@ -134,7 +134,7 @@ describe('Return Requirement Purpose model', () => { let testSecondaryPurpose beforeEach(async () => { - testSecondaryPurpose = SecondaryPurposesSeeder.data[0] + testSecondaryPurpose = SecondaryPurposeHelper.select() const { id: secondaryPurposeId } = testSecondaryPurpose diff --git a/test/models/return-version.model.test.js b/test/models/return-version.model.test.js index 37916b5343..bee50e64c5 100644 --- a/test/models/return-version.model.test.js +++ b/test/models/return-version.model.test.js @@ -8,7 +8,6 @@ const { describe, it, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { generateUUID } = require('../../app/lib/general.lib.js') const LicenceHelper = require('../support/helpers/licence.helper.js') const LicenceModel = require('../../app/models/licence.model.js') const ReturnRequirementHelper = require('../support/helpers/return-requirement.helper.js') @@ -110,7 +109,7 @@ describe('Return Version model', () => { let testUser beforeEach(async () => { - testUser = await UserHelper.add({ username: `${generateUUID()}@test.com` }) + testUser = UserHelper.select() const { id: createdBy } = testUser @@ -133,7 +132,7 @@ describe('Return Version model', () => { expect(result.id).to.equal(testRecord.id) expect(result.user).to.be.an.instanceOf(UserModel) - expect(result.user).to.equal(testUser) + expect(result.user).to.equal(testUser, { skip: ['createdAt', 'password', 'updatedAt'] }) }) }) }) diff --git a/test/models/role.model.test.js b/test/models/role.model.test.js index 9cda342dd7..4531d17ff3 100644 --- a/test/models/role.model.test.js +++ b/test/models/role.model.test.js @@ -4,11 +4,10 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { generateUUID } = require('../../app/lib/general.lib.js') const GroupHelper = require('../support/helpers/group.helper.js') const GroupModel = require('../../app/models/group.model.js') const GroupRoleHelper = require('../support/helpers/group-role.helper.js') @@ -22,14 +21,28 @@ const UserRoleModel = require('../../app/models/user-role.model.js') // Thing under test const RoleModel = require('../../app/models/role.model.js') +const GROUP_ROLE_SUPER_AR_USER_INDEX = 16 +const GROUP_SUPER_INDEX = 5 +const ROLE_AR_USER_INDEX = 6 +const USER_SUPER_INDEX = 1 + describe('Role model', () => { + let testGroup + let testGroupRole let testRecord + let testUser + let testUserRole + + before(async () => { + // This combination has one match in group roles and so ensures we only get one result making testing clearer + testRecord = RoleHelper.select(ROLE_AR_USER_INDEX) + testGroup = GroupHelper.select(GROUP_SUPER_INDEX) + testGroupRole = GroupRoleHelper.select(GROUP_ROLE_SUPER_AR_USER_INDEX) + testUser = UserHelper.select(USER_SUPER_INDEX) + testUserRole = await UserRoleHelper.add({ roleId: testRecord.id, userId: testUser.id }) + }) describe('Basic query', () => { - beforeEach(async () => { - testRecord = await RoleHelper.add() - }) - it('can successfully run a basic query', async () => { const result = await RoleModel.query().findById(testRecord.id) @@ -40,13 +53,6 @@ describe('Role model', () => { describe('Relationships', () => { describe('when linking to group roles', () => { - let testGroupRole - - beforeEach(async () => { - testRecord = await RoleHelper.add() - testGroupRole = await GroupRoleHelper.add({ roleId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await RoleModel.query() .innerJoinRelated('groupRoles') @@ -65,18 +71,11 @@ describe('Role model', () => { expect(result.groupRoles).to.be.an.array() expect(result.groupRoles).to.have.length(1) expect(result.groupRoles[0]).to.be.an.instanceOf(GroupRoleModel) - expect(result.groupRoles[0]).to.equal(testGroupRole) + expect(result.groupRoles[0]).to.equal(testGroupRole, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking to user roles', () => { - let testUserRole - - beforeEach(async () => { - testRecord = await RoleHelper.add() - testUserRole = await UserRoleHelper.add({ roleId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await RoleModel.query() .innerJoinRelated('userRoles') @@ -100,14 +99,6 @@ describe('Role model', () => { }) describe('when linking through group roles to groups', () => { - let testGroup - - beforeEach(async () => { - testRecord = await RoleHelper.add() - testGroup = await GroupHelper.add() - await GroupRoleHelper.add({ roleId: testRecord.id, groupId: testGroup.id }) - }) - it('can successfully run a related query', async () => { const query = await RoleModel.query() .innerJoinRelated('groups') @@ -126,19 +117,11 @@ describe('Role model', () => { expect(result.groups).to.be.an.array() expect(result.groups).to.have.length(1) expect(result.groups[0]).to.be.an.instanceOf(GroupModel) - expect(result.groups[0]).to.equal(testGroup) + expect(result.groups[0]).to.equal(testGroup, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking through user roles to users', () => { - let testUser - - beforeEach(async () => { - testRecord = await RoleHelper.add() - testUser = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - await UserRoleHelper.add({ userId: testUser.id, roleId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await RoleModel.query() .innerJoinRelated('users') @@ -157,7 +140,7 @@ describe('Role model', () => { expect(result.users).to.be.an.array() expect(result.users).to.have.length(1) expect(result.users[0]).to.be.an.instanceOf(UserModel) - expect(result.users[0]).to.equal(testUser) + expect(result.users[0]).to.equal(testUser, { skip: ['createdAt', 'password', 'updatedAt'] }) }) }) }) diff --git a/test/models/secondary-purpose.model.test.js b/test/models/secondary-purpose.model.test.js index 4ab3c1599e..5ee598d896 100644 --- a/test/models/secondary-purpose.model.test.js +++ b/test/models/secondary-purpose.model.test.js @@ -12,13 +12,13 @@ const LicenceVersionPurposeHelper = require('../support/helpers/licence-version- const LicenceVersionPurposeModel = require('../../app/models/licence-version-purpose.model.js') const ReturnRequirementPurposeHelper = require('../support/helpers/return-requirement-purpose.helper.js') const ReturnRequirementPurposeModel = require('../../app/models/return-requirement-purpose.model.js') -const SecondaryPurposeSeeder = require('../support/seeders/secondary-purpose.seeder.js') +const SecondaryPurposeHelper = require('../support/helpers/secondary-purpose.helper.js') // Thing under test const SecondaryPurposeModel = require('../../app/models/secondary-purpose.model.js') describe('Secondary Purpose model', () => { - const testRecordId = SecondaryPurposeSeeder.data[0].id + const testRecordId = SecondaryPurposeHelper.select().id describe('Basic query', () => { it('can successfully run a basic query', async () => { diff --git a/test/models/user-group.model.test.js b/test/models/user-group.model.test.js index e45eb52415..db0dc3a5f1 100644 --- a/test/models/user-group.model.test.js +++ b/test/models/user-group.model.test.js @@ -4,11 +4,10 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { generateUUID } = require('../../app/lib/general.lib.js') const GroupModel = require('../../app/models/group.model.js') const GroupHelper = require('../support/helpers/group.helper.js') const UserGroupHelper = require('../support/helpers/user-group.helper.js') @@ -18,14 +17,23 @@ const UserHelper = require('../support/helpers/user.helper.js') // Thing under test const UserGroupModel = require('../../app/models/user-group.model.js') +const GROUP_WIRS_INDEX = 2 +const USER_GROUP_WIRS_INDEX = 3 +const USER_WIRS_INDEX = 3 + describe('User Group model', () => { + let testGroup let testRecord + let testUser - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await UserGroupHelper.add() - }) + before(async () => { + testRecord = UserGroupHelper.select(USER_GROUP_WIRS_INDEX) + testGroup = GroupHelper.select(GROUP_WIRS_INDEX) + testUser = UserHelper.select(USER_WIRS_INDEX) + }) + + describe('Basic query', () => { it('can successfully run a basic query', async () => { const result = await UserGroupModel.query().findById(testRecord.id) @@ -36,13 +44,6 @@ describe('User Group model', () => { describe('Relationships', () => { describe('when linking to group', () => { - let testGroup - - beforeEach(async () => { - testGroup = await GroupHelper.add() - testRecord = await UserGroupHelper.add({ groupId: testGroup.id }) - }) - it('can successfully run a related query', async () => { const query = await UserGroupModel.query() .innerJoinRelated('group') @@ -59,18 +60,11 @@ describe('User Group model', () => { expect(result.id).to.equal(testRecord.id) expect(result.group).to.be.an.instanceOf(GroupModel) - expect(result.group).to.equal(testGroup) + expect(result.group).to.equal(testGroup, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking to user', () => { - let testUser - - beforeEach(async () => { - testUser = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - testRecord = await UserGroupHelper.add({ userId: testUser.id }) - }) - it('can successfully run a related query', async () => { const query = await UserGroupModel.query() .innerJoinRelated('user') @@ -87,7 +81,7 @@ describe('User Group model', () => { expect(result.id).to.equal(testRecord.id) expect(result.user).to.be.an.instanceOf(UserModel) - expect(result.user).to.equal(testUser) + expect(result.user).to.equal(testUser, { skip: ['createdAt', 'password', 'updatedAt'] }) }) }) }) diff --git a/test/models/user-role.model.test.js b/test/models/user-role.model.test.js index c219b82a43..8dc448109a 100644 --- a/test/models/user-role.model.test.js +++ b/test/models/user-role.model.test.js @@ -4,11 +4,10 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { generateUUID } = require('../../app/lib/general.lib.js') const RoleModel = require('../../app/models/role.model.js') const RoleHelper = require('../support/helpers/role.helper.js') const UserRoleHelper = require('../support/helpers/user-role.helper.js') @@ -18,14 +17,21 @@ const UserHelper = require('../support/helpers/user.helper.js') // Thing under test const UserRoleModel = require('../../app/models/user-role.model.js') +const ROLE_RENEWAL_NOTIFICATIONS_INDEX = 5 +const USER_NPS_INDEX = 6 + describe('User Role model', () => { let testRecord + let testRole + let testUser - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await UserRoleHelper.add() - }) + before(async () => { + testRole = RoleHelper.select(ROLE_RENEWAL_NOTIFICATIONS_INDEX) + testUser = UserHelper.select(USER_NPS_INDEX) + testRecord = await UserRoleHelper.add({ roleId: testRole.id, userId: testUser.id }) + }) + describe('Basic query', () => { it('can successfully run a basic query', async () => { const result = await UserRoleModel.query().findById(testRecord.id) @@ -36,13 +42,6 @@ describe('User Role model', () => { describe('Relationships', () => { describe('when linking to role', () => { - let testRole - - beforeEach(async () => { - testRole = await RoleHelper.add() - testRecord = await UserRoleHelper.add({ roleId: testRole.id }) - }) - it('can successfully run a related query', async () => { const query = await UserRoleModel.query() .innerJoinRelated('role') @@ -59,18 +58,11 @@ describe('User Role model', () => { expect(result.id).to.equal(testRecord.id) expect(result.role).to.be.an.instanceOf(RoleModel) - expect(result.role).to.equal(testRole) + expect(result.role).to.equal(testRole, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking to user', () => { - let testUser - - beforeEach(async () => { - testUser = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - testRecord = await UserRoleHelper.add({ userId: testUser.id }) - }) - it('can successfully run a related query', async () => { const query = await UserRoleModel.query() .innerJoinRelated('user') @@ -87,7 +79,7 @@ describe('User Role model', () => { expect(result.id).to.equal(testRecord.id) expect(result.user).to.be.an.instanceOf(UserModel) - expect(result.user).to.equal(testUser) + expect(result.user).to.equal(testUser, { skip: ['createdAt', 'password', 'updatedAt'] }) }) }) }) diff --git a/test/models/user.model.test.js b/test/models/user.model.test.js index 84274d2613..7296d315f2 100644 --- a/test/models/user.model.test.js +++ b/test/models/user.model.test.js @@ -4,11 +4,10 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const { generateUUID } = require('../../app/lib/general.lib.js') const GroupHelper = require('../support/helpers/group.helper.js') const GroupModel = require('../../app/models/group.model.js') const ReturnVersionHelper = require('../support/helpers/return-version.helper.js') @@ -24,14 +23,29 @@ const UserRoleModel = require('../../app/models/user-role.model.js') // Thing under test const UserModel = require('../../app/models/user.model.js') +const GROUP_WIRS_INDEX = 2 +const ROLE_RETURNS_INDEX = 0 +const USER_GROUP_WIRS_INDEX = 3 +const USER_WIRS_INDEX = 3 + describe('User model', () => { + let testGroup let testRecord + let testRole + let testUserRole + let testUserGroup - describe('Basic query', () => { - beforeEach(async () => { - testRecord = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - }) + before(async () => { + testRecord = UserHelper.select(USER_WIRS_INDEX) + + testRole = RoleHelper.select(ROLE_RETURNS_INDEX) + testGroup = GroupHelper.select(GROUP_WIRS_INDEX) + testUserGroup = UserGroupHelper.select(USER_GROUP_WIRS_INDEX) + + testUserRole = await UserRoleHelper.add({ userId: testRecord.id, roleId: testRole.id }) + }) + describe('Basic query', () => { it('can successfully run a basic query', async () => { const result = await UserModel.query().findById(testRecord.id) @@ -42,14 +56,6 @@ describe('User model', () => { describe('Relationships', () => { describe('when linking through user groups to groups', () => { - let testGroup - - beforeEach(async () => { - testRecord = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - testGroup = await GroupHelper.add() - await UserGroupHelper.add({ userId: testRecord.id, groupId: testGroup.id }) - }) - it('can successfully run a related query', async () => { const query = await UserModel.query() .innerJoinRelated('groups') @@ -68,7 +74,7 @@ describe('User model', () => { expect(result.groups).to.be.an.array() expect(result.groups).to.have.length(1) expect(result.groups[0]).to.be.an.instanceOf(GroupModel) - expect(result.groups[0]).to.equal(testGroup) + expect(result.groups[0]).to.equal(testGroup, { skip: ['createdAt', 'updatedAt'] }) }) }) @@ -76,8 +82,6 @@ describe('User model', () => { let testReturnVersions beforeEach(async () => { - testRecord = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - testReturnVersions = [] for (let i = 0; i < 2; i++) { const returnVersion = await ReturnVersionHelper.add({ createdBy: testRecord.id }) @@ -109,14 +113,6 @@ describe('User model', () => { }) describe('when linking through user roles to roles', () => { - let testRole - - beforeEach(async () => { - testRecord = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - testRole = await RoleHelper.add() - await UserRoleHelper.add({ userId: testRecord.id, roleId: testRole.id }) - }) - it('can successfully run a related query', async () => { const query = await UserModel.query() .innerJoinRelated('roles') @@ -135,18 +131,11 @@ describe('User model', () => { expect(result.roles).to.be.an.array() expect(result.roles).to.have.length(1) expect(result.roles[0]).to.be.an.instanceOf(RoleModel) - expect(result.roles[0]).to.equal(testRole) + expect(result.roles[0]).to.equal(testRole, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking to user groups', () => { - let testUserGroup - - beforeEach(async () => { - testRecord = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - testUserGroup = await UserGroupHelper.add({ userId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await UserModel.query() .innerJoinRelated('userGroups') @@ -165,18 +154,11 @@ describe('User model', () => { expect(result.userGroups).to.be.an.array() expect(result.userGroups).to.have.length(1) expect(result.userGroups[0]).to.be.an.instanceOf(UserGroupModel) - expect(result.userGroups[0]).to.equal(testUserGroup) + expect(result.userGroups[0]).to.equal(testUserGroup, { skip: ['createdAt', 'updatedAt'] }) }) }) describe('when linking to user roles', () => { - let testUserRole - - beforeEach(async () => { - testRecord = await UserHelper.add({ username: `${generateUUID()}@test.com` }) - testUserRole = await UserRoleHelper.add({ userId: testRecord.id }) - }) - it('can successfully run a related query', async () => { const query = await UserModel.query() .innerJoinRelated('userRoles') diff --git a/test/presenters/bill-runs/create-bill-run-event.presenter.test.js b/test/presenters/bill-runs/create-bill-run-event.presenter.test.js index e13e3cc44c..7a9d7afeb8 100644 --- a/test/presenters/bill-runs/create-bill-run-event.presenter.test.js +++ b/test/presenters/bill-runs/create-bill-run-event.presenter.test.js @@ -25,7 +25,7 @@ describe('Create Bill Run Event presenter', () => { let billRun beforeEach(async () => { - const region = await RegionHelper.add() + const region = RegionHelper.select() const testBillRun = await BillRunHelper.add({ regionId: region.id }) billRun = await BillRunModel.query() diff --git a/test/presenters/charging-module/create-transaction.presenter.test.js b/test/presenters/charging-module/create-transaction.presenter.test.js index 43810d9106..98c8e91631 100644 --- a/test/presenters/charging-module/create-transaction.presenter.test.js +++ b/test/presenters/charging-module/create-transaction.presenter.test.js @@ -31,7 +31,7 @@ describe('Charging Module Create Transaction presenter', () => { describe('when provided with a Transaction and Licence instance', () => { beforeEach(async () => { - region = await RegionHelper.add() + region = RegionHelper.select() // NOTE: In the context the presenter is used it is from a Licence instance returned by // FetchChargeVersionsService. We recreate how that instance is formed here, including extracting some of the @@ -40,6 +40,7 @@ describe('Charging Module Create Transaction presenter', () => { regionId: region.id, regions: { historicalAreaCode: 'SAAR', regionalChargeArea: 'Southern' } }) + licence = await LicenceModel.query() .findById(tempLicence.id) .select([ diff --git a/test/requests/charging-module/create-bill-run.request.test.js b/test/requests/charging-module/create-bill-run.request.test.js index 9b233d3f0f..75eee69965 100644 --- a/test/requests/charging-module/create-bill-run.request.test.js +++ b/test/requests/charging-module/create-bill-run.request.test.js @@ -24,7 +24,7 @@ describe('Charging Module Create Bill Run request', () => { beforeEach(async () => { await DatabaseSupport.clean() - testRegion = await RegionHelper.add() + testRegion = RegionHelper.select() }) afterEach(() => { diff --git a/test/services/bill-licences/fetch-bill-licence-summary.service.test.js b/test/services/bill-licences/fetch-bill-licence-summary.service.test.js index 1901d10152..70d40e6596 100644 --- a/test/services/bill-licences/fetch-bill-licence-summary.service.test.js +++ b/test/services/bill-licences/fetch-bill-licence-summary.service.test.js @@ -16,7 +16,7 @@ const BillRunHelper = require('../../support/helpers/bill-run.helper.js') const CompanyHelper = require('../../support/helpers/company.helper.js') const ContactHelper = require('../../support/helpers/contact.helper.js') const LicenceHelper = require('../../support/helpers/licence.helper.js') -const RegionsSeeder = require('../../support/seeders/regions.seeder.js') +const RegionHelper = require('../../support/helpers/region.helper.js') const TransactionHelper = require('../../support/helpers/transaction.helper.js') // Thing under test @@ -37,9 +37,7 @@ describe('Fetch Bill Licence Summary service', () => { let transactionId beforeEach(async () => { - const region = RegionsSeeder.data.find((region) => { - return region.displayName === 'Test Region' - }) + const region = RegionHelper.select(RegionHelper.TEST_REGION_INDEX) regionId = region.id licenceRef = LicenceHelper.generateLicenceRef() diff --git a/test/services/bill-licences/fetch-bill-licence.service.test.js b/test/services/bill-licences/fetch-bill-licence.service.test.js index c4d601b760..86096e19d5 100644 --- a/test/services/bill-licences/fetch-bill-licence.service.test.js +++ b/test/services/bill-licences/fetch-bill-licence.service.test.js @@ -18,8 +18,8 @@ const ChargeElementHelper = require('../../support/helpers/charge-element.helper const ChargeElementModel = require('../../../app/models/charge-element.model.js') const ChargeReferenceHelper = require('../../support/helpers/charge-reference.helper.js') const ChargeReferenceModel = require('../../../app/models/charge-reference.model.js') +const PurposeHelper = require('../../support/helpers/purpose.helper.js') const PurposeModel = require('../../../app/models/purpose.model.js') -const PurposeSeeder = require('../../support/seeders/purposes.seeder.js') const TransactionHelper = require('../../support/helpers/transaction.helper.js') const TransactionModel = require('../../../app/models/transaction.model.js') @@ -91,7 +91,7 @@ describe('Fetch Bill Licence service', () => { describe('and it is for an SROC bill run', () => { beforeEach(async () => { linkedChargeReference = await ChargeReferenceHelper.add() - linkedPurpose = PurposeSeeder.data[0] + linkedPurpose = PurposeHelper.select() const { id: chargeReferenceId } = linkedChargeReference @@ -159,7 +159,7 @@ describe('Fetch Bill Licence service', () => { describe('and it is for a PRESROC bill run', () => { beforeEach(async () => { - linkedPurpose = PurposeSeeder.data[0] + linkedPurpose = PurposeHelper.select() linkedChargeReference = await ChargeReferenceHelper.add({ purposeId: linkedPurpose.id }) const { id: chargeReferenceId } = linkedChargeReference diff --git a/test/services/bill-runs/annual/fetch-billing-accounts.service.test.js b/test/services/bill-runs/annual/fetch-billing-accounts.service.test.js index 3b79a323c0..e12821c079 100644 --- a/test/services/bill-runs/annual/fetch-billing-accounts.service.test.js +++ b/test/services/bill-runs/annual/fetch-billing-accounts.service.test.js @@ -38,7 +38,7 @@ describe('Fetch Billing Accounts service', () => { beforeEach(async () => { await DatabaseSupport.clean() - region = await RegionHelper.add({ chargeRegionId: 'W' }) + region = RegionHelper.select() regionId = region.id licence = await LicenceHelper.add({ regionId }) @@ -110,7 +110,7 @@ describe('Fetch Billing Accounts service', () => { expect(licence.historicalAreaCode).to.equal('SAAR') expect(licence.regionalChargeArea).to.equal('Southern') expect(licence.region.id).to.equal(regionId) - expect(licence.region.chargeRegionId).to.equal('W') + expect(licence.region.chargeRegionId).to.equal(region.chargeRegionId) }) it('includes the change reason in each result', async () => { diff --git a/test/services/bill-runs/cancel-bill-run.service.test.js b/test/services/bill-runs/cancel-bill-run.service.test.js index 6c9cff9fa5..36999c6089 100644 --- a/test/services/bill-runs/cancel-bill-run.service.test.js +++ b/test/services/bill-runs/cancel-bill-run.service.test.js @@ -16,17 +16,18 @@ const RegionHelper = require('../../support/helpers/region.helper.js') const CancelBillRunService = require('../../../app/services/bill-runs/cancel-bill-run.service.js') describe('Cancel Bill Run service', () => { + let region let testBillRunId beforeEach(async () => { await DatabaseSupport.clean() - const { id: regionId } = await RegionHelper.add() + region = RegionHelper.select() const billRun = await BillRunHelper.add({ billRunNumber: 10101, createdAt: new Date('2024-02-28'), externalId: 'f54e53f0-37a0-400f-9f0e-bf8575c17668', - regionId, + regionId: region.id, status: 'ready' }) @@ -46,7 +47,7 @@ describe('Cancel Bill Run service', () => { chargeScheme: 'Current', dateCreated: '28 February 2024', financialYear: '2022 to 2023', - region: 'Avalon' + region: region.displayName }) }) }) diff --git a/test/services/bill-runs/create-bill-run-event.service.test.js b/test/services/bill-runs/create-bill-run-event.service.test.js index 6537c74669..63b1343ae4 100644 --- a/test/services/bill-runs/create-bill-run-event.service.test.js +++ b/test/services/bill-runs/create-bill-run-event.service.test.js @@ -40,7 +40,7 @@ describe('Create Bill Run Event service', () => { let billRun beforeEach(async () => { - const region = await RegionHelper.add() + const region = RegionHelper.select() const testBillRun = await BillRunHelper.add({ regionId: region.id }) billRun = await BillRunModel.query() diff --git a/test/services/bill-runs/create-bill-run.service.test.js b/test/services/bill-runs/create-bill-run.service.test.js index 2274d50994..041f4dc65d 100644 --- a/test/services/bill-runs/create-bill-run.service.test.js +++ b/test/services/bill-runs/create-bill-run.service.test.js @@ -23,7 +23,7 @@ describe('Create Bill Run service', () => { beforeEach(async () => { await DatabaseSupport.clean() - region = await RegionHelper.add() + region = RegionHelper.select() }) describe('when the defaults are not overridden', () => { diff --git a/test/services/bill-runs/determine-blocking-bill-run.service.test.js b/test/services/bill-runs/determine-blocking-bill-run.service.test.js index 51a6139327..e6a0981da0 100644 --- a/test/services/bill-runs/determine-blocking-bill-run.service.test.js +++ b/test/services/bill-runs/determine-blocking-bill-run.service.test.js @@ -3,23 +3,25 @@ // Test framework dependencies const Lab = require('@hapi/lab') const Code = require('@hapi/code') +const Sinon = require('sinon') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const BillRunHelper = require('../../support/helpers/bill-run.helper.js') -const RegionHelper = require('../../support/helpers/region.helper.js') const { determineCurrentFinancialYear } = require('../../../app/lib/general.lib.js') const { generateUUID } = require('../../../app/lib/general.lib.js') +// Things we need to stub +const FetchLiveBillRunsService = require('../../../app/services/bill-runs/fetch-live-bill-runs.service.js') +const FetchMatchingBillRunService = require('../../../app/services/bill-runs/fetch-matching-bill-run.service.js') + // Thing under test const DetermineBlockingBillRunService = require('../../../app/services/bill-runs/determine-blocking-bill-run.service.js') describe('Determine Blocking Bill Run service', () => { let batchType - let billRunIdOne - let billRunIdTwo + let billRunId let financialEndYear let regionId let season @@ -30,12 +32,12 @@ describe('Determine Blocking Bill Run service', () => { toFinancialYearEnding = endDate.getFullYear() - const region = await RegionHelper.add() - - regionId = region.id + regionId = generateUUID() + billRunId = generateUUID() + }) - billRunIdOne = generateUUID() - billRunIdTwo = generateUUID() + afterEach(async () => { + Sinon.restore() }) describe('when the user is setting up an annual bill run', () => { @@ -46,10 +48,8 @@ describe('Determine Blocking Bill Run service', () => { describe('and there is a matching bill run', () => { beforeEach(async () => { - await Promise.all([ - BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'annual', status: 'ready', toFinancialYearEnding, scheme: 'sroc' - }) + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([ + { id: billRunId, regionId, batchType, toFinancialYearEnding } ]) }) @@ -57,16 +57,20 @@ describe('Determine Blocking Bill Run service', () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) }) }) describe('and there is no matching bill run', () => { + beforeEach(async () => { + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([]) + }) + describe('but a live bill run exists for the same year', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding, scheme: 'sroc' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: billRunId, regionId, batchType: 'two_part_tariff', toFinancialYearEnding } + ]) }) it('returns the live bill run', async () => { @@ -74,15 +78,13 @@ describe('Determine Blocking Bill Run service', () => { expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) }) }) describe('but a live bill run exists for a different year', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding: toFinancialYearEnding - 1, scheme: 'sroc' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) }) it('returns no matches', async () => { @@ -93,6 +95,10 @@ describe('Determine Blocking Bill Run service', () => { }) describe('and no live bill runs', () => { + beforeEach(async () => { + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) + }) + it('returns no matches', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) @@ -111,10 +117,8 @@ describe('Determine Blocking Bill Run service', () => { describe('and there is a matching bill run', () => { beforeEach(async () => { - await Promise.all([ - BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding, scheme: 'sroc' - }) + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([ + { id: billRunId, regionId, batchType, toFinancialYearEnding } ]) }) @@ -122,27 +126,31 @@ describe('Determine Blocking Bill Run service', () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) }) }) describe('and there is no matching bill run', () => { describe('but a live bill run exists for the same year', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'supplementary', status: 'ready', toFinancialYearEnding, scheme: 'sroc' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: billRunId, regionId, batchType: 'supplementary', toFinancialYearEnding } + ]) }) it('returns the live bill run', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) }) }) describe('and no live bill runs', () => { + beforeEach(async () => { + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) + }) + it('returns no matches', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) @@ -161,10 +169,8 @@ describe('Determine Blocking Bill Run service', () => { describe('and there is a matching bill run', () => { beforeEach(async () => { - await Promise.all([ - BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding: 2022, summer: true, scheme: 'alcs' - }) + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([ + { id: billRunId, regionId, batchType, toFinancialYearEnding: 2022 } ]) }) @@ -172,31 +178,29 @@ describe('Determine Blocking Bill Run service', () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear, season) expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) }) }) describe('and there is no matching bill run', () => { describe('but a live bill run exists for the same year', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'supplementary', status: 'ready', toFinancialYearEnding: 2022, scheme: 'alcs' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: billRunId, regionId, batchType: 'supplementary', toFinancialYearEnding: 2022 } + ]) }) it('returns the live bill run', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear, season) expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) }) }) describe('but a live bill run exists for a different year', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'annual', status: 'ready', toFinancialYearEnding: toFinancialYearEnding - 1, scheme: 'sroc' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) }) it('returns no matches', async () => { @@ -207,6 +211,10 @@ describe('Determine Blocking Bill Run service', () => { }) describe('and no live bill runs', () => { + beforeEach(async () => { + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) + }) + it('returns no matches', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear, season) @@ -218,6 +226,8 @@ describe('Determine Blocking Bill Run service', () => { }) describe('when the user is setting up a supplementary bill run', () => { + const presrocBillRunId = generateUUID() + beforeEach(() => { batchType = 'supplementary' financialEndYear = toFinancialYearEnding @@ -225,13 +235,9 @@ describe('Determine Blocking Bill Run service', () => { describe('and there is both an SROC and PRESROC matching bill run', () => { beforeEach(async () => { - await Promise.all([ - BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'supplementary', status: 'ready', toFinancialYearEnding, scheme: 'sroc' - }), - BillRunHelper.add({ - id: billRunIdTwo, regionId, batchType: 'supplementary', status: 'ready', toFinancialYearEnding: 2022, scheme: 'alcs' - }) + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([ + { id: billRunId, regionId, batchType, toFinancialYearEnding }, + { id: presrocBillRunId, regionId, batchType, toFinancialYearEnding: 2022 } ]) }) @@ -239,112 +245,128 @@ describe('Determine Blocking Bill Run service', () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(2) - expect(results[0].id).to.equal(billRunIdOne) - expect(results[1].id).to.equal(billRunIdTwo) + expect(results[0].id).to.equal(billRunId) + expect(results[1].id).to.equal(presrocBillRunId) }) }) describe('and there is only an SROC matching bill run', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'supplementary', status: 'ready', toFinancialYearEnding, scheme: 'sroc' - }) + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([ + { id: billRunId, regionId, batchType, toFinancialYearEnding } + ]) }) describe('and no live PRESROC bill runs', () => { + beforeEach(async () => { + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) + }) + it('returns just the single match', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) }) }) describe('and a live PRESROC bill run', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdTwo, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding: 2022, scheme: 'alcs' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: billRunId, regionId, batchType, toFinancialYearEnding }, + { id: presrocBillRunId, regionId, batchType: 'two_part_tariff', toFinancialYearEnding: 2022 } + ]) }) it('returns both the matched and live bill runs', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(2) - expect(results[0].id).to.equal(billRunIdOne) - expect(results[1].id).to.equal(billRunIdTwo) + expect(results[0].id).to.equal(billRunId) + expect(results[1].id).to.equal(presrocBillRunId) }) }) }) describe('and there is only a PRESROC matching bill run', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdTwo, regionId, batchType: 'supplementary', status: 'ready', toFinancialYearEnding: 2022, scheme: 'alcs' - }) + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([ + { id: presrocBillRunId, regionId, batchType, toFinancialYearEnding: 2022 } + ]) }) describe('and no live SROC bill runs', () => { + beforeEach(async () => { + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) + }) + it('returns just the single match', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdTwo) + expect(results[0].id).to.equal(presrocBillRunId) }) }) describe('and a live SROC bill run', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding, scheme: 'sroc' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: billRunId, regionId, batchType: 'two_part_tariff', toFinancialYearEnding }, + { id: presrocBillRunId, regionId, batchType, toFinancialYearEnding: 2022 } + ]) }) it('returns both the matched and live bill runs', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) expect(results).to.have.length(2) - expect(results[1].id).to.equal(billRunIdTwo) - expect(results[0].id).to.equal(billRunIdOne) + expect(results[0].id).to.equal(billRunId) + expect(results[1].id).to.equal(presrocBillRunId) }) }) }) describe('and there is no matching bill run', () => { + beforeEach(async () => { + Sinon.stub(FetchMatchingBillRunService, 'go').resolves([]) + }) + describe('but both a live SROC and PRESROC bill run exists', () => { beforeEach(async () => { - await Promise.all([ - BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'annual', status: 'processing', toFinancialYearEnding, scheme: 'sroc' - }), - BillRunHelper.add({ - id: billRunIdTwo, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding: 2022, scheme: 'alcs' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: billRunId, regionId, batchType: 'two_part_tariff', toFinancialYearEnding }, + { id: presrocBillRunId, regionId, batchType: 'two_part_tariff', toFinancialYearEnding: 2022 } ]) }) + + it('returns both live bill runs', async () => { + const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) + + expect(results).to.have.length(2) + expect(results[0].id).to.equal(billRunId) + expect(results[1].id).to.equal(presrocBillRunId) + }) }) describe('but a live SROC bill run exists', () => { describe('for the same year', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'annual', status: 'processing', toFinancialYearEnding, scheme: 'sroc' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: billRunId, regionId, batchType: 'annual', toFinancialYearEnding } + ]) + }) - it('returns just the single match', async () => { - const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) + it('returns just the single match', async () => { + const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) - expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdOne) - }) + expect(results).to.have.length(1) + expect(results[0].id).to.equal(billRunId) }) }) describe('for a different year', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdOne, regionId, batchType: 'annual', status: 'processing', toFinancialYearEnding: toFinancialYearEnding - 1, scheme: 'sroc' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) }) it('returns no matches', async () => { @@ -357,20 +379,24 @@ describe('Determine Blocking Bill Run service', () => { describe('but a live PRESROC bill run exists', () => { beforeEach(async () => { - await BillRunHelper.add({ - id: billRunIdTwo, regionId, batchType: 'two_part_tariff', status: 'review', toFinancialYearEnding: 2022, scheme: 'alcs' - }) + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([ + { id: presrocBillRunId, regionId, batchType: 'two_part_tariff', toFinancialYearEnding: 2022 } + ]) + }) - it('returns just the single match', async () => { - const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) + it('returns just the single match', async () => { + const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) - expect(results).to.have.length(1) - expect(results[0].id).to.equal(billRunIdTwo) - }) + expect(results).to.have.length(1) + expect(results[0].id).to.equal(presrocBillRunId) }) }) describe('and no live bill runs', () => { + beforeEach(async () => { + Sinon.stub(FetchLiveBillRunsService, 'go').resolves([]) + }) + it('returns no matches', async () => { const results = await DetermineBlockingBillRunService.go(regionId, batchType, financialEndYear) diff --git a/test/services/bill-runs/fetch-bill-run.service.test.js b/test/services/bill-runs/fetch-bill-run.service.test.js index 5a4d415825..5f09cbf7df 100644 --- a/test/services/bill-runs/fetch-bill-run.service.test.js +++ b/test/services/bill-runs/fetch-bill-run.service.test.js @@ -36,7 +36,7 @@ describe('Fetch Bill Run service', () => { await DatabaseSupport.clean() // Create the initial bill run linked and associated region - linkedRegion = await RegionHelper.add() + linkedRegion = RegionHelper.select() testBillRun = await BillRunHelper.add({ regionId: linkedRegion.id }) // Create 3 licences including 1 that is a water undertaker (company). We'll link the first 2 to the first bill to diff --git a/test/services/bill-runs/fetch-bill-runs.service.test.js b/test/services/bill-runs/fetch-bill-runs.service.test.js index fdb6fc3d4f..a1762daa58 100644 --- a/test/services/bill-runs/fetch-bill-runs.service.test.js +++ b/test/services/bill-runs/fetch-bill-runs.service.test.js @@ -19,13 +19,12 @@ const FetchBillRunsService = require('../../../app/services/bill-runs/fetch-bill describe('Fetch Bill Runs service', () => { let page - let regionId + let region beforeEach(async () => { await DatabaseSupport.clean() - const region = await RegionHelper.add() - regionId = region.id + region = RegionHelper.select() // Set the default page size to 3 so we don't have to create loads of bill runs to test the service Sinon.replace(DatabaseConfig, 'defaultPageSize', 3) @@ -38,11 +37,11 @@ describe('Fetch Bill Runs service', () => { describe('when there are bill runs', () => { beforeEach(async () => { await Promise.all([ - _addBillRun(1005, new Date('2024-03-01'), 10000, 1, 2, regionId), - _addBillRun(1002, new Date('2023-01-01'), 20000, 3, 4, regionId), - _addBillRun(1003, new Date('2024-01-01'), 30000, 5, 6, regionId), - _addBillRun(1001, new Date('2022-10-01'), 30000, 7, 8, regionId), - _addBillRun(1004, new Date('2024-02-01'), 30000, 9, 10, regionId) + _addBillRun(1005, new Date('2024-03-01'), 10000, 1, 2, region.id), + _addBillRun(1002, new Date('2023-01-01'), 20000, 3, 4, region.id), + _addBillRun(1003, new Date('2024-01-01'), 30000, 5, 6, region.id), + _addBillRun(1001, new Date('2022-10-01'), 30000, 7, 8, region.id), + _addBillRun(1004, new Date('2024-02-01'), 30000, 9, 10, region.id) ]) }) @@ -64,7 +63,7 @@ describe('Fetch Bill Runs service', () => { status: 'sent', summer: false, numberOfBills: 7, - region: 'Avalon' + region: region.displayName }, { batchType: 'supplementary', @@ -75,7 +74,7 @@ describe('Fetch Bill Runs service', () => { status: 'sent', summer: false, numberOfBills: 15, - region: 'Avalon' + region: region.displayName } ], { skip: ['id'] }) expect(result.total).to.equal(5) diff --git a/test/services/bill-runs/fetch-live-bill-runs.service.test.js b/test/services/bill-runs/fetch-live-bill-runs.service.test.js index 82055f055c..b703992cd7 100644 --- a/test/services/bill-runs/fetch-live-bill-runs.service.test.js +++ b/test/services/bill-runs/fetch-live-bill-runs.service.test.js @@ -4,7 +4,7 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers @@ -22,15 +22,13 @@ describe('Fetch Live Bill Runs service', () => { let financialYearEnding let regionId - beforeEach(async () => { - const region = await RegionHelper.add() - - regionId = region.id - }) - describe('when there is a live bill run', () => { describe('and it is for the current year (SROC)', () => { - beforeEach(async () => { + before(async () => { + const region = RegionHelper.select(0) + + regionId = region.id + billRun = await BillRunHelper.add({ regionId, batchType: 'supplementary', @@ -80,7 +78,11 @@ describe('Fetch Live Bill Runs service', () => { }) describe('and it is for the previous year (SROC)', () => { - beforeEach(async () => { + before(async () => { + const region = RegionHelper.select(1) + + regionId = region.id + billRun = await BillRunHelper.add({ regionId, batchType: 'supplementary', @@ -129,7 +131,11 @@ describe('Fetch Live Bill Runs service', () => { }) describe('and it is for the last PRESROC year (PRESROC)', () => { - beforeEach(async () => { + before(async () => { + const region = RegionHelper.select(2) + + regionId = region.id + billRun = await BillRunHelper.add({ regionId, batchType: 'supplementary', @@ -179,6 +185,12 @@ describe('Fetch Live Bill Runs service', () => { }) describe('when there are no live bill runs', () => { + beforeEach(async () => { + const region = RegionHelper.select(3) + + regionId = region.id + }) + it('returns no matches', async () => { const results = await FetchLiveBillRunsService.go(regionId, financialYearEnding, false) diff --git a/test/services/bill-runs/fetch-matching-bill-run.service.test.js b/test/services/bill-runs/fetch-matching-bill-run.service.test.js index fe233c7e9a..77f1ee08ac 100644 --- a/test/services/bill-runs/fetch-matching-bill-run.service.test.js +++ b/test/services/bill-runs/fetch-matching-bill-run.service.test.js @@ -22,7 +22,8 @@ describe('Fetch Matching Bill Run service', () => { beforeEach(async () => { await DatabaseSupport.clean() - const region = await RegionHelper.add() + const region = RegionHelper.select() + regionId = region.id }) @@ -32,6 +33,7 @@ describe('Fetch Matching Bill Run service', () => { describe('and a bill run for the same financial year exists', () => { beforeEach(async () => { const billRun = await BillRunHelper.add({ regionId, batchType: 'annual', status: 'sent', toFinancialYearEnding: 2024 }) + matchingBillRunId = billRun.id }) @@ -77,6 +79,7 @@ describe('Fetch Matching Bill Run service', () => { const billRun = await BillRunHelper.add({ regionId, batchType: 'two_part_tariff', status: 'sent', toFinancialYearEnding: 2023 }) + matchingBillRunId = billRun.id }) @@ -96,6 +99,7 @@ describe('Fetch Matching Bill Run service', () => { let billRun = await BillRunHelper.add({ regionId, batchType: 'two_part_tariff', status: 'sent', toFinancialYearEnding: 2022, summer: true }) + matchingSummerBillRunId = billRun.id billRun = await BillRunHelper.add({ diff --git a/test/services/bill-runs/fetch-region.service.test.js b/test/services/bill-runs/fetch-region.service.test.js index 294893a758..fc80ea921c 100644 --- a/test/services/bill-runs/fetch-region.service.test.js +++ b/test/services/bill-runs/fetch-region.service.test.js @@ -8,15 +8,13 @@ const { describe, it } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const RegionsSeeder = require('../../support/seeders/regions.seeder.js') +const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test const FetchRegionService = require('../../../app/services/bill-runs/fetch-region.service.js') describe('Fetch Region service', () => { - const region = RegionsSeeder.data.find((region) => { - return region.displayName === 'Test Region' - }) + const region = RegionHelper.select(RegionHelper.TEST_REGION_INDEX) describe('when there is a region with a matching NALD region id', () => { it('returns results', async () => { diff --git a/test/services/bill-runs/initiate-bill-run.service.test.js b/test/services/bill-runs/initiate-bill-run.service.test.js index 36e14fd699..63f262c4f7 100644 --- a/test/services/bill-runs/initiate-bill-run.service.test.js +++ b/test/services/bill-runs/initiate-bill-run.service.test.js @@ -32,7 +32,8 @@ describe('Initiate Bill Run service', () => { beforeEach(async () => { await DatabaseSupport.clean() - const region = await RegionHelper.add() + const region = RegionHelper.select() + regionId = region.id // The InitiateBillRun service does not await the call to the ProcessBillRunService. It is intended to diff --git a/test/services/bill-runs/send-bill-run.service.test.js b/test/services/bill-runs/send-bill-run.service.test.js index ca2cfe44b0..f54d15b4a5 100644 --- a/test/services/bill-runs/send-bill-run.service.test.js +++ b/test/services/bill-runs/send-bill-run.service.test.js @@ -16,36 +16,37 @@ const RegionHelper = require('../../support/helpers/region.helper.js') const SendBillRunService = require('../../../app/services/bill-runs/send-bill-run.service.js') describe('Send Bill Run service', () => { - let testBillRunId + let billRunId + let region beforeEach(async () => { await DatabaseSupport.clean() - const { id: regionId } = await RegionHelper.add() + region = RegionHelper.select() const billRun = await BillRunHelper.add({ billRunNumber: 10101, createdAt: new Date('2024-02-28'), externalId: 'f54e53f0-37a0-400f-9f0e-bf8575c17668', - regionId, + regionId: region.id, status: 'ready' }) - testBillRunId = billRun.id + billRunId = billRun.id }) describe('when a bill with a matching ID exists', () => { it('will fetch the data and format it for use in the send bill run page', async () => { - const result = await SendBillRunService.go(testBillRunId) + const result = await SendBillRunService.go(billRunId) expect(result).to.equal({ - billRunId: testBillRunId, + billRunId, billRunNumber: 10101, billRunStatus: 'ready', billRunType: 'Supplementary', chargeScheme: 'Current', dateCreated: '28 February 2024', financialYear: '2022 to 2023', - region: 'Avalon' + region: region.displayName }) }) }) diff --git a/test/services/bill-runs/setup/determine-financial-year-end.service.test.js b/test/services/bill-runs/setup/determine-financial-year-end.service.test.js index 8f323a4e12..c51c50bc43 100644 --- a/test/services/bill-runs/setup/determine-financial-year-end.service.test.js +++ b/test/services/bill-runs/setup/determine-financial-year-end.service.test.js @@ -27,7 +27,8 @@ describe('Bill Runs Setup Determine Financial Year End service', () => { currentFinancialYearEnd = currentFinancialYear.endDate.getFullYear() - const region = await RegionHelper.add() + const region = RegionHelper.select() + regionId = region.id }) diff --git a/test/services/bill-runs/setup/fetch-regions.service.test.js b/test/services/bill-runs/setup/fetch-regions.service.test.js index f016732a35..d6ae9182fe 100644 --- a/test/services/bill-runs/setup/fetch-regions.service.test.js +++ b/test/services/bill-runs/setup/fetch-regions.service.test.js @@ -7,19 +7,19 @@ const Code = require('@hapi/code') const { describe, it } = exports.lab = Lab.script() const { expect } = Code -// Test helpers -const RegionSeeder = require('../../../support/seeders/regions.seeder.js') +// Test helper +const RegionHelper = require('../../../support/helpers/region.helper.js') // Thing under test const FetchRegionsService = require('../../../../app/services/bill-runs/setup/fetch-regions.service.js') -describe('Bill Runs Setup Fetch Regions service', () => { +describe('Bill Runs Setup - Fetch Regions service', () => { describe('when called', () => { it('returns the ID and display name for each region ordered by display name', async () => { const results = await FetchRegionsService.go() // This is necessary because other region helpers are adding regions into the database as part of their tests. - const expectedRegions = RegionSeeder.data.map((region) => { + const expectedRegions = RegionHelper.data.map((region) => { return { id: region.id, displayName: region.displayName diff --git a/test/services/bill-runs/supplementary/fetch-charge-versions.service.test.js b/test/services/bill-runs/supplementary/fetch-charge-versions.service.test.js index 7abc1801d8..cb29e5696c 100644 --- a/test/services/bill-runs/supplementary/fetch-charge-versions.service.test.js +++ b/test/services/bill-runs/supplementary/fetch-charge-versions.service.test.js @@ -32,7 +32,7 @@ describe('Fetch Charge Versions service', () => { beforeEach(async () => { await DatabaseSupport.clean() - region = await RegionHelper.add() + region = RegionHelper.select() regionId = region.id }) @@ -272,6 +272,7 @@ describe('Fetch Charge Versions service', () => { // This creates an SROC charge version linked to a licence. But the licence won't be marked for supplementary // billing const srocChargeVersion = await ChargeVersionHelper.add() + testRecords = [srocChargeVersion] }) @@ -297,6 +298,7 @@ describe('Fetch Charge Versions service', () => { }) const srocDraftChargeVersion = await ChargeVersionHelper.add({ status: 'draft', licenceId }) + testRecords = [srocDraftChargeVersion] }) @@ -322,6 +324,7 @@ describe('Fetch Charge Versions service', () => { // This creates an ALCS (presroc) charge version linked to a licence marked for supplementary billing const alcsChargeVersion = await ChargeVersionHelper.add({ scheme: 'alcs', licenceId }) + testRecords = [alcsChargeVersion] }) @@ -335,6 +338,7 @@ describe('Fetch Charge Versions service', () => { describe('because none of them have a "billingAccountId"', () => { let licenceId + beforeEach(async () => { billingPeriod = { startDate: new Date('2022-04-01'), @@ -351,6 +355,7 @@ describe('Fetch Charge Versions service', () => { // This creates a charge version with no `billingAccountId` const nullBillingAccountIdChargeVersion = await ChargeVersionHelper .add({ billingAccountId: null, licenceId }) + testRecords = [nullBillingAccountIdChargeVersion] }) @@ -380,6 +385,7 @@ describe('Fetch Charge Versions service', () => { const srocChargeVersion = await ChargeVersionHelper.add( { startDate: new Date('2023-04-01'), licenceId } ) + testRecords = [srocChargeVersion] }) @@ -405,6 +411,7 @@ describe('Fetch Charge Versions service', () => { // This creates an SROC charge version linked to a licence with an different region than selected const otherRegionChargeVersion = await ChargeVersionHelper.add({ licenceId }) + testRecords = [otherRegionChargeVersion] }) @@ -429,6 +436,7 @@ describe('Fetch Charge Versions service', () => { }) const chargeVersion = await ChargeVersionHelper.add({ licenceId }) + await WorkflowHelper.add({ licenceId }) testRecords = [chargeVersion] diff --git a/test/services/bill-runs/supplementary/process-billing-period.service.test.js b/test/services/bill-runs/supplementary/process-billing-period.service.test.js index 98fac6b2bf..80aa138e62 100644 --- a/test/services/bill-runs/supplementary/process-billing-period.service.test.js +++ b/test/services/bill-runs/supplementary/process-billing-period.service.test.js @@ -47,7 +47,8 @@ describe('Supplementary Process billing period service', () => { beforeEach(async () => { await DatabaseSupport.clean() - const { id: regionId } = await RegionHelper.add() + const { id: regionId } = RegionHelper.select() + licence = await LicenceHelper.add({ includeInSrocBilling: true, regionId }) changeReason = await ChangeReasonHelper.add() billingAccount = await BillingAccountHelper.add() @@ -87,6 +88,7 @@ describe('Supplementary Process billing period service', () => { const { id: chargeReferenceId } = await ChargeReferenceHelper.add( { chargeCategoryId: chargeCategory.id, chargeVersionId } ) + await ChargeElementHelper.add({ chargeReferenceId, abstractionPeriodStartDay: 1, @@ -96,6 +98,7 @@ describe('Supplementary Process billing period service', () => { }) const chargeVersionData = await FetchChargeVersionsService.go(licence.regionId, billingPeriod) + chargeVersions = chargeVersionData.chargeVersions const sentTransactions = [{ @@ -163,6 +166,7 @@ describe('Supplementary Process billing period service', () => { const { id: chargeReferenceId } = await ChargeReferenceHelper.add( { chargeCategoryId: chargeCategory.id, chargeVersionId } ) + await ChargeElementHelper.add({ chargeReferenceId, abstractionPeriodStartDay: 1, @@ -172,6 +176,7 @@ describe('Supplementary Process billing period service', () => { }) const chargeVersionData = await FetchChargeVersionsService.go(licence.regionId, billingPeriod) + chargeVersions = chargeVersionData.chargeVersions }) @@ -199,6 +204,7 @@ describe('Supplementary Process billing period service', () => { const { chargeElementId } = await ChargeReferenceHelper.add( { chargeCategoryId: chargeCategory.id, chargeVersionId } ) + await ChargeElementHelper.add({ chargeElementId, abstractionPeriodStartDay: 1, @@ -208,6 +214,7 @@ describe('Supplementary Process billing period service', () => { }) const chargeVersionData = await FetchChargeVersionsService.go(licence.regionId, billingPeriod) + chargeVersions = chargeVersionData.chargeVersions }) @@ -232,9 +239,11 @@ describe('Supplementary Process billing period service', () => { const { id: chargeReferenceId } = await ChargeReferenceHelper.add( { chargeCategoryId: chargeCategory.id, chargeVersionId } ) + await ChargeElementHelper.add({ chargeReferenceId }) const chargeVersionData = await FetchChargeVersionsService.go(licence.regionId, billingPeriod) + chargeVersions = chargeVersionData.chargeVersions }) @@ -256,6 +265,7 @@ describe('Supplementary Process billing period service', () => { describe('because sending the transactions fails', () => { beforeEach(async () => { const thrownError = new BillRunError(new Error(), BillRunModel.errorCodes.failedToCreateCharge) + Sinon.stub(SendTransactionsService, 'go').rejects(thrownError) }) diff --git a/test/services/bill-runs/two-part-tariff/fetch-bill-run-licences.service.test.js b/test/services/bill-runs/two-part-tariff/fetch-bill-run-licences.service.test.js index 24d2593d13..f891e6ef1b 100644 --- a/test/services/bill-runs/two-part-tariff/fetch-bill-run-licences.service.test.js +++ b/test/services/bill-runs/two-part-tariff/fetch-bill-run-licences.service.test.js @@ -41,7 +41,7 @@ describe('Fetch Bill Run Licences service', () => { let region beforeEach(async () => { - region = await RegionHelper.add() + region = RegionHelper.select() billRun = await BillRunHelper.add({ regionId: region.id, batchType: 'two_part_tariff' }) testLicenceReady = await ReviewLicenceHelper.add({ diff --git a/test/services/bill-runs/two-part-tariff/fetch-billing-accounts.service.test.js b/test/services/bill-runs/two-part-tariff/fetch-billing-accounts.service.test.js index eae9b343ed..7b31898383 100644 --- a/test/services/bill-runs/two-part-tariff/fetch-billing-accounts.service.test.js +++ b/test/services/bill-runs/two-part-tariff/fetch-billing-accounts.service.test.js @@ -40,7 +40,7 @@ describe('Fetch Billing Accounts service', () => { let reviewChargeVersion before(async () => { - region = await RegionHelper.add() + region = RegionHelper.select() billRun = await BillRunHelper.add({ regionId: region.id }) licence = await LicenceHelper.add({ regionId: region.id }) diff --git a/test/services/bill-runs/two-part-tariff/fetch-charge-versions.service.test.js b/test/services/bill-runs/two-part-tariff/fetch-charge-versions.service.test.js index 0e10f26679..c9a220ff19 100644 --- a/test/services/bill-runs/two-part-tariff/fetch-charge-versions.service.test.js +++ b/test/services/bill-runs/two-part-tariff/fetch-charge-versions.service.test.js @@ -17,7 +17,7 @@ const DatabaseSupport = require('../../../support/database.js') const LicenceHelper = require('../../../support/helpers/licence.helper.js') const LicenceHolderSeeder = require('../../../support/seeders/licence-holder.seeder.js') const LicenceModel = require('../../../../app/models/licence.model.js') -const PurposeSeeder = require('../../../support/seeders/purposes.seeder.js') +const PurposeHelper = require('../../../support/helpers/purpose.helper.js') const RegionHelper = require('../../../support/helpers/region.helper.js') const WorkflowHelper = require('../../../support/helpers/workflow.helper.js') @@ -39,13 +39,13 @@ describe('Fetch Charge Versions service', () => { beforeEach(async () => { await DatabaseSupport.clean() - purposeId = PurposeSeeder.data.find((purpose) => { return purpose.legacyId === '420' }).id + purposeId = PurposeHelper.data.find((purpose) => { return purpose.legacyId === '420' }).id const chargeCategory = await ChargeCategoryHelper.add({ reference: '4.3.41' }) chargeCategoryId = chargeCategory.id - const region = await RegionHelper.add() + const region = RegionHelper.select() regionId = region.id }) diff --git a/test/services/bill-runs/two-part-tariff/fetch-review-licence-results.service.test.js b/test/services/bill-runs/two-part-tariff/fetch-review-licence-results.service.test.js index 5ca4c314fb..518ecda5ab 100644 --- a/test/services/bill-runs/two-part-tariff/fetch-review-licence-results.service.test.js +++ b/test/services/bill-runs/two-part-tariff/fetch-review-licence-results.service.test.js @@ -16,7 +16,7 @@ const ChargeReferenceHelper = require('../../../support/helpers/charge-reference const ChargeVersionHelper = require('../../../support/helpers/charge-version.helper.js') const DatabaseSupport = require('../../../support/database.js') const LicenceHelper = require('../../../support/helpers/licence.helper.js') -const PurposesSeeder = require('../../../support/seeders/purposes.seeder.js') +const PurposeHelper = require('../../../support/helpers/purpose.helper.js') const RegionHelper = require('../../../support/helpers/region.helper.js') const ReturnLogHelper = require('../../../support/helpers/return-log.helper.js') const ReviewChargeElementHelper = require('../../../support/helpers/review-charge-element.helper.js') @@ -46,7 +46,7 @@ describe('Fetch Review Licence Results Service', () => { describe('when there is a valid bill run', () => { beforeEach(async () => { - region = await RegionHelper.add() + region = RegionHelper.select() billRun = await BillRunHelper.add({ regionId: region.id, batchType: 'two_part_tariff' }) }) @@ -84,7 +84,7 @@ describe('Fetch Review Licence Results Service', () => { chargeReferenceId: chargeReference.id }) - purpose = PurposesSeeder.data[0] + purpose = PurposeHelper.select() chargeElement = await ChargeElementHelper.add({ chargeReferenceId: chargeReference.id, purposeId: purpose.id }) reviewChargeElement = await ReviewChargeElementHelper.add({ reviewChargeReferenceId: reviewChargeReference.id, diff --git a/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js b/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js index 767ba01219..9cd440cdc3 100644 --- a/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js +++ b/test/services/bill-runs/two-part-tariff/process-billing-period.service.test.js @@ -13,7 +13,7 @@ const BillModel = require('../../../../app/models/bill.model.js') const { generateAccountNumber } = require('../../../support/helpers/billing-account.helper.js') const { generateUUID } = require('../../../../app/lib/general.lib.js') const { generateLicenceRef } = require('../../../support/helpers/licence.helper.js') -const { generateChargeRegionId } = require('../../../support/helpers/region.helper.js') +const RegionHelper = require('../../../support/helpers/region.helper.js') // Things we need to stub const BillRunError = require('../../../../app/errors/bill-run.error.js') @@ -24,7 +24,7 @@ const GenerateTransactionService = require('../../../../app/services/bill-runs/t // Thing under test const ProcessBillingPeriodService = require('../../../../app/services/bill-runs/two-part-tariff/process-billing-period.service.js') -describe('Two-part Tariff Process Billing Period service', () => { +describe('Two-part Tariff - Process Billing Period service', () => { const billingPeriod = { startDate: new Date('2022-04-01'), endDate: new Date('2023-03-31') @@ -307,6 +307,8 @@ async function _fetchPersistedBill (billRunId) { } function _licence () { + const region = RegionHelper.select() + return { id: generateUUID(), licenceRef: generateLicenceRef(), @@ -318,8 +320,8 @@ function _licence () { lapsedDate: null, revokedDate: null, region: { - id: generateUUID(), - chargeRegionId: generateChargeRegionId() + id: region.id, + chargeRegionId: region.chargeRegionId } } } diff --git a/test/services/bill-runs/two-part-tariff/remove-bill-run-licence.service.test.js b/test/services/bill-runs/two-part-tariff/remove-bill-run-licence.service.test.js index 398e3fbf8d..179976a677 100644 --- a/test/services/bill-runs/two-part-tariff/remove-bill-run-licence.service.test.js +++ b/test/services/bill-runs/two-part-tariff/remove-bill-run-licence.service.test.js @@ -19,21 +19,24 @@ const RemoveBillRunLicenceService = require('../../../../app/services/bill-runs/ describe('Remove Bill Run Licence service', () => { let billRunId let licenceId + let region beforeEach(async () => { await DatabaseSupport.clean() - const { id: regionId } = await RegionHelper.add({ displayName: 'Test Region' }) + region = RegionHelper.select() const billRun = await BillRunHelper.add({ billRunNumber: 12345, createdAt: new Date('2024-05-03'), - regionId, + regionId: region.id, status: 'review', toFinancialYearEnding: 2023 }) + billRunId = billRun.id const licence = await LicenceHelper.add({ licenceRef: '01/123/ABC' }) + licenceId = licence.id }) @@ -48,7 +51,7 @@ describe('Remove Bill Run Licence service', () => { billRunStatus: 'review', dateCreated: '3 May 2024', financialYear: '2022 to 2023', - region: 'Test Region' + region: region.displayName }) }) }) diff --git a/test/services/bills/fetch-bill-summary.service.test.js b/test/services/bills/fetch-bill-summary.service.test.js index 0ef839c56f..8d390b370b 100644 --- a/test/services/bills/fetch-bill-summary.service.test.js +++ b/test/services/bills/fetch-bill-summary.service.test.js @@ -31,7 +31,7 @@ describe('Fetch Bill Summary service', () => { let billingAccountId let companyId let contactId - let regionId + let region beforeEach(async () => { const company = await CompanyHelper.add() @@ -56,12 +56,10 @@ describe('Fetch Bill Summary service', () => { billingAccountAddressId = billingAccountAddress.id - const region = await RegionHelper.add({ displayName: 'Stormlands' }) - - regionId = region.id + region = RegionHelper.select() const billRun = await BillRunHelper.add({ - billRunNumber: 1075, createdAt: new Date('2023-05-01'), status: 'ready', regionId + billRunNumber: 1075, createdAt: new Date('2023-05-01'), status: 'ready', regionId: region.id }) billRunId = billRun.id @@ -72,8 +70,7 @@ describe('Fetch Bill Summary service', () => { const billId = bill.id for (let i = 0; i < 2; i++) { - const billLicence = await BillLicenceHelper - .add({ billId, licenceRef: `01/0${i + 1}/26/9400` }) + const billLicence = await BillLicenceHelper.add({ billId, licenceRef: `01/0${i + 1}/26/9400` }) billLicences.push(billLicence) } @@ -129,8 +126,8 @@ describe('Fetch Bill Summary service', () => { status: 'ready', toFinancialYearEnding: 2023, region: { - id: regionId, - displayName: 'Stormlands' + id: region.id, + displayName: region.displayName } } }) diff --git a/test/services/bills/fetch-bill.service.test.js b/test/services/bills/fetch-bill.service.test.js index de78cdbaf8..7d8f0dd7b7 100644 --- a/test/services/bills/fetch-bill.service.test.js +++ b/test/services/bills/fetch-bill.service.test.js @@ -28,7 +28,7 @@ describe('Fetch Bill service', () => { let unlinkedBillLicence beforeEach(async () => { - linkedRegion = await RegionHelper.add() + linkedRegion = RegionHelper.select() linkedBillRun = await BillRunHelper.add({ regionId: linkedRegion.id }) testBill = await BillHelper.add({ billRunId: linkedBillRun.id }) diff --git a/test/services/data/load/load.service.test.js b/test/services/data/load/load.service.test.js index d7fa478641..223478e59a 100644 --- a/test/services/data/load/load.service.test.js +++ b/test/services/data/load/load.service.test.js @@ -11,38 +11,44 @@ const { expect } = Code const BillRunModel = require('../../../../app/models/bill-run.model.js') const ChargeCategoryHelper = require('../../../support/helpers/charge-category.helper.js') const ChargeReferenceModel = require('../../../../app/models/charge-reference.model.js') -const ExpandedError = require('../../../../app/errors/expanded.error.js') -const RegionModel = require('../../../../app/models/region.model.js') const { db } = require('../../../../db/db.js') -const { generateUUID } = require('../../../../app/lib/general.lib.js') +const ExpandedError = require('../../../../app/errors/expanded.error.js') +const LicenceHelper = require('../../../support/helpers/licence.helper.js') +const LicenceModel = require('../../../../app/models/licence.model.js') +const RegionHelper = require('../../../support/helpers/region.helper.js') // Thing under test const LoadService = require('../../../../app/services/data/load/load.service.js') describe('Load service', () => { + let licenceRef let payload - let regionId + let region beforeEach(() => { - regionId = generateUUID() + licenceRef = LicenceHelper.generateLicenceRef() + region = RegionHelper.select() }) describe('when the service is called', () => { describe('with a valid payload', () => { beforeEach(() => { payload = { - regions: [ + licences: [ { - id: regionId, - chargeRegionId: 'S', - naldRegionId: 9, - displayName: 'Test Region', - name: 'Test Region' + licenceRef, + regionId: region.id, + regions: { + historicalAreaCode: 'SAAR', + regionalChargeArea: region.name + }, + startDate: '2020-01-01', + waterUndertaker: false } ], billRuns: [ { - regionId, + regionId: region.id, scheme: 'sroc', status: 'sent' } @@ -51,23 +57,23 @@ describe('Load service', () => { }) it('loads the entities into the DB', async () => { - const { billRuns } = await LoadService.go(payload) + const { billRuns, licences } = await LoadService.go(payload) - const region = await RegionModel.query().findById(regionId) + const licence = await LicenceModel.query().findById(licences[0]) - expect(region.displayName).to.equal('Test Region') + expect(licence.licenceRef).to.equal(licenceRef) const billRun = await BillRunModel.query().findById(billRuns[0]) - expect(billRun.regionId).to.equal(regionId) + expect(billRun.regionId).to.equal(region.id) expect(billRun.status).to.equal('sent') }) it('returns the generated and used IDs for the entities', async () => { const result = await LoadService.go(payload) - expect(result.regions).to.exist() - expect(result.regions).not.to.be.empty() + expect(result.licences).to.exist() + expect(result.licences).not.to.be.empty() expect(result.billRuns).to.exist() expect(result.billRuns).not.to.be.empty() @@ -106,9 +112,9 @@ describe('Load service', () => { it('sets the "is_test" flag on the entity instance as part of loading it', async () => { const result = await LoadService.go(payload) - const region = await db('regions').withSchema('water').first('isTest').where('regionId', result.regions[0]) + const licence = await db('licences').withSchema('water').first('isTest').where('licenceId', result.licences[0]) - expect(region.isTest).to.be.true() + expect(licence.isTest).to.be.true() }) }) }) diff --git a/test/services/idm/fetch-user-roles-and-groups.service.test.js b/test/services/idm/fetch-user-roles-and-groups.service.test.js index 2e7e1781d7..d4e5b993b8 100644 --- a/test/services/idm/fetch-user-roles-and-groups.service.test.js +++ b/test/services/idm/fetch-user-roles-and-groups.service.test.js @@ -4,114 +4,118 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers const GroupHelper = require('../../support/helpers/group.helper.js') -const GroupRoleHelper = require('../../support/helpers/group-role.helper.js') const RoleHelper = require('../../support/helpers/role.helper.js') -const UserGroupHelper = require('../../support/helpers/user-group.helper.js') const UserHelper = require('../../support/helpers/user.helper.js') const UserRoleHelper = require('../../support/helpers/user-role.helper.js') -const { generateUUID } = require('../../../app/lib/general.lib.js') // Thing under test const FetchUserRolesAndGroupsService = require('../../../app/services/idm/fetch-user-roles-and-groups.service.js') +const GROUP_ENV_OFFICER_INDEX = 0 +const ROLE_RETURNS_INDEX = 0 +const ROLE_HOF_NOTIFICATIONS_INDEX = 0 +const USER_ENV_OFFICER_INDEX = 2 + describe('Fetch User Roles And Groups service', () => { - let testRoleForUser - let testRoleForGroup - let testUser - let testGroup - - beforeEach(async () => { - testUser = await UserHelper.add( - { - username: `${generateUUID()}@test.com` - } - ) - - // Create a role and assign it directly to the user - testRoleForUser = await RoleHelper.add({ role: 'role_for_user' }) - await UserRoleHelper.add({ userId: testUser.id, roleId: testRoleForUser.id }) - - // Create a role and assign it to the user via a group - testRoleForGroup = await RoleHelper.add({ role: 'role_for_group' }) - testGroup = await GroupHelper.add() - await GroupRoleHelper.add({ groupId: testGroup.id, roleId: testRoleForGroup.id }) - await UserGroupHelper.add({ userId: testUser.id, groupId: testGroup.id }) - - // Create things that we don't assign to the user to test that they aren't returned - await RoleHelper.add({ role: 'not_assigned_role' }) - const notAssignedGroup = await GroupHelper.add({ group: 'not_assigned' }) - const notAssignedGroupRole = await RoleHelper.add({ role: 'not_assigned_group_role' }) - - await GroupRoleHelper.add({ groupId: notAssignedGroup.id, roleId: notAssignedGroupRole.id }) + let duplicateRoleForUser + let groupForUser + let roleForUser + let user + + before(async () => { + user = UserHelper.select(USER_ENV_OFFICER_INDEX) + + // Select a role and assign it directly to the user + roleForUser = RoleHelper.select(ROLE_RETURNS_INDEX) + await UserRoleHelper.add({ userId: user.id, roleId: roleForUser.id }) + + // Select a group and assign it to the user via a group + groupForUser = GroupHelper.select(GROUP_ENV_OFFICER_INDEX) + + // The result will be the users has 3 roles; 1 directly via user roles and 2 via the user group }) describe('when the user exists', () => { it('returns the user', async () => { - const result = await FetchUserRolesAndGroupsService.go(testUser.id) + const result = await FetchUserRolesAndGroupsService.go(user.id) - expect(result.user).to.equal(testUser) + expect(result.user).to.equal(user, { skip: ['createdAt', 'password', 'updatedAt'] }) }) it("returns the user's roles", async () => { - const result = await FetchUserRolesAndGroupsService.go(testUser.id) + const result = await FetchUserRolesAndGroupsService.go(user.id) const roles = result.roles.map((role) => { return role.role }) - expect(roles).to.have.length(2) - expect(roles).to.only.include(['role_for_user', 'role_for_group']) + expect(roles).to.have.length(3) + + // 1 via the user role + expect(roles).to.include('returns') + + // 2 via the user group (environment officer has 2 roles in group_roles) + expect(roles).to.include('hof_notifications') + expect(roles).to.include('manage_gauging_station_licence_links') }) it("returns the user's groups", async () => { - const result = await FetchUserRolesAndGroupsService.go(testUser.id) + const result = await FetchUserRolesAndGroupsService.go(user.id) const groups = result.groups.map((group) => { return group.group }) expect(groups).to.have.length(1) - expect(groups).to.equal(['wirs']) + expect(groups).to.include(groupForUser.group) }) describe('and the user is assigned a role they also have through a group', () => { beforeEach(async () => { - await UserRoleHelper.add({ userId: testUser.id, roleId: testRoleForGroup.id }) + duplicateRoleForUser = RoleHelper.select(ROLE_HOF_NOTIFICATIONS_INDEX) + + await UserRoleHelper.add({ userId: user.id, roleId: duplicateRoleForUser.id }) }) it('returns only one instance of the role', async () => { - const result = await FetchUserRolesAndGroupsService.go(testUser.id) + const result = await FetchUserRolesAndGroupsService.go(user.id) const roles = result.roles.map((role) => { return role.role }) - expect(roles).to.have.length(2) - expect(roles).to.only.include(['role_for_user', 'role_for_group']) + // 1 via the user role (ignoring the duplicate hof_notifications) + expect(roles).to.include('returns') + + // 2 via the user group (environment officer has 2 roles in group_roles) + expect(roles).to.include('hof_notifications') + expect(roles).to.include('manage_gauging_station_licence_links') }) }) }) describe('when the user does not exist', () => { + const unknownUserId = 0 + it('returns "null" for "user"', async () => { - const result = await FetchUserRolesAndGroupsService.go(0) + const result = await FetchUserRolesAndGroupsService.go(unknownUserId) expect(result.user).to.be.null() }) it('returns an empty roles array', async () => { - const result = await FetchUserRolesAndGroupsService.go(0) + const result = await FetchUserRolesAndGroupsService.go(unknownUserId) expect(result.roles).to.be.empty() }) it('returns an empty groups array', async () => { - const result = await FetchUserRolesAndGroupsService.go(0) + const result = await FetchUserRolesAndGroupsService.go(unknownUserId) expect(result.groups).to.be.empty() }) diff --git a/test/services/import/legacy-licence.service.test.js b/test/services/import/legacy-licence.service.test.js index fe3e8a123a..b4fa754fbb 100644 --- a/test/services/import/legacy-licence.service.test.js +++ b/test/services/import/legacy-licence.service.test.js @@ -14,18 +14,16 @@ const FetchLegacyImportLicenceVersionsService = require('../../../app/services/i const FixtureLicence = require('./_fixtures/licence.js') const FixtureVersions = require('./_fixtures/versions.js') const LicenceModel = require('../../../app/models/licence.model.js') -const RegionsSeeder = require('../../support/seeders/regions.seeder.js') +const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test -const LegacyImportLicenceService = +const LegacyLicenceService = require('../../../app/services/import/legacy-licence.service.js') -describe('Legacy import licence service', () => { +describe('Import - Legacy Licence service', () => { const licenceRef = FixtureLicence.LIC_NO - const region = RegionsSeeder.data.find((region) => { - return region.displayName === 'Test Region' - }) + const region = RegionHelper.select(RegionHelper.TEST_REGION_INDEX) beforeEach(async () => { Sinon.stub(FetchLegacyImportLicenceService, 'go').resolves({ @@ -37,7 +35,7 @@ describe('Legacy import licence service', () => { }) it('returns the matching licence data', async () => { - const results = await LegacyImportLicenceService.go(licenceRef) + const results = await LegacyLicenceService.go(licenceRef) const licence = await LicenceModel.query().findById(results.id) diff --git a/test/services/import/persist-licence.service.test.js b/test/services/import/persist-licence.service.test.js index 2fe7a85c12..8ff1495fd7 100644 --- a/test/services/import/persist-licence.service.test.js +++ b/test/services/import/persist-licence.service.test.js @@ -9,21 +9,19 @@ const { expect } = Code // Test helpers const LicenceModel = require('../../../app/models/licence.model.js') -const RegionsSeeder = require('../../support/seeders/regions.seeder.js') const { generateLicenceRef } = require('../../support/helpers/licence.helper.js') +const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test const PersistLicenceService = require('../../../app/services/import/persist-licence.service.js') -describe('Persist licence service', () => { +describe('Import - Persist Licence service', () => { let region let licence beforeEach(async () => { - region = RegionsSeeder.data.find((region) => { - return region.displayName === 'Test Region' - }) + region = RegionHelper.select(RegionHelper.TEST_REGION_INDEX) }) describe('when the licence ref does not exist', () => { diff --git a/test/services/jobs/time-limited/fetch-time-limited-licences.service.test.js b/test/services/jobs/time-limited/fetch-time-limited-licences.service.test.js index 97098b5835..4b1a89bbc9 100644 --- a/test/services/jobs/time-limited/fetch-time-limited-licences.service.test.js +++ b/test/services/jobs/time-limited/fetch-time-limited-licences.service.test.js @@ -26,7 +26,8 @@ describe('Fetch Time Limited Licences service', () => { beforeEach(async () => { await DatabaseSupport.clean() - const region = await RegionHelper.add() + const region = RegionHelper.select() + regionId = region.id }) @@ -37,13 +38,16 @@ describe('Fetch Time Limited Licences service', () => { beforeEach(async () => { const licence = await LicenceHelper.add({ regionId }) + licenceId = licence.id const licenceVersion = await LicenceVersionHelper.add({ licenceId }) + licenceVersionId = licenceVersion.id // This creates a 'current' SROC charge version const chargeVersion = await ChargeVersionHelper.add({ licenceId }) + chargeVersionId = chargeVersion.id const { id: chargeReferenceId } = await ChargeReferenceHelper.add({ chargeVersionId }) diff --git a/test/services/licences/fetch-agreements.service.test.js b/test/services/licences/fetch-agreements.service.test.js index 33f88acb80..ad28cc24ee 100644 --- a/test/services/licences/fetch-agreements.service.test.js +++ b/test/services/licences/fetch-agreements.service.test.js @@ -14,6 +14,8 @@ const LicenceAgreementHelper = require('../../support/helpers/licence-agreement. // Thing under test const FetchAgreementsService = require('../../../app/services/licences/fetch-agreements.service.js') +const FINANCIAL_AGREEMENT_S130U_INDEX = 5 + describe('Fetch Agreements service', () => { const endDate = new Date('2040-05-01') const signedOn = new Date('2022-04-01') @@ -24,7 +26,7 @@ describe('Fetch Agreements service', () => { describe('when the licence has agreements data', () => { before(async () => { - financialAgreement = await FinancialAgreementHelper.add() + financialAgreement = FinancialAgreementHelper.select(FINANCIAL_AGREEMENT_S130U_INDEX) }) describe('and the agreement has not been deleted', () => { diff --git a/test/services/licences/fetch-licence-summary.service.test.js b/test/services/licences/fetch-licence-summary.service.test.js index dd4abe7de1..6856c3f1fd 100644 --- a/test/services/licences/fetch-licence-summary.service.test.js +++ b/test/services/licences/fetch-licence-summary.service.test.js @@ -17,10 +17,10 @@ const LicenceHelper = require('../../support/helpers/licence.helper.js') const LicenceHolderSeeder = require('../../support/seeders/licence-holder.seeder.js') const LicenceVersionHelper = require('../../support/helpers/licence-version.helper.js') const LicenceVersionPurposeConditionHelper = require('../../support/helpers/licence-version-purpose-condition.helper.js') +const LicenceVersionPurposeConditionTypeHelper = require('../../support/helpers/licence-version-purpose-condition-type.helper.js') const LicenceVersionPurposeHelper = require('../../support/helpers/licence-version-purpose.helper.js') -const LicenceVersionPurposesConditionsTypeSeeder = require('../../support/seeders/licence-version-purpose-condition-types.seeder.js') const PermitLicenceHelper = require('../../support/helpers/permit-licence.helper.js') -const PurposeSeeder = require('../../support/seeders/purposes.seeder.js') +const PurposeHelper = require('../../support/helpers/purpose.helper.js') const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test @@ -41,11 +41,11 @@ describe('Fetch Licence Summary service', () => { let region beforeEach(async () => { - licenceVersionPurposeConditionType = LicenceVersionPurposesConditionsTypeSeeder.data.find((conditionType) => { + licenceVersionPurposeConditionType = LicenceVersionPurposeConditionTypeHelper.data.find((conditionType) => { return conditionType.displayTitle === 'Aggregate condition link between licences' }) - region = await RegionHelper.add() + region = RegionHelper.select() licence = await LicenceHelper.add({ expiredDate: null, @@ -61,7 +61,7 @@ describe('Fetch Licence Summary service', () => { licenceId: licence.id, startDate: new Date('2022-05-01') }) - purpose = PurposeSeeder.data[0] + purpose = PurposeHelper.select() licenceVersionPurpose = await LicenceVersionPurposeHelper.add({ licenceVersionId: licenceVersion.id, @@ -115,7 +115,7 @@ describe('Fetch Licence Summary service', () => { startDate: new Date('2022-01-01'), region: { id: region.id, - displayName: 'Avalon' + displayName: region.displayName }, permitLicence: { id: permitLicence.id, diff --git a/test/services/return-requirements/fetch-points.service.test.js b/test/services/return-requirements/fetch-points.service.test.js index f8557d99ff..1dc9cb8781 100644 --- a/test/services/return-requirements/fetch-points.service.test.js +++ b/test/services/return-requirements/fetch-points.service.test.js @@ -15,12 +15,12 @@ const RegionHelper = require('../../support/helpers/region.helper.js') // Thing under test const FetchPointsService = require('../../../app/services/return-requirements/fetch-points.service.js') -describe('Return Requirements - Return requirements Fetch Points service', () => { +describe('Return Requirements - Fetch Points service', () => { let licence let region beforeEach(async () => { - region = await RegionHelper.add() + region = RegionHelper.select() // Create the initial licenceId licence = await LicenceHelper.add({ diff --git a/test/services/return-requirements/fetch-purposes.service.test.js b/test/services/return-requirements/fetch-purposes.service.test.js index 882e64273a..1c0e9925e6 100644 --- a/test/services/return-requirements/fetch-purposes.service.test.js +++ b/test/services/return-requirements/fetch-purposes.service.test.js @@ -10,7 +10,7 @@ const { expect } = Code // Test helpers const LicenceVersionHelper = require('../../support/helpers/licence-version.helper.js') const LicenceVersionPurposeHelper = require('../../support/helpers/licence-version-purpose.helper.js') -const PurposesSeeder = require('../../support/seeders/purposes.seeder.js') +const PurposeHelper = require('../../support/helpers/purpose.helper.js') // Thing under test const FetchPurposesService = require('../../../app/services/return-requirements/fetch-purposes.service.js') @@ -25,9 +25,9 @@ describe('Return Requirements - Fetch Purposes service', () => { // we purposefully don't add them in alphabetical order so we can test they get sorted by the service purposes = [ - { ...PurposesSeeder.data.find((purpose) => { return purpose.description === 'Large Garden Watering' }) }, - { ...PurposesSeeder.data.find((purpose) => { return purpose.description === 'Heat Pump' }) }, - { ...PurposesSeeder.data.find((purpose) => { return purpose.description === 'Horticultural Watering' }) } + { ...PurposeHelper.data.find((purpose) => { return purpose.description === 'Large Garden Watering' }) }, + { ...PurposeHelper.data.find((purpose) => { return purpose.description === 'Heat Pump' }) }, + { ...PurposeHelper.data.find((purpose) => { return purpose.description === 'Horticultural Watering' }) } ] // Create the licenceVersionPurposes. Note - two of them are for the same purpose. This is common in the service diff --git a/test/services/return-requirements/generate-return-version-requirements.service.test.js b/test/services/return-requirements/generate-return-version-requirements.service.test.js index 3e8220351e..cac486abca 100644 --- a/test/services/return-requirements/generate-return-version-requirements.service.test.js +++ b/test/services/return-requirements/generate-return-version-requirements.service.test.js @@ -21,17 +21,19 @@ const FetchPointsService = require('../../../app/services/return-requirements/fe // Thing under test const GenerateReturnVersionRequirementsService = require('../../../app/services/return-requirements/generate-return-version-requirements.service.js') -describe('Generate Return Version Requirements service', () => { +describe('Return Requirements - Generate Return Version Requirements service', () => { let licenceId let licencePoints let naldRegionId let requirements beforeEach(async () => { - const testRegion = await RegionHelper.add() - naldRegionId = testRegion.naldRegionId + const region = RegionHelper.select() + + naldRegionId = region.naldRegionId + + const testLicence = await LicenceHelper.add({ regionId: region.id }) - const testLicence = await LicenceHelper.add({ regionId: testRegion.id }) licenceId = testLicence.id }) diff --git a/test/support/database.js b/test/support/database.js index 3ab727ecc5..ecd32402c8 100644 --- a/test/support/database.js +++ b/test/support/database.js @@ -9,7 +9,18 @@ */ const { db, dbConfig } = require('../../db/db.js') -const ReferenceDataSeeder = require('../../db/seeds/reference-data.js') + +const FinancialAgreementsSeeder = require('../../db/seeds/11-financial-agreements.seed.js') +const GroupRolesSeeder = require('../../db/seeds/08-group-roles.seed.js') +const GroupsSeeder = require('../../db/seeds/06-groups.seed.js') +const LicenceVersionPurposeConditionTypeSeeder = require('../../db/seeds/05-licence-version-purpose-condition-types.seed.js') +const PurposesSeeder = require('../../db/seeds/02-purposes.seed.js') +const PrimaryPurposesSeeder = require('../../db/seeds/03-primary-purposes.seed.js') +const RegionsSeeder = require('../../db/seeds/01-regions.seed.js') +const RolesSeeder = require('../../db/seeds/07-roles.seed.js') +const SecondaryPurposesSeeder = require('../../db/seeds/04-secondary-purposes.seed.js') +const UserGroupsSeeder = require('../../db/seeds/10-user-groups.seed.js') +const UsersSeeder = require('../../db/seeds/09-users.seed.js') const LEGACY_SCHEMAS = ['crm', 'crm_v2', 'idm', 'permit', 'returns', 'water'] @@ -29,8 +40,9 @@ async function clean () { await db.raw(`TRUNCATE TABLE ${tables.join(',')} RESTART IDENTITY;`) } - // TODO: when all calls this function are removed from the tests remove this call - await ReferenceDataSeeder.seed() + + // TODO: when all calls to DatabaseSupport.clean() (this function) have been removed from the tests we can drop this + await _seed() } /** @@ -70,6 +82,21 @@ function _migrationTables () { return [dbConfig.migrations.tableName, `${dbConfig.migrations.tableName}_lock`] } +async function _seed () { + // NOTE: Order matches the order they are seeded via Knex seeding. Do not alphabetize! + await RegionsSeeder.seed() + await PurposesSeeder.seed() + await PrimaryPurposesSeeder.seed() + await SecondaryPurposesSeeder.seed() + await LicenceVersionPurposeConditionTypeSeeder.seed() + await GroupsSeeder.seed() + await RolesSeeder.seed() + await GroupRolesSeeder.seed() + await UsersSeeder.seed() + await UserGroupsSeeder.seed() + await FinancialAgreementsSeeder.seed() +} + async function _tableNames (schema) { const result = await db('pg_tables') .select('tablename') diff --git a/test/support/general.js b/test/support/general.js index 8529899f5c..7d7b810cf7 100644 --- a/test/support/general.js +++ b/test/support/general.js @@ -77,7 +77,26 @@ function randomInteger (min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } +/** + * Select a random entry from an array of entries + * + * Was built when we started using real reference data within the unit tests, for example, regions and purposes. + * + * Where we want to select a real value but don't care which, this can be used by the helper so select a random entry + * from the reference data. + * + * @param {Object[]} data - an array of values to randomly select from + * + * @returns a random entry from the data provided + */ +function selectRandomEntry (data) { + const randomIndex = randomInteger(0, data.length - 1) + + return data[randomIndex] +} + module.exports = { postRequestOptions, - randomInteger + randomInteger, + selectRandomEntry } diff --git a/test/support/helpers/financial-agreement.helper.js b/test/support/helpers/financial-agreement.helper.js index 5afd0d2765..0a52467889 100644 --- a/test/support/helpers/financial-agreement.helper.js +++ b/test/support/helpers/financial-agreement.helper.js @@ -4,57 +4,32 @@ * @module FinancialAgreementHelper */ -const FinancialAgreementModel = require('../../../app/models/financial-agreement.model.js') -const { randomInteger } = require('../general.js') +const { data: financialAgreements } = require('../../../db/seeds/data/financial-agreements.js') +const { selectRandomEntry } = require('../general.js') /** - * Add a new financial agreement + * Select an entry from the reference data entries seeded at the start of testing * - * If no `data` is provided, default values will be used. These are + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * - `code` - [randomly generated - S127] - * - `description` - [randomly generated - Section S127] + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @returns {Promise} The instance of the newly created record + * @returns {Object} The selected reference entry or one picked at random */ -async function add (data = {}) { - const insertData = defaults(data) - - return FinancialAgreementModel.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 code = data.code ? data.code : generateFinancialAgreementCode() - - const defaults = { - code, - description: `Section ${code}` +function select (index = -1) { + if (index > -1) { + return financialAgreements[index] } - return { - ...defaults, - ...data - } -} - -function generateFinancialAgreementCode () { - return `S${randomInteger(100, 999)}` + return selectRandomEntry(financialAgreements) } module.exports = { - add, - defaults, - generateFinancialAgreementCode + data: financialAgreements, + select } diff --git a/test/support/helpers/group-role.helper.js b/test/support/helpers/group-role.helper.js index 26e238c0ae..361e902555 100644 --- a/test/support/helpers/group-role.helper.js +++ b/test/support/helpers/group-role.helper.js @@ -4,50 +4,32 @@ * @module GroupRoleHelper */ -const { generateUUID } = require('../../../app/lib/general.lib.js') -const GroupRoleModel = require('../../../app/models/group-role.model.js') +const { selectRandomEntry } = require('../general.js') +const GroupRoles = require('../../../db/seeds/data/group-roles.js') /** - * Add a new group role + * Select an entry from the reference data entries seeded at the start of testing * - * If no `data` is provided, default values will be used. These are + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * - `groupId` - [random UUID] - * - `roleId` - [random UUID] + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @returns {Promise} The instance of the newly created record + * @returns {Object} The selected reference entry or one picked at random */ -function add (data = {}) { - const insertData = defaults(data) - - return GroupRoleModel.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 defaults = { - groupId: generateUUID(), - roleId: generateUUID() +function select (index = -1) { + if (index > -1) { + return GroupRoles.data[index] } - return { - ...defaults, - ...data - } + return selectRandomEntry(GroupRoles.data) } module.exports = { - add, - defaults + data: GroupRoles.data, + select } diff --git a/test/support/helpers/group.helper.js b/test/support/helpers/group.helper.js index 25334995f2..9e2c053a87 100644 --- a/test/support/helpers/group.helper.js +++ b/test/support/helpers/group.helper.js @@ -4,52 +4,32 @@ * @module GroupHelper */ -const { generateUUID } = require('../../../app/lib/general.lib.js') -const GroupModel = require('../../../app/models/group.model.js') +const { selectRandomEntry } = require('../general.js') +const Groups = require('../../../db/seeds/data/groups.js') /** - * Add a new group + * Select an entry from the reference data entries seeded at the start of testing * - * If no `data` is provided, default values will be used. These are + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * - `group` - wirs - * - `description` - Waste Industry Regulatory Services + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @returns {Promise} The instance of the newly created record + * @returns {Object} The selected reference entry or one picked at random */ -function add (data = {}) { - const insertData = defaults(data) - - return GroupModel.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 defaults = { - // We create a random uuid as the id is NOT generated by the db, unlike most other tables - id: generateUUID(), - group: 'wirs', - description: 'Waste Industry Regulatory Services' +function select (index = -1) { + if (index > -1) { + return Groups.data[index] } - return { - ...defaults, - ...data - } + return selectRandomEntry(Groups.data) } module.exports = { - add, - defaults + data: Groups.data, + select } diff --git a/test/support/helpers/licence-version-purpose-condition-type.helper.js b/test/support/helpers/licence-version-purpose-condition-type.helper.js index 5d8b5513c2..5e8275f822 100644 --- a/test/support/helpers/licence-version-purpose-condition-type.helper.js +++ b/test/support/helpers/licence-version-purpose-condition-type.helper.js @@ -4,62 +4,32 @@ * @module LicenceVersionPurposeConditionTypeHelper */ -const { timestampForPostgres } = require('../../../app/lib/general.lib.js') -const LicenceVersionPurposeConditionTypeModel = require('../../../app/models/licence-version-purpose-condition-type.model.js') +const { selectRandomEntry } = require('../general.js') +const { data: licenceVersionPurposeConditionTypes } = require('../../../db/seeds/data/licence-version-purpose-condition-types.js') /** - * Add a new licence version purpose + * Select an entry from the reference data entries seeded at the start of testing * - * If no `data` is provided, default values will be used. These are + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * - `code` - COMB - * - `subcode` - LINK - * - `description` - Condition To Indicate Licence Split On Nald - * - `subcodeDescription` - Link Between Split Licences - * - `displayTitle` - Link between split licences - * - `createdAt` - new Date() - * - `updatedAt` - new Date() + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @returns {Promise} The instance of the newly created record + * @returns {Object} The selected reference entry or one picked at random */ -async function add (data = {}) { - const insertData = defaults(data) - - return LicenceVersionPurposeConditionTypeModel.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 = { - code: 'COMB', - subcode: 'LINK', - description: 'Condition To Indicate Licence Split On Nald', - subcodeDescription: 'Link Between Split Licences', - displayTitle: 'Link between split licences', - createdAt: timestamp, - updatedAt: timestamp +function select (index = -1) { + if (index > -1) { + return licenceVersionPurposeConditionTypes[index] } - return { - ...defaults, - ...data - } + return selectRandomEntry(licenceVersionPurposeConditionTypes) } module.exports = { - add, - defaults + data: licenceVersionPurposeConditionTypes, + select } diff --git a/test/support/helpers/primary-purpose.helper.js b/test/support/helpers/primary-purpose.helper.js index 333b5d32b8..120d233518 100644 --- a/test/support/helpers/primary-purpose.helper.js +++ b/test/support/helpers/primary-purpose.helper.js @@ -1,72 +1,35 @@ 'use strict' /** - * @module PurposeHelper + * @module PrimaryPurposeHelper */ -const PrimaryPurposeModel = require('../../../app/models/primary-purpose.model.js') -const { randomInteger } = require('../general.js') - -// NOTE: Taken from water.purposes_primary -const PRIMARY_PURPOSES = [ - { code: 'A', description: 'Agriculture' }, - { code: 'E', description: 'Environmental' }, - { code: 'I', description: 'Industrial, Commercial And Public Services' }, - { code: 'M', description: 'Amenity' }, - { code: 'P', description: 'Production Of Energy' }, - { code: 'W', description: 'Water Supply' }, - { code: 'X', description: 'Impounding' }, - { code: 'C', description: 'Crown And Government' } -] +const { selectRandomEntry } = require('../general.js') +const { data: primaryPurposes } = require('../../../db/seeds/data/primary-purposes.js') /** - * Add a new primary purpose - * - * If no `data` is provided, default values will be used. These are - * - * - `legacyId` - [randomly selected - A] - * - `description` - [randomly selected - Agriculture] + * Select an entry from the reference data entries seeded at the start of testing * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * @returns {Promise} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return PrimaryPurposeModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * 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 {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @returns {Object} The selected reference entry or one picked at random */ -function defaults (data = {}) { - const { code, description } = generatePrimaryPurpose() - - const defaults = { - legacyId: data.legacyId ? data.legacyId : code, - description: data.description ? data.description : description +function select (index = -1) { + if (index > -1) { + return primaryPurposes[index] } - return { - ...defaults, - ...data - } -} - -function generatePrimaryPurpose () { - return PRIMARY_PURPOSES[randomInteger(0, 7)] + return selectRandomEntry(primaryPurposes) } module.exports = { - add, - defaults, - generatePrimaryPurpose + data: primaryPurposes, + select } diff --git a/test/support/helpers/purpose.helper.js b/test/support/helpers/purpose.helper.js index c116b65e69..94b3087289 100644 --- a/test/support/helpers/purpose.helper.js +++ b/test/support/helpers/purpose.helper.js @@ -4,61 +4,32 @@ * @module PurposeHelper */ -const PurposeModel = require('../../../app/models/purpose.model.js') -const { randomInteger } = require('../general.js') +const { selectRandomEntry } = require('../general.js') +const { data: purposes } = require('../../../db/seeds/data/purposes.js') /** - * Add a new purpose + * Select an entry from the reference data entries seeded at the start of testing * - * If no `data` is provided, default values will be used. These are + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * - `legacyId` - [randomly generated - 420] - * - `description` - Spray Irrigation - Storage - * - `lossFactor` - high - * - `twoPartTariff` - true + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @returns {Promise} The instance of the newly created record + * @returns {Object} The selected reference entry or one picked at random */ -function add (data = {}) { - const insertData = defaults(data) - - return PurposeModel.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 defaults = { - legacyId: generatePurposeCode(), - description: 'Spray Irrigation - Storage', - lossFactor: 'high', - twoPartTariff: true - } - - return { - ...defaults, - ...data +function select (index = -1) { + if (index > -1) { + return purposes[index] } -} - -function generatePurposeCode () { - const numbering = randomInteger(1, 999) - return `${numbering}0` + return selectRandomEntry(purposes) } module.exports = { - add, - defaults, - generatePurposeCode + data: purposes, + select } diff --git a/test/support/helpers/region.helper.js b/test/support/helpers/region.helper.js index ad0b27443a..bab04d8ba4 100644 --- a/test/support/helpers/region.helper.js +++ b/test/support/helpers/region.helper.js @@ -4,66 +4,35 @@ * @module RegionHelper */ -const { randomInteger } = require('../general.js') -const RegionModel = require('../../../app/models/region.model.js') +const { selectRandomEntry } = require('../general.js') +const { data: regions } = require('../../../db/seeds/data/regions.js') + +const TEST_REGION_INDEX = 8 /** - * Add a new region - * - * If no `data` is provided, default values will be used. These are + * Select an entry from the reference data entries seeded at the start of testing * - * - `chargeRegionId` - [selected based on randomly generated naldRegionId] - * - `naldRegionId` - [randomly generated - 8] - * - `name` - Kingdom of Avalon - * - `displayName` - Avalon + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database - * - * @returns {Promise} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return RegionModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * 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 {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @returns {Object} The selected reference entry or one picked at random */ -function defaults (data = {}) { - const naldRegionId = randomInteger(1, 8) - const defaults = { - chargeRegionId: generateChargeRegionId(naldRegionId), - naldRegionId, - name: 'Kingdom of Avalon', - displayName: 'Avalon' - } - - return { - ...defaults, - ...data +function select (index = -1) { + if (index > -1) { + return regions[index] } -} - -function generateChargeRegionId (naldRegionId = null) { - if (!naldRegionId) { - naldRegionId = randomInteger(1, 8) - } - - const chargeRegionIds = ['A', 'B', 'Y', 'N', 'E', 'S', 'T', 'W'] - return chargeRegionIds[naldRegionId - 1] + return selectRandomEntry(regions) } module.exports = { - add, - defaults, - generateChargeRegionId + data: regions, + select, + TEST_REGION_INDEX } diff --git a/test/support/helpers/return-requirement-purpose.helper.js b/test/support/helpers/return-requirement-purpose.helper.js index 231d89f209..b5eb3b28d2 100644 --- a/test/support/helpers/return-requirement-purpose.helper.js +++ b/test/support/helpers/return-requirement-purpose.helper.js @@ -6,13 +6,10 @@ const { generateUUID } = require('../../../app/lib/general.lib.js') const { randomInteger } = require('../general.js') -const { generatePrimaryPurpose } = require('./primary-purpose.helper.js') -const { generatePurposeCode } = require('./purpose.helper.js') -const { generateSecondaryPurpose } = require('./secondary-purpose.helper.js') -const primaryPurposesData = require('../seeders/data/primary-purposes.data.js') -const purposeData = require('../seeders/data/purposes.data.js') -const secondaryPurposeData = require('../seeders/data/secondary-purposes.data.js') +const PrimaryPurposeHelper = require('./primary-purpose.helper.js') +const PurposeHelper = require('./purpose.helper.js') const ReturnRequirementPurposeModel = require('../../../app/models/return-requirement-purpose.model.js') +const SecondaryPurposeHelper = require('../helpers/secondary-purpose.helper.js') /** * Add a new return requirement purpose @@ -20,9 +17,9 @@ const ReturnRequirementPurposeModel = require('../../../app/models/return-requir * If no `data` is provided, default values will be used. These are * * - `externalId` - [randomly generated - 9:99999:A:AGR:400] - * - `purposeId` - [random UUID] - * - `primaryPurposeId` - [random UUID] - * - `secondaryPurposeId` - [random UUID] + * - `purposeId` - [randomly selected UUID from purposes] + * - `primaryPurposeId` - [randomly selected UUID from primary purposes] + * - `secondaryPurposeId` - [randomly selected UUID from secondary purposes] * - `returnRequirementId` - [random UUID] * * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database @@ -46,13 +43,17 @@ function add (data = {}) { * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database */ function defaults (data = {}) { - const externalId = `9:${randomInteger(100, 99999)}:${_generatePrimaryCode()}:${_generateSecondaryCode()}:${generatePurposeCode()}` + const purpose = PurposeHelper.select() + const primaryPurpose = PrimaryPurposeHelper.select() + const secondaryPurpose = SecondaryPurposeHelper.select() + + const externalId = `9:${randomInteger(100, 99999)}:${primaryPurpose.legacyId}:${secondaryPurpose.legacyId}:${purpose.legacyId}` const defaults = { externalId, - purposeId: _getRandomOption(purposeData), - primaryPurposeId: _getRandomOption(primaryPurposesData), - secondaryPurposeId: _getRandomOption(secondaryPurposeData), + purposeId: purpose.id, + primaryPurposeId: primaryPurpose.id, + secondaryPurposeId: secondaryPurpose.id, returnRequirementId: generateUUID() } @@ -62,20 +63,6 @@ function defaults (data = {}) { } } -function _generatePrimaryCode () { - return generatePrimaryPurpose().code -} - -function _generateSecondaryCode () { - return generateSecondaryPurpose().code -} - -function _getRandomOption (data) { - const randomOne = randomInteger(0, data.length - 1) - - return data[randomOne].id -} - module.exports = { add, defaults diff --git a/test/support/helpers/role.helper.js b/test/support/helpers/role.helper.js index ead0cc9c19..1c702e2df3 100644 --- a/test/support/helpers/role.helper.js +++ b/test/support/helpers/role.helper.js @@ -4,49 +4,32 @@ * @module RoleHelper */ -const RoleModel = require('../../../app/models/role.model.js') +const { selectRandomEntry } = require('../general.js') +const { data: roles } = require('../../../db/seeds/data/roles.js') /** - * Add a new role + * Select an entry from the reference data entries seeded at the start of testing * - * If no `data` is provided, default values will be used. These are + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * - `role` - billing - * - `description` - Administer billing + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @returns {Promise} The instance of the newly created record + * @returns {Object} The selected reference entry or one picked at random */ -function add (data = {}) { - const insertData = defaults(data) - - return RoleModel.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 defaults = { - role: 'billing', - description: 'Administer billing' +function select (index = -1) { + if (index > -1) { + return roles[index] } - return { - ...defaults, - ...data - } + return selectRandomEntry(roles) } module.exports = { - add, - defaults + data: roles, + select } diff --git a/test/support/helpers/secondary-purpose.helper.js b/test/support/helpers/secondary-purpose.helper.js index 21df24f131..2cf495559e 100644 --- a/test/support/helpers/secondary-purpose.helper.js +++ b/test/support/helpers/secondary-purpose.helper.js @@ -1,84 +1,35 @@ 'use strict' /** - * @module PurposeHelper + * @module SecondaryPurposeHelper */ -const SecondaryPurposeModel = require('../../../app/models/secondary-purpose.model.js') -const { randomInteger } = require('../general.js') - -// NOTE: This is only a subset. There 63 of these codes that could be used. Taken from water.purposes_secondary -const SECONDARY_PURPOSES = [ - { code: 'AGR', description: 'General Agriculture' }, - { code: 'AQF', description: 'Aquaculture Fish' }, - { code: 'AQP', description: 'Aquaculture Plant' }, - { code: 'BRW', description: 'Breweries/Wine' }, - { code: 'BUS', description: 'Business Parks' }, - { code: 'CHE', description: 'Chemicals' }, - { code: 'CON', description: 'Construction' }, - { code: 'CRN', description: 'Crown And Government' }, - { code: 'DAR', description: 'Dairies' }, - { code: 'ELC', description: 'Electricity' }, - { code: 'EXT', description: 'Extractive' }, - { code: 'FAD', description: 'Food & Drink' }, - { code: 'FOR', description: 'Forestry' }, - { code: 'GOF', description: 'Golf Courses' }, - { code: 'HOL', description: 'Holiday Sites, Camp Sites & Tourist Attractions' }, - { code: 'HOS', description: 'Hospitals' }, - { code: 'PAD', description: 'Public Administration' }, - { code: 'PAP', description: 'Paper And Printing' }, - { code: 'PET', description: 'Petrochemicals' }, - { code: 'PRI', description: 'Private Non-Industrial' } -] +const { selectRandomEntry } = require('../general.js') +const { data: secondaryPurposes } = require('../../../db/seeds/data/secondary-purposes.js') /** - * Add a new secondary purpose - * - * If no `data` is provided, default values will be used. These are - * - * - `legacyId` - [randomly selected - AGR] - * - `description` - [randomly selected - General Agriculture] + * Select an entry from the reference data entries seeded at the start of testing * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. * - * @returns {Promise} The instance of the newly created record - */ -function add (data = {}) { - const insertData = defaults(data) - - return SecondaryPurposeModel.query() - .insert({ ...insertData }) - .returning('*') -} - -/** - * Returns the defaults used + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. * - * 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 {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data * - * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database + * @returns {Object} The selected reference entry or one picked at random */ -function defaults (data = {}) { - const { code, description } = generateSecondaryPurpose() - - const defaults = { - legacyId: data.legacyId ? data.legacyId : code, - description: data.description ? data.description : description +function select (index = -1) { + if (index > -1) { + return secondaryPurposes[index] } - return { - ...defaults, - ...data - } -} - -function generateSecondaryPurpose () { - return SECONDARY_PURPOSES[randomInteger(0, 7)] + return selectRandomEntry(secondaryPurposes) } module.exports = { - add, - defaults, - generateSecondaryPurpose + data: secondaryPurposes, + select } diff --git a/test/support/helpers/user-group.helper.js b/test/support/helpers/user-group.helper.js index 4d1c59e8e7..35a852c64a 100644 --- a/test/support/helpers/user-group.helper.js +++ b/test/support/helpers/user-group.helper.js @@ -5,8 +5,12 @@ */ const { generateUUID } = require('../../../app/lib/general.lib.js') +const { selectRandomEntry } = require('../general.js') const { generateUserId } = require('./user.helper.js') const UserGroupModel = require('../../../app/models/user-group.model.js') +const { data: userGroups } = require('../../../db/seeds/data/user-groups.js') + +const DEFAULT_INDEX = 4 /** * Add a new user group @@ -50,7 +54,32 @@ function defaults (data = {}) { } } +/** + * Select an entry from the reference data entries seeded at the start of testing + * + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. + * + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. + * + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data + * + * @returns {Object} The selected reference entry or one picked at random + */ +function select (index = -1) { + if (index > -1) { + return userGroups[index] + } + + return selectRandomEntry(userGroups) +} + module.exports = { add, - defaults + data: userGroups, + DEFAULT_INDEX, + defaults, + select } diff --git a/test/support/helpers/user.helper.js b/test/support/helpers/user.helper.js index b7d97da38c..77610e277e 100644 --- a/test/support/helpers/user.helper.js +++ b/test/support/helpers/user.helper.js @@ -4,15 +4,19 @@ * @module UserHelper */ -const { randomInteger } = require('../general.js') +const { randomInteger, selectRandomEntry } = require('../general.js') +const { generateUUID } = require('../../../app/lib/general.lib.js') const UserModel = require('../../../app/models/user.model.js') +const { data: users } = require('../../../db/seeds/data/users.js') + +const DEFAULT_INDEX = 4 /** * Add a new user * * If no `data` is provided, default values will be used. These are * - * - `username` - user.name@test.com + * - `username` - [random UUID]@wrls.gov.uk * - `password` - P@55word (note that this is salted and hashed before being persisted) * - `resetRequired` - 0 * - `badLogins` - 0 @@ -43,7 +47,7 @@ function add (data = {}) { */ function defaults (data = {}) { const defaults = { - username: 'user.name@test.com', + username: `${generateUUID()}@wrls.gov.uk`, password: 'P@55word', resetRequired: 0, badLogins: 0, @@ -57,11 +61,37 @@ function defaults (data = {}) { } function generateUserId () { - return randomInteger(100001, 199999) + // The last ID in the pre-seeded users is 100009 + return randomInteger(100010, 199999) +} + +/** + * Select an entry from the reference data entries seeded at the start of testing + * + * Because this helper is linked to a reference record instead of a transaction, we don't expect these to be created + * when using the service. + * + * So, they are seeded automatically when tests are run. Tests that need to link to a record can use this method to + * select a specific entry, or have it it return one at random. + * + * @param {Number} [index=-1] - The reference entry to select. Defaults to -1 which means an entry will be returned at + * random from the reference data + * + * @returns {Object} The selected reference entry or one picked at random + */ +function select (index = -1) { + if (index > -1) { + return users[index] + } + + return selectRandomEntry(users) } module.exports = { add, + data: users, + DEFAULT_INDEX, defaults, - generateUserId + generateUserId, + select } diff --git a/test/support/seeders/data/primary-purposes.data.js b/test/support/seeders/data/primary-purposes.data.js deleted file mode 100644 index 1cff89bbbc..0000000000 --- a/test/support/seeders/data/primary-purposes.data.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict' - -module.exports = [ - { - id: 'b6bb3b77-cfe8-4f22-8dc9-e92713ca3156', - legacyId: 'A', - description: 'Agriculture', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '3019b676-e8c6-4bc1-8336-b7862489af70', - legacyId: 'E', - description: 'Environmental', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'd6259e5e-9bb4-4743-b565-e61ec05afc0a', - legacyId: 'I', - description: 'Industrial, Commercial And Public Services', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'c096d17b-7369-4c6b-a230-fb0bc5109b9c', - legacyId: 'M', - description: 'Amenity', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '4bbc388a-38cf-425c-bf8f-d609e6a5f57a', - legacyId: 'P', - description: 'Production Of Energy', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '7cc6fa87-2e69-4cb3-9abb-a5700ec49b36', - legacyId: 'W', - description: 'Water Supply', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '6ad72bf8-ab8e-4fb6-99b0-f54a7acc3a53', - legacyId: 'X', - description: 'Impounding', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'a38014a7-b8e3-4c3d-87b4-32ca1616d29b', - legacyId: 'C', - description: 'Crown And Government', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - } -] diff --git a/test/support/seeders/data/secondary-purposes.data.js b/test/support/seeders/data/secondary-purposes.data.js deleted file mode 100644 index f5d08940ff..0000000000 --- a/test/support/seeders/data/secondary-purposes.data.js +++ /dev/null @@ -1,445 +0,0 @@ -'use strict' - -module.exports = [ - { - id: '2457bfeb-a120-4b57-802a-46494bd22f82', - legacyId: 'AGR', - description: 'General Agriculture', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'd0708921-b5c1-4a5b-b63b-31aab46016d8', - legacyId: 'AQF', - description: 'Aquaculture Fish', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '3dce7cf6-0ed2-48bd-ad6c-bf6c71f79db4', - legacyId: 'AQP', - description: 'Aquaculture Plant', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'c05b93dd-a48f-49a8-a5e6-782252c18f9d', - legacyId: 'BRW', - description: 'Breweries/Wine', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '2d7ba5e2-b6e7-4ed2-a2d1-a1ad3f97229f', - legacyId: 'BUS', - description: 'Business Parks', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '7ea553b0-03df-4081-bcaa-e1f704aa3291', - legacyId: 'CHE', - description: 'Chemicals', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'ceb6ae1d-fc8c-4cf9-ae01-269122c24c8d', - legacyId: 'CON', - description: 'Construction', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'd83bc811-b48f-4e87-9916-4c47943d224c', - legacyId: 'CRN', - description: 'Crown And Government', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'a50fd6db-96ab-46c6-804e-ddd035a4f1b4', - legacyId: 'DAR', - description: 'Dairies', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'c8d3f626-a4d5-42ea-9f72-6e22c524048e', - legacyId: 'ELC', - description: 'Electricity', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'e35d4bf3-9c23-48fc-881c-a258b78bb2d1', - legacyId: 'EXT', - description: 'Extractive', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'eb7c97fd-a718-4136-a7a2-4f0151e0188c', - legacyId: 'FAD', - description: 'Food & Drink', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '9ca665af-5356-4a6d-b509-b96ee64de27a', - legacyId: 'FOR', - description: 'Forestry', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'a76d6e29-88d1-4fa3-9136-d961a9cdb1bf', - legacyId: 'GOF', - description: 'Golf Courses', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'ce8e913d-55fb-4f21-b2b2-755084661662', - legacyId: 'HOL', - description: 'Holiday Sites, Camp Sites & Tourist Attractions', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'c7be8e7a-37a0-459a-bf26-97af4f120834', - legacyId: 'HOR', - description: 'Horticulture And Nurseries', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '2c397566-4cde-41c0-8cca-d152b554f528', - legacyId: 'HOS', - description: 'Hospitals', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'c4165b99-f4f1-4124-acad-e512371d1bf7', - legacyId: 'HTL', - description: 'Hotels, Public Houses And Conference Centres', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '70abbcfd-b7b9-4eef-9f39-29468b32ce0e', - legacyId: 'IND', - description: 'Industrial/Commercial/Energy/Public Services', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'ecd25cff-3749-49fd-ba30-9c86483b4a12', - legacyId: 'LAU', - description: 'Laundry', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'ad036953-0e1b-43ce-a359-f452f344c4da', - legacyId: 'MCH', - description: 'Machinery And Electronics', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'd5fb3110-0dde-46f7-b7a3-94d06052d8fc', - legacyId: 'MEC', - description: 'Mechanical Non Electrical', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '86708863-167a-4865-8383-cde90e7d1b43', - legacyId: 'MIN', - description: 'Mineral Products', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'af1eec87-602c-41de-a982-2417bbc320a2', - legacyId: 'MTL', - description: 'Metal', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '7cff0b52-da01-4a29-b29c-a4bba4036324', - legacyId: 'MUN', - description: 'Municipal Grounds', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '4bbdf3ff-9efa-4303-bea8-f7d419022922', - legacyId: 'NAV', - description: 'Navigation', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '00237355-5b18-44d9-a5c4-785e387739fb', - legacyId: 'NRE', - description: 'Non-Remedial River/Wetland Support', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '789824ee-140e-4871-b453-b17ac12ab095', - legacyId: 'ORC', - description: 'Orchards', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '303cc214-6118-417b-b6ee-343257af3a3b', - legacyId: 'OTE', - description: 'Other Environmental Improvements', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'f85ab791-e943-4492-a3bb-c0b3ad3f0712', - legacyId: 'OTI', - description: 'Other Industrial/Commercial/Public Services', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'a58ef991-ea9b-4443-862d-811327ccd3dc', - legacyId: 'PAD', - description: 'Public Administration', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'e882c4a6-53d0-4b82-b921-499d25428134', - legacyId: 'PAP', - description: 'Paper And Printing', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'f47814e0-a7a9-45be-af75-4d2b58784be0', - legacyId: 'PET', - description: 'Petrochemicals', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '6cbb6f30-64bd-47e5-b852-5089e7088568', - legacyId: 'PRI', - description: 'Private Non-Industrial', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '8b324ba7-7d82-46d8-ae61-bc343e388745', - legacyId: 'PRV', - description: 'Private Water Supply', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '2362c06f-bb1e-4649-8342-29cc94017774', - legacyId: 'PUM', - description: 'Pump & Treat', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '866fe96a-1536-4396-85b3-de073ac2f92f', - legacyId: 'PWS', - description: 'Public Water Supply', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '1d3877a3-c5cf-405a-9e07-4fe94689f4ea', - legacyId: 'PWU', - description: 'Private Water Undertaking', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'b9a4c156-77e8-4891-95f4-6df745e58d42', - legacyId: 'RAC', - description: 'Racecourses', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '04d711bb-607c-4814-aca9-0c6bd39e0ea4', - legacyId: 'REF', - description: 'Refuse And Recycling', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '3c2a4aab-c2a7-4fea-aa06-bcdf2d9e499f', - legacyId: 'REM', - description: 'Remedial River/Wetland Support', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'c0691f62-1022-4cef-8f9b-575099f62c20', - legacyId: 'RES', - description: 'Research Non- University/College', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'b6d3c0c8-4018-4553-9393-0e87a3c91a8f', - legacyId: 'RET', - description: 'Retail', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'bf32cb71-8f5a-4055-913a-3c16942abb8c', - legacyId: 'RUB', - description: 'Rubber', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'f06c3e2e-0a0b-4cd5-81bd-04d527bd8533', - legacyId: 'SCH', - description: 'Schools And Colleges', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'b6fbd08e-a4ad-4a9b-871e-30ac51efee78', - legacyId: 'SLA', - description: 'Slaughtering', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '59bd435f-5651-4cc5-bdf8-fb8e3697f95a', - legacyId: 'SPO', - description: 'Sports Grounds/Facilities', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '9c16f3e8-4ea0-4292-8802-8427bec0571a', - legacyId: 'TRA', - description: 'Transport', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'c778de53-0fd4-445e-af86-3c441f79e07b', - legacyId: 'TXT', - description: 'Textiles & Leather', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '9b1828d1-d319-4d6c-a620-d9828c455fcb', - legacyId: 'UNK', - description: 'Unknown (for impounding exluding impoundments for HEP)', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'eecd8280-9b2f-4279-b300-d4b3441bd87c', - legacyId: 'WAT', - description: 'Water Supply Related', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'f6f7b6bf-6447-4092-8cd6-27cae2d8956e', - legacyId: 'ZOO', - description: 'Zoos/Kennels/Stables', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '490db793-eddf-4fed-93ce-78260b8d0584', - legacyId: 'CRW', - description: 'Crown - Other', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '09545099-a184-4e98-be2a-e749d24392e9', - legacyId: 'GOV', - description: 'Government Departments - Other', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '6bb0f9c0-7ef0-4da4-9c0f-2a60fc6b4785', - legacyId: 'DET', - description: 'Detention And Immigration Centres', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '3fd3cf82-47ff-4f01-b523-96959f2d635b', - legacyId: 'VIF', - description: 'Visiting Armed Forces', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '8b836771-6d7e-4277-b03f-473c0504d02b', - legacyId: 'FRT', - description: 'Government Or Crown Forestry', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '489930ca-cef9-4604-86be-31ef01954cc7', - legacyId: 'CVY', - description: 'Statutory Water Conservancy Operation', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '607f230d-e96b-4383-a588-f7976cbf3e6b', - legacyId: 'HRB', - description: 'Ports And Harbour Authority Operation', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '075d0549-4075-47f4-9e1a-944e96a63eec', - legacyId: 'DRG', - description: 'Drainage Operations', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '678fa94b-6320-44d2-a21d-5d772040de6c', - legacyId: 'POW', - description: 'Domestic Premises Related', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: 'd4f9f23b-1602-4846-9b05-07a49d2bd9c5', - legacyId: 'MOD', - description: 'Mod (Ministry Of Defence)', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - }, - { - id: '7f75f247-5b3c-4785-954c-207456a4d6c4', - legacyId: 'OOG', - description: 'Onshore Oil and Gas Extraction', - createdAt: new Date('2024-07-22'), - updatedAt: new Date('2024-07-22') - } -] diff --git a/test/support/seeders/licence-abstraction-data.seeder.js b/test/support/seeders/licence-abstraction-data.seeder.js index 82913d10c4..66c7911119 100644 --- a/test/support/seeders/licence-abstraction-data.seeder.js +++ b/test/support/seeders/licence-abstraction-data.seeder.js @@ -4,18 +4,16 @@ * @module LicenceAbstractionDataSeeder */ -const SecondaryPurposesSeeder = require('./secondary-purpose.seeder.js') -const PrimaryPurposesSeeder = require('./primary-purpose.seeder.js') -const PurposesSeeder = require('./purposes.seeder.js') - const FinancialAgreementHelper = require('../helpers/financial-agreement.helper.js') -const FinancialAgreementModel = require('../../../app/models/financial-agreement.model.js') const LicenceFinancialAgreement = require('../helpers/licence-agreement.helper.js') const LicenceHelper = require('../helpers/licence.helper.js') const LicenceVersionHelper = require('../helpers/licence-version.helper.js') const LicenceVersionPurposeHelper = require('../helpers/licence-version-purpose.helper.js') const PermitLicenceHelper = require('../helpers/permit-licence.helper.js') +const PrimaryPurposeHelper = require('../helpers/primary-purpose.helper.js') +const PurposeHelper = require('../helpers/purpose.helper.js') const RegionHelper = require('../helpers/region.helper.js') +const SecondaryPurposeHelper = require('../helpers/secondary-purpose.helper.js') const { generateLicenceRef } = require('../helpers/licence.helper.js') const { generateLicenceVersionExternalId } = require('../helpers/licence-version.helper.js') const { generateLicenceVersionPurposeExternalId } = require('../helpers/licence-version-purpose.helper.js') @@ -47,7 +45,7 @@ async function seed (optionalLicenceRef = undefined) { licenceRef = optionalLicenceRef } - const { id: regionId } = await RegionHelper.add({ naldRegionId: 1 }) + const { id: regionId } = RegionHelper.select() const { id: licenceId } = await LicenceHelper.add({ licenceRef, regionId }) records.regionId = regionId @@ -65,24 +63,8 @@ async function seed (optionalLicenceRef = undefined) { } async function _financialAgreements () { - let section126 = await FinancialAgreementModel.query().select().where('code', 'S126').limit(1).first() - let twoPartTariff = await FinancialAgreementModel.query().select().where('code', 'S127').limit(1).first() - - // This is temporary whilst the clean is called by other tests. - // TODO: remove this when all database cleans have been removed - if (!section126) { - section126 = await FinancialAgreementHelper.add({ - code: 'S126', - description: 'Section 126' - }) - } - - if (!twoPartTariff) { - twoPartTariff = await FinancialAgreementHelper.add({ - code: 'S127', - description: 'Section 127 (Two Part Tariff)' - }) - } + const section126 = FinancialAgreementHelper.select(2) + const twoPartTariff = FinancialAgreementHelper.select(3) return { section126Id: section126.id, twoPartTariffId: twoPartTariff.id } } @@ -210,19 +192,19 @@ async function _permitLicence (licenceRef) { } async function _purposes () { - const { id: heatPumpId } = PurposesSeeder.data.find((purpose) => { return purpose.legacyId === '200' }) + const { id: heatPumpId } = PurposeHelper.data.find((purpose) => { return purpose.legacyId === '200' }) - const { id: sprayIrrigationDirectId } = PurposesSeeder.data.find((purpose) => { return purpose.legacyId === '400' }) + const { id: sprayIrrigationDirectId } = PurposeHelper.data.find((purpose) => { return purpose.legacyId === '400' }) - const { id: vegetableWashingId } = PurposesSeeder.data.find((purpose) => { return purpose.legacyId === '460' }) + const { id: vegetableWashingId } = PurposeHelper.data.find((purpose) => { return purpose.legacyId === '460' }) - const { id: primaryAgricultureId } = PrimaryPurposesSeeder.data.find((purpose) => { return purpose.legacyId === 'A' }) + const { id: primaryAgricultureId } = PrimaryPurposeHelper.data.find((purpose) => { return purpose.legacyId === 'A' }) - const { id: primaryElectricityId } = PrimaryPurposesSeeder.data.find((purpose) => { return purpose.legacyId === 'P' }) + const { id: primaryElectricityId } = PrimaryPurposeHelper.data.find((purpose) => { return purpose.legacyId === 'P' }) - const { id: secondaryAgricultureId } = SecondaryPurposesSeeder.data.find((purpose) => { return purpose.legacyId === 'AGR' }) + const { id: secondaryAgricultureId } = SecondaryPurposeHelper.data.find((purpose) => { return purpose.legacyId === 'AGR' }) - const { id: secondaryElectricityId } = SecondaryPurposesSeeder.data.find((purpose) => { return purpose.legacyId === 'ELC' }) + const { id: secondaryElectricityId } = SecondaryPurposeHelper.data.find((purpose) => { return purpose.legacyId === 'ELC' }) return { purposes: { heatPumpId, sprayIrrigationDirectId, vegetableWashingId }, diff --git a/test/support/seeders/licence-version-purpose-condition-types.seeder.js b/test/support/seeders/licence-version-purpose-condition-types.seeder.js deleted file mode 100644 index 3c21b7bb72..0000000000 --- a/test/support/seeders/licence-version-purpose-condition-types.seeder.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' - -/** - * @module LicenceVersionPurposeConditionTypeSeeder - */ - -const data = require('./data/licence-versions-purposes-condition-types.data.js') -const { buildSeedValueString } = require('./seed-builder.js') -const { db } = require('../../../db/db.js') - -const keys = ['id', 'code', 'subcode', 'description', 'subcodeDescription', 'displayTitle', 'createdAt', 'updatedAt'] - -/** - * Add all of the licence version purpose conditions to the database - * - */ -async function seed () { - await db.raw(` - INSERT INTO public.licence_version_purpose_condition_types (id, code, subcode, description, subcode_description, display_title, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)} - ON CONFLICT (code, subcode) - DO UPDATE SET - description = excluded.description, - subcode_description = excluded.subcode_description, - display_title = excluded.display_title, - updated_at = now() - ` - ) -} - -module.exports = { - data, - seed -} diff --git a/test/support/seeders/primary-purpose.seeder.js b/test/support/seeders/primary-purpose.seeder.js deleted file mode 100644 index 4ba0753100..0000000000 --- a/test/support/seeders/primary-purpose.seeder.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict' - -/** - * @module PrimaryPurposesSeeder - */ - -const data = require('./data/primary-purposes.data.js') -const { buildSeedValueString } = require('./seed-builder.js') -const { db } = require('../../../db/db.js') - -const keys = ['id', 'legacyId', 'description', 'createdAt', 'updatedAt'] - -/** - * Add all the purposes to the database - * - */ -async function seed () { - await db.raw(` - INSERT INTO public.primary_purposes (id, legacy_id, description, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)} - ON CONFLICT (legacy_id) - DO UPDATE SET - description = excluded.description, - updated_at = now() - ` - ) -} - -module.exports = { - seed, - data -} diff --git a/test/support/seeders/purposes.seeder.js b/test/support/seeders/purposes.seeder.js deleted file mode 100644 index 756d1b874a..0000000000 --- a/test/support/seeders/purposes.seeder.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' - -/** - * @module PurposesSeeder - */ - -const data = require('./data/purposes.data.js') -const { buildSeedValueString } = require('./seed-builder.js') -const { db } = require('../../../db/db.js') - -const keys = ['id', 'legacyId', 'description', 'lossFactor', 'twoPartTariff', 'createdAt', 'updatedAt'] - -/** - * Add all the purposes to the database - * - */ -async function seed () { - await db.raw(` - INSERT INTO public.purposes (id, legacy_id, description, loss_factor, two_part_tariff, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)} - ON CONFLICT (legacy_id) - DO UPDATE SET - description = excluded.description, - loss_factor = excluded.loss_factor, - two_part_tariff = excluded.two_part_tariff, - updated_at = now() - ` - ) -} - -module.exports = { - seed, - data -} diff --git a/test/support/seeders/regions.seeder.js b/test/support/seeders/regions.seeder.js deleted file mode 100644 index 626610a4ed..0000000000 --- a/test/support/seeders/regions.seeder.js +++ /dev/null @@ -1,52 +0,0 @@ -'use strict' - -/** - * @module RegionsSeeder - */ - -const data = require('./data/region.data.js') -const { db } = require('../../../db/db.js') - -/** - * Add all the regions to the database - * - * Regions does not have a unique constraint. - * Therefore we need to either insert a new record(with our fixed id) - * or update the record where the charge region id and nald region id are a match - * - */ -async function seed () { - for (const index in data) { - await db.raw(` - UPDATE public.regions - SET charge_region_id = '${data[index].chargeRegionId}', - nald_region_id = '${data[index].naldRegionId}', - name = 'fella', - display_name = '${data[index].displayName}', - updated_at = now() - WHERE charge_region_id = '${data[index].chargeRegionId}' - AND nald_region_id = '${data[index].naldRegionId}'; - - INSERT INTO public.regions (id, charge_region_id, nald_region_id, name, display_name, created_at, updated_at) - SELECT - '${data[index].id}', - '${data[index].chargeRegionId}', - '${data[index].naldRegionId}', - '${data[index].name}', - '${data[index].displayName}', - '${data[index].createdAt.toISOString()}', - now() - WHERE NOT EXISTS (SELECT 1 - FROM public.regions - WHERE charge_region_id = '${data[index].chargeRegionId}' - AND nald_region_id = '${data[index].naldRegionId}'); - - ` - ) - } -} - -module.exports = { - seed, - data -} diff --git a/test/support/seeders/requirements-for-returns.seeder.js b/test/support/seeders/requirements-for-returns.seeder.js index 3f9a2bd774..f02459d1c1 100644 --- a/test/support/seeders/requirements-for-returns.seeder.js +++ b/test/support/seeders/requirements-for-returns.seeder.js @@ -5,13 +5,12 @@ */ const LicenceHelper = require('../helpers/licence.helper.js') -const PurposesSeeder = require('../seeders/purposes.seeder.js') +const PurposeHelper = require('../helpers/purpose.helper.js') const ReturnRequirementHelper = require('../helpers/return-requirement.helper.js') const ReturnRequirementPointHelper = require('../helpers/return-requirement-point.helper.js') const ReturnRequirementPurposeHelper = require('../helpers/return-requirement-purpose.helper.js') const ReturnVersionHelper = require('../helpers/return-version.helper.js') const UserHelper = require('../helpers/user.helper.js') -const { generateUUID } = require('../../../app/lib/general.lib.js') /** * Add a complete 'requirements for returns' record, including return version, requirements, points and purposes @@ -32,8 +31,8 @@ const { generateUUID } = require('../../../app/lib/general.lib.js') * it is linked to */ async function seed () { - // Create a user - const user = await UserHelper.add({ username: `${generateUUID()}@wrls.gov.uk` }) + // Select a user + const user = UserHelper.select() // Create a licence const licence = await LicenceHelper.add() @@ -93,7 +92,7 @@ async function _returnRequirement ( returnRequirement.returnRequirementPoints = [point] - const purpose = PurposesSeeder.data.find((purpose) => { + const purpose = PurposeHelper.data.find((purpose) => { return purpose.legacyId === '420' }) diff --git a/test/support/seeders/secondary-purpose.seeder.js b/test/support/seeders/secondary-purpose.seeder.js deleted file mode 100644 index 5d6ab5c035..0000000000 --- a/test/support/seeders/secondary-purpose.seeder.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict' - -/** - * @module SecondaryPurposesSeeder - */ - -const data = require('./data/secondary-purposes.data.js') -const { buildSeedValueString } = require('./seed-builder.js') -const { db } = require('../../../db/db.js') - -const keys = ['id', 'legacyId', 'description', 'createdAt', 'updatedAt'] - -/** - * Add all the purposes to the database - * - */ -async function seed () { - await db.raw(` - INSERT INTO public.secondary_purposes (id, legacy_id, description, created_at, updated_at) - VALUES ${buildSeedValueString(keys, data)} - ON CONFLICT (legacy_id) - DO UPDATE SET - description = excluded.description, - updated_at = now() - ` - ) -} - -module.exports = { - seed, - data -} diff --git a/test/support/seeders/seed-builder.js b/test/support/seeders/seed-builder.js deleted file mode 100644 index 0be45792f1..0000000000 --- a/test/support/seeders/seed-builder.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict' - -function buildSeedValueString (keys, data) { - let valueString = '' - - data.forEach((obj, i) => { - valueString += '(' - keys.forEach((key, index) => { - try { - if (key === 'createdAt' || key === 'updatedAt') { - valueString += `'${obj[key].toISOString()}'` - } else { - valueString += `'${obj[key]}'` - } - - if (index < keys.length - 1) { - valueString += ',' - } - } catch (e) { - console.error('Failed: ', obj) - } - }) - - valueString += ')' - - if (i < data.length - 1) { - valueString += ',' - } - }) - - return valueString -} - -module.exports = { - buildSeedValueString -}