summaryrefslogtreecommitdiff
path: root/spec/models/ci/pipeline_artifact_spec.rb
blob: 3fe09f05caba6406d1eb80c22c915e7c5c974b74 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PipelineArtifact, type: :model do
  let(:coverage_report) { create(:ci_pipeline_artifact, :with_coverage_report) }

  describe 'associations' do
    it { is_expected.to belong_to(:pipeline) }
    it { is_expected.to belong_to(:project) }
  end

  it_behaves_like 'having unique enum values'

  it_behaves_like 'UpdateProjectStatistics' do
    let_it_be(:pipeline, reload: true) { create(:ci_pipeline) }

    subject { build(:ci_pipeline_artifact, :with_code_coverage_with_multiple_files, pipeline: pipeline) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:pipeline) }
    it { is_expected.to validate_presence_of(:project) }
    it { is_expected.to validate_presence_of(:file_type) }
    it { is_expected.to validate_presence_of(:file_format) }
    it { is_expected.to validate_presence_of(:size) }
    it { is_expected.to validate_presence_of(:file) }

    context 'when attributes are valid' do
      it 'returns no errors' do
        expect(coverage_report).to be_valid
      end
    end

    context 'when file_store is invalid' do
      it 'returns errors' do
        coverage_report.file_store = 0

        expect(coverage_report).to be_invalid
        expect(coverage_report.errors.full_messages).to eq(["File store is not included in the list"])
      end
    end

    context 'when size is over 10 megabytes' do
      it 'returns errors' do
        coverage_report.size = 11.megabytes

        expect(coverage_report).to be_invalid
      end
    end
  end

  describe 'file is being stored' do
    subject { create(:ci_pipeline_artifact, :with_coverage_report) }

    context 'when existing object has local store' do
      it_behaves_like 'mounted file in local store'
    end

    context 'when direct upload is enabled' do
      before do
        stub_artifacts_object_storage(Ci::PipelineArtifactUploader, direct_upload: true)
      end

      context 'when file is stored' do
        it_behaves_like 'mounted file in object store'
      end
    end

    context 'when file contains multi-byte characters' do
      let(:coverage_report_multibyte) { create(:ci_pipeline_artifact, :with_coverage_multibyte_characters) }

      it 'sets the size in bytesize' do
        expect(coverage_report_multibyte.size).to eq(14)
      end
    end
  end

  describe '.report_exists?' do
    subject(:pipeline_artifact) { Ci::PipelineArtifact.report_exists?(file_type) }

    context 'when file_type is code_coverage' do
      let(:file_type) { :code_coverage }

      context 'when pipeline artifact has a coverage report' do
        let!(:pipeline_artifact) { create(:ci_pipeline_artifact, :with_coverage_report) }

        it 'returns true' do
          expect(pipeline_artifact).to be_truthy
        end
      end

      context 'when pipeline artifact does not have a coverage report' do
        it 'returns false' do
          expect(pipeline_artifact).to be_falsey
        end
      end
    end

    context 'when file_type is code_quality_mr_diff' do
      let(:file_type) { :code_quality_mr_diff }

      context 'when pipeline artifact has a codequality mr diff report' do
        let!(:pipeline_artifact) { create(:ci_pipeline_artifact, :with_codequality_mr_diff_report) }

        it 'returns true' do
          expect(pipeline_artifact).to be_truthy
        end
      end

      context 'when pipeline artifact does not have a codequality mr diff report' do
        it 'returns false' do
          expect(pipeline_artifact).to be_falsey
        end
      end
    end

    context 'when file_type is nil' do
      let(:file_type) { nil }

      it 'returns false' do
        expect(pipeline_artifact).to be_falsey
      end
    end
  end

  describe '.find_by_file_type' do
    subject(:pipeline_artifact) { Ci::PipelineArtifact.find_by_file_type(file_type) }

    context 'when file_type is code_coverage' do
      let(:file_type) { :code_coverage }

      context 'when pipeline artifact has a coverage report' do
        let!(:coverage_report) { create(:ci_pipeline_artifact, :with_coverage_report) }

        it 'returns a pipeline artifact with a coverage report' do
          expect(pipeline_artifact.file_type).to eq('code_coverage')
        end
      end

      context 'when pipeline artifact does not have a coverage report' do
        it 'returns nil' do
          expect(pipeline_artifact).to be_nil
        end
      end
    end

    context 'when file_type is code_quality_mr_diff' do
      let(:file_type) { :code_quality_mr_diff }

      context 'when pipeline artifact has a quality report' do
        let!(:coverage_report) { create(:ci_pipeline_artifact, :with_codequality_mr_diff_report) }

        it 'returns a pipeline artifact with a quality report' do
          expect(pipeline_artifact.file_type).to eq('code_quality_mr_diff')
        end
      end

      context 'when pipeline artifact does not have a quality report' do
        it 'returns nil' do
          expect(pipeline_artifact).to be_nil
        end
      end
    end

    context 'when file_type is nil' do
      let(:file_type) { nil }

      it 'returns nil' do
        expect(pipeline_artifact).to be_nil
      end
    end
  end

  describe '#present' do
    subject(:presenter) { report.present }

    context 'when file_type is code_coverage' do
      let(:report) { coverage_report }

      it 'uses code coverage presenter' do
        expect(presenter).to be_kind_of(Ci::PipelineArtifacts::CodeCoveragePresenter)
      end
    end

    context 'when file_type is code_quality_mr_diff' do
      let(:report) { create(:ci_pipeline_artifact, :with_codequality_mr_diff_report) }

      it 'uses code codequality mr diff presenter' do
        expect(presenter).to be_kind_of(Ci::PipelineArtifacts::CodeQualityMrDiffPresenter)
      end
    end
  end
end