discourse/spec/models/bookmark_spec.rb
Alan Guo Xiang Tan 1e05175364
PERF: Avoid running query unnecessarily when updating bookmark. (#14276)
* Avoid loading an entire ActiveRecord object when saving and updating.
* Avoid running a DB query when `post_id` or `user_id` is not changed.
2021-09-09 10:50:26 +10:00

96 lines
3.8 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe Bookmark do
fab!(:post) { Fabricate(:post) }
context 'validations' do
it 'does not allow user to bookmark a post twice' do
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
user = bookmark.user
bookmark_2 = Fabricate.build(:bookmark,
post: post,
topic: post.topic,
user: user
)
expect(bookmark_2.valid?).to eq(false)
end
end
describe "#cleanup!" do
it "deletes bookmarks attached to a deleted post which has been deleted for > 3 days" do
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
bookmark2 = Fabricate(:bookmark, post: Fabricate(:post, topic: post.topic))
post.trash!
post.update(deleted_at: 4.days.ago)
Bookmark.cleanup!
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
end
it "runs a SyncTopicUserBookmarked job for all deleted bookmark unique topics to make sure topic_user.bookmarked is in sync" do
post2 = Fabricate(:post)
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
bookmark2 = Fabricate(:bookmark, post: Fabricate(:post, topic: post.topic))
bookmark3 = Fabricate(:bookmark, post: post2, topic: post2.topic)
bookmark4 = Fabricate(:bookmark, post: post2, topic: post2.topic)
post.trash!
post.update(deleted_at: 4.days.ago)
post2.trash!
post2.update(deleted_at: 4.days.ago)
Bookmark.cleanup!
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
expect(Bookmark.find_by(id: bookmark3.id)).to eq(nil)
expect(Bookmark.find_by(id: bookmark4.id)).to eq(nil)
expect_job_enqueued(job: :sync_topic_user_bookmarked, args: { topic_id: post.topic_id })
expect_job_enqueued(job: :sync_topic_user_bookmarked, args: { topic_id: post2.topic_id })
end
it "deletes bookmarks attached to a deleted topic which has been deleted for > 3 days" do
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
bookmark2 = Fabricate(:bookmark, topic: post.topic, post: Fabricate(:post, topic: post.topic))
bookmark3 = Fabricate(:bookmark)
post.topic.trash!
post.topic.update(deleted_at: 4.days.ago)
Bookmark.cleanup!
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
expect(Bookmark.find_by(id: bookmark2.id)).to eq(nil)
expect(Bookmark.find_by(id: bookmark3.id)).to eq(bookmark3)
end
it "does not delete bookmarks attached to posts that are not deleted or that have not met the 3 day grace period" do
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
bookmark2 = Fabricate(:bookmark)
Bookmark.cleanup!
expect(Bookmark.find(bookmark.id)).to eq(bookmark)
post.trash!
Bookmark.cleanup!
expect(Bookmark.find(bookmark.id)).to eq(bookmark)
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
end
describe "bookmark limits" do
fab!(:user) { Fabricate(:user) }
it "does not get the bookmark limit error because it is not creating a new bookmark (for users already over the limit)" do
Fabricate(:bookmark, user: user)
Fabricate(:bookmark, user: user)
last_bookmark = Fabricate(:bookmark, user: user)
SiteSetting.max_bookmarks_per_user = 2
expect { last_bookmark.clear_reminder! }.not_to raise_error
end
it "gets the bookmark limit error when creating a new bookmark over the limit" do
Fabricate(:bookmark, user: user)
Fabricate(:bookmark, user: user)
SiteSetting.max_bookmarks_per_user = 2
expect { Fabricate(:bookmark, user: user) }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
end