PERF: Shave off some test-suite time (#15183)

This commit is contained in:
Daniel Waterworth 2021-12-03 14:54:07 -06:00 committed by GitHub
parent 657c137384
commit 63112f89a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 67 deletions

View File

@ -3,6 +3,7 @@
require 'rails_helper' require 'rails_helper'
describe PostActionCreator do describe PostActionCreator do
fab!(:admin) { Fabricate(:admin) }
fab!(:user) { Fabricate(:user) } fab!(:user) { Fabricate(:user) }
fab!(:post) { Fabricate(:post) } fab!(:post) { Fabricate(:post) }
let(:like_type_id) { PostActionType.types[:like] } let(:like_type_id) { PostActionType.types[:like] }
@ -147,8 +148,6 @@ describe PostActionCreator do
end end
describe "When the post was already reviewed by staff" do describe "When the post was already reviewed by staff" do
fab!(:admin) { Fabricate(:admin) }
before { reviewable.perform(admin, :ignore) } before { reviewable.perform(admin, :ignore) }
it "fails because the post was recently reviewed" do it "fails because the post was recently reviewed" do
@ -221,7 +220,6 @@ describe PostActionCreator do
end end
context "queue_for_review" do context "queue_for_review" do
fab!(:admin) { Fabricate(:admin) }
it 'fails if the user is not a staff member' do it 'fails if the user is not a staff member' do
creator = PostActionCreator.new( creator = PostActionCreator.new(

View File

@ -492,7 +492,7 @@ describe PostAction do
it "shouldn't change given_likes unless likes are given or removed" do it "shouldn't change given_likes unless likes are given or removed" do
freeze_time(Time.zone.now) freeze_time(Time.zone.now)
PostActionCreator.like(codinghorror, Fabricate(:post)) PostActionCreator.like(codinghorror, post)
expect(value_for(codinghorror.id, Date.today)).to eq(1) expect(value_for(codinghorror.id, Date.today)).to eq(1)
PostActionType.types.each do |type_name, type_id| PostActionType.types.each do |type_name, type_id|
@ -513,20 +513,17 @@ describe PostAction do
describe 'flagging' do describe 'flagging' do
it 'does not allow you to flag stuff twice, even if the reason is different' do it 'does not allow you to flag stuff twice, even if the reason is different' do
post = Fabricate(:post)
expect(PostActionCreator.spam(eviltrout, post)).to be_success expect(PostActionCreator.spam(eviltrout, post)).to be_success
expect(PostActionCreator.off_topic(eviltrout, post)).to be_failed expect(PostActionCreator.off_topic(eviltrout, post)).to be_failed
end end
it 'allows you to flag stuff again if your previous flag was removed' do it 'allows you to flag stuff again if your previous flag was removed' do
post = Fabricate(:post)
PostActionCreator.spam(eviltrout, post) PostActionCreator.spam(eviltrout, post)
PostActionDestroyer.destroy(eviltrout, post, :spam) PostActionDestroyer.destroy(eviltrout, post, :spam)
expect(PostActionCreator.spam(eviltrout, post)).to be_success expect(PostActionCreator.spam(eviltrout, post)).to be_success
end end
it 'should update counts when you clear flags' do it 'should update counts when you clear flags' do
post = Fabricate(:post)
reviewable = PostActionCreator.spam(eviltrout, post).reviewable reviewable = PostActionCreator.spam(eviltrout, post).reviewable
expect(post.reload.spam_count).to eq(1) expect(post.reload.spam_count).to eq(1)
@ -874,7 +871,6 @@ describe PostAction do
describe ".lookup_for" do describe ".lookup_for" do
it "returns the correct map" do it "returns the correct map" do
user = Fabricate(:user) user = Fabricate(:user)
post = Fabricate(:post)
post_action = PostActionCreator.create(user, post, :bookmark).post_action post_action = PostActionCreator.create(user, post, :bookmark).post_action
map = PostAction.lookup_for(user, [post.topic], post_action.post_action_type_id) map = PostAction.lookup_for(user, [post.topic], post_action.post_action_type_id)
@ -898,7 +894,6 @@ describe PostAction do
it "should create a notification in the related topic" do it "should create a notification in the related topic" do
Jobs.run_immediately! Jobs.run_immediately!
post = Fabricate(:post)
user = Fabricate(:user) user = Fabricate(:user)
stub_image_size stub_image_size
result = PostActionCreator.create(user, post, :spam, message: "WAT") result = PostActionCreator.create(user, post, :spam, message: "WAT")
@ -915,7 +910,6 @@ describe PostAction do
skip "should not add a moderator post when post is flagged via private message" do skip "should not add a moderator post when post is flagged via private message" do
Jobs.run_immediately! Jobs.run_immediately!
post = Fabricate(:post)
user = Fabricate(:user) user = Fabricate(:user)
result = PostActionCreator.create(user, post, :notify_user, message: "WAT") result = PostActionCreator.create(user, post, :notify_user, message: "WAT")
action = result.post_action action = result.post_action
@ -986,8 +980,6 @@ describe PostAction do
end end
describe "triggers Discourse events" do describe "triggers Discourse events" do
fab!(:post) { Fabricate(:post) }
it 'triggers a flag_created event' do it 'triggers a flag_created event' do
event = DiscourseEvent.track(:flag_created) { PostActionCreator.spam(eviltrout, post) } event = DiscourseEvent.track(:flag_created) { PostActionCreator.spam(eviltrout, post) }
expect(event).to be_present expect(event).to be_present

View File

@ -6,6 +6,7 @@ require 'rails_helper'
describe Topic do describe Topic do
let(:now) { Time.zone.local(2013, 11, 20, 8, 0) } let(:now) { Time.zone.local(2013, 11, 20, 8, 0) }
fab!(:user) { Fabricate(:user) } fab!(:user) { Fabricate(:user) }
fab!(:admin) { Fabricate(:admin) }
fab!(:another_user) { Fabricate(:user) } fab!(:another_user) { Fabricate(:user) }
fab!(:trust_level_2) { Fabricate(:user, trust_level: SiteSetting.min_trust_level_to_allow_invite) } fab!(:trust_level_2) { Fabricate(:user, trust_level: SiteSetting.min_trust_level_to_allow_invite) }
@ -249,8 +250,6 @@ describe Topic do
end end
context 'admin topic title' do context 'admin topic title' do
let(:admin) { Fabricate(:admin) }
it 'allows really short titles' do it 'allows really short titles' do
pm = Fabricate.build(:private_message_topic, user: admin, title: 'a') pm = Fabricate.build(:private_message_topic, user: admin, title: 'a')
expect(pm).to be_valid expect(pm).to be_valid
@ -747,7 +746,6 @@ describe Topic do
context "when invited_user has enabled allow_list" do context "when invited_user has enabled allow_list" do
fab!(:user2) { Fabricate(:user) } fab!(:user2) { Fabricate(:user) }
fab!(:admin) { Fabricate(:admin) }
fab!(:pm) { Fabricate(:private_message_topic, user: user, topic_allowed_users: [ fab!(:pm) { Fabricate(:private_message_topic, user: user, topic_allowed_users: [
Fabricate.build(:topic_allowed_user, user: user), Fabricate.build(:topic_allowed_user, user: user),
Fabricate.build(:topic_allowed_user, user: user2) Fabricate.build(:topic_allowed_user, user: user2)
@ -1864,7 +1862,6 @@ describe Topic do
Fabricate(:topic_timer, execute_at: 5.hours.from_now).topic Fabricate(:topic_timer, execute_at: 5.hours.from_now).topic
end end
fab!(:admin) { Fabricate(:admin) }
fab!(:trust_level_4) { Fabricate(:trust_level_4) } fab!(:trust_level_4) { Fabricate(:trust_level_4) }
it 'can take a number of hours as an integer' do it 'can take a number of hours as an integer' do
@ -2955,7 +2952,6 @@ describe Topic do
describe "#cannot_permanently_delete_reason" do describe "#cannot_permanently_delete_reason" do
fab!(:post) { Fabricate(:post) } fab!(:post) { Fabricate(:post) }
let!(:topic) { post.topic } let!(:topic) { post.topic }
fab!(:admin) { Fabricate(:admin) }
before do before do
freeze_time freeze_time

View File

@ -4,7 +4,7 @@ require 'rails_helper'
describe GroupsController do describe GroupsController do
fab!(:user) { Fabricate(:user) } fab!(:user) { Fabricate(:user) }
let(:other_user) { Fabricate(:user) } fab!(:other_user) { Fabricate(:user) }
let(:group) { Fabricate(:group, users: [user]) } let(:group) { Fabricate(:group, users: [user]) }
let(:moderator_group_id) { Group::AUTO_GROUPS[:moderators] } let(:moderator_group_id) { Group::AUTO_GROUPS[:moderators] }
fab!(:admin) { Fabricate(:admin) } fab!(:admin) { Fabricate(:admin) }
@ -92,7 +92,7 @@ describe GroupsController do
sign_in(user) sign_in(user)
end end
let!(:other_group) { Fabricate(:group, name: "other_group", users: [user, other_user]) } fab!(:other_group) { Fabricate(:group, name: "other_group", users: [user, other_user]) }
context "with default (descending) order" do context "with default (descending) order" do
it "sorts by name" do it "sorts by name" do
@ -213,7 +213,7 @@ describe GroupsController do
expect(group_names).to contain_exactly("0_0") expect(group_names).to contain_exactly("0_0")
# logged in user # logged in user
sign_in(Fabricate(:user)) sign_in(user)
get "/groups.json", params: { username: u.username } get "/groups.json", params: { username: u.username }
expect(response.status).to eq(200) expect(response.status).to eq(200)
@ -256,8 +256,6 @@ describe GroupsController do
end end
context 'viewing as an admin' do context 'viewing as an admin' do
fab!(:admin) { Fabricate(:admin) }
before do before do
sign_in(admin) sign_in(admin)
group.add(admin) group.add(admin)
@ -356,7 +354,7 @@ describe GroupsController do
describe '#show' do describe '#show' do
it "ensures the group can be seen" do it "ensures the group can be seen" do
sign_in(Fabricate(:user)) sign_in(user)
group.update!(visibility_level: Group.visibility_levels[:owners]) group.update!(visibility_level: Group.visibility_levels[:owners])
get "/groups/#{group.name}.json" get "/groups/#{group.name}.json"
@ -397,7 +395,7 @@ describe GroupsController do
context 'as an admin' do context 'as an admin' do
it "returns the right response" do it "returns the right response" do
sign_in(Fabricate(:admin)) sign_in(admin)
get "/groups/#{group.name}.json" get "/groups/#{group.name}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
@ -448,7 +446,7 @@ describe GroupsController do
describe "#posts" do describe "#posts" do
it "ensures the group can be seen" do it "ensures the group can be seen" do
sign_in(Fabricate(:user)) sign_in(user)
group.update!(visibility_level: Group.visibility_levels[:owners]) group.update!(visibility_level: Group.visibility_levels[:owners])
get "/groups/#{group.name}/posts.json" get "/groups/#{group.name}/posts.json"
@ -457,7 +455,7 @@ describe GroupsController do
end end
it "ensures the group members can be seen" do it "ensures the group members can be seen" do
sign_in(Fabricate(:user)) sign_in(user)
group.update!(members_visibility_level: Group.visibility_levels[:owners]) group.update!(members_visibility_level: Group.visibility_levels[:owners])
get "/groups/#{group.name}/posts.json" get "/groups/#{group.name}/posts.json"
@ -487,7 +485,7 @@ describe GroupsController do
describe "#members" do describe "#members" do
it "returns correct error code with invalid params" do it "returns correct error code with invalid params" do
sign_in(Fabricate(:user)) sign_in(user)
get "/groups/#{group.name}/members.json?limit=-1" get "/groups/#{group.name}/members.json?limit=-1"
expect(response.status).to eq(400) expect(response.status).to eq(400)
@ -500,7 +498,7 @@ describe GroupsController do
end end
it "ensures the group can be seen" do it "ensures the group can be seen" do
sign_in(Fabricate(:user)) sign_in(user)
group.update!(visibility_level: Group.visibility_levels[:owners]) group.update!(visibility_level: Group.visibility_levels[:owners])
get "/groups/#{group.name}/members.json" get "/groups/#{group.name}/members.json"
@ -1188,7 +1186,7 @@ describe GroupsController do
end end
it "can show group requests" do it "can show group requests" do
sign_in(Fabricate(:admin)) sign_in(admin)
user4 = Fabricate(:user) user4 = Fabricate(:user)
request4 = Fabricate(:group_request, user: user4, group: group) request4 = Fabricate(:group_request, user: user4, group: group)
@ -1217,7 +1215,7 @@ describe GroupsController do
describe 'as an admin' do describe 'as an admin' do
before do before do
sign_in(Fabricate(:admin)) sign_in(admin)
end end
it "should allow members to be filterable by username" do it "should allow members to be filterable by username" do
@ -1296,11 +1294,10 @@ describe GroupsController do
end end
context 'when user is an admin' do context 'when user is an admin' do
fab!(:user) { Fabricate(:admin) } fab!(:group) { Fabricate(:group, users: [admin], automatic: true) }
let(:group) { Fabricate(:group, users: [user], automatic: true) }
before do before do
sign_in(user) sign_in(admin)
end end
it "cannot add members to automatic groups" do it "cannot add members to automatic groups" do
@ -1314,8 +1311,6 @@ describe GroupsController do
end end
describe "membership edits" do describe "membership edits" do
fab!(:admin) { Fabricate(:admin) }
context '#add_members' do context '#add_members' do
before do before do
sign_in(admin) sign_in(admin)
@ -1370,7 +1365,7 @@ describe GroupsController do
group.add_owner(user) group.add_owner(user)
sign_in(user) sign_in(user)
put "/groups/#{group.id}/members.json", params: { usernames: Fabricate(:user).username } put "/groups/#{group.id}/members.json", params: { usernames: other_user.username }
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
@ -1564,8 +1559,6 @@ describe GroupsController do
end end
context 'public group' do context 'public group' do
fab!(:other_user) { Fabricate(:user) }
before do before do
group.update!( group.update!(
public_admission: true, public_admission: true,
@ -1713,7 +1706,6 @@ describe GroupsController do
end end
context 'public group' do context 'public group' do
fab!(:other_user) { Fabricate(:user) }
let(:group) { Fabricate(:public_group, users: [other_user]) } let(:group) { Fabricate(:public_group, users: [other_user]) }
context "admin" do context "admin" do
@ -1923,8 +1915,6 @@ describe GroupsController do
end end
context 'when user is an admin' do context 'when user is an admin' do
fab!(:admin) { Fabricate(:admin) }
before do before do
sign_in(admin) sign_in(admin)
end end
@ -2106,7 +2096,7 @@ describe GroupsController do
context 'as an admin' do context 'as an admin' do
it "returns the right response" do it "returns the right response" do
sign_in(Fabricate(:admin)) sign_in(admin)
get '/groups/search.json?ignore_automatic=true' get '/groups/search.json?ignore_automatic=true'
@ -2141,7 +2131,7 @@ describe GroupsController do
end end
describe 'for an admin user' do describe 'for an admin user' do
before { sign_in(Fabricate(:admin)) } before { sign_in(admin) }
it 'should return 200' do it 'should return 200' do
get '/groups/custom/new' get '/groups/custom/new'

View File

@ -202,7 +202,7 @@ describe UsersController do
end end
context 'valid token' do context 'valid token' do
let!(:user) { Fabricate(:user) } fab!(:user) { Fabricate(:user) }
let!(:user_auth_token) { UserAuthToken.generate!(user_id: user.id) } let!(:user_auth_token) { UserAuthToken.generate!(user_id: user.id) }
let!(:email_token) { Fabricate(:email_token, user: user, scope: EmailToken.scopes[:password_reset]) } let!(:email_token) { Fabricate(:email_token, user: user, scope: EmailToken.scopes[:password_reset]) }

View File

@ -371,7 +371,6 @@ describe UserDestroyer do
describe "Destroying a user with security key" do describe "Destroying a user with security key" do
let!(:security_key) { Fabricate(:user_security_key_with_random_credential, user: user) } let!(:security_key) { Fabricate(:user_security_key_with_random_credential, user: user) }
fab!(:admin) { Fabricate(:admin) }
it "removes the security key" do it "removes the security key" do
UserDestroyer.new(admin).destroy(user) UserDestroyer.new(admin).destroy(user)
@ -381,7 +380,6 @@ describe UserDestroyer do
describe "Destroying a user with a bookmark" do describe "Destroying a user with a bookmark" do
let!(:bookmark) { Fabricate(:bookmark, user: user) } let!(:bookmark) { Fabricate(:bookmark, user: user) }
fab!(:admin) { Fabricate(:admin) }
it "removes the bookmark" do it "removes the bookmark" do
UserDestroyer.new(admin).destroy(user) UserDestroyer.new(admin).destroy(user)

View File

@ -7,6 +7,13 @@ describe UserMerger do
fab!(:source_user) { Fabricate(:user, username: 'alice1', email: 'alice@work.com') } fab!(:source_user) { Fabricate(:user, username: 'alice1', email: 'alice@work.com') }
fab!(:walter) { Fabricate(:walter_white) } fab!(:walter) { Fabricate(:walter_white) }
fab!(:p1) { Fabricate(:post) }
fab!(:p2) { Fabricate(:post) }
fab!(:p3) { Fabricate(:post) }
fab!(:p4) { Fabricate(:post) }
fab!(:p5) { Fabricate(:post) }
fab!(:p6) { Fabricate(:post) }
def merge_users!(source = nil, target = nil) def merge_users!(source = nil, target = nil)
source ||= source_user source ||= source_user
target ||= target_user target ||= target_user
@ -154,13 +161,6 @@ describe UserMerger do
end end
it "merges likes" do it "merges likes" do
p1 = Fabricate(:post)
p2 = Fabricate(:post)
p3 = Fabricate(:post)
p4 = Fabricate(:post)
p5 = Fabricate(:post)
p6 = Fabricate(:post)
now = Time.zone.now now = Time.zone.now
freeze_time(now - 1.day) freeze_time(now - 1.day)
@ -334,9 +334,6 @@ describe UserMerger do
context "post actions" do context "post actions" do
it "merges post actions" do it "merges post actions" do
p1 = Fabricate(:post)
p2 = Fabricate(:post)
p3 = Fabricate(:post)
type_ids = PostActionType.public_type_ids + [PostActionType.flag_types.values.first] type_ids = PostActionType.public_type_ids + [PostActionType.flag_types.values.first]
type_ids.each do |type| type_ids.each do |type|
@ -357,11 +354,6 @@ describe UserMerger do
end end
it "updates post actions" do it "updates post actions" do
p1 = Fabricate(:post)
p2 = Fabricate(:post)
p3 = Fabricate(:post)
p4 = Fabricate(:post)
action1 = PostActionCreator.create(source_user, p1, :off_topic).post_action action1 = PostActionCreator.create(source_user, p1, :off_topic).post_action
action1.update_attribute(:deleted_by_id, source_user.id) action1.update_attribute(:deleted_by_id, source_user.id)
@ -384,7 +376,7 @@ describe UserMerger do
end end
it "updates post revisions" do it "updates post revisions" do
post = Fabricate(:post) post = p1
post_revision = Fabricate(:post_revision, post: post, user: source_user) post_revision = Fabricate(:post_revision, post: post, user: source_user)
merge_users! merge_users!
@ -410,9 +402,9 @@ describe UserMerger do
end end
it "merges post timings" do it "merges post timings" do
post1 = Fabricate(:post) post1 = p1
post2 = Fabricate(:post) post2 = p2
post3 = Fabricate(:post) post3 = p3
create_post_timing(post1, source_user, 12345) create_post_timing(post1, source_user, 12345)
create_post_timing(post2, source_user, 9876) create_post_timing(post2, source_user, 9876)
@ -678,8 +670,8 @@ describe UserMerger do
# action_type and user_id are not nullable # action_type and user_id are not nullable
# target_topic_id and acting_user_id are nullable, but always have a value # target_topic_id and acting_user_id are nullable, but always have a value
fab!(:post1) { Fabricate(:post) } fab!(:post1) { p1 }
fab!(:post2) { Fabricate(:post) } fab!(:post2) { p2 }
def log_like_action(acting_user, user, post) def log_like_action(acting_user, user, post)
UserAction.log_action!(action_type: UserAction::LIKE, UserAction.log_action!(action_type: UserAction::LIKE,