diff options
Diffstat (limited to 'spec/workers/jira_connect/sync_project_worker_spec.rb')
-rw-r--r-- | spec/workers/jira_connect/sync_project_worker_spec.rb | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/spec/workers/jira_connect/sync_project_worker_spec.rb b/spec/workers/jira_connect/sync_project_worker_spec.rb index 5c0e7e7609c..d172bde2400 100644 --- a/spec/workers/jira_connect/sync_project_worker_spec.rb +++ b/spec/workers/jira_connect/sync_project_worker_spec.rb @@ -3,6 +3,12 @@ require 'spec_helper' RSpec.describe JiraConnect::SyncProjectWorker, factory_default: :keep do + include AfterNextHelpers + + it_behaves_like 'worker with data consistency', + described_class, + data_consistency: :delayed + describe '#perform' do let_it_be(:project) { create_default(:project).freeze } @@ -14,6 +20,22 @@ RSpec.describe JiraConnect::SyncProjectWorker, factory_default: :keep do let(:jira_connect_sync_service) { JiraConnect::SyncService.new(project) } let(:job_args) { [project.id, update_sequence_id] } let(:update_sequence_id) { 1 } + let(:request_path) { '/rest/devinfo/0.10/bulk' } + let(:request_body) do + { + repositories: [ + Atlassian::JiraConnect::Serializers::RepositoryEntity.represent( + project, + merge_requests: [mr_with_jira_description, mr_with_jira_title], + update_sequence_id: update_sequence_id + ) + ] + } + end + + def perform(project_id, update_sequence_id) + described_class.new.perform(project_id, update_sequence_id) + end before do stub_request(:post, 'https://sample.atlassian.net/rest/devinfo/0.10/bulk').to_return(status: 200, body: '', headers: {}) @@ -24,54 +46,37 @@ RSpec.describe JiraConnect::SyncProjectWorker, factory_default: :keep do context 'when the project is not found' do it 'does not raise an error' do - expect { described_class.new.perform('non_existing_record_id', update_sequence_id) }.not_to raise_error + expect { perform('non_existing_record_id', update_sequence_id) }.not_to raise_error end end it 'avoids N+1 database queries' do - control_count = ActiveRecord::QueryRecorder.new { described_class.new.perform(project.id, update_sequence_id) }.count + control_count = ActiveRecord::QueryRecorder.new { perform(project.id, update_sequence_id) }.count create(:merge_request, :unique_branches, title: 'TEST-123') - expect { described_class.new.perform(project.id, update_sequence_id) }.not_to exceed_query_limit(control_count) + expect { perform(project.id, update_sequence_id) }.not_to exceed_query_limit(control_count) end - it_behaves_like 'an idempotent worker' do - let(:request_path) { '/rest/devinfo/0.10/bulk' } - let(:request_body) do - { - repositories: [ - Atlassian::JiraConnect::Serializers::RepositoryEntity.represent( - project, - merge_requests: [mr_with_jira_description, mr_with_jira_title], - update_sequence_id: update_sequence_id - ) - ] - } - end - - it 'sends the request with custom update_sequence_id' do - allow_next_instances_of(Atlassian::JiraConnect::Client, IdempotentWorkerHelper::WORKER_EXEC_TIMES) do |client| - expect(client).to receive(:post).with(request_path, request_body) - end + it 'sends the request with custom update_sequence_id' do + allow_next(Atlassian::JiraConnect::Client).to receive(:post) + .with(request_path, request_body) - subject - end + perform(project.id, update_sequence_id) + end - context 'when the number of merge requests to sync is higher than the limit' do - let!(:most_recent_merge_request) { create(:merge_request, :unique_branches, description: 'TEST-323', title: 'TEST-123') } + context 'when the number of merge requests to sync is higher than the limit' do + let!(:most_recent_merge_request) { create(:merge_request, :unique_branches, description: 'TEST-323', title: 'TEST-123') } - before do - stub_const("#{described_class}::MERGE_REQUEST_LIMIT", 1) - end + before do + stub_const("#{described_class}::MERGE_REQUEST_LIMIT", 1) + end - it 'syncs only the most recent merge requests within the limit' do - expect(jira_connect_sync_service).to receive(:execute) - .exactly(IdempotentWorkerHelper::WORKER_EXEC_TIMES).times - .with(merge_requests: [most_recent_merge_request], update_sequence_id: update_sequence_id) + it 'syncs only the most recent merge requests within the limit' do + expect(jira_connect_sync_service).to receive(:execute) + .with(merge_requests: [most_recent_merge_request], update_sequence_id: update_sequence_id) - subject - end + perform(project.id, update_sequence_id) end end end |