diff --git a/spec/search/models/concerns/account/search_spec.rb b/spec/search/models/concerns/account/search_spec.rb index de12161ef9f..7b1fc695dba 100644 --- a/spec/search/models/concerns/account/search_spec.rb +++ b/spec/search/models/concerns/account/search_spec.rb @@ -3,48 +3,48 @@ require 'rails_helper' RSpec.describe Account::Search do - describe 'a non-discoverable account becoming discoverable' do - let(:account) { Account.find_by(username: 'search_test_account_1') } + describe 'Callbacks for discoverable changes' do + let(:results) { AccountsIndex.filter(term: { username: account.username }) } - context 'when picking a non-discoverable account' do - it 'its bio is not in the AccountsIndex' do - results = AccountsIndex.filter(term: { username: account.username }) - expect(results.count).to eq(1) - expect(results.first.text).to be_nil + context 'with a non-discoverable account' do + let(:account) { Fabricate :account, discoverable: false, note: 'Account note' } + + context 'when looking for the non discoverable account' do + it 'is missing account bio in the AccountsIndex' do + expect(results.count) + .to eq(1) + expect(results.first.text) + .to be_nil + end + end + + context 'when the account becomes discoverable' do + it 'has an account bio in the AccountsIndex' do + expect { account.update! discoverable: true } + .to change { results.first.text }.from(be_blank).to(account.note) + .and not_change(results, :count).from(1) + end end end - context 'when the non-discoverable account becomes discoverable' do - it 'its bio is added to the AccountsIndex' do - account.discoverable = true - account.save! + describe 'with a discoverable account' do + let(:account) { Fabricate :account, discoverable: true } - results = AccountsIndex.filter(term: { username: account.username }) - expect(results.count).to eq(1) - expect(results.first.text).to eq(account.note) + context 'when looking for the account' do + it 'is present in the AccountsIndex' do + expect(results.count) + .to eq(1) + expect(results.first.text) + .to eq(account.note) + end end - end - end - describe 'a discoverable account becoming non-discoverable' do - let(:account) { Account.find_by(username: 'search_test_account_0') } - - context 'when picking an discoverable account' do - it 'has its bio in the AccountsIndex' do - results = AccountsIndex.filter(term: { username: account.username }) - expect(results.count).to eq(1) - expect(results.first.text).to eq(account.note) - end - end - - context 'when the discoverable account becomes non-discoverable' do - it 'its bio is removed from the AccountsIndex' do - account.discoverable = false - account.save! - - results = AccountsIndex.filter(term: { username: account.username }) - expect(results.count).to eq(1) - expect(results.first.text).to be_nil + context 'when the account becomes non-discoverable' do + it 'is missing from the AccountsIndex' do + expect { account.update! discoverable: false } + .to change { results.first.text }.from(account.note).to(be_blank) + .and not_change(results, :count).from(1) + end end end end diff --git a/spec/search/models/concerns/account/statuses_search_spec.rb b/spec/search/models/concerns/account/statuses_search_spec.rb index bce1aecd750..716d1b28ffe 100644 --- a/spec/search/models/concerns/account/statuses_search_spec.rb +++ b/spec/search/models/concerns/account/statuses_search_spec.rb @@ -3,50 +3,55 @@ require 'rails_helper' RSpec.describe Account::StatusesSearch, :inline_jobs do - describe 'a non-indexable account becoming indexable' do - let(:account) { Account.find_by(username: 'search_test_account_1') } + describe 'Callbacks for indexable changes' do + let(:account) { Fabricate :account, indexable: } + let(:public_statuses_results) { PublicStatusesIndex.filter(term: { account_id: account.id }) } + let(:statuses_results) { StatusesIndex.filter(term: { account_id: account.id }) } - context 'when picking a non-indexable account' do - it 'has no statuses in the PublicStatusesIndex' do - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(0) + before do + Fabricate :status, account:, visibility: :private + Fabricate :status, account:, visibility: :public + end + + context 'with a non-indexable account' do + let(:indexable) { false } + + context 'when looking for statuses from the account' do + it 'does not have public index statuses' do + expect(public_statuses_results.count) + .to eq(0) + expect(statuses_results.count) + .to eq(account.statuses.count) + end end - it 'has statuses in the StatusesIndex' do - expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) + context 'when the non-indexable account becomes indexable' do + it 'does have public index statuses' do + expect { account.update! indexable: true } + .to change(public_statuses_results, :count).to(account.statuses.public_visibility.count) + .and not_change(statuses_results, :count).from(account.statuses.count) + end end end - context 'when the non-indexable account becomes indexable' do - it 'adds the public statuses to the PublicStatusesIndex' do - account.indexable = true - account.save! + describe 'with an indexable account' do + let(:indexable) { true } - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.public_visibility.count) - expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) - end - end - end - - describe 'an indexable account becoming non-indexable' do - let(:account) { Account.find_by(username: 'search_test_account_0') } - - context 'when picking an indexable account' do - it 'has statuses in the PublicStatusesIndex' do - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.public_visibility.count) + context 'when picking an indexable account' do + it 'does have public index statuses' do + expect(public_statuses_results.count) + .to eq(account.statuses.public_visibility.count) + expect(statuses_results.count) + .to eq(account.statuses.count) + end end - it 'has statuses in the StatusesIndex' do - expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) - end - end - - context 'when the indexable account becomes non-indexable' do - it 'removes the statuses from the PublicStatusesIndex' do - account.indexable = false - account.save! - - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(0) - expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) + context 'when the indexable account becomes non-indexable' do + it 'does not have public index statuses' do + expect { account.update! indexable: false } + .to change(public_statuses_results, :count).to(0) + .and not_change(statuses_results, :count).from(account.statuses.count) + end end end end diff --git a/spec/support/search.rb b/spec/support/search.rb new file mode 100644 index 00000000000..adf99caf9e5 --- /dev/null +++ b/spec/support/search.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + config.before :suite do + if search_examples_present? + Chewy.settings[:enabled] = true + # Configure chewy to use `urgent` strategy to index documents immediately + Chewy.strategy(:urgent) + else + Chewy.settings[:enabled] = false + end + end + + config.after :each, :search do + search_indices.each(&:delete) + end + + private + + def search_indices + [ + AccountsIndex, + PublicStatusesIndex, + StatusesIndex, + TagsIndex, + ] + end + + def search_examples_present? + RSpec.world.filtered_examples.values.flatten.any? { |example| example.metadata[:search] == true } + end +end diff --git a/spec/support/search_data_manager.rb b/spec/support/search_data_manager.rb deleted file mode 100644 index d521bceeed5..00000000000 --- a/spec/support/search_data_manager.rb +++ /dev/null @@ -1,81 +0,0 @@ -# frozen_string_literal: true - -class SearchDataManager - def prepare_test_data - 4.times do |i| - username = "search_test_account_#{i}" - account = Fabricate.create(:account, username: username, indexable: i.even?, discoverable: i.even?, note: "Lover of #{i}.") - 2.times do |j| - Fabricate.create(:status, account: account, text: "#{username}'s #{j} post", visibility: j.even? ? :public : :private) - end - end - - 3.times do |i| - Fabricate.create(:tag, name: "search_test_tag_#{i}") - end - end - - def indexes - [ - AccountsIndex, - PublicStatusesIndex, - StatusesIndex, - TagsIndex, - ] - end - - def populate_indexes - indexes.each do |index_class| - index_class.purge! - index_class.import! - end - end - - def remove_indexes - indexes.each(&:delete!) - end - - def cleanup_test_data - Status.destroy_all - Account.destroy_all - Tag.destroy_all - end -end - -RSpec.configure do |config| - config.before :suite do - if search_examples_present? - Chewy.settings[:enabled] = true - # Configure chewy to use `urgent` strategy to index documents - Chewy.strategy(:urgent) - - # Create search data - search_data_manager.prepare_test_data - else - Chewy.settings[:enabled] = false - end - end - - config.after :suite do - if search_examples_present? - # Clean up after search data - search_data_manager.cleanup_test_data - end - end - - config.around :each, :search do |example| - search_data_manager.populate_indexes - example.run - search_data_manager.remove_indexes - end - - private - - def search_data_manager - @search_data_manager ||= SearchDataManager.new - end - - def search_examples_present? - RSpec.world.filtered_examples.values.flatten.any? { |example| example.metadata[:search] == true } - end -end