Welcome to mirror list, hosted at ThFree Co, Russian Federation.

backfill_projects_with_coverage_spec.rb « background_migration « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4a65ecf8c75567083be5c14db3eddc9ffb9fa04b (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::BackfillProjectsWithCoverage,
               :suppress_gitlab_schemas_validate_connection, schema: 20210818185845 do
  let(:projects) { table(:projects) }
  let(:project_ci_feature_usages) { table(:project_ci_feature_usages) }
  let(:ci_pipelines) { table(:ci_pipelines) }
  let(:ci_daily_build_group_report_results) { table(:ci_daily_build_group_report_results) }
  let(:group) { table(:namespaces).create!(name: 'user', path: 'user') }
  let(:project_1) { projects.create!(namespace_id: group.id) }
  let(:project_2) { projects.create!(namespace_id: group.id) }
  let(:pipeline_1) { ci_pipelines.create!(project_id: project_1.id, source: 13) }
  let(:pipeline_2) { ci_pipelines.create!(project_id: project_1.id, source: 13) }
  let(:pipeline_3) { ci_pipelines.create!(project_id: project_2.id, source: 13) }
  let(:pipeline_4) { ci_pipelines.create!(project_id: project_2.id, source: 13) }

  subject { described_class.new }

  describe '#perform' do
    before do
      ci_daily_build_group_report_results.create!(
        id: 1,
        project_id: project_1.id,
        date: 4.days.ago,
        last_pipeline_id: pipeline_1.id,
        ref_path: 'main',
        group_name: 'rspec',
        data: { coverage: 95.0 },
        default_branch: true,
        group_id: group.id
      )

      ci_daily_build_group_report_results.create!(
        id: 2,
        project_id: project_1.id,
        date: 3.days.ago,
        last_pipeline_id: pipeline_2.id,
        ref_path: 'main',
        group_name: 'rspec',
        data: { coverage: 95.0 },
        default_branch: true,
        group_id: group.id
      )

      ci_daily_build_group_report_results.create!(
        id: 3,
        project_id: project_2.id,
        date: 2.days.ago,
        last_pipeline_id: pipeline_3.id,
        ref_path: 'main',
        group_name: 'rspec',
        data: { coverage: 95.0 },
        default_branch: true,
        group_id: group.id
      )

      ci_daily_build_group_report_results.create!(
        id: 4,
        project_id: project_2.id,
        date: 1.day.ago,
        last_pipeline_id: pipeline_4.id,
        ref_path: 'test_branch',
        group_name: 'rspec',
        data: { coverage: 95.0 },
        default_branch: false,
        group_id: group.id
      )

      stub_const("#{described_class}::INSERT_DELAY_SECONDS", 0)
    end

    it 'creates entries per project and default_branch combination in the given range', :aggregate_failures do
      subject.perform(1, 4, 2)

      entries = project_ci_feature_usages.order('project_id ASC, default_branch DESC')

      expect(entries.count).to eq(3)
      expect(entries[0]).to have_attributes(project_id: project_1.id, feature: 1, default_branch: true)
      expect(entries[1]).to have_attributes(project_id: project_2.id, feature: 1, default_branch: true)
      expect(entries[2]).to have_attributes(project_id: project_2.id, feature: 1, default_branch: false)
    end

    context 'when an entry for the project and default branch combination already exists' do
      before do
        subject.perform(1, 4, 2)
      end

      it 'does not create a new entry' do
        expect { subject.perform(1, 4, 2) }.not_to change { project_ci_feature_usages.count }
      end
    end
  end
end