Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Remove more DatabaseSupport.clean() from tests #1241

Merged
merged 5 commits into from
Aug 7, 2024

Conversation

jonathangoulding
Copy link
Collaborator

@jonathangoulding jonathangoulding commented Aug 7, 2024

DEFRA/water-abstraction-team#124

This change, like all things, initially started with small changes in the bills and licences service tests. As we moved on to the return requirements, most of them were straight-forward. However, some complexity was added with the licence-abstraction-data.seeder.js. Some temporary code has been introduced to create the financial agreements if they don't exist (they are queried and used if they do). This is necessary as some tests are still cleaning the database.

We had a pattern of calling await DatabaseSupport.clean() in a beforeEach() in any unit test file that depended on adding data to support its tests.

The problem is that the project is becoming so large that it is no longer sustainable. It is also a blocker to moving to a test framework that supports ECMAScript Modules, the official standard format to package JavaScript code. Unit test frameworks other than Hapi Lab run tests in parallel by default. This means you can't have one test running DB clean while another is trying to load data.

New tests avoid the pattern, and where we can, we've been updating old ones when we have had cause to update the unit tests.

But there are always those where it'll be some time before we have cause to look at them again. So, rather than waiting, this change handles updating all tests in test/services/bill-licences to drop their dependence on DatabaseSupport.clean().

We had a pattern of calling `await DatabaseSupport.clean()` in a `beforeEach()` in any unit test file that depended on adding data to support its tests.

The problem is that the project is becoming so large that it is no longer sustainable. It is also a blocker to moving to a test framework that supports [ECMAScript Modules](https://nodejs.org/api/esm.html), [the official standard format](https://tc39.github.io/ecma262/#sec-modules) to package JavaScript code. Unit test frameworks other than [Hapi Lab](https://hapi.dev/module/lab/) run tests in parallel by default. This means you can't have one test running DB clean while another is trying to load data.

New tests avoid the pattern, and where we can, we've been updating old ones when we have had cause to update the unit tests.

But there are always those where it'll be some time before we have cause to look at them again. So, rather than waiting, this change handles updating all tests in `test/services/bill-licences` to drop their dependence on `DatabaseSupport.clean()`.
@jonathangoulding jonathangoulding added the testing A change to the tests or CI label Aug 7, 2024
@jonathangoulding jonathangoulding self-assigned this Aug 7, 2024
@jonathangoulding
Copy link
Collaborator Author

Tested with the seed and clean commented out to ensure the test will work generically.

code: 'S127',
description: 'Section 127 (Two Part Tariff)'
})
const section126 = await FinancialAgreementModel.query().select().where('code', 'S126').limit(1).first()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary / easy to manage duplicate insert for what was the same data.

If these can be seeder then this can use the seeder data files in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is okay.

Then reviewer can you resolve.

@jonathangoulding jonathangoulding marked this pull request as ready for review August 7, 2024 15:19
@jonathangoulding
Copy link
Collaborator Author

jonathangoulding commented Aug 7, 2024

Most of the changes are the removal of the clean database code.

Some changes have been minimally added to generate unique data i.e generateLicenceRef() / generateUUID().
This keeps the actually test changes to a minimum.

The biggest changes are in the licence-abstraction-data.seeder.js.

licence-abstraction-data.seeder.js

@Cruikshanks Cruikshanks changed the title Remove DatabaseSupport.clean() Remove more DatabaseSupport.clean() from tests Aug 7, 2024
Copy link
Member

@Cruikshanks Cruikshanks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cruikshanks Cruikshanks merged commit 46f637e into main Aug 7, 2024
6 checks passed
@Cruikshanks Cruikshanks deleted the remove-database-clean-1 branch August 7, 2024 16:17
@Cruikshanks
Copy link
Member

Sorry @jonathangoulding . I merged on your behalf because both this and my PR #1230 touch a lot of the same files. Having this merged means I can crack on and deal with the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing A change to the tests or CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants