提交 f77c952a 编写于 作者: T Timothy Andrew

Test the `code` cycle analytics phase.

- Move the "data belongs to other project" test case into the generated
  tests, and remove the explicit tests from the `code` and `plan` phases.
上级 98c9d120
require 'spec_helper'
describe 'CycleAnalytics#code', 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_commit_referencing_issue(issue)
sha = project.repository.commit_file(user, FFaker::Product.brand, "content", "Commit for ##{issue.iid}", "master", false)
commit = project.repository.commit(sha)
commit.create_cross_references!(user)
end
def create_merge_request_closing_issue(issue, message: nil)
source_branch = FFaker::Product.brand
project.repository.add_branch(user, source_branch, 'master')
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
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) { context.create_commit_referencing_issue(data[:issue]) }]],
end_time_conditions: [["merge request that closes issue is created", -> (context, data) { context.create_merge_request_closing_issue(data[:issue]) }]])
context "when a regular merge request (that doesn't close the issue) is created" do
it "returns nil" do
5.times do
issue = create(:issue, project: project)
create_commit_referencing_issue(issue)
create_merge_request_closing_issue(issue, message: "Closes nothing")
end
expect(subject.code).to be_nil
end
end
end
......@@ -22,17 +22,4 @@ describe 'CycleAnalytics#issue', models: true do
expect(subject.issue).to be_nil
end
end
context "when the issue belongs to a different project" do
it 'returns nil' do
other_project = create(:project)
5.times do
issue = create(:issue, project: other_project)
issue.update(milestone: create(:milestone, project: other_project))
end
expect(subject.issue).to be_nil
end
end
end
......@@ -28,16 +28,4 @@ describe 'CycleAnalytics#plan', feature: true do
expect(subject.issue).to be_nil
end
end
it "does not include issues from other projects" do
other_project = create(:project)
list_label = create(:label, lists: [create(:list)])
issue = create(:issue, project: other_project)
issue.update(milestone: create(:milestone))
issue.update(label_ids: [list_label.id])
create_commit_referencing_issue(issue)
expect(subject.issue).to be_nil
end
end
......@@ -45,6 +45,37 @@ module CycleAnalyticsHelpers
median_time_difference = time_differences.sort[2]
expect(subject.send(phase)).to be_within(2).of(median_time_difference)
end
context "when the data belongs to another project" do
let(:other_project) { create(:project) }
it "returns nil" do
# Use a stub to "trick" the data/condition functions
# into using another project. This saves us from having to
# define separate data/condition functions for this particular
# test case.
allow(self).to receive(:project) { other_project }
5.times do
data = data_fn[self]
start_time = Time.now
end_time = rand(1..10).days.from_now
start_time_conditions.each do |condition_name, condition_fn|
Timecop.freeze(start_time) { condition_fn[self, data] }
end
end_time_conditions.each do |condition_name, condition_fn|
Timecop.freeze(end_time) { condition_fn[self, data] }
end
end
# Turn off the stub before checking assertions
allow(self).to receive(:project).and_call_original
expect(subject.send(phase)).to be_nil
end
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册