2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Jobs::ReindexSearch do
|
2020-11-09 14:06:07 +08:00
|
|
|
before do
|
|
|
|
SearchIndexer.enable
|
|
|
|
Jobs.run_immediately!
|
|
|
|
end
|
2017-08-16 19:38:34 +08:00
|
|
|
|
|
|
|
let(:locale) { "fr" }
|
|
|
|
# This works since test db has a small record less than limit.
|
|
|
|
# Didn't check `topic` because topic doesn't have posts in fabrication
|
|
|
|
# thus no search data
|
|
|
|
%w[post category user].each do |m|
|
|
|
|
it "should rebuild `#{m}` when default_locale changed" do
|
|
|
|
SiteSetting.default_locale = "en"
|
|
|
|
model = Fabricate(m.to_sym)
|
|
|
|
SiteSetting.default_locale = locale
|
|
|
|
subject.execute({})
|
2019-05-07 09:27:05 +08:00
|
|
|
expect(model.public_send("#{m}_search_data").locale).to eq locale
|
2017-08-16 19:38:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should rebuild `#{m}` when INDEX_VERSION changed" do
|
|
|
|
model = Fabricate(m.to_sym)
|
|
|
|
# so that search data can be reindexed
|
2019-05-07 09:27:05 +08:00
|
|
|
search_data = model.public_send("#{m}_search_data")
|
2019-04-29 15:32:25 +08:00
|
|
|
search_data.update!(version: 0)
|
2017-08-16 19:38:34 +08:00
|
|
|
model.reload
|
|
|
|
|
|
|
|
subject.execute({})
|
2019-05-07 09:27:05 +08:00
|
|
|
expect(model.public_send("#{m}_search_data").version).to eq(
|
2020-07-23 14:10:05 +08:00
|
|
|
"SearchIndexer::#{m.upcase}_INDEX_VERSION".constantize,
|
|
|
|
)
|
2017-08-16 19:38:34 +08:00
|
|
|
end
|
|
|
|
end
|
2019-04-01 10:06:27 +08:00
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
describe "rebuild_posts" do
|
2019-04-02 07:12:39 +08:00
|
|
|
class FakeIndexer
|
|
|
|
def self.index(post, force:)
|
2020-07-24 09:29:54 +08:00
|
|
|
get_posts.push(post)
|
2019-04-02 07:12:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.posts
|
2020-07-24 09:29:54 +08:00
|
|
|
get_posts
|
2019-04-02 07:12:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
2020-07-24 09:29:54 +08:00
|
|
|
get_posts.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.get_posts
|
|
|
|
@posts ||= []
|
2019-04-02 07:12:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after { FakeIndexer.reset }
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
it "should not reindex posts that belong to a deleted topic or have been trashed" do
|
2019-04-02 07:36:53 +08:00
|
|
|
post = Fabricate(:post)
|
|
|
|
post2 = Fabricate(:post)
|
2019-04-08 16:38:43 +08:00
|
|
|
post3 = Fabricate(:post)
|
|
|
|
PostSearchData.delete_all
|
2019-04-02 07:36:53 +08:00
|
|
|
post2.topic.trash!
|
2019-04-08 16:38:43 +08:00
|
|
|
post3.trash!
|
2019-04-02 07:36:53 +08:00
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
subject.rebuild_posts(indexer: FakeIndexer)
|
2019-04-02 07:36:53 +08:00
|
|
|
|
|
|
|
expect(FakeIndexer.posts).to contain_exactly(post)
|
|
|
|
end
|
|
|
|
|
2020-07-23 14:52:20 +08:00
|
|
|
it "should not reindex posts with a developmental version" do
|
|
|
|
post = Fabricate(:post, version: SearchIndexer::MIN_POST_REINDEX_VERSION + 1)
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
subject.rebuild_posts(indexer: FakeIndexer)
|
2020-07-23 14:52:20 +08:00
|
|
|
|
|
|
|
expect(FakeIndexer.posts).to eq([])
|
|
|
|
end
|
|
|
|
|
2019-04-02 07:12:39 +08:00
|
|
|
it "should not reindex posts with empty raw" do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
post.post_search_data.destroy!
|
|
|
|
|
|
|
|
post2 = Fabricate.build(:post, raw: "", post_type: Post.types[:small_action])
|
|
|
|
|
|
|
|
post2.save!(validate: false)
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
subject.rebuild_posts(indexer: FakeIndexer)
|
2019-04-02 07:12:39 +08:00
|
|
|
|
|
|
|
expect(FakeIndexer.posts).to contain_exactly(post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#execute" do
|
2019-04-08 16:51:39 +08:00
|
|
|
it "should clean up topic_search_data of trashed topics" do
|
|
|
|
topic = Fabricate(:post).topic
|
|
|
|
topic2 = Fabricate(:post).topic
|
|
|
|
|
|
|
|
[topic, topic2].each { |t| SearchIndexer.index(t, force: true) }
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
freeze_time(1.day.ago) { topic.trash! }
|
2019-04-08 16:51:39 +08:00
|
|
|
|
|
|
|
expect { subject.execute({}) }.to change { TopicSearchData.count }.by(-1)
|
|
|
|
expect(Topic.pluck(:id)).to contain_exactly(topic2.id)
|
|
|
|
|
|
|
|
expect(TopicSearchData.pluck(:topic_id)).to contain_exactly(topic2.topic_search_data.topic_id)
|
|
|
|
end
|
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
it "should clean up post_search_data of posts with empty raw or posts from trashed topics" do
|
2019-04-02 07:12:39 +08:00
|
|
|
post = Fabricate(:post)
|
|
|
|
post2 = Fabricate(:post, post_type: Post.types[:small_action])
|
|
|
|
post2.raw = ""
|
|
|
|
post2.save!(validate: false)
|
2019-04-03 10:10:41 +08:00
|
|
|
post3 = Fabricate(:post)
|
|
|
|
post3.topic.trash!
|
2019-06-04 15:19:44 +08:00
|
|
|
post4, post5, post6 = nil
|
2019-04-03 10:10:41 +08:00
|
|
|
|
2021-01-25 18:23:36 +08:00
|
|
|
freeze_time(1.day.ago) do
|
2019-04-03 10:10:41 +08:00
|
|
|
post4 = Fabricate(:post)
|
|
|
|
post4.topic.trash!
|
2019-06-04 15:19:44 +08:00
|
|
|
|
|
|
|
post5 = Fabricate(:post)
|
|
|
|
post6 = Fabricate(:post, topic_id: post5.topic_id)
|
|
|
|
post6.trash!
|
2019-04-03 10:10:41 +08:00
|
|
|
end
|
|
|
|
|
2019-06-04 15:19:44 +08:00
|
|
|
expect { subject.execute({}) }.to change { PostSearchData.count }.by(-3)
|
2019-04-02 07:12:39 +08:00
|
|
|
|
2019-06-04 15:19:44 +08:00
|
|
|
expect(Post.pluck(:id)).to contain_exactly(post.id, post2.id, post3.id, post4.id, post5.id)
|
2019-04-03 10:10:41 +08:00
|
|
|
|
2019-04-08 16:51:39 +08:00
|
|
|
expect(PostSearchData.pluck(:post_id)).to contain_exactly(post.id, post3.id, post5.id)
|
2019-04-02 07:12:39 +08:00
|
|
|
end
|
2019-04-01 10:06:27 +08:00
|
|
|
end
|
2017-08-16 19:38:34 +08:00
|
|
|
end
|