debian-mirror-gitlab/spec/models/issue/metrics_spec.rb

100 lines
3.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Issue::Metrics do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-09-29 09:46:39 +05:30
subject { create(:issue, project: project) }
2020-01-01 13:55:28 +05:30
describe '.for_issues' do
subject(:scope) { described_class.for_issues([issue1, issue2]) }
let(:issue1) { create(:issue) }
let(:issue2) { create(:issue) }
it 'returns metrics associated with given issues' do
create(:issue)
expect(scope).to match_array([issue1.metrics, issue2.metrics])
end
end
describe '.with_first_mention_not_earlier_than' do
subject(:scope) { described_class.with_first_mention_not_earlier_than(timestamp) }
2020-06-23 00:09:42 +05:30
let(:timestamp) { DateTime.current }
2020-01-01 13:55:28 +05:30
it 'returns metrics without mentioning in commit or with mentioning after given timestamp' do
issue1 = create(:issue)
issue2 = create(:issue).tap { |i| i.metrics.update!(first_mentioned_in_commit_at: timestamp + 1.day) }
create(:issue).tap { |i| i.metrics.update!(first_mentioned_in_commit_at: timestamp - 1.day) }
expect(scope).to match_array([issue1.metrics, issue2.metrics])
end
end
2016-09-29 09:46:39 +05:30
describe "when recording the default set of issue metrics on issue save" do
context "milestones" do
it "records the first time an issue is associated with a milestone" do
2020-06-23 00:09:42 +05:30
time = Time.current
2021-04-29 21:17:54 +05:30
travel_to(time) { subject.update!(milestone: create(:milestone, project: project)) }
2016-09-29 09:46:39 +05:30
metrics = subject.metrics
expect(metrics).to be_present
2016-11-03 12:29:30 +05:30
expect(metrics.first_associated_with_milestone_at).to be_like_time(time)
2016-09-29 09:46:39 +05:30
end
it "does not record the second time an issue is associated with a milestone" do
2020-06-23 00:09:42 +05:30
time = Time.current
2021-04-29 21:17:54 +05:30
travel_to(time) { subject.update!(milestone: create(:milestone, project: project)) }
travel_to(time + 2.hours) { subject.update!(milestone: nil) }
travel_to(time + 6.hours) { subject.update!(milestone: create(:milestone, project: project)) }
2016-09-29 09:46:39 +05:30
metrics = subject.metrics
expect(metrics).to be_present
2016-11-03 12:29:30 +05:30
expect(metrics.first_associated_with_milestone_at).to be_like_time(time)
2016-09-29 09:46:39 +05:30
end
end
context "list labels" do
it "records the first time an issue is associated with a list label" do
2019-09-30 21:07:59 +05:30
list_label = create(:list).label
2020-06-23 00:09:42 +05:30
time = Time.current
2021-04-29 21:17:54 +05:30
travel_to(time) { subject.update!(label_ids: [list_label.id]) }
2016-09-29 09:46:39 +05:30
metrics = subject.metrics
expect(metrics).to be_present
2016-11-03 12:29:30 +05:30
expect(metrics.first_added_to_board_at).to be_like_time(time)
2016-09-29 09:46:39 +05:30
end
it "does not record the second time an issue is associated with a list label" do
2020-06-23 00:09:42 +05:30
time = Time.current
2019-09-30 21:07:59 +05:30
first_list_label = create(:list).label
2021-04-29 21:17:54 +05:30
travel_to(time) { subject.update!(label_ids: [first_list_label.id]) }
2019-09-30 21:07:59 +05:30
second_list_label = create(:list).label
2021-04-29 21:17:54 +05:30
travel_to(time + 5.hours) { subject.update!(label_ids: [second_list_label.id]) }
2016-09-29 09:46:39 +05:30
metrics = subject.metrics
expect(metrics).to be_present
2016-11-03 12:29:30 +05:30
expect(metrics.first_added_to_board_at).to be_like_time(time)
2016-09-29 09:46:39 +05:30
end
end
2021-06-08 01:23:25 +05:30
describe "#record!" do
it "does not cause an N+1 query" do
label = create(:label)
subject.update!(label_ids: [label.id])
control_count = ActiveRecord::QueryRecorder.new { Issue::Metrics.find_by(issue: subject).record! }.count
additional_labels = create_list(:label, 4)
subject.update!(label_ids: additional_labels.map(&:id))
expect { Issue::Metrics.find_by(issue: subject).record! }.not_to exceed_query_limit(control_count)
end
end
2016-09-29 09:46:39 +05:30
end
end