Fabricators generating duplicate names because of aging local test database

I just had a bunch of tests fail with errors like this:

 ActiveRecord::RecordInvalid:                                                                                                                      
       Validation failed: Name has already been taken           

and it looks like this is the problem:

  fab!(:the_group) { Fabricate(:group) }

These all worked until recently. I saw this on some other plugin that I was working on and, I think, I contrived to set a random name myself, but these worked, I believe, as recently as yesterday.

Has something changed, or have I done something silly by mistake?

EDIT: With the help of Claude and the Discourse Helper Bot, I tried replacing the above fab with

  Fabricator(:the_group) do
    name { sequence(:group_name) { |i| "Test Group #{i}" } }
  end

but couldn’t quite figure out how to do it.

I then used

  let(:the_group) { Fabricate(:group) }

instead of the above !fab and this has reduced the number of failures from 12 to 1, but it’s still failing with

  1) Pfaffmanager::ServersController servers admin can get servers owned by a group                                                                    
     Failure/Error: let(:the_group) { Fabricate(:group) }                                                                                              
                                                                                                                                                       
     ActiveRecord::RecordInvalid:                                                                                                                      
       Validation failed: Name has already been taken   

I don’t think anything around these tests has changed since yesterday at 10:30 AM CDT when I did the last commit.

I reverted to an earlier commit and now it fails with

                                                                                                                                                       
  1) Pfaffmanager::ServersController servers includes groups that the user is in                                                                       
     Failure/Error: let(:the_group) { Fabricate(:group) }                                                                                              
                                                                                                                                                       
     ActiveRecord::RecordInvalid:                                                                                                                      
       Validation failed: Name has already been taken                 

and I ran it again without making any changes and now it fails on

  1) Pfaffmanager::ServersController servers can update group id if group owner                 
     Failure/Error: let(:the_group) { Fabricate(:group) }                                                                                              
                                                                                                                                                       
     ActiveRecord::RecordInvalid:                                                                                                                      
       Validation failed: Name has already been taken         

So it seems that it’s failing on one test, and it’s random?

1 Like

Perhaps your local test database got into a bad state? I’d try running:

RAILS_ENV=test bin/rake db:drop db:create db:migrate
3 Likes

OMG. So it was, indeed, gremlins.

Strangely, the first db:create didn’t create the multisite database, but when I ran just the db:create rake task by itself it did. And then the migration succeeded, and then it Just Worked.

And @Lilly you and your :eyes: crack me up.

I see, so something like I’d had that test database a long time and the randomly generated names started clashing with previous ones. That now makes sense. I changed my upgrade script (that does a git pull, refreshes code from all-the-plugins, checks ruby version and so one) to drop, create, migrate the test database.

4 Likes

hah. well i was eyeballing your code snippets because i was pretty sure that it should have worked because:

2 Likes

In theory the test database is supposed to be automatically cleaned up after every test run. So it should never end up in this state. But… if test processes get terminated badly somehow, I think it’s possible for that cleanup (technically, the restoration of a pg savepoint) to be skipped.

So this shouldn’t be a normal occurrence. Personally, I’d only bother with the drop/create/migrate if I start getting weird failures.

But, if you’re ok with the wait, there’s no harm in recreating your test database regularly :+1:

3 Likes

That makes sense. I do love ^c.

I hate waiting. I’ll comment it out and hope I remember. :slight_smile:

Thanks again for figuring this out. I was sure I was going crazy.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.