diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 00:09:46 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-08 00:09:46 +0300 |
commit | 6867eff1f997a881cd3ea64109f7ba2d4b42fde4 (patch) | |
tree | a0d4b0ced920ff41311e578b406ed9a7136e8f42 /spec/rubocop/cop/migration/datetime_spec.rb | |
parent | 3290d46655f07d7ae3dca788d6df9f326972ffd8 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop/cop/migration/datetime_spec.rb')
-rw-r--r-- | spec/rubocop/cop/migration/datetime_spec.rb | 138 |
1 files changed, 117 insertions, 21 deletions
diff --git a/spec/rubocop/cop/migration/datetime_spec.rb b/spec/rubocop/cop/migration/datetime_spec.rb index 0a771003100..e3023406dce 100644 --- a/spec/rubocop/cop/migration/datetime_spec.rb +++ b/spec/rubocop/cop/migration/datetime_spec.rb @@ -12,9 +12,69 @@ describe RuboCop::Cop::Migration::Datetime do subject(:cop) { described_class.new } - let(:migration_with_datetime) do + let(:create_table_migration_with_datetime) do %q( - class Users < ActiveRecord::Migration[4.2] + class Users < ActiveRecord::Migration[6.0] + DOWNTIME = false + + def change + create_table :users do |t| + t.string :username, null: false + t.datetime :last_sign_in + end + end + end + ) + end + + let(:create_table_migration_with_timestamp) do + %q( + class Users < ActiveRecord::Migration[6.0] + DOWNTIME = false + + def change + create_table :users do |t| + t.string :username, null: false + t.timestamp :last_sign_in + end + end + end + ) + end + + let(:create_table_migration_without_datetime) do + %q( + class Users < ActiveRecord::Migration[6.0] + DOWNTIME = false + + 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] + DOWNTIME = false + + 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] DOWNTIME = false def change @@ -25,9 +85,9 @@ describe RuboCop::Cop::Migration::Datetime do ) end - let(:migration_with_timestamp) do + let(:add_column_migration_with_timestamp) do %q( - class Users < ActiveRecord::Migration[4.2] + class Users < ActiveRecord::Migration[6.0] DOWNTIME = false def change @@ -38,9 +98,9 @@ describe RuboCop::Cop::Migration::Datetime do ) end - let(:migration_without_datetime) do + let(:add_column_migration_without_datetime) do %q( - class Users < ActiveRecord::Migration[4.2] + class Users < ActiveRecord::Migration[6.0] DOWNTIME = false def change @@ -50,9 +110,9 @@ describe RuboCop::Cop::Migration::Datetime do ) end - let(:migration_with_datetime_with_timezone) do + let(:add_column_migration_with_datetime_with_timezone) do %q( - class Users < ActiveRecord::Migration[4.2] + class Users < ActiveRecord::Migration[6.0] DOWNTIME = false def change @@ -68,18 +128,54 @@ describe RuboCop::Cop::Migration::Datetime do allow(cop).to receive(:in_migration?).and_return(true) end - it 'registers an offense when the ":datetime" data type is used' do - inspect_source(migration_with_datetime) + it 'registers an offense when the ":datetime" data type is used on create_table' do + inspect_source(create_table_migration_with_datetime) + + aggregate_failures do + expect(cop.offenses.size).to eq(1) + expect(cop.offenses.map(&:line)).to eq([8]) + expect(cop.offenses.first.message).to include('`datetime`') + end + end + + it 'registers an offense when the ":timestamp" data type is used on create_table' do + inspect_source(create_table_migration_with_timestamp) + + aggregate_failures do + expect(cop.offenses.size).to eq(1) + expect(cop.offenses.map(&:line)).to eq([8]) + expect(cop.offenses.first.message).to include('timestamp') + end + end + + it 'does not register an offense when the ":datetime" data type is not used on create_table' do + inspect_source(create_table_migration_without_datetime) + + aggregate_failures do + expect(cop.offenses.size).to eq(0) + end + end + + it 'does not register an offense when the ":datetime_with_timezone" data type is used on create_table' do + inspect_source(create_table_migration_with_datetime_with_timezone) + + aggregate_failures do + expect(cop.offenses.size).to eq(0) + end + end + + it 'registers an offense when the ":datetime" data type is used on add_column' do + inspect_source(add_column_migration_with_datetime) aggregate_failures do expect(cop.offenses.size).to eq(1) expect(cop.offenses.map(&:line)).to eq([7]) - expect(cop.offenses.first.message).to include('datetime') + expect(cop.offenses.first.message).to include('`datetime`') end end - it 'registers an offense when the ":timestamp" data type is used' do - inspect_source(migration_with_timestamp) + it 'registers an offense when the ":timestamp" data type is used on add_column' do + inspect_source(add_column_migration_with_timestamp) aggregate_failures do expect(cop.offenses.size).to eq(1) @@ -88,16 +184,16 @@ describe RuboCop::Cop::Migration::Datetime do end end - it 'does not register an offense when the ":datetime" data type is not used' do - inspect_source(migration_without_datetime) + it 'does not register an offense when the ":datetime" data type is not used on add_column' do + inspect_source(add_column_migration_without_datetime) aggregate_failures do expect(cop.offenses.size).to eq(0) end end - it 'does not register an offense when the ":datetime_with_timezone" data type is used' do - inspect_source(migration_with_datetime_with_timezone) + it 'does not register an offense when the ":datetime_with_timezone" data type is used on add_column' do + inspect_source(add_column_migration_with_datetime_with_timezone) aggregate_failures do expect(cop.offenses.size).to eq(0) @@ -107,10 +203,10 @@ describe RuboCop::Cop::Migration::Datetime do context 'outside of migration' do it 'registers no offense' do - inspect_source(migration_with_datetime) - inspect_source(migration_with_timestamp) - inspect_source(migration_without_datetime) - inspect_source(migration_with_datetime_with_timezone) + inspect_source(add_column_migration_with_datetime) + inspect_source(add_column_migration_with_timestamp) + inspect_source(add_column_migration_without_datetime) + inspect_source(add_column_migration_with_datetime_with_timezone) expect(cop.offenses.size).to eq(0) end |