PERF: Speed up the tests by pre-fabricating more things (#15318)

This commit is contained in:
Daniel Waterworth
2021-12-15 11:41:14 -06:00
committed by GitHub
parent 6120dde65c
commit 102fa71ef3
12 changed files with 118 additions and 158 deletions

View File

@@ -26,8 +26,21 @@ end
RSpec::Matchers.define_negated_matcher :not_add_notification, :add_notification
describe PostAlerter do
fab!(:category) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post) }
fab!(:private_message_topic) { Fabricate(:private_message_topic) }
fab!(:private_message_topic_post1) { Fabricate(:post, topic: private_message_topic) }
fab!(:private_message_topic_post2) { Fabricate(:post, topic: private_message_topic) }
fab!(:group) { Fabricate(:group) }
fab!(:admin) { Fabricate(:admin) }
fab!(:evil_trout) { Fabricate(:evil_trout) }
fab!(:coding_horror) { Fabricate(:coding_horror) }
fab!(:walterwhite) { Fabricate(:walter_white) }
fab!(:user) { Fabricate(:user) }
fab!(:tl2_user) { Fabricate(:user, trust_level: TrustLevel[2]) }
@@ -174,7 +187,6 @@ describe PostAlerter do
end
it "Doesn't notify non-admin users when their post is quoted inside a whisper" do
admin = Fabricate(:admin)
group.add(admin)
TopicUser.change(user2.id, pm.id, notification_level: TopicUser.notification_levels[:regular])
@@ -193,14 +205,13 @@ describe PostAlerter do
context "unread" do
it "does not return whispers as unread posts" do
op = Fabricate(:post)
_whisper = Fabricate(:post, raw: 'this is a whisper post',
user: Fabricate(:admin),
topic: op.topic,
reply_to_post_number: op.post_number,
user: admin,
topic: post.topic,
reply_to_post_number: post.post_number,
post_type: Post.types[:whisper])
expect(PostAlerter.new.first_unread_post(op.user, op.topic)).to be_blank
expect(PostAlerter.new.first_unread_post(post.user, post.topic)).to be_blank
end
end
@@ -211,8 +222,6 @@ describe PostAlerter do
post = Fabricate(:post, raw: 'I love waffles')
admin = Fabricate(:admin)
expect do
post.revise(admin, raw: 'I made a revision')
end.to add_notification(post.user, :edited)
@@ -254,8 +263,6 @@ describe PostAlerter do
it 'notifies flaggers when flagged post gets unhidden by edit' do
post = create_post
walterwhite = Fabricate(:walter_white)
coding_horror = Fabricate(:coding_horror)
PostActionNotifier.enable
Reviewable.set_priorities(high: 4.0)
@@ -295,8 +302,8 @@ describe PostAlerter do
end
context 'quotes' do
let(:category) { Fabricate(:category) }
let(:topic) { Fabricate(:topic, category: category) }
fab!(:category) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic, category: category) }
it 'does not notify for muted users' do
post = Fabricate(:post, raw: '[quote="EvilTrout, post:1"]whatup[/quote]', topic: topic)
@@ -383,8 +390,6 @@ describe PostAlerter do
expect(user.notifications.count).to eq(1)
topic = Fabricate(:topic)
watcher = Fabricate(:user)
TopicUser.create!(user_id: watcher.id, topic_id: topic.id, notification_level: TopicUser.notification_levels[:watching])
@@ -409,7 +414,6 @@ describe PostAlerter do
it "doesn't notify the linked user if the user is staged and the category is restricted and allows strangers" do
staged_user = Fabricate(:staged)
group = Fabricate(:group)
group_member = Fabricate(:user)
group.add(group_member)
@@ -431,9 +435,8 @@ describe PostAlerter do
end
context '@here' do
let(:topic) { Fabricate(:topic) }
let(:post) { create_post_with_alerts(raw: "Hello @here how are you?", user: tl2_user, topic: topic) }
let(:other_post) { Fabricate(:post, topic: topic) }
fab!(:other_post) { Fabricate(:post, topic: topic) }
before do
Jobs.run_immediately!
@@ -567,13 +570,12 @@ describe PostAlerter do
end
it "notification comes from editor if mention is added later" do
admin = Fabricate(:admin)
post = create_post_with_alerts(user: user, raw: 'No mention here.')
expect {
post.revise(admin, raw: "Mention @eviltrout in this edit.")
}.to change(evil_trout.notifications, :count)
n = evil_trout.notifications.last
expect(n.data_hash["original_username"]).to eq(admin.username)
post = create_post_with_alerts(user: user, raw: 'No mention here.')
expect {
post.revise(admin, raw: "Mention @eviltrout in this edit.")
}.to change(evil_trout.notifications, :count)
n = evil_trout.notifications.last
expect(n.data_hash["original_username"]).to eq(admin.username)
end
it "doesn't notify the last post editor if they mention themselves" do
@@ -984,7 +986,6 @@ describe PostAlerter do
describe "create_notification_alert" do
it "does nothing for suspended users" do
evil_trout.update_columns(suspended_till: 1.year.from_now)
post = Fabricate(:post)
events = nil
messages = MessageBus.track_publish do
@@ -1006,7 +1007,6 @@ describe PostAlerter do
it "does not publish to MessageBus /notification-alert if the user has not been seen for > 30 days, but still sends a push notification" do
evil_trout.update_columns(last_seen_at: 31.days.ago)
post = Fabricate(:post)
SiteSetting.allowed_user_api_push_urls = "https://site2.com/push"
UserApiKey.create!(user_id: evil_trout.id,
@@ -1035,7 +1035,6 @@ describe PostAlerter do
end
describe "watching_first_post" do
fab!(:group) { Fabricate(:group) }
fab!(:user) { Fabricate(:user) }
fab!(:category) { Fabricate(:category) }
fab!(:tag) { Fabricate(:tag) }
@@ -1092,8 +1091,6 @@ describe PostAlerter do
context "replies" do
it "triggers :before_create_notifications_for_users" do
user = Fabricate(:user)
topic = Fabricate(:topic)
_post = Fabricate(:post, user: user, topic: topic)
reply = Fabricate(:post, topic: topic, reply_to_post_number: 1)
events = DiscourseEvent.track_events do
@@ -1103,8 +1100,6 @@ describe PostAlerter do
end
it "notifies about regular reply" do
user = Fabricate(:user)
topic = Fabricate(:topic)
_post = Fabricate(:post, user: user, topic: topic)
reply = Fabricate(:post, topic: topic, reply_to_post_number: 1)
@@ -1114,10 +1109,6 @@ describe PostAlerter do
end
it "doesn't notify regular user about whispered reply" do
user = Fabricate(:user)
admin = Fabricate(:admin)
topic = Fabricate(:topic)
_post = Fabricate(:post, user: user, topic: topic)
whispered_reply = Fabricate(:post, user: admin, topic: topic, post_type: Post.types[:whisper], reply_to_post_number: 1)
@@ -1127,11 +1118,9 @@ describe PostAlerter do
end
it "notifies staff user about whispered reply" do
user = Fabricate(:user)
admin1 = Fabricate(:admin)
admin2 = Fabricate(:admin)
topic = Fabricate(:topic)
_post = Fabricate(:post, user: user, topic: topic)
whispered_reply1 = Fabricate(:post, user: admin1, topic: topic, post_type: Post.types[:whisper], reply_to_post_number: 1)
@@ -1231,8 +1220,6 @@ describe PostAlerter do
context "category" do
context "watching" do
it "triggers :before_create_notifications_for_users" do
user = Fabricate(:user)
category = Fabricate(:category)
topic = Fabricate(:topic, category: category)
post = Fabricate(:post, topic: topic)
level = CategoryUser.notification_levels[:watching]
@@ -1244,10 +1231,7 @@ describe PostAlerter do
end
it "notifies staff about whispered post" do
category = Fabricate(:category)
topic = Fabricate(:topic, category: category)
admin = Fabricate(:admin)
user = Fabricate(:user)
level = CategoryUser.notification_levels[:watching]
CategoryUser.set_notification_level_for_category(admin, level, category.id)
CategoryUser.set_notification_level_for_category(user, level, category.id)
@@ -1263,7 +1247,6 @@ describe PostAlerter do
it "notifies a staged user about a private post, but only if the user has access" do
staged_member = Fabricate(:staged)
staged_non_member = Fabricate(:staged)
group = Fabricate(:group)
group_member = Fabricate(:user)
group.add(group_member)
@@ -1289,7 +1272,6 @@ describe PostAlerter do
end
it "does not update existing unread notification" do
category = Fabricate(:category)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:watching], category.id)
topic = Fabricate(:topic, category: category)
@@ -1313,7 +1295,6 @@ describe PostAlerter do
context "tags" do
context "watching" do
it "triggers :before_create_notifications_for_users" do
user = Fabricate(:user)
tag = Fabricate(:tag)
topic = Fabricate(:topic, tags: [tag])
post = Fabricate(:post, topic: topic)
@@ -1350,7 +1331,6 @@ describe PostAlerter do
topic = Fabricate(:topic, tags: [tag])
post = Fabricate(:post, topic: topic)
tag_group = Fabricate(:tag_group, tags: [tag])
group = Fabricate(:group)
Fabricate(:tag_group_permission, tag_group: tag_group, group: group)
TagUser.change(user.id, tag.id, TagUser.notification_levels[:watching])
@@ -1363,7 +1343,6 @@ describe PostAlerter do
topic = Fabricate(:topic, tags: [tag])
post = Fabricate(:post, topic: topic)
tag_group = Fabricate(:tag_group, tags: [tag])
group = Fabricate(:group)
Fabricate(:group_user, group: group, user: user)
Fabricate(:tag_group_permission, tag_group: tag_group, group: group)
@@ -1377,7 +1356,6 @@ describe PostAlerter do
fab!(:user) { Fabricate(:user) }
fab!(:other_tag) { Fabricate(:tag) }
fab!(:watched_tag) { Fabricate(:tag) }
fab!(:post) { Fabricate(:post) }
before do
SiteSetting.tagging_enabled = true
@@ -1413,7 +1391,6 @@ describe PostAlerter do
fab!(:other_tag3) { Fabricate(:tag) }
fab!(:user) { Fabricate(:user) }
fab!(:staged) { Fabricate(:staged) }
fab!(:admin) { Fabricate(:admin) }
before do
SiteSetting.tagging_enabled = true
@@ -1440,7 +1417,6 @@ describe PostAlerter do
context "with tag groups" do
fab!(:tag) { Fabricate(:tag) }
fab!(:group) { Fabricate(:group) }
fab!(:user) { Fabricate(:user) }
fab!(:topic) { Fabricate(:topic, tags: [tag]) }
fab!(:post) { Fabricate(:post, topic: topic) }
@@ -1498,7 +1474,6 @@ describe PostAlerter do
end
describe '#extract_linked_users' do
fab!(:topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post, topic: topic) }
fab!(:post2) { Fabricate(:post) }
@@ -1523,7 +1498,6 @@ describe PostAlerter do
end
describe '#notify_post_users' do
fab!(:topic) { Fabricate(:topic) }
fab!(:post) { Fabricate(:post, topic: topic) }
fab!(:last_editor) { Fabricate(:user) }
fab!(:tag) { Fabricate(:tag) }
@@ -1611,26 +1585,17 @@ describe PostAlerter do
end
it "does not error if SMTP is enabled and the topic has no incoming email or allowed groups" do
topic = Fabricate(:private_message_topic)
Fabricate(:post, topic: topic)
post = Fabricate(:post, topic: topic)
expect { PostAlerter.new.after_save_post(post, true) }.not_to raise_error
end
it "does not error if SMTP is enabled and the topic has no incoming email but does have an allowed group" do
topic = Fabricate(:private_message_topic)
Fabricate(:post, topic: topic)
post = Fabricate(:post, topic: topic)
TopicAllowedGroup.create(topic: topic, group: Fabricate(:group))
TopicAllowedGroup.create(topic: private_message_topic, group: group)
expect { PostAlerter.new.after_save_post(post, true) }.not_to raise_error
end
it "does not error if SMTP is enabled and the topic has no incoming email but has multiple allowed groups" do
topic = Fabricate(:private_message_topic)
Fabricate(:post, topic: topic)
post = Fabricate(:post, topic: topic)
TopicAllowedGroup.create(topic: topic, group: Fabricate(:group))
TopicAllowedGroup.create(topic: topic, group: Fabricate(:group))
TopicAllowedGroup.create(topic: private_message_topic, group: group)
TopicAllowedGroup.create(topic: private_message_topic, group: Fabricate(:group))
expect { PostAlerter.new.after_save_post(post, true) }.not_to raise_error
end