-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsafe_column_deletion_spec.rb
119 lines (102 loc) · 3.86 KB
/
unsafe_column_deletion_spec.rb
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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Neeto::UnsafeColumnDeletion, :config do
let(:config) { RuboCop::Config.new }
it "registers an offense when an unsafe column is deleted" do
snippet = <<~RUBY
remove_column :neeto_team_members_engine_permissions, :category, :string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense("neeto_team_members_engine_permissions", "category")}
RUBY
expect_offense(snippet)
snippet = <<~RUBY
remove_column "neeto_themes_engine_themes", "position", :string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense("neeto_themes_engine_themes", "position")}
RUBY
expect_offense(snippet)
end
it "registers an offense when an unsafe column is deleted using change_table block" do
snippet = <<~RUBY
change_table :users do |t|
t.remove :email, default: "", null: false, type: :string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense("users", "email", true)}
t.string :last_name, type: :string
end
RUBY
expect_offense(snippet)
snippet = <<~RUBY
change_table "users" do |t|
t.string "last_name"
t.remove "email", default: "", null: false, type: :string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense("users", "email", true)}
end
RUBY
expect_offense(snippet)
end
it "registers multiple offenses when multiple unsafe columns are deleted using change_table block" do
snippet = <<~RUBY
change_table :users do |t|
t.string :last_name
t.remove :email, default: "", null: false, type: :string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense("users", "email", true)}
t.datetime :datetime
t.remove :deactivated_at, type: :datetime
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense("users", "deactivated_at", true)}
end
RUBY
expect_offense(snippet)
end
it "does not register an offense when a safe column is deleted" do
snippet = <<~RUBY
remove_column :users, :email_deprecated_on_2024_08_09
RUBY
expect_no_offenses(snippet)
snippet = <<~RUBY
remove_column "users", "email_deprecated_on_2024_08_09"
RUBY
expect_no_offenses(snippet)
end
it "does not trigger an offense when a safe column is deleted with change_table block" do
snippet = <<~RUBY
change_table :users do |t|
t.remove :email_deprecated_on_2024_08_09, default: "", null: false, type: :string
t.string :last_name, type: :string
end
RUBY
expect_no_offenses(snippet)
snippet = <<~RUBY
change_table "users" do |t|
t.string "last_name"
t.remove "email_deprecated_on_2024_08_09", default: "", null: false, type: :string
end
RUBY
expect_no_offenses(snippet)
end
it "does not register an offense when the column is dropped in the down method" do
snippet = <<~RUBY
def down
remove_column :users, :email
change_table :users do |t|
t.remove :email
end
end
RUBY
expect_no_offenses(snippet)
end
it "registers an offense when multiple columns are dropped in a single remove operation" do
snippet = <<~RUBY
change_table :users do |t|
t.remove :email, :first_name
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense("users", "email", true)}
t.string :last_name, type: :string
end
RUBY
expect_offense(snippet)
end
private
def offense(table_name, column_name, change_table = false)
message_template = change_table ?
RuboCop::Cop::Neeto::UnsafeColumnDeletion::MSG_CHANGE_TABLE
: RuboCop::Cop::Neeto::UnsafeColumnDeletion::MSG_REMOVE_COLUMN
message = format(message_template, table_name:, column_name:)
"Neeto/UnsafeColumnDeletion: #{message}"
end
end