From e64c55569689d3b5c62549bdb8b93595e9bbc9b1 Mon Sep 17 00:00:00 2001 From: jsgoldstein Date: Tue, 5 Sep 2023 11:32:31 -0400 Subject: [PATCH] Try to make better end to end search tests and make them work --- Vagrantfile | 3 +- spec/rails_helper.rb | 9 ++- .../concerns/account_statuses_search_spec.rb | 57 +++++++++++------- spec/spec_helper.rb | 58 +++++++++---------- 4 files changed, 72 insertions(+), 55 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index 1117d62fff2cf1..4303f8e067c23f 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -76,7 +76,8 @@ path.logs: /var/log/elasticsearch network.host: 0.0.0.0 http.port: 9200 discovery.seed_hosts: ["localhost"] -cluster.initial_master_nodes: ["node-1"]' > /etc/elasticsearch/elasticsearch.yml +cluster.initial_master_nodes: ["node-1"] +xpack.security.enabled: false' > /etc/elasticsearch/elasticsearch.yml sudo systemctl restart elasticsearch diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 7aa6aacd2d3760..c24e0c0ed9bbac 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -36,7 +36,7 @@ # System tests config DatabaseCleaner.strategy = [:deletion] streaming_server_manager = StreamingServerManager.new -search_data_populator = SearchDataPopulator.new +search_data_manager = SearchDataManager.new Devise::Test::ControllerHelpers.module_eval do alias_method :original_sign_in, :sign_in @@ -128,13 +128,16 @@ def get(path, headers: nil, sign_with: nil, **args) streaming_server_manager.start(port: STREAMING_PORT) end - search_data_populator.populate_search_indices if RUN_SEARCH_SPECS + if RUN_SEARCH_SPECS + Chewy.strategy(:urgent) + search_data_manager.populate + end end config.after :suite do streaming_server_manager.stop - search_data_populator.delete_search_indices if RUN_SEARCH_SPECS + search_data_manager.destroy if RUN_SEARCH_SPECS end config.around :each, type: :system do |example| diff --git a/spec/search/models/concerns/account_statuses_search_spec.rb b/spec/search/models/concerns/account_statuses_search_spec.rb index 5e62cfe861229a..1139e997ae1605 100644 --- a/spec/search/models/concerns/account_statuses_search_spec.rb +++ b/spec/search/models/concerns/account_statuses_search_spec.rb @@ -3,40 +3,53 @@ require 'rails_helper' describe AccountStatusesSearch do - let(:account) { Fabricate(:account, indexable: indexable) } - # indexable must be true in order for the statuses to end up in the index - let(:indexable) { true } - before do allow(Chewy).to receive(:enabled?).and_return(true) end - describe '#add_to_public_statuses_index!' do - it 'adds the statuses to the PublicStatusesIndex' do - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(0) + describe 'a non-indexable account becoming indexable' do + let(:account) { Account.find_by(username: 'search_test_account_2') } - Fabricate(:status, account: account, text: 'status 1', visibility: :public) - Fabricate(:status, account: account, text: 'status 2', visibility: :public) - account.add_to_public_statuses_index! + 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) + end - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(2) + it 'has statuses in the StatusesIndex' do + expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) + end end - end - - describe '#remove_from_public_statuses_index!' do - let(:status_one) { Fabricate(:status, account: account, text: 'status 1') } - let(:status_two) { Fabricate(:status, account: account, text: 'status 2') } - before do - PublicStatusesIndex.import([status_one, status_two]) + context 'when the non-indexable account becomes indexable' do + it 'adds the public statuses to the PublicStatusesIndex' do + account.indexable = true + account.save! + expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.where(visibility: :public).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_1') } - it 'removes the statuses from the PublicStatusesIndex' do - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(2) + 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.where(visibility: :public).count) + end - account.remove_from_public_statuses_index! + it 'has statuses in the StatusesIndex' do + expect(StatusesIndex.filter(term: { account_id: account.id }).count).to eq(account.statuses.count) + end + end - expect(PublicStatusesIndex.filter(term: { account_id: account.id }).count).to eq(0) + 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) + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ac422d6ef00bce..261669af4fc8a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -130,41 +130,41 @@ def stop end end -class SearchDataPopulator - def populate_search_indices - create_indices - populate_indices +class SearchDataManager + def indexes + [ + AccountsIndex, + PublicStatusesIndex, + StatusesIndex, + TagsIndex, + ] end - def create_indices - AccountsIndex.create - PublicStatusesIndex.create - StatusesIndex.create - TagsIndex.create - end - - def populate_indices - a = Fabricate(:account, indexable: true) - b = Fabricate(:account, indexable: false) - AccountsIndex.import! + def populate + 4.times do |i| + username = "search_test_account_#{i + 1}" + account = Fabricate.create(:account, username: username, indexable: i.even?) + 2.times do |j| + Fabricate.create(:status, account: account, text: "#{username}'s #{j + 1} post", visibility: j.even? ? :public : :private) + end + end - Fabricate(:status, account: a, text: 'Lorem ipsum', visibility: :public) - Fabricate(:status, account: a, text: 'Foo #bar', visibility: :private) + 3.times do |i| + Fabricate.create(:tag, name: "search_test_tag_#{i}") + end - Fabricate(:status, account: b, text: 'Lorem ipsum', visibility: :public) - Fabricate(:status, account: b, text: 'Bar #foo', visibility: :private) - PublicStatusesIndex.import! - StatusesIndex.import! + indexes.each do |index_class| + index_class.purge! + index_class.import! + end - Fabricate(:tag) - Fabricate(:tag) - TagsIndex.import! end - def delete_search_indices - AccountsIndex.delete - PublicStatusesIndex.delete - StatusesIndex.delete - TagsIndex.delete + def destroy + Status.destroy_all + Account.destroy_all + Tag.destroy_all + + indexes.each(&:delete!) end end