Commit 084c7d9b authored by Robert May's avatar Robert May Committed by Mike Kozono

Speed improvements to spec/models/cycle_analytics

This drops about 1 minute from the run time for the specs in
spec/models/cycle_analytics. It swaps a number of `let` to `let_it_be`
and also removes a number of lines which seem to not have any effect
on the success of the specs.
parent e46ecc8e
...@@ -5,11 +5,12 @@ require 'spec_helper' ...@@ -5,11 +5,12 @@ require 'spec_helper'
describe 'CycleAnalytics#code' do describe 'CycleAnalytics#code' do
extend CycleAnalyticsHelpers::TestGeneration extend CycleAnalyticsHelpers::TestGeneration
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) } subject { project_level }
context 'with deployment' do context 'with deployment' do
generate_cycle_analytics_spec( generate_cycle_analytics_spec(
...@@ -24,8 +25,6 @@ describe 'CycleAnalytics#code' do ...@@ -24,8 +25,6 @@ describe 'CycleAnalytics#code' do
context.create_merge_request_closing_issue(context.user, context.project, data[:issue]) context.create_merge_request_closing_issue(context.user, context.project, data[:issue])
end]], end]],
post_fn: -> (context, data) do post_fn: -> (context, data) do
context.merge_merge_requests_closing_issue(context.user, context.project, data[:issue])
context.deploy_master(context.user, context.project)
end) end)
context "when a regular merge request (that doesn't close the issue) is created" do context "when a regular merge request (that doesn't close the issue) is created" do
...@@ -56,7 +55,6 @@ describe 'CycleAnalytics#code' do ...@@ -56,7 +55,6 @@ describe 'CycleAnalytics#code' do
context.create_merge_request_closing_issue(context.user, context.project, data[:issue]) context.create_merge_request_closing_issue(context.user, context.project, data[:issue])
end]], end]],
post_fn: -> (context, data) do post_fn: -> (context, data) do
context.merge_merge_requests_closing_issue(context.user, context.project, data[:issue])
end) end)
context "when a regular merge request (that doesn't close the issue) is created" do context "when a regular merge request (that doesn't close the issue) is created" do
......
...@@ -3,12 +3,12 @@ ...@@ -3,12 +3,12 @@
require 'spec_helper' require 'spec_helper'
describe CycleAnalytics::GroupLevel do describe CycleAnalytics::GroupLevel do
let(:group) { create(:group)} let_it_be(:group) { create(:group)}
let(:project) { create(:project, :repository, namespace: group) } let_it_be(:project) { create(:project, :repository, namespace: group) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let(:issue) { create(:issue, project: project, created_at: 2.days.ago) } let_it_be(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
let(:milestone) { create(:milestone, project: project) } 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(: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) } let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha, head_pipeline_of: mr) }
......
...@@ -5,11 +5,12 @@ require 'spec_helper' ...@@ -5,11 +5,12 @@ require 'spec_helper'
describe 'CycleAnalytics#issue' do describe 'CycleAnalytics#issue' do
extend CycleAnalyticsHelpers::TestGeneration extend CycleAnalyticsHelpers::TestGeneration
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) } subject { project_level }
generate_cycle_analytics_spec( generate_cycle_analytics_spec(
phase: :issue, phase: :issue,
...@@ -28,10 +29,6 @@ describe 'CycleAnalytics#issue' do ...@@ -28,10 +29,6 @@ describe 'CycleAnalytics#issue' do
end end
end]], end]],
post_fn: -> (context, data) do post_fn: -> (context, data) do
if data[:issue].persisted?
context.create_merge_request_closing_issue(context.user, context.project, data[:issue].reload)
context.merge_merge_requests_closing_issue(context.user, context.project, data[:issue])
end
end) end)
context "when a regular label (instead of a list label) is added to the issue" do context "when a regular label (instead of a list label) is added to the issue" do
......
...@@ -5,17 +5,18 @@ require 'spec_helper' ...@@ -5,17 +5,18 @@ require 'spec_helper'
describe 'CycleAnalytics#plan' do describe 'CycleAnalytics#plan' do
extend CycleAnalyticsHelpers::TestGeneration extend CycleAnalyticsHelpers::TestGeneration
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) } subject { project_level }
generate_cycle_analytics_spec( generate_cycle_analytics_spec(
phase: :plan, phase: :plan,
data_fn: -> (context) do data_fn: -> (context) do
{ {
issue: context.create(:issue, project: context.project), issue: context.build(:issue, project: context.project),
branch_name: context.generate(:branch) branch_name: context.generate(:branch)
} }
end, end,
...@@ -32,8 +33,6 @@ describe 'CycleAnalytics#plan' do ...@@ -32,8 +33,6 @@ describe 'CycleAnalytics#plan' do
context.create_commit_referencing_issue(data[:issue], branch_name: data[:branch_name]) context.create_commit_referencing_issue(data[:issue], branch_name: data[:branch_name])
end]], end]],
post_fn: -> (context, data) do post_fn: -> (context, data) do
context.create_merge_request_closing_issue(context.user, context.project, data[:issue], source_branch: data[:branch_name])
context.merge_merge_requests_closing_issue(context.user, context.project, data[:issue])
end) end)
context "when a regular label (instead of a list label) is added to the issue" do context "when a regular label (instead of a list label) is added to the issue" do
......
...@@ -5,11 +5,12 @@ require 'spec_helper' ...@@ -5,11 +5,12 @@ require 'spec_helper'
describe 'CycleAnalytics#production' do describe 'CycleAnalytics#production' do
extend CycleAnalyticsHelpers::TestGeneration extend CycleAnalyticsHelpers::TestGeneration
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) } subject { project_level }
generate_cycle_analytics_spec( generate_cycle_analytics_spec(
phase: :production, phase: :production,
...@@ -24,13 +25,7 @@ describe 'CycleAnalytics#production' do ...@@ -24,13 +25,7 @@ describe 'CycleAnalytics#production' do
["production deploy happens after merge request is merged (along with other changes)", ["production deploy happens after merge request is merged (along with other changes)",
lambda do |context, data| lambda do |context, data|
# Make other changes on master # Make other changes on master
sha = context.project.repository.create_file( context.project.repository.commit("sha_that_does_not_matter")
context.user,
context.generate(:branch),
'content',
message: 'commit message',
branch_name: 'master')
context.project.repository.commit(sha)
context.deploy_master(context.user, context.project) context.deploy_master(context.user, context.project)
end]]) end]])
...@@ -47,7 +42,7 @@ describe 'CycleAnalytics#production' do ...@@ -47,7 +42,7 @@ describe 'CycleAnalytics#production' do
context "when the deployment happens to a non-production environment" do context "when the deployment happens to a non-production environment" do
it "returns nil" do it "returns nil" do
issue = create(:issue, project: project) issue = build(:issue, project: project)
merge_request = create_merge_request_closing_issue(user, project, issue) merge_request = create_merge_request_closing_issue(user, project, issue)
MergeRequests::MergeService.new(project, user).execute(merge_request) MergeRequests::MergeService.new(project, user).execute(merge_request)
deploy_master(user, project, environment: 'staging') deploy_master(user, project, environment: 'staging')
......
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
require 'spec_helper' require 'spec_helper'
describe CycleAnalytics::ProjectLevel do describe CycleAnalytics::ProjectLevel do
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let(:issue) { create(:issue, project: project, created_at: 2.days.ago) } let_it_be(:issue) { create(:issue, project: project, created_at: 2.days.ago) }
let(:milestone) { create(:milestone, project: project) } 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(: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) } let(:pipeline) { create(:ci_empty_pipeline, status: 'created', project: project, ref: mr.source_branch, sha: mr.source_branch_sha, head_pipeline_of: mr) }
......
...@@ -5,9 +5,9 @@ require 'spec_helper' ...@@ -5,9 +5,9 @@ require 'spec_helper'
describe 'CycleAnalytics#review' do describe 'CycleAnalytics#review' do
extend CycleAnalyticsHelpers::TestGeneration extend CycleAnalyticsHelpers::TestGeneration
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) } subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
......
...@@ -5,11 +5,12 @@ require 'spec_helper' ...@@ -5,11 +5,12 @@ require 'spec_helper'
describe 'CycleAnalytics#staging' do describe 'CycleAnalytics#staging' do
extend CycleAnalyticsHelpers::TestGeneration extend CycleAnalyticsHelpers::TestGeneration
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) } subject { project_level }
generate_cycle_analytics_spec( generate_cycle_analytics_spec(
phase: :staging, phase: :staging,
...@@ -28,14 +29,7 @@ describe 'CycleAnalytics#staging' do ...@@ -28,14 +29,7 @@ describe 'CycleAnalytics#staging' do
["production deploy happens after merge request is merged (along with other changes)", ["production deploy happens after merge request is merged (along with other changes)",
lambda do |context, data| lambda do |context, data|
# Make other changes on master # Make other changes on master
sha = context.project.repository.create_file( context.project.repository.commit("this_sha_apparently_does_not_matter")
context.user,
context.generate(:branch),
'content',
message: 'commit message',
branch_name: 'master')
context.project.repository.commit(sha)
context.deploy_master(context.user, context.project) context.deploy_master(context.user, context.project)
end]]) end]])
......
...@@ -5,16 +5,19 @@ require 'spec_helper' ...@@ -5,16 +5,19 @@ require 'spec_helper'
describe 'CycleAnalytics#test' do describe 'CycleAnalytics#test' do
extend CycleAnalyticsHelpers::TestGeneration extend CycleAnalyticsHelpers::TestGeneration
let(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:from_date) { 10.days.ago } let_it_be(:from_date) { 10.days.ago }
let(:user) { create(:user, :admin) } let_it_be(:user) { create(:user, :admin) }
let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:project_level) { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) }
let!(:merge_request) { create_merge_request_closing_issue(user, project, issue) }
subject { CycleAnalytics::ProjectLevel.new(project, options: { from: from_date }) } subject { project_level }
generate_cycle_analytics_spec( generate_cycle_analytics_spec(
phase: :test, phase: :test,
data_fn: lambda do |context| data_fn: lambda do |context|
issue = context.create(:issue, project: context.project) issue = context.issue
merge_request = context.create_merge_request_closing_issue(context.user, context.project, 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 = 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 } { pipeline: pipeline, issue: issue }
...@@ -22,20 +25,15 @@ describe 'CycleAnalytics#test' do ...@@ -22,20 +25,15 @@ describe 'CycleAnalytics#test' do
start_time_conditions: [["pipeline is started", -> (context, data) { data[:pipeline].run! }]], start_time_conditions: [["pipeline is started", -> (context, data) { data[:pipeline].run! }]],
end_time_conditions: [["pipeline is finished", -> (context, data) { data[:pipeline].succeed! }]], end_time_conditions: [["pipeline is finished", -> (context, data) { data[:pipeline].succeed! }]],
post_fn: -> (context, data) do post_fn: -> (context, data) do
context.merge_merge_requests_closing_issue(context.user, context.project, data[:issue])
end) end)
context "when the pipeline is for a regular merge request (that doesn't close an issue)" do context "when the pipeline is for a regular merge request (that doesn't close an issue)" do
it "returns nil" do it "returns nil" do
issue = create(:issue, project: project)
merge_request = create_merge_request_closing_issue(user, project, issue)
pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha) pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha)
pipeline.run! pipeline.run!
pipeline.succeed! pipeline.succeed!
merge_merge_requests_closing_issue(user, project, issue)
expect(subject[:test].project_median).to be_nil expect(subject[:test].project_median).to be_nil
end end
end end
...@@ -53,30 +51,22 @@ describe 'CycleAnalytics#test' do ...@@ -53,30 +51,22 @@ describe 'CycleAnalytics#test' do
context "when the pipeline is dropped (failed)" do context "when the pipeline is dropped (failed)" do
it "returns nil" do it "returns nil" do
issue = create(:issue, project: project)
merge_request = create_merge_request_closing_issue(user, project, issue)
pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha) pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha)
pipeline.run! pipeline.run!
pipeline.drop! pipeline.drop!
merge_merge_requests_closing_issue(user, project, issue)
expect(subject[:test].project_median).to be_nil expect(subject[:test].project_median).to be_nil
end end
end end
context "when the pipeline is cancelled" do context "when the pipeline is cancelled" do
it "returns nil" do it "returns nil" do
issue = create(:issue, project: project)
merge_request = create_merge_request_closing_issue(user, project, issue)
pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha) pipeline = create(:ci_pipeline, ref: "refs/heads/#{merge_request.source_branch}", sha: merge_request.diff_head_sha)
pipeline.run! pipeline.run!
pipeline.cancel! pipeline.cancel!
merge_merge_requests_closing_issue(user, project, issue)
expect(subject[:test].project_median).to be_nil expect(subject[:test].project_median).to be_nil
end end
end end
......
...@@ -27,6 +27,8 @@ module CycleAnalyticsHelpers ...@@ -27,6 +27,8 @@ module CycleAnalyticsHelpers
scenarios = combinations_of_start_time_conditions.product(combinations_of_end_time_conditions) scenarios = combinations_of_start_time_conditions.product(combinations_of_end_time_conditions)
scenarios.each do |start_time_conditions, end_time_conditions| scenarios.each do |start_time_conditions, end_time_conditions|
let_it_be(:other_project) { create(:project, :repository) }
context "start condition: #{start_time_conditions.map(&:first).to_sentence}" do context "start condition: #{start_time_conditions.map(&:first).to_sentence}" do
context "end condition: #{end_time_conditions.map(&:first).to_sentence}" do context "end condition: #{end_time_conditions.map(&:first).to_sentence}" do
it "finds the median of available durations between the two conditions", :sidekiq_might_not_need_inline do it "finds the median of available durations between the two conditions", :sidekiq_might_not_need_inline do
...@@ -56,8 +58,6 @@ module CycleAnalyticsHelpers ...@@ -56,8 +58,6 @@ module CycleAnalyticsHelpers
end end
context "when the data belongs to another project" do context "when the data belongs to another project" do
let(:other_project) { create(:project, :repository) }
it "returns nil" do it "returns nil" do
# Use a stub to "trick" the data/condition functions # Use a stub to "trick" the data/condition functions
# into using another project. This saves us from having to # into using another project. This saves us from having to
......
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