summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/reports/test_case_spec.rb
blob: d21359368b8235c68ebcd7c4d73a6809bbc954f6 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Reports::TestCase, :aggregate_failures do
  describe '#initialize' do
    let(:test_case) { described_class.new(params) }

    context 'when required params are given' do
      let(:job) { build(:ci_build) }
      let(:params) { attributes_for(:report_test_case).merge!(job: job) }

      it 'initializes an instance', :aggregate_failures do
        expect { test_case }.not_to raise_error

        expect(test_case).to have_attributes(
          suite_name: params[:suite_name],
          name: params[:name],
          classname: params[:classname],
          file: params[:file],
          execution_time: params[:execution_time],
          status: params[:status],
          system_output: params[:system_output],
          job: params[:job]
        )

        key = "#{test_case.suite_name}_#{test_case.classname}_#{test_case.name}"
        expect(test_case.key).to eq(Digest::SHA256.hexdigest(key))
      end
    end

    shared_examples 'param is missing' do |param|
      let(:job) { build(:ci_build) }
      let(:params) { attributes_for(:report_test_case).merge!(job: job) }

      it 'raises an error' do
        params.delete(param)

        expect { test_case }.to raise_error(KeyError)
      end
    end

    context 'when suite_name is missing' do
      it_behaves_like 'param is missing', :suite_name
    end

    context 'when classname is missing' do
      it_behaves_like 'param is missing', :classname
    end

    context 'when name is missing' do
      it_behaves_like 'param is missing', :name
    end

    context 'when attachment is present' do
      let_it_be(:job) { create(:ci_build) }

      let(:attachment_test_case) { build(:report_test_case, :failed_with_attachment, job: job) }

      it "initializes the attachment if present" do
        expect(attachment_test_case.attachment).to eq("some/path.png")
      end

      it '#has_attachment?' do
        expect(attachment_test_case.has_attachment?).to be_truthy
      end

      it '#attachment_url' do
        expect(attachment_test_case.attachment_url).to match(%r{file/some/path.png})
      end
    end

    context 'when attachment is missing' do
      let(:test_case) { build(:report_test_case) }

      it '#has_attachment?' do
        expect(test_case.has_attachment?).to be_falsy
      end

      it '#attachment_url' do
        expect(test_case.attachment_url).to be_nil
      end
    end
  end

  describe '#set_recent_failures' do
    it 'sets the recent_failures information' do
      test_case = build(:report_test_case)

      test_case.set_recent_failures(1, 'master')

      expect(test_case.recent_failures).to eq(
        count: 1,
        base_branch: 'master'
      )
    end
  end
end