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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/migration/datetime'
RSpec.describe RuboCop::Cop::Migration::Datetime do
subject(:cop) { described_class.new }
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
add_column(:users, :username, :text)
add_column(:users, :last_sign_in, :datetime)
end
end
)
end
let(:add_column_migration_with_timestamp) do
%q(
class Users < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column(:users, :username, :text)
add_column(:users, :last_sign_in, :timestamp)
end
end
)
end
let(:add_column_migration_without_datetime) do
%q(
class Users < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column(:users, :username, :text)
end
end
)
end
let(:add_column_migration_with_datetime_with_timezone) do
%q(
class Users < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column(:users, :username, :text)
add_column(:users, :last_sign_in, :datetime_with_timezone)
end
end
)
end
context 'when in migration' do
before do
allow(cop).to receive(:in_migration?).and_return(true)
end
it 'registers an offense when the ":datetime" data type is used on create_table' do
expect_offense(<<~RUBY)
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
^^^^^^^^ Do not use the `datetime` data type[...]
end
end
end
RUBY
end
it 'registers an offense when the ":timestamp" data type is used on create_table' do
expect_offense(<<~RUBY)
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
^^^^^^^^^ Do not use the `timestamp` data type[...]
end
end
end
RUBY
end
it 'does not register an offense when the ":datetime" data type is not used on create_table' do
expect_no_offenses(create_table_migration_without_datetime)
end
it 'does not register an offense when the ":datetime_with_timezone" data type is used on create_table' do
expect_no_offenses(create_table_migration_with_datetime_with_timezone)
end
it 'registers an offense when the ":datetime" data type is used on add_column' do
expect_offense(<<~RUBY)
class Users < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column(:users, :username, :text)
add_column(:users, :last_sign_in, :datetime)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use the `datetime` data type[...]
end
end
RUBY
end
it 'registers an offense when the ":timestamp" data type is used on add_column' do
expect_offense(<<~RUBY)
class Users < ActiveRecord::Migration[6.0]
DOWNTIME = false
def change
add_column(:users, :username, :text)
add_column(:users, :last_sign_in, :timestamp)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use the `timestamp` data type[...]
end
end
RUBY
end
it 'does not register an offense when the ":datetime" data type is not used on add_column' do
expect_no_offenses(add_column_migration_without_datetime)
end
it 'does not register an offense when the ":datetime_with_timezone" data type is used on add_column' do
expect_no_offenses(add_column_migration_with_datetime_with_timezone)
end
end
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)
end
end
end
|