-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
unused_ignored_columns_spec.rb
130 lines (116 loc) · 3.8 KB
/
unused_ignored_columns_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
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::UnusedIgnoredColumns, :config do
context 'without db/schema.rb' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = [:real_name]
end
RUBY
end
end
context 'with db/schema.rb' do
include_context 'with SchemaLoader'
let(:schema) { <<~RUBY }
ActiveRecord::Schema.define(version: 2020_02_02_075409) do
create_table "users", force: :cascade do |t|
t.string "account", null: false
end
end
RUBY
context 'with an unused ignored column as a Symbol' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = [:real_name]
^^^^^^^^^^ Remove `real_name` from `ignored_columns` because the column does not exist.
end
RUBY
end
end
context 'with an used/unused ignored column in a mixin' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
module Abc
self.ignored_columns = [:real_name]
end
RUBY
end
end
context 'with an unused ignored column as a String' do
it 'registers an offense' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = ['real_name']
^^^^^^^^^^^ Remove `real_name` from `ignored_columns` because the column does not exist.
end
RUBY
end
end
context 'when ignored_columns= receives existent column as a Symbol' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = [:account]
end
RUBY
end
end
context 'when ignored_columns= receives existent column as a String' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = ['account']
end
RUBY
end
end
context 'with existent and nonexistent columns as Symbol' do
it 'registers an offense to the nonexistent column' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = [:real_name, :account]
^^^^^^^^^^ Remove `real_name` from `ignored_columns` because the column does not exist.
end
RUBY
end
end
context 'with existent and nonexistent columns as String' do
it 'registers an offense to the nonexistent column' do
expect_offense(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = ['real_name', 'account']
^^^^^^^^^^^ Remove `real_name` from `ignored_columns` because the column does not exist.
end
RUBY
end
end
context 'when ignored_columns= receives not a literal' do
it 'does nothing' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = array
end
RUBY
end
end
end
context 'with no tables db/schema.rb' do
include_context 'with SchemaLoader'
let(:schema) { <<~RUBY }
ActiveRecord::Schema.define(version: 2020_02_02_075409) do
enable_extension 'plpgsql'
end
RUBY
context 'with an unused ignored column as a Symbol' do
# NOTE: For example, it is not possible to track externally managed databases.
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class User < ApplicationRecord
self.ignored_columns = [:real_name]
end
RUBY
end
end
end
end