Skip to content

Commit

Permalink
Try to make better end to end search tests and make them work
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgoldstein committed Sep 5, 2023
1 parent 5633938 commit e64c555
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 55 deletions.
3 changes: 2 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
57 changes: 35 additions & 22 deletions spec/search/models/concerns/account_statuses_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 29 additions & 29 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 160 in spec/spec_helper.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body end. (https://rubystyle.guide#empty-lines-around-bodies)
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

0 comments on commit e64c555

Please sign in to comment.