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

schedule_async_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: 59e03db07c0bcaf411cc4545af15f3bb7b55f878 (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
# frozen_string_literal: true

require 'rubocop_spec_helper'

require_relative '../../../../rubocop/cop/migration/schedule_async'

RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do
  let(:cop) { described_class.new }
  let(:source) do
    <<~SOURCE
      def up
        BackgroundMigrationWorker.perform_async(ClazzName, "Bar", "Baz")
      end
    SOURCE
  end

  shared_examples 'a disabled cop' do
    it 'does not register any offenses' do
      expect_no_offenses(source)
    end
  end

  context 'outside of a migration' do
    it_behaves_like 'a disabled cop'
  end

  context 'in a migration' do
    before do
      allow(cop).to receive(:in_migration?).and_return(true)
    end

    context 'in an old migration' do
      before do
        allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE - 5)
      end

      it_behaves_like 'a disabled cop'
    end

    context 'that is recent' do
      before do
        allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE + 5)
      end

      context 'BackgroundMigrationWorker.perform_async' do
        it 'adds an offense when calling `BackgroundMigrationWorker.peform_async`' do
          expect_offense(<<~RUBY)
            def up
              BackgroundMigrationWorker.perform_async(ClazzName, "Bar", "Baz")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
            end
          RUBY
        end
      end

      context 'CiDatabaseWorker.perform_async' do
        it 'adds an offense when calling `CiDatabaseWorker.peform_async`' do
          expect_offense(<<~RUBY)
            def up
              CiDatabaseWorker.perform_async(ClazzName, "Bar", "Baz")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
            end
          RUBY
        end
      end

      context 'BackgroundMigrationWorker.perform_in' do
        it 'adds an offense' do
          expect_offense(<<~RUBY)
            def up
              BackgroundMigrationWorker
              ^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
                .perform_in(delay, ClazzName, "Bar", "Baz")
            end
          RUBY
        end
      end

      context 'CiDatabaseWorker.perform_in' do
        it 'adds an offense' do
          expect_offense(<<~RUBY)
            def up
              CiDatabaseWorker
              ^^^^^^^^^^^^^^^^ Don't call [...]
                .perform_in(delay, ClazzName, "Bar", "Baz")
            end
          RUBY
        end
      end

      context 'BackgroundMigrationWorker.bulk_perform_async' do
        it 'adds an offense' do
          expect_offense(<<~RUBY)
            def up
              BackgroundMigrationWorker
              ^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
                .bulk_perform_async(jobs)
            end
          RUBY
        end
      end

      context 'CiDatabaseWorker.bulk_perform_async' do
        it 'adds an offense' do
          expect_offense(<<~RUBY)
            def up
              BackgroundMigration::CiDatabaseWorker
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
                .bulk_perform_async(jobs)
            end
          RUBY
        end
      end

      context 'BackgroundMigrationWorker.bulk_perform_in' do
        it 'adds an offense' do
          expect_offense(<<~RUBY)
            def up
              ::BackgroundMigrationWorker
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
                .bulk_perform_in(5.minutes, jobs)
            end
          RUBY
        end
      end

      context 'CiDatabaseWorker.bulk_perform_in' do
        it 'adds an offense' do
          expect_offense(<<~RUBY)
            def up
              ::BackgroundMigration::CiDatabaseWorker
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...]
                .bulk_perform_in(5.minutes, jobs)
            end
          RUBY
        end
      end
    end
  end
end