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

unfinished_dependencies_spec.rb « migration « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cac4887185618502ef686ca4200650a18e1f5c6a (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
# frozen_string_literal: true

require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/migration/unfinished_dependencies'

RSpec.describe RuboCop::Cop::Migration::UnfinishedDependencies, feature_category: :database do
  let(:version) { 20230307160250 }

  let(:migration) do
    <<~RUBY
      class TestMigration < Gitlab::Database::Migration[2.1]
        def perform; end
      end
    RUBY
  end

  before do
    allow(cop).to receive(:in_migration?).and_return(true)

    allow(cop).to receive(:version).and_return(version)
  end

  shared_examples 'migration with rubocop offense' do
    it 'registers an offense' do
      expect_offense(migration)
    end
  end

  shared_examples 'migration without any rubocop offense' do
    it 'does not register any offense' do
      expect_no_offenses(migration)
    end
  end

  context 'without any dependent batched background migrations' do
    it_behaves_like 'migration without any rubocop offense'
  end

  context 'with dependent batched background migrations' do
    let(:dependent_migration_versions) { [20230307160240] }

    let(:migration) do
      <<~RUBY
        class TestMigration < Gitlab::Database::Migration[2.1]
          DEPENDENT_BATCHED_BACKGROUND_MIGRATIONS = #{dependent_migration_versions}

          def perform; end
        end
      RUBY
    end

    context 'with unfinished dependent migration' do
      before do
        allow(cop).to receive(:fetch_finalized_by)
          .with(dependent_migration_versions.first)
          .and_return(nil)
      end

      it_behaves_like 'migration with rubocop offense' do
        let(:migration) do
          <<~RUBY
            class TestMigration < Gitlab::Database::Migration[2.1]
              DEPENDENT_BATCHED_BACKGROUND_MIGRATIONS = #{dependent_migration_versions}
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(described_class::NOT_FINALIZED_MSG, version: dependent_migration_versions.first)}

              def perform; end
            end
          RUBY
        end
      end
    end

    context 'with incorrectly finalized dependent migration' do
      let(:dependent_migration_versions) { [20230307160240, 20230307160230] }

      before do
        allow(cop).to receive(:fetch_finalized_by)
          .with(dependent_migration_versions.first)
          .and_return(version - 10)

        allow(cop).to receive(:fetch_finalized_by)
          .with(dependent_migration_versions.last)
          .and_return(version + 10)
      end

      it_behaves_like 'migration with rubocop offense' do
        let(:migration) do
          <<~RUBY
            class TestMigration < Gitlab::Database::Migration[2.1]
              DEPENDENT_BATCHED_BACKGROUND_MIGRATIONS = #{dependent_migration_versions}
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format(described_class::FINALIZED_BY_LATER_MIGRATION_MSG, version: dependent_migration_versions.last)}

              def perform; end
            end
          RUBY
        end
      end
    end

    context 'with properly finalized dependent background migrations' do
      before do
        allow_next_instance_of(RuboCop::BatchedBackgroundMigrations) do |bbms|
          allow(bbms).to receive(:finalized_by).and_return(version - 5)
        end
      end

      it_behaves_like 'migration without any rubocop offense'
    end
  end

  context 'for non migrations' do
    before do
      allow(cop).to receive(:in_migration?).and_return(false)
    end

    it_behaves_like 'migration without any rubocop offense'
  end
end