mirror of
https://github.com/mastodon/mastodon.git
synced 2026-03-21 18:05:23 -05:00
Clean up search tagged specs (#38085)
This commit is contained in:
parent
71f9763e68
commit
cec60d5b71
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
32
spec/support/search.rb
Normal file
32
spec/support/search.rb
Normal 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
|
||||
|
|
@ -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
|
||||
Loading…
Reference in New Issue
Block a user