diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2016-09-02 17:33:32 +0530 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2016-09-02 17:43:03 +0530 |
commit | 98c9d1207708cd7d9fc37167e65e8a26b668aaa7 (patch) | |
tree | 21e4f509596cda4cd5edfc680457345f9a2af405 /spec/models/cycle_analytics | |
parent | 0f748605941248290d258b86408c26f33c2ff7b4 (diff) | |
download | gitlab-ce-98c9d1207708cd7d9fc37167e65e8a26b668aaa7.tar.gz |
Refactor cycle analytics specs.
1. Generalise the specs that will be common across all cycle analytics
phases.
2. Rewrite specs `issue` and `plan` to use this abstracted testing
strategy.
3. Specs that are specific to a given phase, or unwieldy to test in an
abstracted manner, are added to each phase's spec.
Diffstat (limited to 'spec/models/cycle_analytics')
-rw-r--r-- | spec/models/cycle_analytics/issue_spec.rb | 85 | ||||
-rw-r--r-- | spec/models/cycle_analytics/plan_spec.rb | 132 |
2 files changed, 37 insertions, 180 deletions
diff --git a/spec/models/cycle_analytics/issue_spec.rb b/spec/models/cycle_analytics/issue_spec.rb index a176a15b2b7..ef4de184d64 100644 --- a/spec/models/cycle_analytics/issue_spec.rb +++ b/spec/models/cycle_analytics/issue_spec.rb @@ -5,44 +5,14 @@ describe 'CycleAnalytics#issue', models: true do let(:from_date) { 10.days.ago } subject { CycleAnalytics.new(project, from: from_date) } - context "when a milestone is added to the issue" do - it "calculates the median of available durations (between issue creation and milestone addition)" do - time_differences = Array.new(5) do - start_time = Time.now - end_time = rand(1..10).days.from_now - - milestone = create(:milestone, project: project) - issue = Timecop.freeze(start_time) { create(:issue, project: project) } - Timecop.freeze(end_time) { issue.update(milestone: milestone) } - - end_time - start_time - end - - median_time_difference = time_differences.sort[2] - expect(subject.issue).to eq(median_time_difference) - end - end - - context "when a label is added to the issue" do - context "when the label is a list-label" do - it "calculates the median of available durations (between issue creation and label addition)" do - time_differences = Array.new(5) do - start_time = Time.now - end_time = rand(1..10).days.from_now - - list_label = create(:label, lists: [create(:list)]) - issue = Timecop.freeze(start_time) { create(:issue, project: project) } - Timecop.freeze(end_time) { issue.update(label_ids: [list_label.id]) } - - end_time - start_time - end - - median_time_difference = time_differences.sort[2] - expect(subject.issue).to eq(median_time_difference) - end - end - - it "does not make a calculation for regular labels" do + 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) { data[:issue].update(milestone: context.create(:milestone, project: context.project)) if data[:issue].persisted? }], + ["list label added to issue", -> (context, data) { data[:issue].update(label_ids: [context.create(:label, lists: [context.create(:list)]).id]) if data[:issue].persisted? }]]) + + context "when a regular label (instead of a list label) is added to the issue" do + it "returns nil" do 5.times do regular_label = create(:label) issue = create(:issue, project: project) @@ -53,39 +23,14 @@ describe 'CycleAnalytics#issue', models: true do end end - context "when a milestone and list-label are both added to the issue" do - it "calculates the median of available durations (between issue creation and milestone addition)" do - start_time = Time.now - milestone_add_time = rand(1..10).days.from_now - list_label_add_time = rand(1..10).days.from_now - - milestone = create(:milestone, project: project) - list_label = create(:label, lists: [create(:list)]) - issue = Timecop.freeze(start_time) { create(:issue, project: project) } - Timecop.freeze(milestone_add_time) { issue.update(milestone: milestone) } - Timecop.freeze(list_label_add_time) { issue.update(label_ids: [list_label.id]) } - - expect(subject.issue).to eq(milestone_add_time - start_time) - end - - it "does not include issues from other projects" do - milestone = create(:milestone, project: project) - list_label = create(:label, lists: [create(:list)]) - issue = create(:issue) - issue.update(milestone: milestone) - issue.update(label_ids: [list_label.id]) + context "when the issue belongs to a different project" do + it 'returns nil' do + other_project = create(:project) - expect(subject.issue).to be_nil - end - - it "excludes issues created before the 'from' date" do - before_from_date = from_date - 5.days - - milestone = create(:milestone, project: project) - list_label = create(:label, lists: [create(:list)]) - issue = Timecop.freeze(before_from_date) { create(:issue, project: project)} - issue.update(milestone: milestone) - issue.update(label_ids: [list_label.id]) + 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 diff --git a/spec/models/cycle_analytics/plan_spec.rb b/spec/models/cycle_analytics/plan_spec.rb index e3eeb0e505c..a5d4128d938 100644 --- a/spec/models/cycle_analytics/plan_spec.rb +++ b/spec/models/cycle_analytics/plan_spec.rb @@ -6,126 +6,38 @@ describe 'CycleAnalytics#plan', feature: true do let(:user) { create(:user, :admin) } subject { CycleAnalytics.new(project, from: from_date) } - def create_commit_referencing_issue(issue, time: Time.now) - sha = Timecop.freeze(time) { project.repository.commit_file(user, FFaker::Product.brand, "content", "Commit for ##{issue.iid}", "master", false) } + 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! end - context "when a milestone is added to the issue" do - context "when the issue is mentioned in a commit" do - it "calculates the median of available durations between the two" do - time_differences = Array.new(5) do - start_time = Time.now - end_time = rand(1..10).days.from_now + generate_cycle_analytics_spec(phase: :plan, + data_fn: -> (context) { { issue: context.create(:issue, project: context.project) } }, + start_time_conditions: [["issue associated with a milestone", -> (context, data) { data[:issue].update(milestone: context.create(:milestone, project: context.project)) }], + ["list label added to issue", -> (context, data) { data[:issue].update(label_ids: [context.create(:label, lists: [context.create(:list)]).id]) }]], + end_time_conditions: [["issue mentioned in a commit", -> (context, data) { context.create_commit_referencing_issue(data[:issue]) }]]) - milestone = create(:milestone, project: project) - issue = create(:issue, project: project) + context "when a regular label (instead of a list label) is added to the issue" do + it "returns nil" do + label = create(:label) + issue = create(:issue, project: project) + issue.update(label_ids: [label.id]) + create_commit_referencing_issue(issue) - Timecop.freeze(start_time) { issue.update(milestone: milestone) } - create_commit_referencing_issue(issue, time: end_time) - - end_time - start_time - end - - median_time_difference = time_differences.sort[2] - - # Use `be_within` to account for time lost between Rails invoking CLI git - # and the commit being created, which Timecop can't freeze. - expect(subject.plan).to be_within(2).of(median_time_difference) - end - end - end - - context "when a label is added to the issue" do - context "when the issue is mentioned in a commit" do - context "when the label is a list-label" do - it "calculates the median of available durations between the two" do - time_differences = Array.new(5) do - start_time = Time.now - end_time = rand(1..10).days.from_now - - issue = create(:issue, project: project) - list_label = create(:label, lists: [create(:list)]) - - Timecop.freeze(start_time) { issue.update(label_ids: [list_label.id]) } - create_commit_referencing_issue(issue, time: end_time) - - end_time - start_time - end - - median_time_difference = time_differences.sort[2] - - # Use `be_within` to account for time lost between Rails invoking CLI git - # and the commit being created, which Timecop can't freeze. - expect(subject.plan).to be_within(2).of(median_time_difference) - end - end - - it "does not make a calculation for regular labels" do - 5.times do - regular_label = create(:label) - issue = create(:issue, project: project) - issue.update(label_ids: [regular_label.id]) - - create_commit_referencing_issue(issue) - end - - expect(subject.plan).to be_nil - end + expect(subject.issue).to be_nil end end - context "when a milestone and list-label are both added to the issue" do - context "when the issue is mentioned in a commit" do - it "calculates the median of available durations between the two (using milestone addition as the 'start_time')" do - time_differences = Array.new(5) do - label_addition_time = Time.now - milestone_addition_time = rand(2..12).hours.from_now - end_time = rand(1..10).days.from_now - - issue = create(:issue, project: project) - milestone = create(:milestone, project: project) - list_label = create(:label, lists: [create(:list)]) - - Timecop.freeze(label_addition_time) { issue.update(label_ids: [list_label.id]) } - Timecop.freeze(milestone_addition_time) { issue.update(milestone: milestone) } - create_commit_referencing_issue(issue, time: end_time) - - end_time - milestone_addition_time - end - - median_time_difference = time_differences.sort[2] + it "does not include issues from other projects" do + other_project = create(:project) - # Use `be_within` to account for time lost between Rails invoking CLI git - # and the commit being created, which Timecop can't freeze. - expect(subject.plan).to be_within(2).of(median_time_difference) - end + 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) - 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 - - it "excludes issues created before the 'from' date" do - before_from_date = from_date - 5.days - - milestone = create(:milestone, project: project) - list_label = create(:label, lists: [create(:list)]) - issue = Timecop.freeze(before_from_date) { create(:issue, project: project)} - issue.update(milestone: milestone) - issue.update(label_ids: [list_label.id]) - create_commit_referencing_issue(issue) - - expect(subject.issue).to be_nil - end - end + expect(subject.issue).to be_nil end end |