Generate Random Rather than Sequential Test Emails - #51400
Conversation
We expect emails to be globally unique, but the method we currently use to generate test emails for facilitators and workshop organizers means that multiple calls to `build :workshop_organizer` will result in the same email being generated for each user, which will cause an error if we attempt to persist both of them. This isn't a problem right now, because some unexpected behavior in FactoryBot v4 means that record are often automatically persisted upon creation. Starting in v5, we are much more likely to encounter this edge case, so in anticipation of updating to that version we change the generation logic to use unique emails rather than attempting to enforce an arbitrary sequence.
davidsbailey
left a comment
There was a problem hiding this comment.
Thank you for the thoughtful analysis, @Hamms ! Based on the information you've laid out, I think the solution you have here is simplest and best. Just to confirm my understanding, switching from build to create would also work, but is undesirable because it creates extra DB queries, is that correct?
|
Yep! Although I'm not particularly worried about extra records, since they get cleaned up at the end of a test run anyway. It's mostly undesirable because in order to switch from |
We expect emails to be globally unique, but the method we currently use to generate test emails for facilitators and workshop organizers means that multiple calls to
build :workshop_organizerwill result in the same email being generated for each user, which will cause an error if we attempt to persist both of them.This isn't a problem right now, because some unexpected behavior in FactoryBot v4 means that records are often automatically persisted upon creation. Starting in v5, we are much more likely to encounter the edge case of multiple records being initially built but not persisted only for a subset of them to be persisted after the fact. In anticipation of updating to that version, we change the generation logic to use unique emails rather than attempting to enforce an arbitrary sequence.
Links
I did some digging into the history of this email generation logic, and couldn't find any evidence that we cared about the sequential nature of the old approach; it seemed like we were just going with the simplest, cleanest option in the absence of a need for anything this complicated.
Testing story
Verified on another branch that this change resolves the issues with FactoryBot v5.
Follow-up work
Upgrade FactoryBot to v5