Clean up search tagged specs (#38085)

This commit is contained in:
Matt Jankowski
2026-03-10 05:46:03 -04:00
committed by GitHub
parent 71f9763e68
commit cec60d5b71
4 changed files with 107 additions and 151 deletions

View File

@@ -3,48 +3,48 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Account::Search do RSpec.describe Account::Search do
describe 'a non-discoverable account becoming discoverable' do describe 'Callbacks for discoverable changes' do
let(:account) { Account.find_by(username: 'search_test_account_1') } let(:results) { AccountsIndex.filter(term: { username: account.username }) }
context 'when picking a non-discoverable account' do context 'with a non-discoverable account' do
it 'its bio is not in the AccountsIndex' do let(:account) { Fabricate :account, discoverable: false, note: 'Account note' }
results = AccountsIndex.filter(term: { username: account.username })
expect(results.count).to eq(1) context 'when looking for the non discoverable account' do
expect(results.first.text).to be_nil 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
end end
context 'when the non-discoverable account becomes discoverable' do describe 'with a discoverable account' do
it 'its bio is added to the AccountsIndex' do let(:account) { Fabricate :account, discoverable: true }
account.discoverable = true
account.save!
results = AccountsIndex.filter(term: { username: account.username }) context 'when looking for the account' do
expect(results.count).to eq(1) it 'is present in the AccountsIndex' do
expect(results.first.text).to eq(account.note) expect(results.count)
.to eq(1)
expect(results.first.text)
.to eq(account.note)
end
end end
end
end
describe 'a discoverable account becoming non-discoverable' do context 'when the account becomes non-discoverable' do
let(:account) { Account.find_by(username: 'search_test_account_0') } it 'is missing from the AccountsIndex' do
expect { account.update! discoverable: false }
context 'when picking an discoverable account' do .to change { results.first.text }.from(account.note).to(be_blank)
it 'has its bio in the AccountsIndex' do .and not_change(results, :count).from(1)
results = AccountsIndex.filter(term: { username: account.username }) end
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
end end
end end
end end

View File

@@ -3,50 +3,55 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Account::StatusesSearch, :inline_jobs do RSpec.describe Account::StatusesSearch, :inline_jobs do
describe 'a non-indexable account becoming indexable' do describe 'Callbacks for indexable changes' do
let(:account) { Account.find_by(username: 'search_test_account_1') } 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 before do
it 'has no statuses in the PublicStatusesIndex' do Fabricate :status, account:, visibility: :private
expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(0) 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 end
it 'has statuses in the StatusesIndex' do context 'when the non-indexable account becomes indexable' do
expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) 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
end end
context 'when the non-indexable account becomes indexable' do describe 'with an indexable account' do
it 'adds the public statuses to the PublicStatusesIndex' do let(:indexable) { true }
account.indexable = true
account.save!
expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.public_visibility.count) context 'when picking an indexable account' do
expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) it 'does have public index statuses' do
end expect(public_statuses_results.count)
end .to eq(account.statuses.public_visibility.count)
end expect(statuses_results.count)
.to eq(account.statuses.count)
describe 'an indexable account becoming non-indexable' do end
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)
end end
it 'has statuses in the StatusesIndex' do context 'when the indexable account becomes non-indexable' do
expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) it 'does not have public index statuses' do
end expect { account.update! indexable: false }
end .to change(public_statuses_results, :count).to(0)
.and not_change(statuses_results, :count).from(account.statuses.count)
context 'when the indexable account becomes non-indexable' do end
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)
end end
end end
end end

32
spec/support/search.rb Normal file
View File

@@ -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

View File

@@ -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