summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-10-10 21:06:01 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-10-10 21:06:01 +0000
commitf607152a0802a68067343ad73f989033cb8e9a06 (patch)
treebbc16fd5f827ea5e30527d455a01dd6b1249a19c /spec/lib
parent7c862041c66833ebf49d9964f1797d93b1829690 (diff)
downloadgitlab-ce-f607152a0802a68067343ad73f989033cb8e9a06.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/gitlab/analytics/cycle_analytics/base_query_builder_spec.rb62
-rw-r--r--spec/lib/gitlab/analytics/cycle_analytics/records_fetcher_spec.rb131
-rw-r--r--spec/lib/gitlab/cycle_analytics/code_stage_spec.rb7
-rw-r--r--spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb7
-rw-r--r--spec/lib/gitlab/cycle_analytics/plan_stage_spec.rb7
-rw-r--r--spec/lib/gitlab/cycle_analytics/shared_stage_spec.rb18
-rw-r--r--spec/lib/gitlab/cycle_analytics/test_stage_spec.rb25
7 files changed, 250 insertions, 7 deletions
diff --git a/spec/lib/gitlab/analytics/cycle_analytics/base_query_builder_spec.rb b/spec/lib/gitlab/analytics/cycle_analytics/base_query_builder_spec.rb
new file mode 100644
index 00000000000..0fc9d3c1e9e
--- /dev/null
+++ b/spec/lib/gitlab/analytics/cycle_analytics/base_query_builder_spec.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::Analytics::CycleAnalytics::BaseQueryBuilder do
+ let_it_be(:project) { create(:project, :empty_repo) }
+ let_it_be(:mr1) { create(:merge_request, target_project: project, source_project: project, allow_broken: true, created_at: 3.months.ago) }
+ let_it_be(:mr2) { create(:merge_request, target_project: project, source_project: project, allow_broken: true, created_at: 1.month.ago) }
+ let(:params) { {} }
+ let(:records) do
+ stage = build(:cycle_analytics_project_stage, {
+ start_event_identifier: :merge_request_created,
+ end_event_identifier: :merge_request_merged,
+ project: project
+ })
+ described_class.new(stage: stage, params: params).build.to_a
+ end
+
+ before do
+ mr1.metrics.update!(merged_at: 1.month.ago)
+ mr2.metrics.update!(merged_at: Time.now)
+ end
+
+ around do |example|
+ Timecop.freeze { example.run }
+ end
+
+ describe 'date range parameters' do
+ context 'when filters by only the `from` parameter' do
+ before do
+ params[:from] = 4.months.ago
+ end
+
+ it { expect(records.size).to eq(2) }
+ end
+
+ context 'when filters by both `from` and `to` parameters' do
+ before do
+ params.merge!(from: 4.months.ago, to: 2.months.ago)
+ end
+
+ it { expect(records.size).to eq(1) }
+ end
+
+ context 'invalid date range is provided' do
+ before do
+ params.merge!(from: 1.month.ago, to: 10.months.ago)
+ end
+
+ it { expect(records.size).to eq(0) }
+ end
+ end
+
+ it 'scopes query within the target project' do
+ other_mr = create(:merge_request, source_project: create(:project), allow_broken: true, created_at: 2.months.ago)
+ other_mr.metrics.update!(merged_at: 1.month.ago)
+
+ params[:from] = 1.year.ago
+
+ expect(records.size).to eq(2)
+ end
+end
diff --git a/spec/lib/gitlab/analytics/cycle_analytics/records_fetcher_spec.rb b/spec/lib/gitlab/analytics/cycle_analytics/records_fetcher_spec.rb
new file mode 100644
index 00000000000..334cab0b799
--- /dev/null
+++ b/spec/lib/gitlab/analytics/cycle_analytics/records_fetcher_spec.rb
@@ -0,0 +1,131 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::Analytics::CycleAnalytics::RecordsFetcher do
+ around do |example|
+ Timecop.freeze { example.run }
+ end
+
+ let_it_be(:project) { create(:project, :empty_repo) }
+ let_it_be(:user) { create(:user) }
+
+ subject do
+ Gitlab::Analytics::CycleAnalytics::DataCollector.new(
+ stage: stage,
+ params: {
+ from: 1.year.ago,
+ current_user: user
+ }
+ ).records_fetcher.serialized_records
+ end
+
+ describe '#serialized_records' do
+ shared_context 'when records are loaded by maintainer' do
+ before do
+ project.add_user(user, Gitlab::Access::MAINTAINER)
+ end
+
+ it 'returns all records' do
+ expect(subject.size).to eq(2)
+ end
+ end
+
+ describe 'for issue based stage' do
+ let_it_be(:issue1) { create(:issue, project: project) }
+ let_it_be(:issue2) { create(:issue, project: project, confidential: true) }
+ let(:stage) do
+ build(:cycle_analytics_project_stage, {
+ start_event_identifier: :plan_stage_start,
+ end_event_identifier: :issue_first_mentioned_in_commit,
+ project: project
+ })
+ end
+
+ before do
+ issue1.metrics.update(first_added_to_board_at: 3.days.ago, first_mentioned_in_commit_at: 2.days.ago)
+ issue2.metrics.update(first_added_to_board_at: 3.days.ago, first_mentioned_in_commit_at: 2.days.ago)
+ end
+
+ context 'when records are loaded by guest' do
+ before do
+ project.add_user(user, Gitlab::Access::GUEST)
+ end
+
+ it 'filters out confidential issues' do
+ expect(subject.size).to eq(1)
+ expect(subject.first[:iid].to_s).to eq(issue1.iid.to_s)
+ end
+ end
+
+ include_context 'when records are loaded by maintainer'
+ end
+
+ describe 'for merge request based stage' do
+ let(:mr1) { create(:merge_request, created_at: 5.days.ago, source_project: project, allow_broken: true) }
+ let(:mr2) { create(:merge_request, created_at: 4.days.ago, source_project: project, allow_broken: true) }
+ let(:stage) do
+ build(:cycle_analytics_project_stage, {
+ start_event_identifier: :merge_request_created,
+ end_event_identifier: :merge_request_merged,
+ project: project
+ })
+ end
+
+ before do
+ mr1.metrics.update(merged_at: 3.days.ago)
+ mr2.metrics.update(merged_at: 3.days.ago)
+ end
+
+ include_context 'when records are loaded by maintainer'
+ end
+
+ describe 'special case' do
+ let(:mr1) { create(:merge_request, source_project: project, allow_broken: true, created_at: 20.days.ago) }
+ let(:mr2) { create(:merge_request, source_project: project, allow_broken: true, created_at: 19.days.ago) }
+ let(:ci_build1) { create(:ci_build) }
+ let(:ci_build2) { create(:ci_build) }
+ let(:default_stages) { Gitlab::Analytics::CycleAnalytics::DefaultStages }
+ let(:stage) { build(:cycle_analytics_project_stage, default_stages.params_for_test_stage.merge(project: project)) }
+
+ before do
+ mr1.metrics.update!({
+ merged_at: 5.days.ago,
+ first_deployed_to_production_at: 1.day.ago,
+ latest_build_started_at: 5.days.ago,
+ latest_build_finished_at: 1.day.ago,
+ pipeline: ci_build1.pipeline
+ })
+ mr2.metrics.update!({
+ merged_at: 10.days.ago,
+ first_deployed_to_production_at: 5.days.ago,
+ latest_build_started_at: 9.days.ago,
+ latest_build_finished_at: 7.days.ago,
+ pipeline: ci_build2.pipeline
+ })
+ end
+
+ context 'returns build records' do
+ shared_examples 'orders build records by `latest_build_finished_at`' do
+ it 'orders by `latest_build_finished_at`' do
+ build_ids = subject.map { |item| item[:id] }
+
+ expect(build_ids).to eq([ci_build1.id, ci_build2.id])
+ end
+ end
+
+ context 'when requesting records for default test stage' do
+ include_examples 'orders build records by `latest_build_finished_at`'
+ end
+
+ context 'when requesting records for default staging stage' do
+ before do
+ stage.assign_attributes(default_stages.params_for_staging_stage)
+ end
+
+ include_examples 'orders build records by `latest_build_finished_at`'
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/cycle_analytics/code_stage_spec.rb b/spec/lib/gitlab/cycle_analytics/code_stage_spec.rb
index 6959778661b..aa12bc21d22 100644
--- a/spec/lib/gitlab/cycle_analytics/code_stage_spec.rb
+++ b/spec/lib/gitlab/cycle_analytics/code_stage_spec.rb
@@ -26,6 +26,13 @@ describe Gitlab::CycleAnalytics::CodeStage do
it_behaves_like 'base stage'
+ context 'when using the new query backend' do
+ include_examples 'Gitlab::Analytics::CycleAnalytics::DataCollector backend examples' do
+ let(:expected_record_count) { 2 }
+ let(:expected_ordered_attribute_values) { [mr_2.title, mr_1.title] }
+ end
+ end
+
describe '#project_median' do
around do |example|
Timecop.freeze { example.run }
diff --git a/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb b/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb
index b99debf8cac..497db88d850 100644
--- a/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb
+++ b/spec/lib/gitlab/cycle_analytics/issue_stage_spec.rb
@@ -21,6 +21,13 @@ describe Gitlab::CycleAnalytics::IssueStage do
it_behaves_like 'base stage'
+ context 'when using the new query backend' do
+ include_examples 'Gitlab::Analytics::CycleAnalytics::DataCollector backend examples' do
+ let(:expected_record_count) { 3 }
+ let(:expected_ordered_attribute_values) { [issue_3.title, issue_2.title, issue_1.title] }
+ end
+ end
+
describe '#median' do
around do |example|
Timecop.freeze { example.run }
diff --git a/spec/lib/gitlab/cycle_analytics/plan_stage_spec.rb b/spec/lib/gitlab/cycle_analytics/plan_stage_spec.rb
index 60d180015c4..01a46f5ba65 100644
--- a/spec/lib/gitlab/cycle_analytics/plan_stage_spec.rb
+++ b/spec/lib/gitlab/cycle_analytics/plan_stage_spec.rb
@@ -21,6 +21,13 @@ describe Gitlab::CycleAnalytics::PlanStage do
it_behaves_like 'base stage'
+ context 'when using the new query backend' do
+ include_examples 'Gitlab::Analytics::CycleAnalytics::DataCollector backend examples' do
+ let(:expected_record_count) { 2 }
+ let(:expected_ordered_attribute_values) { [issue_1.title, issue_2.title] }
+ end
+ end
+
describe '#project_median' do
around do |example|
Timecop.freeze { example.run }
diff --git a/spec/lib/gitlab/cycle_analytics/shared_stage_spec.rb b/spec/lib/gitlab/cycle_analytics/shared_stage_spec.rb
index b3a6c2c4239..c5b17aafdd2 100644
--- a/spec/lib/gitlab/cycle_analytics/shared_stage_spec.rb
+++ b/spec/lib/gitlab/cycle_analytics/shared_stage_spec.rb
@@ -52,3 +52,21 @@ shared_examples 'calculate #median with date range' do
it { expect(stage.project_median).to eq(nil) }
end
end
+
+shared_examples 'Gitlab::Analytics::CycleAnalytics::DataCollector backend examples' do
+ let(:stage_params) { Gitlab::Analytics::CycleAnalytics::DefaultStages.send("params_for_#{stage_name}_stage").merge(project: project) }
+ let(:stage) { Analytics::CycleAnalytics::ProjectStage.new(stage_params) }
+ let(:data_collector) { Gitlab::Analytics::CycleAnalytics::DataCollector.new(stage: stage, params: { from: stage_options[:from], current_user: project.creator }) }
+ let(:attribute_to_verify) { :title }
+
+ context 'provides the same results as the old implementation' do
+ it 'for the median' do
+ expect(data_collector.median.seconds).to eq(ISSUES_MEDIAN)
+ end
+
+ it 'for the list of event records' do
+ records = data_collector.records_fetcher.serialized_records
+ expect(records.map { |event| event[attribute_to_verify] }).to eq(expected_ordered_attribute_values)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/cycle_analytics/test_stage_spec.rb b/spec/lib/gitlab/cycle_analytics/test_stage_spec.rb
index bbb53cefae7..e347f36dfce 100644
--- a/spec/lib/gitlab/cycle_analytics/test_stage_spec.rb
+++ b/spec/lib/gitlab/cycle_analytics/test_stage_spec.rb
@@ -12,17 +12,20 @@ describe Gitlab::CycleAnalytics::TestStage do
it_behaves_like 'base stage'
describe '#median' do
+ let(:mr_1) { create(:merge_request, :closed, source_project: project, created_at: 60.minutes.ago) }
+ let(:mr_2) { create(:merge_request, :closed, source_project: project, created_at: 40.minutes.ago, source_branch: 'A') }
+ let(:mr_3) { create(:merge_request, source_project: project, created_at: 10.minutes.ago, source_branch: 'B') }
+ let(:mr_4) { create(:merge_request, source_project: project, created_at: 10.minutes.ago, source_branch: 'C') }
+ let(:mr_5) { create(:merge_request, source_project: project, created_at: 10.minutes.ago, source_branch: 'D') }
+ let(:ci_build1) { create(:ci_build, project: project) }
+ let(:ci_build2) { create(:ci_build, project: project) }
+
before do
issue_1 = create(:issue, project: project, created_at: 90.minutes.ago)
issue_2 = create(:issue, project: project, created_at: 60.minutes.ago)
issue_3 = create(:issue, project: project, created_at: 60.minutes.ago)
- mr_1 = create(:merge_request, :closed, source_project: project, created_at: 60.minutes.ago)
- mr_2 = create(:merge_request, :closed, source_project: project, created_at: 40.minutes.ago, source_branch: 'A')
- mr_3 = create(:merge_request, source_project: project, created_at: 10.minutes.ago, source_branch: 'B')
- mr_4 = create(:merge_request, source_project: project, created_at: 10.minutes.ago, source_branch: 'C')
- mr_5 = create(:merge_request, source_project: project, created_at: 10.minutes.ago, source_branch: 'D')
- mr_1.metrics.update!(latest_build_started_at: 32.minutes.ago, latest_build_finished_at: 2.minutes.ago)
- mr_2.metrics.update!(latest_build_started_at: 62.minutes.ago, latest_build_finished_at: 32.minutes.ago)
+ mr_1.metrics.update!(latest_build_started_at: 32.minutes.ago, latest_build_finished_at: 2.minutes.ago, pipeline_id: ci_build1.commit_id)
+ mr_2.metrics.update!(latest_build_started_at: 62.minutes.ago, latest_build_finished_at: 32.minutes.ago, pipeline_id: ci_build2.commit_id)
mr_3.metrics.update!(latest_build_started_at: nil, latest_build_finished_at: nil)
mr_4.metrics.update!(latest_build_started_at: nil, latest_build_finished_at: nil)
mr_5.metrics.update!(latest_build_started_at: nil, latest_build_finished_at: nil)
@@ -43,5 +46,13 @@ describe Gitlab::CycleAnalytics::TestStage do
end
include_examples 'calculate #median with date range'
+
+ context 'when using the new query backend' do
+ include_examples 'Gitlab::Analytics::CycleAnalytics::DataCollector backend examples' do
+ let(:expected_record_count) { 2 }
+ let(:attribute_to_verify) { :id }
+ let(:expected_ordered_attribute_values) { [mr_1.metrics.pipeline.builds.first.id, mr_2.metrics.pipeline.builds.first.id] }
+ end
+ end
end
end