2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-18 14:10:07 -04:00
|
|
|
require 'rubocop_spec_helper'
|
2020-04-21 11:21:10 -04:00
|
|
|
require_relative '../../../../rubocop/cop/migration/prevent_strings'
|
|
|
|
|
2021-01-07 13:10:38 -05:00
|
|
|
RSpec.describe RuboCop::Cop::Migration::PreventStrings do
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when in migration' do
|
2020-04-21 11:21:10 -04:00
|
|
|
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
|
2021-02-17 16:09:06 -05:00
|
|
|
expect_offense(<<~RUBY, msg: "Do not use the `string` data type, use `text` instead.[...]")
|
2020-04-21 11:21:10 -04:00
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
def up
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :username, null: false
|
2021-02-17 16:09:06 -05:00
|
|
|
^^^^^^ %{msg}
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
t.timestamps_with_timezone null: true
|
|
|
|
|
|
|
|
t.string :password
|
2021-02-17 16:09:06 -05:00
|
|
|
^^^^^^ %{msg}
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
add_column(:users, :bio, :string)
|
2021-02-17 16:09:06 -05:00
|
|
|
^^^^^^^^^^ %{msg}
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
add_column_with_default(:users, :url, :string, default: '/-/user', allow_null: false, limit: 255)
|
2021-02-17 16:09:06 -05:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^ %{msg}
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
change_column_type_concurrently :users, :commit_id, :string
|
2021-02-17 16:09:06 -05:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ %{msg}
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
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
|
|
|
|
|
2020-06-10 17:09:29 -04:00
|
|
|
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]
|
|
|
|
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
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when using down method' do
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'registers no offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
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
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when outside of migration' do
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'registers no offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
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
|