2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
require 'fast_spec_helper'
|
2017-09-10 17:25:29 +05:30
|
|
|
require_relative '../../../../rubocop/cop/migration/datetime'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Migration::Datetime do
|
2017-09-10 17:25:29 +05:30
|
|
|
subject(:cop) { described_class.new }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:create_table_migration_without_datetime) do
|
|
|
|
%q(
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
def change
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :username, null: false
|
|
|
|
t.string :password
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:create_table_migration_with_datetime_with_timezone) do
|
|
|
|
%q(
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
def change
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :username, null: false
|
|
|
|
t.datetime_with_timezone :last_sign_in
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:add_column_migration_with_datetime) do
|
|
|
|
%q(
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
2017-09-10 17:25:29 +05:30
|
|
|
def change
|
|
|
|
add_column(:users, :username, :text)
|
|
|
|
add_column(:users, :last_sign_in, :datetime)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:add_column_migration_with_timestamp) do
|
2018-03-17 18:26:18 +05:30
|
|
|
%q(
|
2020-04-22 19:07:51 +05:30
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
2018-03-17 18:26:18 +05:30
|
|
|
def change
|
|
|
|
add_column(:users, :username, :text)
|
|
|
|
add_column(:users, :last_sign_in, :timestamp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:add_column_migration_without_datetime) do
|
2017-09-10 17:25:29 +05:30
|
|
|
%q(
|
2020-04-22 19:07:51 +05:30
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
2017-09-10 17:25:29 +05:30
|
|
|
def change
|
|
|
|
add_column(:users, :username, :text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:add_column_migration_with_datetime_with_timezone) do
|
2017-09-10 17:25:29 +05:30
|
|
|
%q(
|
2020-04-22 19:07:51 +05:30
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
2017-09-10 17:25:29 +05:30
|
|
|
def change
|
|
|
|
add_column(:users, :username, :text)
|
|
|
|
add_column(:users, :last_sign_in, :datetime_with_timezone)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when in migration' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
allow(cop).to receive(:in_migration?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'registers an offense when the ":datetime" data type is used on create_table' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
def change
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :username, null: false
|
|
|
|
t.datetime :last_sign_in
|
|
|
|
^^^^^^^^ Do not use the `datetime` data type[...]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when the ":timestamp" data type is used on create_table' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
def change
|
|
|
|
create_table :users do |t|
|
|
|
|
t.string :username, null: false
|
|
|
|
t.timestamp :last_sign_in
|
|
|
|
^^^^^^^^^ Do not use the `timestamp` data type[...]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not register an offense when the ":datetime" data type is not used on create_table' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses(create_table_migration_without_datetime)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not register an offense when the ":datetime_with_timezone" data type is used on create_table' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses(create_table_migration_with_datetime_with_timezone)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when the ":datetime" data type is used on add_column' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
def change
|
|
|
|
add_column(:users, :username, :text)
|
|
|
|
add_column(:users, :last_sign_in, :datetime)
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use the `datetime` data type[...]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'registers an offense when the ":timestamp" data type is used on add_column' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class Users < ActiveRecord::Migration[6.0]
|
|
|
|
def change
|
|
|
|
add_column(:users, :username, :text)
|
|
|
|
add_column(:users, :last_sign_in, :timestamp)
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use the `timestamp` data type[...]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does not register an offense when the ":datetime" data type is not used on add_column' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses(add_column_migration_without_datetime)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does not register an offense when the ":datetime_with_timezone" data type is used on add_column' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses(add_column_migration_with_datetime_with_timezone)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when outside of migration' do
|
|
|
|
it 'registers no offense', :aggregate_failures do
|
|
|
|
expect_no_offenses(add_column_migration_with_datetime)
|
|
|
|
expect_no_offenses(add_column_migration_with_timestamp)
|
|
|
|
expect_no_offenses(add_column_migration_without_datetime)
|
|
|
|
expect_no_offenses(add_column_migration_with_datetime_with_timezone)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|