summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/danger/weightage/reviewers_spec.rb
blob: cca81f4d9b534271e1398f20ac54550945bb8e00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

require 'gitlab/danger/weightage/reviewers'

RSpec.describe Gitlab::Danger::Weightage::Reviewers do
  let(:multiplier) { Gitlab::Danger::Weightage::CAPACITY_MULTIPLIER }
  let(:regular_reviewer) { double('Teammate', hungry: false, reduced_capacity: false) }
  let(:hungry_reviewer) { double('Teammate', hungry: true, reduced_capacity: false) }
  let(:reduced_capacity_reviewer) { double('Teammate', hungry: false, reduced_capacity: true) }
  let(:reviewers) do
    [
      hungry_reviewer,
      regular_reviewer,
      reduced_capacity_reviewer
    ]
  end

  let(:regular_traintainer) { double('Teammate', hungry: false, reduced_capacity: false) }
  let(:hungry_traintainer) { double('Teammate', hungry: true, reduced_capacity: false) }
  let(:reduced_capacity_traintainer) { double('Teammate', hungry: false, reduced_capacity: true) }
  let(:traintainers) do
    [
      hungry_traintainer,
      regular_traintainer,
      reduced_capacity_traintainer
    ]
  end

  let(:hungry_reviewer_count) { Gitlab::Danger::Weightage::BASE_REVIEWER_WEIGHT * multiplier + described_class::DEFAULT_REVIEWER_WEIGHT }
  let(:hungry_traintainer_count) { described_class::TRAINTAINER_WEIGHT * multiplier + described_class::DEFAULT_REVIEWER_WEIGHT }
  let(:reviewer_count) { Gitlab::Danger::Weightage::BASE_REVIEWER_WEIGHT * multiplier }
  let(:traintainer_count) { Gitlab::Danger::Weightage::BASE_REVIEWER_WEIGHT * described_class::TRAINTAINER_WEIGHT * multiplier }
  let(:reduced_capacity_reviewer_count) { Gitlab::Danger::Weightage::BASE_REVIEWER_WEIGHT }
  let(:reduced_capacity_traintainer_count) { described_class::TRAINTAINER_WEIGHT }

  subject(:weighted_reviewers) { described_class.new(reviewers, traintainers).execute }

  describe '#execute', :aggregate_failures do
    it 'weights the reviewers overall' do
      reviewers_count = hungry_reviewer_count + reviewer_count + reduced_capacity_reviewer_count
      traintainers_count = hungry_traintainer_count + traintainer_count + reduced_capacity_traintainer_count

      expect(weighted_reviewers.count).to eq reviewers_count + traintainers_count
    end

    it 'has total count of hungry reviewers and traintainers' do
      expect(weighted_reviewers.count(&:hungry)).to eq hungry_reviewer_count + hungry_traintainer_count
      expect(weighted_reviewers.count { |r| r.object_id == hungry_reviewer.object_id }).to eq hungry_reviewer_count
      expect(weighted_reviewers.count { |r| r.object_id == hungry_traintainer.object_id }).to eq hungry_traintainer_count
    end

    it 'has total count of regular reviewers and traintainers' do
      expect(weighted_reviewers.count { |r| r.object_id == regular_reviewer.object_id }).to eq reviewer_count
      expect(weighted_reviewers.count { |r| r.object_id == regular_traintainer.object_id }).to eq traintainer_count
    end

    it 'has count of reduced capacity reviewers' do
      expect(weighted_reviewers.count(&:reduced_capacity)).to eq reduced_capacity_reviewer_count + reduced_capacity_traintainer_count
      expect(weighted_reviewers.count { |r| r.object_id == reduced_capacity_reviewer.object_id }).to eq reduced_capacity_reviewer_count
      expect(weighted_reviewers.count { |r| r.object_id == reduced_capacity_traintainer.object_id }).to eq reduced_capacity_traintainer_count
    end
  end
end