summaryrefslogtreecommitdiff
path: root/spec/models/ci/group_spec.rb
blob: 838fa63cb1f655ba5e8a609414d5451a3060b4a8 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'spec_helper'

describe Ci::Group do
  subject do
    described_class.new('test', name: 'rspec', jobs: jobs)
  end

  let!(:jobs) { build_list(:ci_build, 1, :success) }

  it { is_expected.to include_module(StaticModel) }

  it { is_expected.to respond_to(:stage) }
  it { is_expected.to respond_to(:name) }
  it { is_expected.to respond_to(:jobs) }
  it { is_expected.to respond_to(:status) }

  describe '#size' do
    it 'returns the number of statuses in the group' do
      expect(subject.size).to eq(1)
    end
  end

  describe '#detailed_status' do
    context 'when there is only one item in the group' do
      it 'calls the status from the object itself' do
        expect(jobs.first).to receive(:detailed_status)

        expect(subject.detailed_status(double(:user)))
      end
    end

    context 'when there are more than one commit status in the group' do
      let(:jobs) do
        [create(:ci_build, :failed),
         create(:ci_build, :success)]
      end

      it 'fabricates a new detailed status object' do
        expect(subject.detailed_status(double(:user)))
          .to be_a(Gitlab::Ci::Status::Failed)
      end
    end
  end

  describe '.fabricate' do
    let(:pipeline) { create(:ci_empty_pipeline) }
    let(:stage) { create(:ci_stage_entity, pipeline: pipeline) }

    before do
      create_build(:ci_build, name: 'rspec 0 2')
      create_build(:ci_build, name: 'rspec 0 1')
      create_build(:ci_build, name: 'spinach 0 1')
      create_build(:commit_status, name: 'aaaaa')
    end

    it 'returns an array of three groups' do
      expect(stage.groups).to be_a Array
      expect(stage.groups).to all(be_a described_class)
      expect(stage.groups.size).to eq 3
    end

    it 'returns groups with correctly ordered statuses' do
      expect(stage.groups.first.jobs.map(&:name))
        .to eq ['aaaaa']
      expect(stage.groups.second.jobs.map(&:name))
        .to eq ['rspec 0 1', 'rspec 0 2']
      expect(stage.groups.third.jobs.map(&:name))
        .to eq ['spinach 0 1']
    end

    it 'returns groups with correct names' do
      expect(stage.groups.map(&:name))
        .to eq %w[aaaaa rspec spinach]
    end

    context 'when a name is nil on legacy pipelines' do
      before do
        pipeline.builds.first.update_attribute(:name, nil)
      end

      it 'returns an array of three groups' do
        expect(stage.groups.map(&:name))
          .to eq ['', 'aaaaa', 'rspec', 'spinach']
      end
    end

    def create_build(type, status: 'success', **opts)
      create(type, pipeline: pipeline,
                   stage: stage.name,
                   status: status,
                   stage_id: stage.id,
                   **opts)
    end
  end
end