Skip to content

Commit

Permalink
Generate unique references in organisation factory
Browse files Browse the repository at this point in the history
Use Faker's `unique` feature to guarantee unique strings for the
`iati_reference` and `beis_organisation_reference` in the organisation
factory.

We have seen the test suite fail with an intermittent failure, unrelated
to actual code failure, caused by the organisation created failing the
uniqueness check for its `iati_reference`:

```
ActiveRecord::RecordInvalid:
Validation failed: Iati reference Iati reference has already been taken
```

E.g. https://github.com/UKGovernmentBEIS/beis-report-official-development-assistance/actions/runs/6978961311/job/18991313569

We suspect this intermittent failure is caused by Faker's pseudo-random
generator accidentally producing the same string within the same spec.
The IATI reference and BEIS organisation reference are more susceptible
to this, because the randomly generated sequences are only 5 characters
long.

Using Faker's unique feature means it's also a good idea to clear the
record of unique values that have been returned between each spec, to
avoid running out of combinations. (Not statistically likely, but why
take chances?)
  • Loading branch information
CristinaRO committed Dec 4, 2023
1 parent 0217077 commit c4f1c16
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spec/factories/organisation.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FactoryBot.define do
factory :__organisation do
sequence(:name) { |n| "#{Faker::Company.name} #{n}" }
beis_organisation_reference { Faker::Alphanumeric.alpha(number: 5).upcase! }
iati_reference { "GB-GOV-#{Faker::Alphanumeric.alpha(number: 5).upcase!}" }
beis_organisation_reference { Faker::Alphanumeric.unique.alpha(number: 5).upcase! }
iati_reference { "GB-GOV-#{Faker::Alphanumeric.unique.alpha(number: 5).upcase!}" }
organisation_type { "10" }
default_currency { "GBP" }
language_code { "en" }
Expand Down
5 changes: 5 additions & 0 deletions spec/support/faker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.configure do |config|
config.before(:each) do
Faker::Alphanumeric.unique.clear
end
end

0 comments on commit c4f1c16

Please sign in to comment.