diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2016-09-07 08:05:01 +0300 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2016-09-07 08:09:46 +0300 |
commit | f77c952ae71ec03c9691e44479f0e4a7fc5ef8e8 (patch) | |
tree | 484d1a631a52ea9fc4d8930a1fd1794d98ad2e97 | |
parent | 98c9d1207708cd7d9fc37167e65e8a26b668aaa7 (diff) |
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.
-rw-r--r-- | spec/models/cycle_analytics/code_spec.rb | 46 | ||||
-rw-r--r-- | spec/models/cycle_analytics/issue_spec.rb | 13 | ||||
-rw-r--r-- | spec/models/cycle_analytics/plan_spec.rb | 12 | ||||
-rw-r--r-- | spec/support/cycle_analytics_helpers.rb | 31 |
4 files changed, 77 insertions, 25 deletions
diff --git a/spec/models/cycle_analytics/code_spec.rb b/spec/models/cycle_analytics/code_spec.rb new file mode 100644 index 00000000000..1eaccb5e937 --- /dev/null +++ b/spec/models/cycle_analytics/code_spec.rb @@ -0,0 +1,46 @@ +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 diff --git a/spec/models/cycle_analytics/issue_spec.rb b/spec/models/cycle_analytics/issue_spec.rb index ef4de184d64..06715498d26 100644 --- a/spec/models/cycle_analytics/issue_spec.rb +++ b/spec/models/cycle_analytics/issue_spec.rb @@ -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 diff --git a/spec/models/cycle_analytics/plan_spec.rb b/spec/models/cycle_analytics/plan_spec.rb index a5d4128d938..4cae87e9d89 100644 --- a/spec/models/cycle_analytics/plan_spec.rb +++ b/spec/models/cycle_analytics/plan_spec.rb @@ -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 diff --git a/spec/support/cycle_analytics_helpers.rb b/spec/support/cycle_analytics_helpers.rb index ba18b390c1a..b2ca42d7cce 100644 --- a/spec/support/cycle_analytics_helpers.rb +++ b/spec/support/cycle_analytics_helpers.rb @@ -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 |