summaryrefslogtreecommitdiff
path: root/spec/services/ci/test_cases_service_spec.rb
blob: b61d308640f6fa659e6bc27ba6fa18fbdb17f9e5 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::TestCasesService, :aggregate_failures do
  describe '#execute' do
    subject(:execute_service) { described_class.new.execute(build) }

    context 'when build has test reports' do
      let(:build) { create(:ci_build, :success, :test_reports) } # The test report has 2 test case failures

      it 'creates test case failures records' do
        execute_service

        expect(Ci::TestCase.count).to eq(2)
        expect(Ci::TestCaseFailure.count).to eq(2)
      end

      context 'when feature flag for test failure history is disabled' do
        before do
          stub_feature_flags(test_failure_history: false)
        end

        it 'does not persist data' do
          execute_service

          expect(Ci::TestCase.count).to eq(0)
          expect(Ci::TestCaseFailure.count).to eq(0)
        end
      end

      context 'when build is not for the default branch' do
        before do
          build.update_column(:ref, 'new-feature')
        end

        it 'does not persist data' do
          execute_service

          expect(Ci::TestCase.count).to eq(0)
          expect(Ci::TestCaseFailure.count).to eq(0)
        end
      end

      context 'when test failure data have already been persisted with the same exact attributes' do
        before do
          execute_service
        end

        it 'does not fail but does not persist new data' do
          expect { described_class.new.execute(build) }.not_to raise_error

          expect(Ci::TestCase.count).to eq(2)
          expect(Ci::TestCaseFailure.count).to eq(2)
        end
      end

      context 'when test failure data have duplicates within the same payload (happens when the JUnit report has duplicate test case names but have different failures)' do
        let(:build) { create(:ci_build, :success, :test_reports_with_duplicate_failed_test_names) } # The test report has 2 test case failures but with the same test case keys

        it 'does not fail but does not persist duplicate data' do
          expect { described_class.new.execute(build) }.not_to raise_error

          expect(Ci::TestCase.count).to eq(1)
          expect(Ci::TestCaseFailure.count).to eq(1)
        end
      end

      context 'when number of failed test cases exceed the limit' do
        before do
          stub_const("#{described_class.name}::MAX_TRACKABLE_FAILURES", 1)
        end

        it 'does not persist data' do
          execute_service

          expect(Ci::TestCase.count).to eq(0)
          expect(Ci::TestCaseFailure.count).to eq(0)
        end
      end
    end

    context 'when build has no test reports' do
      let(:build) { create(:ci_build, :running) }

      it 'does not persist data' do
        execute_service

        expect(Ci::TestCase.count).to eq(0)
        expect(Ci::TestCaseFailure.count).to eq(0)
      end
    end
  end
end