summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/migration/prevent_strings_spec.rb
blob: 6882dca1926f4c8e49fea081938bcc8babc40318 (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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../../rubocop/cop/migration/prevent_strings'

RSpec.describe RuboCop::Cop::Migration::PreventStrings, type: :rubocop do
  include CopHelper

  subject(:cop) { described_class.new }

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

    context 'when the string data type is used' do
      it 'registers an offense' do
        expect_offense(<<~RUBY)
          class Users < ActiveRecord::Migration[6.0]
            DOWNTIME = false

            def up
              create_table :users do |t|
                t.string :username, null: false
                  ^^^^^^ #{described_class::MSG}

                t.timestamps_with_timezone null: true

                t.string :password
                  ^^^^^^ #{described_class::MSG}
              end

              add_column(:users, :bio, :string)
              ^^^^^^^^^^ #{described_class::MSG}

              add_column_with_default(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255)
              ^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG}

              change_column_type_concurrently :users, :commit_id, :string
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG}
            end
          end
        RUBY

        expect(cop.offenses.map(&:cop_name)).to all(eq('Migration/PreventStrings'))
      end
    end

    context 'when the string data type is not used' do
      it 'registers no offense' do
        expect_no_offenses(<<~RUBY)
          class Users < ActiveRecord::Migration[6.0]
            DOWNTIME = false

            def up
              create_table :users do |t|
                t.integer :not_a_string, null: false
                t.timestamps_with_timezone null: true
              end

              add_column(:users, :not_a_string, :integer)
            end
          end
        RUBY
      end
    end

    context 'when the text data type is used' do
      it 'registers no offense' do
        expect_no_offenses(<<~RUBY)
          class Users < ActiveRecord::Migration[6.0]
            DOWNTIME = false

            def up
              create_table :users do |t|
                t.text :username, null: false
                t.timestamps_with_timezone null: true
                t.text :password
              end

              add_column(:users, :bio, :text)
              add_column_with_default(:users, :url, :text, default: '/-/user', allow_null: false, limit: 255)
              change_column_type_concurrently :users, :commit_id, :text
            end
          end
        RUBY
      end
    end

    context 'when the string data type is used for arrays' do
      it 'registers no offense' do
        expect_no_offenses(<<~RUBY)
          class TestStringArrays < ActiveRecord::Migration[6.0]
            DOWNTIME = false

            def up
              create_table :test_string_arrays, id: false do |t|
                t.integer :test_id, null: false
                t.string :name, array: true, default: [], null: false
              end

              add_column :test_string_arrays, :email, :string, array: true
              add_column_with_default :test_string_arrays, :role, :string, default: [], array: true
              change_column_type_concurrently :test_string_arrays, :test_id, :string, array: true
            end
          end
        RUBY
      end
    end

    context 'on down' do
      it 'registers no offense' do
        expect_no_offenses(<<~RUBY)
          class Users < ActiveRecord::Migration[6.0]
            DOWNTIME = false

            def up
              remove_column :users, :bio
              remove_column :users, :url

              drop_table :test_strings
            end

            def down
              create_table :test_strings, id: false do |t|
                t.integer :test_id, null: false
                t.string :name
              end

              add_column(:users, :bio, :string)
              add_column_with_default(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255)
              change_column_type_concurrently :users, :commit_id, :string
            end
          end
        RUBY
      end
    end
  end

  context 'outside of migration' do
    it 'registers no offense' do
      expect_no_offenses(<<~RUBY)
        class Users < ActiveRecord::Migration[6.0]
          DOWNTIME = false

          def up
            create_table :users do |t|
              t.string :username, null: false
              t.timestamps_with_timezone null: true
              t.string :password
            end

            add_column(:users, :bio, :string)
            add_column_with_default(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255)
            change_column_type_concurrently :users, :commit_id, :string
          end
        end
      RUBY
    end
  end
end