Test the `production` cycle analytics phase.
Add a `before_end_fn` option to the code that generates cycle analytics specs. `before_end_fn` is called before the end conditions are. Used for data setup that needs to be called after the start conditions and before the end conditions.
This commit is contained in:
parent
dd112ef117
commit
de483c6834
|
@ -0,0 +1,81 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'CycleAnalytics#production', feature: true do
|
||||
let(:project) { create(:project) }
|
||||
let(:from_date) { 10.days.ago }
|
||||
let(:user) { create(:user, :admin) }
|
||||
subject { CycleAnalytics.new(project, from: from_date) }
|
||||
|
||||
def create_merge_request_closing_issue(issue, message: nil)
|
||||
source_branch = random_git_name
|
||||
project.repository.add_branch(user, source_branch, 'master')
|
||||
sha = project.repository.commit_file(user, random_git_name, "content", "commit message", source_branch, false)
|
||||
project.repository.commit(sha)
|
||||
|
||||
opts = {
|
||||
title: 'Awesome merge_request',
|
||||
description: message || "Fixes #{issue.to_reference}",
|
||||
source_branch: source_branch,
|
||||
target_branch: 'master'
|
||||
}
|
||||
|
||||
MergeRequests::CreateService.new(project, user, opts).execute
|
||||
end
|
||||
|
||||
def merge_merge_requests_closing_issue(issue)
|
||||
merge_requests = issue.closed_by_merge_requests
|
||||
merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user).execute(merge_request) }
|
||||
end
|
||||
|
||||
def deploy_master(environment: 'production')
|
||||
CreateDeploymentService.new(project, user, {
|
||||
environment: environment,
|
||||
ref: 'master',
|
||||
tag: false,
|
||||
sha: project.repository.commit('master').sha
|
||||
}).execute
|
||||
end
|
||||
|
||||
generate_cycle_analytics_spec(phase: :production,
|
||||
data_fn: -> (context) { { issue: context.build(:issue, project: context.project) } },
|
||||
start_time_conditions: [["issue is created", -> (context, data) { data[:issue].save }]],
|
||||
before_end_fn: lambda do |context, data|
|
||||
context.create_merge_request_closing_issue(data[:issue])
|
||||
context.merge_merge_requests_closing_issue(data[:issue])
|
||||
end,
|
||||
end_time_conditions:
|
||||
[["merge request that closes issue is deployed to production", -> (context, data) { context.deploy_master }],
|
||||
["production deploy happens after merge request is merged (along with other changes)",
|
||||
lambda do |context, data|
|
||||
# Make other changes on master
|
||||
sha = context.project.repository.commit_file(context.user, context.random_git_name, "content", "commit message", 'master', false)
|
||||
context.project.repository.commit(sha)
|
||||
|
||||
context.deploy_master
|
||||
end]])
|
||||
|
||||
context "when a regular merge request (that doesn't close the issue) is merged and deployed" do
|
||||
it "returns nil" do
|
||||
5.times do
|
||||
merge_request = create(:merge_request)
|
||||
MergeRequests::MergeService.new(project, user).execute(merge_request)
|
||||
deploy_master
|
||||
end
|
||||
|
||||
expect(subject.production).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when the deployment happens to a non-production environment" do
|
||||
it "returns nil" do
|
||||
5.times do
|
||||
issue = create(:issue, project: project)
|
||||
merge_request = create_merge_request_closing_issue(issue)
|
||||
MergeRequests::MergeService.new(project, user).execute(merge_request)
|
||||
deploy_master(environment: 'staging')
|
||||
end
|
||||
|
||||
expect(subject.production).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
|
@ -16,8 +16,9 @@ module CycleAnalyticsHelpers
|
|||
# end_time_conditions: An array of `conditions`. Each condition is an tuple of `condition_name` and `condition_fn`. `condition_fn` is called with
|
||||
# `context` (no lexical scope, so need to do `context.create` for factories, for example) and `data` (from the `data_fn`).
|
||||
# Each `condition_fn` is expected to implement a case which consitutes the end of the given cycle analytics phase.
|
||||
# before_end_fn: This function is run before calling the end time conditions. Used for setup that needs to be run between the start and end conditions.
|
||||
|
||||
def generate_cycle_analytics_spec(phase:, data_fn:, start_time_conditions:, end_time_conditions:)
|
||||
def generate_cycle_analytics_spec(phase:, data_fn:, start_time_conditions:, end_time_conditions:, before_end_fn: nil)
|
||||
combinations_of_start_time_conditions = (1..start_time_conditions.size).flat_map { |size| start_time_conditions.combination(size).to_a }
|
||||
combinations_of_end_time_conditions = (1..end_time_conditions.size).flat_map { |size| end_time_conditions.combination(size).to_a }
|
||||
|
||||
|
@ -35,6 +36,9 @@ module CycleAnalyticsHelpers
|
|||
Timecop.freeze(start_time) { condition_fn[self, data] }
|
||||
end
|
||||
|
||||
# Run `before_end_fn` at the midpoint between `start_time` and `end_time`
|
||||
Timecop.freeze(start_time + (end_time - start_time)/2) { before_end_fn[self, data] } if before_end_fn
|
||||
|
||||
end_time_conditions.each do |condition_name, condition_fn|
|
||||
Timecop.freeze(end_time) { condition_fn[self, data] }
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue