diff options
Diffstat (limited to 'spec/models/cycle_analytics')
-rw-r--r-- | spec/models/cycle_analytics/code_spec.rb | 73 | ||||
-rw-r--r-- | spec/models/cycle_analytics/issue_spec.rb | 46 | ||||
-rw-r--r-- | spec/models/cycle_analytics/plan_spec.rb | 52 | ||||
-rw-r--r-- | spec/models/cycle_analytics/project_level_spec.rb | 34 | ||||
-rw-r--r-- | spec/models/cycle_analytics/project_level_stage_adapter_spec.rb | 38 | ||||
-rw-r--r-- | spec/models/cycle_analytics/review_spec.rb | 34 | ||||
-rw-r--r-- | spec/models/cycle_analytics/staging_spec.rb | 56 | ||||
-rw-r--r-- | spec/models/cycle_analytics/test_spec.rb | 73 |
8 files changed, 38 insertions, 368 deletions
diff --git a/spec/models/cycle_analytics/code_spec.rb b/spec/models/cycle_analytics/code_spec.rb deleted file mode 100644 index ca612cba654..00000000000 --- a/spec/models/cycle_analytics/code_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'CycleAnalytics#code' do - extend CycleAnalyticsHelpers::TestGeneration - - let_it_be(:project) { create(:project, :repository) } - let_it_be(:from_date) { 10.days.ago } - let_it_be(:user) { project.owner } - let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) } - - subject { project_level } - - context 'with deployment' do - generate_cycle_analytics_spec( - phase: :code, - data_fn: -> (context) { { issue: context.create(:issue, project: context.project) } }, - start_time_conditions: [["issue mentioned in a commit", - -> (context, data) do - context.create_commit_referencing_issue(data[:issue]) - end]], - end_time_conditions: [["merge request that closes issue is created", - -> (context, data) do - context.create_merge_request_closing_issue(context.user, context.project, data[:issue]) - end]], - post_fn: -> (context, data) do - end) - - context "when a regular merge request (that doesn't close the issue) is created" do - it "returns nil" do - issue = create(:issue, project: project) - - create_commit_referencing_issue(issue) - create_merge_request_closing_issue(user, project, issue, message: "Closes nothing") - - merge_merge_requests_closing_issue(user, project, issue) - deploy_master(user, project) - - expect(subject[:code].project_median).to be_nil - end - end - end - - context 'without deployment' do - generate_cycle_analytics_spec( - phase: :code, - data_fn: -> (context) { { issue: context.create(:issue, project: context.project) } }, - start_time_conditions: [["issue mentioned in a commit", - -> (context, data) do - context.create_commit_referencing_issue(data[:issue]) - end]], - end_time_conditions: [["merge request that closes issue is created", - -> (context, data) do - context.create_merge_request_closing_issue(context.user, context.project, data[:issue]) - end]], - post_fn: -> (context, data) do - end) - - context "when a regular merge request (that doesn't close the issue) is created" do - it "returns nil" do - issue = create(:issue, project: project) - - create_commit_referencing_issue(issue) - create_merge_request_closing_issue(user, project, issue, message: "Closes nothing") - - merge_merge_requests_closing_issue(user, project, issue) - - expect(subject[:code].project_median).to be_nil - end - end - end -end diff --git a/spec/models/cycle_analytics/issue_spec.rb b/spec/models/cycle_analytics/issue_spec.rb deleted file mode 100644 index 66d21f6925f..00000000000 --- a/spec/models/cycle_analytics/issue_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'CycleAnalytics#issue' do - extend CycleAnalyticsHelpers::TestGeneration - - let_it_be(:project) { create(:project, :repository) } - let_it_be(:from_date) { 10.days.ago } - let_it_be(:user) { project.owner } - let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) } - - subject { project_level } - - generate_cycle_analytics_spec( - phase: :issue, - data_fn: -> (context) { { issue: context.build(:issue, project: context.project) } }, - start_time_conditions: [["issue created", -> (context, data) { data[:issue].save! }]], - end_time_conditions: [["issue associated with a milestone", - -> (context, data) do - if data[:issue].persisted? - data[:issue].update!(milestone: context.create(:milestone, project: context.project)) - end - end], - ["list label added to issue", - -> (context, data) do - if data[:issue].persisted? - data[:issue].update!(label_ids: [context.create(:list).label_id]) - end - end]], - post_fn: -> (context, data) do - end) - - context "when a regular label (instead of a list label) is added to the issue" do - it "returns nil" do - regular_label = create(:label) - issue = create(:issue, project: project) - issue.update!(label_ids: [regular_label.id]) - - create_merge_request_closing_issue(user, project, issue) - merge_merge_requests_closing_issue(user, project, issue) - - expect(subject[:issue].project_median).to be_nil - end - end -end diff --git a/spec/models/cycle_analytics/plan_spec.rb b/spec/models/cycle_analytics/plan_spec.rb deleted file mode 100644 index acaf767db01..00000000000 --- a/spec/models/cycle_analytics/plan_spec.rb +++ /dev/null @@ -1,52 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'CycleAnalytics#plan' do - extend CycleAnalyticsHelpers::TestGeneration - - let_it_be(:project) { create(:project, :repository) } - let_it_be(:from_date) { 10.days.ago } - let_it_be(:user) { project.owner } - let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) } - - subject { project_level } - - generate_cycle_analytics_spec( - phase: :plan, - data_fn: -> (context) do - { - issue: context.build(:issue, project: context.project), - branch_name: context.generate(:branch) - } - end, - start_time_conditions: [["issue associated with a milestone", - -> (context, data) do - data[:issue].update!(milestone: context.create(:milestone, project: context.project)) - end], - ["list label added to issue", - -> (context, data) do - data[:issue].update!(label_ids: [context.create(:list).label_id]) - end]], - end_time_conditions: [["issue mentioned in a commit", - -> (context, data) do - context.create_commit_referencing_issue(data[:issue], branch_name: data[:branch_name]) - end]], - post_fn: -> (context, data) do - end) - - context "when a regular label (instead of a list label) is added to the issue" do - it "returns nil" do - branch_name = generate(:branch) - label = create(:label) - issue = create(:issue, project: project) - issue.update!(label_ids: [label.id]) - create_commit_referencing_issue(issue, branch_name: branch_name) - - create_merge_request_closing_issue(user, project, issue, source_branch: branch_name) - merge_merge_requests_closing_issue(user, project, issue) - - expect(subject[:issue].project_median).to be_nil - end - end -end diff --git a/spec/models/cycle_analytics/project_level_spec.rb b/spec/models/cycle_analytics/project_level_spec.rb deleted file mode 100644 index c2d421c03d8..00000000000 --- a/spec/models/cycle_analytics/project_level_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe CycleAnalytics::ProjectLevel do - let_it_be(:project) { create(:project, :repository) } - let_it_be(:from_date) { 10.days.ago } - let_it_be(:user) { project.owner } - let_it_be(:issue) { create(:issue, project: project, created_at: 2.days.ago) } - let_it_be(:milestone) { create(:milestone, project: project) } - let(:mr) { create_merge_request_closing_issue(user, project, issue, commit_message: "References #{issue.to_reference}") } - let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha, head_pipeline_of: mr) } - - subject { described_class.new(project, options: { from: from_date }) } - - describe '#all_medians_by_stage' do - before do - allow_next_instance_of(Gitlab::ReferenceExtractor) do |instance| - allow(instance).to receive(:issues).and_return([issue]) - end - - create_cycle(user, project, issue, mr, milestone, pipeline) - deploy_master(user, project) - end - - it 'returns every median for each stage for a specific project' do - values = described_class::STAGES.each_with_object({}) do |stage_name, hsh| - hsh[stage_name] = subject[stage_name].project_median.presence - end - - expect(subject.all_medians_by_stage).to eq(values) - end - end -end diff --git a/spec/models/cycle_analytics/project_level_stage_adapter_spec.rb b/spec/models/cycle_analytics/project_level_stage_adapter_spec.rb new file mode 100644 index 00000000000..9bdee292938 --- /dev/null +++ b/spec/models/cycle_analytics/project_level_stage_adapter_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe CycleAnalytics::ProjectLevelStageAdapter, type: :model do + let_it_be(:stage_name) { :review } # pre-defined, default stage + let_it_be(:merge_request) do + create(:merge_request, created_at: 5.hours.ago).tap do |mr| + mr.metrics.update!(merged_at: mr.created_at + 1.hour) + end + end + + let_it_be(:project) { merge_request.target_project } + + let(:stage) do + params = Gitlab::Analytics::CycleAnalytics::DefaultStages.find_by_name!(stage_name).merge(project: project) + Analytics::CycleAnalytics::ProjectStage.new(params) + end + + around do |example| + freeze_time { example.run } + end + + subject { described_class.new(stage, from: 1.month.ago, to: Time.zone.now, current_user: merge_request.author) } + + it 'calculates median' do + expect(subject.median).to be_within(1.hour).of(0.5) + end + + it 'lists events' do + expect(subject.events.size).to eq(1) + expect(subject.events.first[:title]).to eq(merge_request.title) + end + + it 'presents the data as json' do + expect(subject.as_json).to include({ title: 'Review', value: 'about 1 hour' }) + end +end diff --git a/spec/models/cycle_analytics/review_spec.rb b/spec/models/cycle_analytics/review_spec.rb deleted file mode 100644 index 06d9cfbf8c0..00000000000 --- a/spec/models/cycle_analytics/review_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'CycleAnalytics#review' do - extend CycleAnalyticsHelpers::TestGeneration - - let_it_be(:project) { create(:project, :repository) } - let_it_be(:from_date) { 10.days.ago } - let_it_be(:user) { project.owner } - - subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) } - - generate_cycle_analytics_spec( - phase: :review, - data_fn: -> (context) { { issue: context.create(:issue, project: context.project) } }, - start_time_conditions: [["merge request that closes issue is created", - -> (context, data) do - context.create_merge_request_closing_issue(context.user, context.project, data[:issue]) - end]], - end_time_conditions: [["merge request that closes issue is merged", - -> (context, data) do - context.merge_merge_requests_closing_issue(context.user, context.project, data[:issue]) - end]], - post_fn: nil) - - context "when a regular merge request (that doesn't close the issue) is created and merged" do - it "returns nil" do - MergeRequests::MergeService.new(project, user).execute(create(:merge_request)) - - expect(subject[:review].project_median).to be_nil - end - end -end diff --git a/spec/models/cycle_analytics/staging_spec.rb b/spec/models/cycle_analytics/staging_spec.rb deleted file mode 100644 index 50cb49d6309..00000000000 --- a/spec/models/cycle_analytics/staging_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'CycleAnalytics#staging' do - extend CycleAnalyticsHelpers::TestGeneration - - let_it_be(:project) { create(:project, :repository) } - let_it_be(:from_date) { 10.days.ago } - let_it_be(:user) { project.owner } - let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) } - - subject { project_level } - - generate_cycle_analytics_spec( - phase: :staging, - data_fn: lambda do |context| - issue = context.create(:issue, project: context.project) - { issue: issue, merge_request: context.create_merge_request_closing_issue(context.user, context.project, issue) } - end, - start_time_conditions: [["merge request that closes issue is merged", - -> (context, data) do - context.merge_merge_requests_closing_issue(context.user, context.project, data[:issue]) - end]], - end_time_conditions: [["merge request that closes issue is deployed to production", - -> (context, data) do - context.deploy_master(context.user, context.project) - end], - ["production deploy happens after merge request is merged (along with other changes)", - lambda do |context, data| - # Make other changes on master - context.project.repository.commit("this_sha_apparently_does_not_matter") - context.deploy_master(context.user, context.project) - end]]) - - context "when a regular merge request (that doesn't close the issue) is merged and deployed" do - it "returns nil" do - merge_request = create(:merge_request) - MergeRequests::MergeService.new(project, user).execute(merge_request) - deploy_master(user, project) - - expect(subject[:staging].project_median).to be_nil - end - end - - context "when the deployment happens to a non-production environment" do - it "returns nil" do - issue = create(:issue, project: project) - merge_request = create_merge_request_closing_issue(user, project, issue) - MergeRequests::MergeService.new(project, user).execute(merge_request) - deploy_master(user, project, environment: 'staging') - - expect(subject[:staging].project_median).to be_nil - end - end -end diff --git a/spec/models/cycle_analytics/test_spec.rb b/spec/models/cycle_analytics/test_spec.rb deleted file mode 100644 index 8f65c047b15..00000000000 --- a/spec/models/cycle_analytics/test_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'CycleAnalytics#test' do - extend CycleAnalyticsHelpers::TestGeneration - - let_it_be(:project) { create(:project, :repository) } - let_it_be(:from_date) { 10.days.ago } - let_it_be(:user) { project.owner } - let_it_be(:issue) { create(:issue, project: project) } - let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date, current_user: user }) } - let!(:merge_request) { create_merge_request_closing_issue(user, project, issue) } - - subject { project_level } - - generate_cycle_analytics_spec( - phase: :test, - data_fn: lambda do |context| - issue = context.issue - merge_request = context.create_merge_request_closing_issue(context.user, context.project, issue) - pipeline = context.create(:ci_pipeline, ref: merge_request.source_branch, sha: merge_request.diff_head_sha, project: context.project, head_pipeline_of: merge_request) - { pipeline: pipeline, issue: issue } - end, - start_time_conditions: [["pipeline is started", -> (context, data) { data[:pipeline].run! }]], - end_time_conditions: [["pipeline is finished", -> (context, data) { data[:pipeline].succeed! }]], - post_fn: -> (context, data) do - end) - - context "when the pipeline is for a regular merge request (that doesn't close an issue)" do - it "returns nil" do - pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha) - - pipeline.run! - pipeline.succeed! - - expect(subject[:test].project_median).to be_nil - end - end - - context "when the pipeline is not for a merge request" do - it "returns nil" do - pipeline = create(:ci_pipeline, ref: "refs/heads/master", sha: project.repository.commit('master').sha) - - pipeline.run! - pipeline.succeed! - - expect(subject[:test].project_median).to be_nil - end - end - - context "when the pipeline is dropped (failed)" do - it "returns nil" do - pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha) - - pipeline.run! - pipeline.drop! - - expect(subject[:test].project_median).to be_nil - end - end - - context "when the pipeline is cancelled" do - it "returns nil" do - pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha) - - pipeline.run! - pipeline.cancel! - - expect(subject[:test].project_median).to be_nil - end - end -end |