Commit ce1e1ef8 authored by Dylan Griffith's avatar Dylan Griffith Committed by Dmitry Gruzd

Elastic_indexer_worker_spec is now passing

But index_record_service_spec still flaky
parent 7e3811f0
...@@ -5,6 +5,11 @@ require 'spec_helper' ...@@ -5,6 +5,11 @@ require 'spec_helper'
describe Elastic::IndexRecordService, :elastic do describe Elastic::IndexRecordService, :elastic do
subject { described_class.new } subject { described_class.new }
# Create admin user and search globally to avoid dealing with permissions in
# these tests
let(:user) { create(:admin) }
let(:search_options) { { options: { current_user: user, project_ids: :any }} }
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
...@@ -27,13 +32,21 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -27,13 +32,21 @@ describe Elastic::IndexRecordService, :elastic do
it 'indexes new records' do it 'indexes new records' do
object = create(type) object = create(type)
Sidekiq::Testing.disable! do
if type != :project
# You cannot find anything in the index if it's parent project is
# not first indexed.
subject.execute(object.project, true)
end
end
# Prevent records from being added via bulk indexing updates # Prevent records from being added via bulk indexing updates
::Elastic::ProcessBookkeepingService.clear_tracking! ::Elastic::ProcessBookkeepingService.clear_tracking!
expect do expect do
expect(subject.execute(object, true)).to eq(true) expect(subject.execute(object, true)).to eq(true)
ensure_elasticsearch_index! ensure_elasticsearch_index!
end.to change { Elasticsearch::Model.search('*').records.size }.by(1) end.to change { object.class.elastic_search('*', search_options).total_count }.by(1)
end end
it 'updates the index when object is changed' do it 'updates the index when object is changed' do
...@@ -41,6 +54,13 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -41,6 +54,13 @@ describe Elastic::IndexRecordService, :elastic do
Sidekiq::Testing.disable! do Sidekiq::Testing.disable! do
object = create(type) object = create(type)
if type != :project
# You cannot find anything in the index if it's parent project is
# not first indexed.
subject.execute(object.project, true)
end
expect(subject.execute(object, true)).to eq(true) expect(subject.execute(object, true)).to eq(true)
object.update(attribute => "new") object.update(attribute => "new")
end end
...@@ -48,7 +68,7 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -48,7 +68,7 @@ describe Elastic::IndexRecordService, :elastic do
expect do expect do
expect(subject.execute(object, false)).to eq(true) expect(subject.execute(object, false)).to eq(true)
ensure_elasticsearch_index! ensure_elasticsearch_index!
end.to change { Elasticsearch::Model.search('new').records.size }.by(1) end.to change { object.class.elastic_search('new', search_options).total_count }.by(1)
end end
it 'ignores Elasticsearch::Transport::Transport::Errors::NotFound errors' do it 'ignores Elasticsearch::Transport::Transport::Errors::NotFound errors' do
...@@ -74,7 +94,11 @@ describe Elastic::IndexRecordService, :elastic do ...@@ -74,7 +94,11 @@ describe Elastic::IndexRecordService, :elastic do
end end
# Nothing should be in the index at this point # Nothing should be in the index at this point
expect(Elasticsearch::Model.search('*').total_count).to be(0) expect(Project.elastic_search('*', search_options).total_count).to be(0)
expect(Issue.elastic_search('*', search_options).total_count).to be(0)
expect(Milestone.elastic_search('*', search_options).total_count).to be(0)
expect(MergeRequest.elastic_search('*', search_options).total_count).to be(0)
expect(ProjectSnippet.elastic_search('*', search_options).total_count).to be(0)
end end
it 'indexes records associated with the project' do it 'indexes records associated with the project' do
......
...@@ -5,7 +5,10 @@ require 'spec_helper' ...@@ -5,7 +5,10 @@ require 'spec_helper'
describe ElasticIndexerWorker, :elastic do describe ElasticIndexerWorker, :elastic do
subject { described_class.new } subject { described_class.new }
let(:search_options) { { options: { public_and_internal_projects: true }} } # Create admin user and search globally to avoid dealing with permissions in
# these tests
let(:user) { create(:admin) }
let(:search_options) { { options: { current_user: user, project_ids: :any }} }
before do before do
stub_ee_application_setting(elasticsearch_indexing: true) stub_ee_application_setting(elasticsearch_indexing: true)
...@@ -25,12 +28,12 @@ describe ElasticIndexerWorker, :elastic do ...@@ -25,12 +28,12 @@ describe ElasticIndexerWorker, :elastic do
describe 'Indexing, updating, and deleting records' do describe 'Indexing, updating, and deleting records' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
where(:type, :name, :attribute) do where(:type, :name) do
:project | "Project" | :name :project | "Project"
:issue | "Issue" | :title :issue | "Issue"
:note | "Note" | :note :note | "Note"
:milestone | "Milestone" | :title :milestone | "Milestone"
:merge_request | "MergeRequest" | :title :merge_request | "MergeRequest"
end end
with_them do with_them do
...@@ -49,6 +52,13 @@ describe ElasticIndexerWorker, :elastic do ...@@ -49,6 +52,13 @@ describe ElasticIndexerWorker, :elastic do
Sidekiq::Testing.disable! do Sidekiq::Testing.disable! do
object = create(type) object = create(type)
if type != :project
# You cannot find anything in the index if it's parent project is
# not first indexed.
subject.perform("index", "Project", object.project.id, object.project.es_id)
end
subject.perform("index", name, object.id, object.es_id) subject.perform("index", name, object.id, object.es_id)
ensure_elasticsearch_index! ensure_elasticsearch_index!
object.destroy object.destroy
...@@ -57,7 +67,7 @@ describe ElasticIndexerWorker, :elastic do ...@@ -57,7 +67,7 @@ describe ElasticIndexerWorker, :elastic do
expect do expect do
subject.perform("delete", name, object.id, object.es_id, { 'es_parent' => object.es_parent }) subject.perform("delete", name, object.id, object.es_id, { 'es_parent' => object.es_parent })
ensure_elasticsearch_index! ensure_elasticsearch_index!
end.to change { Elasticsearch::Model.search('*').total_count }.by(-1) end.to change { object.class.elastic_search('*', search_options).total_count }.by(-1)
end end
end end
end end
...@@ -66,7 +76,7 @@ describe ElasticIndexerWorker, :elastic do ...@@ -66,7 +76,7 @@ describe ElasticIndexerWorker, :elastic do
project, issue, milestone, note, merge_request = nil project, issue, milestone, note, merge_request = nil
Sidekiq::Testing.disable! do Sidekiq::Testing.disable! do
project = create :project, :repository, :public project = create :project, :repository
subject.perform("index", "Project", project.id, project.es_id) subject.perform("index", "Project", project.id, project.es_id)
issue = create :issue, project: project issue = create :issue, project: project
...@@ -86,11 +96,11 @@ describe ElasticIndexerWorker, :elastic do ...@@ -86,11 +96,11 @@ describe ElasticIndexerWorker, :elastic do
ensure_elasticsearch_index! ensure_elasticsearch_index!
## All database objects + data from repository. The absolute value does not matter ## All database objects + data from repository. The absolute value does not matter
expect(Project.elastic_search('*', search_options).records.to_a).to include(project) expect(Project.elastic_search('*', search_options).records).to include(project)
expect(Issue.elastic_search('*', search_options).records.to_a).to include(issue) expect(Issue.elastic_search('*', search_options).records).to include(issue)
expect(Milestone.elastic_search('*', search_options).records.to_a).to include(milestone) expect(Milestone.elastic_search('*', search_options).records).to include(milestone)
expect(Note.elastic_search('*', search_options).records.to_a).to include(note) expect(Note.elastic_search('*', search_options).records).to include(note)
expect(MergeRequest.elastic_search('*', search_options).records.to_a).to include(merge_request) expect(MergeRequest.elastic_search('*', search_options).records).to include(merge_request)
subject.perform("delete", "Project", project.id, project.es_id) subject.perform("delete", "Project", project.id, project.es_id)
ensure_elasticsearch_index! ensure_elasticsearch_index!
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment