summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/reports/test_suite_comparer_spec.rb
blob: c44d32ddb7de277cfe00b1cab218cf4dc221dd9c (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Reports::TestSuiteComparer, :aggregate_failures do
  include TestReportsHelper

  let(:comparer) { described_class.new(name, base_suite, head_suite) }
  let(:name) { 'rspec' }
  let(:base_suite) { Gitlab::Ci::Reports::TestSuite.new(name) }
  let(:head_suite) { Gitlab::Ci::Reports::TestSuite.new(name) }
  let(:test_case_success) { create_test_case_java_success }
  let(:test_case_failed) { create_test_case_java_failed }
  let(:test_case_error) { create_test_case_java_error }

  describe '#new_failures' do
    subject { comparer.new_failures }

    context 'when head suite has a newly failed test case which does not exist in base' do
      before do
        base_suite.add_test_case(test_case_success)
        head_suite.add_test_case(test_case_failed)
      end

      it 'returns the failed test case' do
        is_expected.to eq([test_case_failed])
      end
    end

    context 'when head suite still has a failed test case which failed in base' do
      before do
        base_suite.add_test_case(test_case_failed)
        head_suite.add_test_case(test_case_failed)
      end

      it 'does not return the failed test case' do
        is_expected.to be_empty
      end
    end

    context 'when head suite has a success test case which failed in base' do
      before do
        base_suite.add_test_case(test_case_failed)
        head_suite.add_test_case(test_case_success)
      end

      it 'does not return the failed test case' do
        is_expected.to be_empty
      end
    end
  end

  describe '#existing_failures' do
    subject { comparer.existing_failures }

    context 'when head suite has a newly failed test case which does not exist in base' do
      before do
        base_suite.add_test_case(test_case_success)
        head_suite.add_test_case(test_case_failed)
      end

      it 'returns the failed test case' do
        is_expected.to be_empty
      end
    end

    context 'when head suite still has a failed test case which failed in base' do
      before do
        base_suite.add_test_case(test_case_failed)
        head_suite.add_test_case(test_case_failed)
      end

      it 'does not return the failed test case' do
        is_expected.to eq([test_case_failed])
      end
    end

    context 'when head suite has a success test case which failed in base' do
      before do
        base_suite.add_test_case(test_case_failed)
        head_suite.add_test_case(test_case_success)
      end

      it 'does not return the failed test case' do
        is_expected.to be_empty
      end
    end
  end

  describe '#resolved_failures' do
    subject { comparer.resolved_failures }

    context 'when head suite has a newly failed test case which does not exist in base' do
      before do
        base_suite.add_test_case(test_case_success)
        head_suite.add_test_case(test_case_failed)
      end

      it 'returns the failed test case' do
        is_expected.to be_empty
      end

      it 'returns the correct resolved count' do
        expect(comparer.resolved_count).to eq(0)
      end
    end

    context 'when head suite still has a failed test case which failed in base' do
      before do
        base_suite.add_test_case(test_case_failed)
        head_suite.add_test_case(test_case_failed)
      end

      it 'does not return the failed test case' do
        is_expected.to be_empty
      end

      it 'returns the correct resolved count' do
        expect(comparer.resolved_count).to eq(0)
      end
    end

    context 'when head suite has a success test case which failed in base' do
      before do
        base_suite.add_test_case(test_case_failed)
        head_suite.add_test_case(test_case_success)
      end

      it 'does not return the resolved test case' do
        is_expected.to eq([test_case_success])
      end

      it 'returns the correct resolved count' do
        expect(comparer.resolved_count).to eq(1)
      end
    end
  end

  describe '#new_errors' do
    subject { comparer.new_errors }

    context 'when head suite has a new error test case which does not exist in base' do
      before do
        base_suite.add_test_case(test_case_success)
        head_suite.add_test_case(test_case_error)
      end

      it 'returns the error test case' do
        is_expected.to eq([test_case_error])
      end
    end

    context 'when head suite still has an error test case which errored in base' do
      before do
        base_suite.add_test_case(test_case_error)
        head_suite.add_test_case(test_case_error)
      end

      it 'does not return the error test case' do
        is_expected.to be_empty
      end
    end

    context 'when head suite has a success test case which errored in base' do
      before do
        base_suite.add_test_case(test_case_error)
        head_suite.add_test_case(test_case_success)
      end

      it 'does not return the error test case' do
        is_expected.to be_empty
      end
    end
  end

  describe '#existing_errors' do
    subject { comparer.existing_errors }

    context 'when head suite has a new error test case which does not exist in base' do
      before do
        base_suite.add_test_case(test_case_success)
        head_suite.add_test_case(test_case_error)
      end

      it 'does not return the error test case' do
        is_expected.to be_empty
      end
    end

    context 'when head suite still has an error test case which errored in base' do
      before do
        base_suite.add_test_case(test_case_error)
        head_suite.add_test_case(test_case_error)
      end

      it 'returns the error test case' do
        is_expected.to eq([test_case_error])
      end
    end

    context 'when head suite has a success test case which errored in base' do
      before do
        base_suite.add_test_case(test_case_error)
        head_suite.add_test_case(test_case_success)
      end

      it 'does not return the error test case' do
        is_expected.to be_empty
      end
    end
  end

  describe '#resolved_errors' do
    subject { comparer.resolved_errors }

    context 'when head suite has a new error test case which does not exist in base' do
      before do
        base_suite.add_test_case(test_case_success)
        head_suite.add_test_case(test_case_error)
      end

      it 'does not return the error test case' do
        is_expected.to be_empty
      end

      it 'returns the correct resolved count' do
        expect(comparer.resolved_count).to eq(0)
      end
    end

    context 'when head suite still has an error test case which errored in base' do
      before do
        base_suite.add_test_case(test_case_error)
        head_suite.add_test_case(test_case_error)
      end

      it 'does not return the error test case' do
        is_expected.to be_empty
      end

      it 'returns the correct resolved count' do
        expect(comparer.resolved_count).to eq(0)
      end
    end

    context 'when head suite has a success test case which errored in base' do
      before do
        base_suite.add_test_case(test_case_error)
        head_suite.add_test_case(test_case_success)
      end

      it 'returns the resolved test case' do
        is_expected.to eq([test_case_success])
      end

      it 'returns the correct resolved count' do
        expect(comparer.resolved_count).to eq(1)
      end
    end
  end

  describe '#total_count' do
    subject { comparer.total_count }

    before do
      head_suite.add_test_case(test_case_success)
    end

    it 'returns the total test counts in head suite' do
      is_expected.to eq(1)
    end
  end

  describe '#failed_count' do
    subject { comparer.failed_count }

    context 'when there are a new failure and an existing failure' do
      let(:test_case_1_success) { create_test_case_rspec_success }
      let(:test_case_1_failed) { create_test_case_rspec_failed }
      let(:test_case_2_failed) { create_test_case_rspec_failed('case2') }

      before do
        base_suite.add_test_case(test_case_1_success)
        base_suite.add_test_case(test_case_2_failed)
        head_suite.add_test_case(test_case_1_failed)
        head_suite.add_test_case(test_case_2_failed)
      end

      it 'returns the correct count' do
        is_expected.to eq(2)
      end
    end

    context 'when there is a new failure' do
      before do
        base_suite.add_test_case(test_case_success)
        head_suite.add_test_case(test_case_failed)
      end

      it 'returns the correct count' do
        is_expected.to eq(1)
      end
    end

    context 'when there is an existing failure' do
      before do
        base_suite.add_test_case(test_case_failed)
        head_suite.add_test_case(test_case_failed)
      end

      it 'returns the correct count' do
        is_expected.to eq(1)
      end
    end
  end

  describe '#total_status' do
    subject { comparer.total_status }

    context 'when all test cases in head suite are success' do
      before do
        head_suite.add_test_case(test_case_success)
      end

      it 'returns the total status in head suite' do
        is_expected.to eq(Gitlab::Ci::Reports::TestCase::STATUS_SUCCESS)
      end
    end

    context 'when there is a failed test case in head suite' do
      before do
        head_suite.add_test_case(test_case_failed)
      end

      it 'returns the total status in head suite as failed' do
        is_expected.to eq(Gitlab::Ci::Reports::TestCase::STATUS_FAILED)
      end
    end

    context 'when there is an error test case in head suite' do
      before do
        head_suite.add_test_case(test_case_error)
      end

      it 'returns the total status in head suite as failed' do
        is_expected.to eq(Gitlab::Ci::Reports::TestCase::STATUS_FAILED)
      end
    end
  end

  describe '#limited_tests' do
    subject(:limited_tests) { comparer.limited_tests }

    context 'limits amount of tests returned' do
      before do
        stub_const("#{described_class}::DEFAULT_MAX_TESTS", 2)
        stub_const("#{described_class}::DEFAULT_MIN_TESTS", 1)
      end

      context 'prefers new over existing and resolved' do
        before do
          3.times { add_new_failure }
          3.times { add_new_error }
          3.times { add_existing_failure }
          3.times { add_existing_error }
          3.times { add_resolved_failure }
          3.times { add_resolved_error }
        end

        it 'returns 2 of each new category, and 1 of each resolved and existing' do
          expect(limited_tests.new_failures.count).to eq(2)
          expect(limited_tests.new_errors.count).to eq(2)
          expect(limited_tests.existing_failures.count).to eq(1)
          expect(limited_tests.existing_errors.count).to eq(1)
          expect(limited_tests.resolved_failures.count).to eq(1)
          expect(limited_tests.resolved_errors.count).to eq(1)
        end

        it 'does not affect the overall count' do
          expect(summary).to include(total: 18, resolved: 6, failed: 6, errored: 6)
        end
      end

      context 'prefers existing over resolved' do
        before do
          3.times { add_existing_failure }
          3.times { add_existing_error }
          3.times { add_resolved_failure }
          3.times { add_resolved_error }
        end

        it 'returns 2 of each existing category, and 1 of each resolved' do
          expect(limited_tests.new_failures.count).to eq(0)
          expect(limited_tests.new_errors.count).to eq(0)
          expect(limited_tests.existing_failures.count).to eq(2)
          expect(limited_tests.existing_errors.count).to eq(2)
          expect(limited_tests.resolved_failures.count).to eq(1)
          expect(limited_tests.resolved_errors.count).to eq(1)
        end

        it 'does not affect the overall count' do
          expect(summary).to include(total: 12, resolved: 6, failed: 3, errored: 3)
        end
      end

      context 'limits amount of resolved' do
        before do
          3.times { add_resolved_failure }
          3.times { add_resolved_error }
        end

        it 'returns 2 of each resolved category' do
          expect(limited_tests.new_failures.count).to eq(0)
          expect(limited_tests.new_errors.count).to eq(0)
          expect(limited_tests.existing_failures.count).to eq(0)
          expect(limited_tests.existing_errors.count).to eq(0)
          expect(limited_tests.resolved_failures.count).to eq(2)
          expect(limited_tests.resolved_errors.count).to eq(2)
        end

        it 'does not affect the overall count' do
          expect(summary).to include(total: 6, resolved: 6, failed: 0, errored: 0)
        end
      end
    end

    def summary
      {
        total: comparer.total_count,
        resolved: comparer.resolved_count,
        failed: comparer.failed_count,
        errored: comparer.error_count
      }
    end

    def add_new_failure
      failed_case = create_test_case_rspec_failed(SecureRandom.hex)
      head_suite.add_test_case(failed_case)
    end

    def add_new_error
      error_case = create_test_case_rspec_error(SecureRandom.hex)
      head_suite.add_test_case(error_case)
    end

    def add_existing_failure
      failed_case = create_test_case_rspec_failed(SecureRandom.hex)
      base_suite.add_test_case(failed_case)
      head_suite.add_test_case(failed_case)
    end

    def add_existing_error
      error_case = create_test_case_rspec_error(SecureRandom.hex)
      base_suite.add_test_case(error_case)
      head_suite.add_test_case(error_case)
    end

    def add_resolved_failure
      case_name = SecureRandom.hex
      failed_case = create_test_case_java_failed(case_name)
      success_case = create_test_case_java_success(case_name)
      base_suite.add_test_case(failed_case)
      head_suite.add_test_case(success_case)
    end

    def add_resolved_error
      case_name = SecureRandom.hex
      error_case = create_test_case_java_error(case_name)
      success_case = create_test_case_java_success(case_name)
      base_suite.add_test_case(error_case)
      head_suite.add_test_case(success_case)
    end
  end
end