2017-08-16 07:38:34 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Jobs::ReindexSearch do
|
|
|
|
before { SearchIndexer.enable }
|
|
|
|
after { SearchIndexer.disable }
|
|
|
|
|
|
|
|
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({})
|
|
|
|
expect(model.send("#{m}_search_data").locale).to eq locale
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should rebuild `#{m}` when INDEX_VERSION changed" do
|
|
|
|
model = Fabricate(m.to_sym)
|
|
|
|
# so that search data can be reindexed
|
|
|
|
search_data = model.send("#{m}_search_data")
|
|
|
|
search_data.update_attributes!(version: 0)
|
|
|
|
model.reload
|
|
|
|
|
|
|
|
subject.execute({})
|
2019-04-01 21:52:59 -04:00
|
|
|
expect(model.send("#{m}_search_data").version).to eq SearchIndexer::INDEX_VERSION
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-31 22:06:27 -04:00
|
|
|
|
2019-04-01 19:12:39 -04:00
|
|
|
describe 'rebuild_problem_posts' do
|
|
|
|
class FakeIndexer
|
|
|
|
def self.index(post, force:)
|
|
|
|
@posts ||= []
|
|
|
|
@posts.push(post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.posts
|
|
|
|
@posts
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@posts.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FakeIndexer.reset
|
|
|
|
end
|
|
|
|
|
2019-04-08 04:38:43 -04:00
|
|
|
it (
|
|
|
|
'should not reindex posts that belong to a deleted topic ' \
|
|
|
|
'or have been trashed'
|
|
|
|
) do
|
2019-04-01 19:36:53 -04:00
|
|
|
post = Fabricate(:post)
|
|
|
|
post2 = Fabricate(:post)
|
2019-04-08 04:38:43 -04:00
|
|
|
post3 = Fabricate(:post)
|
|
|
|
PostSearchData.delete_all
|
2019-04-01 19:36:53 -04:00
|
|
|
post2.topic.trash!
|
2019-04-08 04:38:43 -04:00
|
|
|
post3.trash!
|
2019-04-01 19:36:53 -04:00
|
|
|
|
|
|
|
subject.rebuild_problem_posts(indexer: FakeIndexer)
|
|
|
|
|
|
|
|
expect(FakeIndexer.posts).to contain_exactly(post)
|
|
|
|
end
|
|
|
|
|
2019-04-01 19:12:39 -04: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)
|
|
|
|
|
|
|
|
subject.rebuild_problem_posts(indexer: FakeIndexer)
|
|
|
|
|
|
|
|
expect(FakeIndexer.posts).to contain_exactly(post)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
2019-04-08 04:51:39 -04: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) }
|
|
|
|
|
|
|
|
freeze_time(described_class::CLEANUP_GRACE_PERIOD) do
|
|
|
|
topic.trash!
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-04-02 22:10:41 -04:00
|
|
|
it(
|
|
|
|
"should clean up post_search_data of posts with empty raw or posts from " \
|
|
|
|
"trashed topics"
|
|
|
|
) do
|
|
|
|
|
2019-04-01 19:12:39 -04:00
|
|
|
post = Fabricate(:post)
|
|
|
|
post2 = Fabricate(:post, post_type: Post.types[:small_action])
|
|
|
|
post2.raw = ""
|
|
|
|
post2.save!(validate: false)
|
2019-04-02 22:10:41 -04:00
|
|
|
post3 = Fabricate(:post)
|
|
|
|
post3.topic.trash!
|
|
|
|
post4 = nil
|
|
|
|
|
2019-04-08 04:51:39 -04:00
|
|
|
freeze_time(described_class::CLEANUP_GRACE_PERIOD) do
|
2019-04-02 22:10:41 -04:00
|
|
|
post4 = Fabricate(:post)
|
|
|
|
post4.topic.trash!
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { subject.execute({}) }.to change { PostSearchData.count }.by(-2)
|
2019-04-01 19:12:39 -04:00
|
|
|
|
2019-04-08 04:51:39 -04:00
|
|
|
expect(Post.pluck(:id)).to contain_exactly(
|
2019-04-02 22:10:41 -04:00
|
|
|
post.id, post2.id, post3.id, post4.id
|
|
|
|
)
|
|
|
|
|
2019-04-08 04:51:39 -04:00
|
|
|
expect(PostSearchData.pluck(:post_id)).to contain_exactly(
|
2019-04-02 22:10:41 -04:00
|
|
|
post.post_search_data.post_id, post3.post_search_data.post_id
|
|
|
|
)
|
2019-04-01 19:12:39 -04:00
|
|
|
end
|
2019-03-31 22:06:27 -04:00
|
|
|
end
|
2017-08-16 07:38:34 -04:00
|
|
|
end
|