Add new public status index (#26344)

Co-authored-by: Eugen Rochko <eugen@zeonfederated.com>
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
jsgoldstein 2023-08-24 10:40:04 -04:00 committed by GitHub
parent 96bcee66fb
commit 30c191aaa0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 584 additions and 87 deletions

View file

@ -0,0 +1,31 @@
# frozen_string_literal: true
require 'rails_helper'
describe PublicStatusesIndex do
describe 'Searching the index' do
before do
mock_elasticsearch_response(described_class, raw_response)
end
it 'returns results from a query' do
results = described_class.query(match: { name: 'status' })
expect(results).to eq []
end
end
def raw_response
{
took: 3,
hits: {
hits: [
{
_id: '0',
_score: 1.6375021,
},
],
},
}
end
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
require 'rails_helper'
describe Importer::PublicStatusesIndexImporter do
describe 'import!' do
let(:pool) { Concurrent::FixedThreadPool.new(5) }
let(:importer) { described_class.new(batch_size: 123, executor: pool) }
before { Fabricate(:status, account: Fabricate(:account, indexable: true)) }
it 'indexes relevant statuses' do
expect { importer.import! }.to update_index(PublicStatusesIndex)
end
end
end

View file

@ -9,8 +9,8 @@ describe SearchQueryTransformer do
it 'sets attributes' do
transformer = described_class.new.apply(parser)
expect(transformer.should_clauses.first).to be_a(SearchQueryTransformer::TermClause)
expect(transformer.must_clauses.first).to be_nil
expect(transformer.should_clauses.first).to be_nil
expect(transformer.must_clauses.first).to be_a(SearchQueryTransformer::TermClause)
expect(transformer.must_not_clauses.first).to be_nil
expect(transformer.filter_clauses.first).to be_nil
end

View file

@ -0,0 +1,66 @@
# frozen_string_literal: true
require 'rails_helper'
describe AccountStatusesSearch do
let(:account) { Fabricate(:account, indexable: indexable) }
before do
allow(Chewy).to receive(:enabled?).and_return(true)
end
describe '#enqueue_update_public_statuses_index' do
before do
allow(account).to receive(:enqueue_add_to_public_statuses_index)
allow(account).to receive(:enqueue_remove_from_public_statuses_index)
end
context 'when account is indexable' do
let(:indexable) { true }
it 'enqueues add_to_public_statuses_index and not to remove_from_public_statuses_index' do
account.enqueue_update_public_statuses_index
expect(account).to have_received(:enqueue_add_to_public_statuses_index)
expect(account).to_not have_received(:enqueue_remove_from_public_statuses_index)
end
end
context 'when account is not indexable' do
let(:indexable) { false }
it 'enqueues remove_from_public_statuses_index and not to add_to_public_statuses_index' do
account.enqueue_update_public_statuses_index
expect(account).to have_received(:enqueue_remove_from_public_statuses_index)
expect(account).to_not have_received(:enqueue_add_to_public_statuses_index)
end
end
end
describe '#enqueue_add_to_public_statuses_index' do
let(:indexable) { true }
let(:worker) { AddToPublicStatusesIndexWorker }
before do
allow(worker).to receive(:perform_async)
end
it 'enqueues AddToPublicStatusesIndexWorker' do
account.enqueue_add_to_public_statuses_index
expect(worker).to have_received(:perform_async).with(account.id)
end
end
describe '#enqueue_remove_from_public_statuses_index' do
let(:indexable) { false }
let(:worker) { RemoveFromPublicStatusesIndexWorker }
before do
allow(worker).to receive(:perform_async)
end
it 'enqueues RemoveFromPublicStatusesIndexWorker' do
account.enqueue_remove_from_public_statuses_index
expect(worker).to have_received(:perform_async).with(account.id)
end
end
end

View file

@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'rails_helper'
describe AddToPublicStatusesIndexWorker do
describe '#perform' do
let(:account) { Fabricate(:account, indexable: indexable) }
let(:account_id) { account.id }
before do
allow(Account).to receive(:find).with(account_id).and_return(account) unless account.nil?
allow(account).to receive(:add_to_public_statuses_index!) unless account.nil?
end
context 'when account is indexable' do
let(:indexable) { true }
it 'adds the account to the public statuses index' do
subject.perform(account_id)
expect(account).to have_received(:add_to_public_statuses_index!)
end
end
context 'when account is not indexable' do
let(:indexable) { false }
it 'does not add the account to public statuses index' do
subject.perform(account_id)
expect(account).to_not have_received(:add_to_public_statuses_index!)
end
end
context 'when account does not exist' do
let(:account) { nil }
let(:account_id) { 999 }
it 'does not raise an error' do
expect { subject.perform(account_id) }.to_not raise_error
end
end
end
end

View file

@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'rails_helper'
describe RemoveFromPublicStatusesIndexWorker do
describe '#perform' do
let(:account) { Fabricate(:account, indexable: indexable) }
let(:account_id) { account.id }
before do
allow(Account).to receive(:find).with(account_id).and_return(account) unless account.nil?
allow(account).to receive(:remove_from_public_statuses_index!) unless account.nil?
end
context 'when account is not indexable' do
let(:indexable) { false }
it 'removes the account from public statuses index' do
subject.perform(account_id)
expect(account).to have_received(:remove_from_public_statuses_index!)
end
end
context 'when account is indexable' do
let(:indexable) { true }
it 'does not remove the account from public statuses index' do
subject.perform(account_id)
expect(account).to_not have_received(:remove_from_public_statuses_index!)
end
end
context 'when account does not exist' do
let(:account) { nil }
let(:account_id) { 999 }
it 'does not raise an error' do
expect { subject.perform(account_id) }.to_not raise_error
end
end
end
end