Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Make test data helpers more 'unique' #452

Merged
merged 15 commits into from
Oct 8, 2023

Conversation

Cruikshanks
Copy link
Member

@Cruikshanks Cruikshanks commented Oct 5, 2023

https://eaflood.atlassian.net/browse/WATER-4144

We've been investigating changing our unit test framework recently (see Investigate switching to jest and water-abstraction-ava).

A key issue we'll have if we switch is that most other frameworks run tests asynchronously. This means our pattern of clean-setup-assert won't work. At best we can clean before any tests run but then the test data we set up must not clash.

To achieve that we need to make the test data the helpers generate as unique as possible, which means moving away from hard-coded values.

We're still playing with frameworks but there is nothing stopping us from making this change now ahead of a decision.

So, this change covers making the helpers generate more unique and fixing any tests that break.


Notes

  • Add randomInteger() to new GeneralHelper

Where a helper is hard-coding a UUID for a field is going to be easy to make it unique; we just call our GeneralLib.generateUUID() function.

But many helpers also need to generate references, licence numbers being a good example. If we are going to make these unique we're going to need a means to generate random numbers. Often they need to be within a certain range, for example, 100 to 999 because the number must be a certain number of digits long.

So, we're adding a new randomInteger() function which can handle this requirement. Doing so forced us to make a decision as to where to put it. We decided to create a GeneralHelper module that can be used going forward for any shared functionality in the helpers.

https://eaflood.atlassian.net/browse/WATER-4144

We've been investigating changing our unit test framework recently (see [Investigate switching to jest](#430) and [water-abstraction-ava](https://github.com/DEFRA/water-abstraction-ava)).

A key issue we'll have if we switch is that most other frameworks run tests asynchronously. This means our pattern of clean-setup-assert won't work. At best we can clean before any tests run but then the test data we setup must not clash.

To achieve that we need to make the test data the helpers generate as unique as possible, which means moving away from hard coded values.

We're still playing with frameworks but there is nothing stopping us making this change now ahead of a decision.

So, this change covers making the helpers generate more unique and fixing any tests that breaks.
@Cruikshanks Cruikshanks added the housekeeping Refactoring, tidying up or other work which supports the project label Oct 5, 2023
@Cruikshanks Cruikshanks self-assigned this Oct 5, 2023
Where a helper is hard coding a UUID for a field is going to be easy to make unique; we just call our `GeneralLib.generateUUID()` function.

But many helpers also need to generate references, licence number being a good example. If we are going to make these unique we're going to need a means to generate random numbers. Often they need to be within a certain range, for example, 100 to 999 because the number must be a certain number of digits long.

So, we're adding a new `randomInteger()` function which can handle this requirement. Doing so forced us to make a decision as to where to put it. We decided to create a `GeneralHelper` module that can be used going forward for any shared functionality in the helpers.
The tests relied on known uprn and company numbers. They needed to be tweaked to work with generated numbers which meant the tests needed to access the same reference generators as the helpers.
@Cruikshanks Cruikshanks force-pushed the make-test-helper-records-unique branch from fea9cf3 to 01b5be8 Compare October 5, 2023 15:55
@Cruikshanks Cruikshanks marked this pull request as ready for review October 6, 2023 12:59
@Cruikshanks
Copy link
Member Author

This is bigger than I'd like it to be. But it made sense to do all the helpers at the same time, especially as the changes are generally consistent across them.

Happy to walk/talk through anything if anyone has questions.

Copy link
Contributor

@Beckyrose200 Beckyrose200 left a comment

Choose a reason for hiding this comment

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

Just a few things I noticed, but looks good :D

Copy link
Contributor

@Jozzey Jozzey left a comment

Choose a reason for hiding this comment

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

Just spotted 1 minor infraction 😁

Copy link
Contributor

@Jozzey Jozzey 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 1f972f3 into main Oct 8, 2023
6 checks passed
@Cruikshanks Cruikshanks deleted the make-test-helper-records-unique branch October 8, 2023 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, tidying up or other work which supports the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants