DEV: Prefabrication (test optimization) (#7414)

DEV: Prefabrication (test optimization) (#7414)

  • Introduced fab!, a helper that creates database state for a group

It’s almost identical to let_it_be, except:

  1. It creates a new object for each test by default,
  2. You can disable it using PREFABRICATION=0
diff --git a/spec/components/admin_confirmation_spec.rb b/spec/components/admin_confirmation_spec.rb
index a6efab9..1c09b8f 100644
--- a/spec/components/admin_confirmation_spec.rb
+++ b/spec/components/admin_confirmation_spec.rb
@@ -5,8 +5,8 @@ require 'rails_helper'
 
 describe AdminConfirmation do
 
-  let(:admin) { Fabricate(:admin) }
-  let(:user) { Fabricate(:user) }
+  fab!(:admin) { Fabricate(:admin) }
+  fab!(:user) { Fabricate(:user) }
 
   describe "create_confirmation" do
     it "raises an error for non-admins" do
diff --git a/spec/components/admin_user_index_query_spec.rb b/spec/components/admin_user_index_query_spec.rb
index d2f0afd..2e23232 100644
--- a/spec/components/admin_user_index_query_spec.rb
+++ b/spec/components/admin_user_index_query_spec.rb
@@ -93,8 +93,8 @@ describe AdminUserIndexQuery do
   end
 
   describe 'with a suspected user' do
-    let(:user) { Fabricate(:active_user, created_at: 1.day.ago) }
-    let(:bot) { Fabricate(:active_user, id: -10, created_at: 1.day.ago) }
+    fab!(:user) { Fabricate(:active_user, created_at: 1.day.ago) }
+    fab!(:bot) { Fabricate(:active_user, id: -10, created_at: 1.day.ago) }
 
     it 'finds the suspected user' do
       bot
@@ -106,8 +106,8 @@ describe AdminUserIndexQuery do
 
   describe "with a pending user" do
 
-    let!(:user) { Fabricate(:user, active: true, approved: false) }
-    let!(:inactive_user) { Fabricate(:user, approved: false, active: false) }
+    fab!(:user) { Fabricate(:user, active: true, approved: false) }
+    fab!(:inactive_user) { Fabricate(:user, approved: false, active: false) }
 
     it "finds the unapproved user" do
       query = ::AdminUserIndexQuery.new(query: 'pending')
@@ -116,7 +116,7 @@ describe AdminUserIndexQuery do
     end
 
     context 'and a suspended pending user' do
-      let!(:suspended_user) { Fabricate(:user, approved: false, suspended_at: 1.hour.ago, suspended_till: 20.years.from_now) }
+      fab!(:suspended_user) { Fabricate(:user, approved: false, suspended_at: 1.hour.ago, suspended_till: 20.years.from_now) }
       it "doesn't return the suspended user" do
         query = ::AdminUserIndexQuery.new(query: 'pending')
         expect(query.find_users).not_to include(suspended_user)
@@ -149,8 +149,8 @@ describe AdminUserIndexQuery do
 
   describe "with an admin user" do
 
-    let!(:user) { Fabricate(:user, admin: true) }
-    let!(:user2) { Fabricate(:user, admin: false) }
+    fab!(:user) { Fabricate(:user, admin: true) }
+    fab!(:user2) { Fabricate(:user, admin: false) }
 
     it "finds the admin" do
       query = ::AdminUserIndexQuery.new(query: 'admins')
@@ -161,8 +161,8 @@ describe AdminUserIndexQuery do
 
   describe "with a moderator" do
 
-    let!(:user) { Fabricate(:user, moderator: true) }
-    let!(:user2) { Fabricate(:user, moderator: false) }
+    fab!(:user) { Fabricate(:user, moderator: true) }
+    fab!(:user2) { Fabricate(:user, moderator: false) }
 
     it "finds the moderator" do
       query = ::AdminUserIndexQuery.new(query: 'moderators')
@@ -173,8 +173,8 @@ describe AdminUserIndexQuery do
 
   describe "with a silenced user" do
 
-    let!(:user) { Fabricate(:user, silenced_till: 1.year.from_now) }
-    let!(:user2) { Fabricate(:user) }
+    fab!(:user) { Fabricate(:user, silenced_till: 1.year.from_now) }
+    fab!(:user2) { Fabricate(:user) }
 
     it "finds the silenced user" do
       query = ::AdminUserIndexQuery.new(query: 'silenced')
@@ -185,8 +185,8 @@ describe AdminUserIndexQuery do
 
   describe "with a staged user" do
 
-    let!(:user) { Fabricate(:user, staged: true) }
-    let!(:user2) { Fabricate(:user, staged: false) }
+    fab!(:user) { Fabricate(:user, staged: true) }
+    fab!(:user2) { Fabricate(:user, staged: false) }
 
     it "finds the staged user" do
       query = ::AdminUserIndexQuery.new(query: 'staged')
@@ -256,7 +256,7 @@ describe AdminUserIndexQuery do
 
     context "by ip address fragment" do
 
-      let!(:user) { Fabricate(:user, ip_address: "117.207.94.9") }
+      fab!(:user) { Fabricate(:user, ip_address: "117.207.94.9") }
 
       it "matches the ip address" do
         query = ::AdminUserIndexQuery.new(filter: " 117.207.94.9 ")
diff --git a/spec/components/auth/default_current_user_provider_spec.rb b/spec/components/auth/default_current_user_provider_spec.rb
index 3027a16..d6320a9 100644
--- a/spec/components/auth/default_current_user_provider_spec.rb
+++ b/spec/components/auth/default_current_user_provider_spec.rb
@@ -368,7 +368,7 @@ describe Auth::DefaultCurrentUserProvider do
   end
 
   describe "#current_user" do
-    let(:user) { Fabricate(:user) }
+    fab!(:user) { Fabricate(:user) }
 
     let(:unhashed_token) do
       new_provider = provider('/')
@@ -605,7 +605,7 @@ describe Auth::DefaultCurrentUserProvider do
   end
 
   context "user api" do
-    let :user do
+    fab! :user do
       Fabricate(:user)
     end
 
diff --git a/spec/components/auth/facebook_authenticator_spec.rb b/spec/components/auth/facebook_authenticator_spec.rb
index babac8f..eae3e2b 100644
--- a/spec/components/auth/facebook_authenticator_spec.rb
+++ b/spec/components/auth/facebook_authenticator_spec.rb
@@ -49,7 +49,7 @@ describe Auth::FacebookAuthenticator do
   end
 
   context 'description_for_user' do
-    let(:user) { Fabricate(:user) }
+    fab!(:user) { Fabricate(:user) }
 
     it 'returns empty string if no entry for user' do
       expect(authenticator.description_for_user(user)).to eq("")
@@ -62,7 +62,7 @@ describe Auth::FacebookAuthenticator do
   end
 
   context 'revoke' do
-    let(:user) { Fabricate(:user) }
+    fab!(:user) { Fabricate(:user) }
     let(:authenticator) { Auth::FacebookAuthenticator.new }
 
     it 'raises exception if no entry for user' do
diff --git a/spec/components/auth/github_authenticator_spec.rb b/spec/components/auth/github_authenticator_spec.rb
index 0e7264e..37c14b1 100644
--- a/spec/components/auth/github_authenticator_spec.rb
+++ b/spec/components/auth/github_authenticator_spec.rb
@@ -23,7 +23,7 @@ end
 
 describe Auth::GithubAuthenticator do
   let(:authenticator) { described_class.new }
-  let(:user) { Fabricate(:user) }
+  fab!(:user) { Fabricate(:user) }
 
   context 'after_authenticate' do
     let(:data) do
@@ -251,7 +251,7 @@ describe Auth::GithubAuthenticator do
   end
 
   context 'revoke' do
-    let(:user) { Fabricate(:user) }
+    fab!(:user) { Fabricate(:user) }
     let(:authenticator) { Auth::GithubAuthenticator.new }
 
     it 'raises exception if no entry for user' do
@@ -271,8 +271,8 @@ describe Auth::GithubAuthenticator do
     let(:job_klass) { Jobs::DownloadAvatarFromUrl }
 
     context 'when user has a custom avatar' do
-      let(:user_avatar) { Fabricate(:user_avatar, custom_upload: Fabricate(:upload)) }
-      let(:user_with_custom_avatar) { Fabricate(:user, user_avatar: user_avatar) }
+      fab!(:user_avatar) { Fabricate(:user_avatar, custom_upload: Fabricate(:upload)) }
+      fab!(:user_with_custom_avatar) { Fabricate(:user, user_avatar: user_avatar) }
 
       it 'does not enqueue a download_avatar_from_url job' do
         expect {
diff --git a/spec/components/auth/google_oauth2_authenticator_spec.rb b/spec/components/auth/google_oauth2_authenticator_spec.rb
index 19824c7..71bd5cc 100644
--- a/spec/components/auth/google_oauth2_authenticator_spec.rb
+++ b/spec/components/auth/google_oauth2_authenticator_spec.rb
@@ -116,7 +116,7 @@ describe Auth::GoogleOAuth2Authenticator do
   end
 
   context 'revoke' do
-    let(:user) { Fabricate(:user) }
+    fab!(:user) { Fabricate(:user) }
     let(:authenticator) { Auth::GoogleOAuth2Authenticator.new }
 
     it 'raises exception if no entry for user' do
diff --git a/spec/components/auth/instagram_authenticator_spec.rb b/spec/components/auth/instagram_authenticator_spec.rb
index 2efd982..d78c632 100644
--- a/spec/components/auth/instagram_authenticator_spec.rb
+++ b/spec/components/auth/instagram_authenticator_spec.rb
@@ -42,7 +42,7 @@ describe Auth::InstagramAuthenticator do
   end
 

[... diff too long, it was truncated ...]

GitHub sha: e2195881