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

background_migration_base_class_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: 8cc85ac692ca7c36a88a55f9fd0cea365cf8110c (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
# frozen_string_literal: true

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

RSpec.describe RuboCop::Cop::Migration::BackgroundMigrationBaseClass do
  context 'when the migration class inherits from BatchedMigrationJob' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            class MyJob < BatchedMigrationJob
              def perform
                connection.execute("select 1")
              end
            end
          end
        end
      RUBY
    end
  end

  context 'when the migration class inherits from the namespaced BatchedMigrationJob' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            class MyJob < Gitlab::BackgroundMigration::BatchedMigrationJob
              def perform
                connection.execute("select 1")
              end
            end
          end
        end
      RUBY
    end
  end

  context 'when the migration class inherits from the top-level namespaced BatchedMigrationJob' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            class MyJob < ::Gitlab::BackgroundMigration::BatchedMigrationJob
              def perform
                connection.execute("select 1")
              end
            end
          end
        end
      RUBY
    end
  end

  context 'when a nested class is used inside the job class' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            class MyJob < BatchedMigrationJob
              class Project < ApplicationRecord
                self.table_name = 'projects'
              end

              def perform
                Project.update!(name: 'hi')
              end
            end
          end
        end
      RUBY
    end
  end

  context 'when the migration class inherits from another class' do
    it 'registers an offense' do
      expect_offense(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            class MyJob < SomeOtherClass
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG}
            end
          end
        end
      RUBY
    end
  end

  context 'when the migration class does not inherit from anything' do
    it 'registers an offense' do
      expect_offense(<<~RUBY)
        module Gitlab
          module BackgroundMigration
            class MyJob
            ^^^^^^^^^^^ #{described_class::MSG}
            end
          end
        end
      RUBY
    end
  end
end