/
have_db_index_matcher.rb
287 lines (259 loc) · 7.57 KB
/
have_db_index_matcher.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
module Shoulda
module Matchers
module ActiveRecord
# The `have_db_index` matcher tests that the table that backs your model
# has a specific index.
#
# You can specify one column:
#
# class CreateBlogs < ActiveRecord::Migration
# def change
# create_table :blogs do |t|
# t.integer :user_id
# end
#
# add_index :blogs, :user_id
# end
# end
#
# # RSpec
# RSpec.describe Blog, type: :model do
# it { should have_db_index(:user_id) }
# end
#
# # Minitest (Shoulda)
# class BlogTest < ActiveSupport::TestCase
# should have_db_index(:user_id)
# end
#
# Or you can specify a group of columns:
#
# class CreateBlogs < ActiveRecord::Migration
# def change
# create_table :blogs do |t|
# t.integer :user_id
# t.string :name
# end
#
# add_index :blogs, :user_id, :name
# end
# end
#
# # RSpec
# RSpec.describe Blog, type: :model do
# it { should have_db_index([:user_id, :name]) }
# end
#
# # Minitest (Shoulda)
# class BlogTest < ActiveSupport::TestCase
# should have_db_index([:user_id, :name])
# end
#
# Finally, if you're using Rails 5 and PostgreSQL, you can also specify an
# expression:
#
# class CreateLoggedErrors < ActiveRecord::Migration
# def change
# create_table :logged_errors do |t|
# t.string :code
# t.jsonb :content
# end
#
# add_index :logged_errors, 'lower(code)::text'
# end
# end
#
# # RSpec
# RSpec.describe LoggedError, type: :model do
# it { should have_db_index('lower(code)::text') }
# end
#
# # Minitest (Shoulda)
# class LoggedErrorTest < ActiveSupport::TestCase
# should have_db_index('lower(code)::text')
# end
#
# #### Qualifiers
#
# ##### unique
#
# Use `unique` to assert that the index is either unique or non-unique:
#
# class CreateBlogs < ActiveRecord::Migration
# def change
# create_table :blogs do |t|
# t.string :domain
# t.integer :user_id
# end
#
# add_index :blogs, :domain, unique: true
# add_index :blogs, :user_id
# end
# end
#
# # RSpec
# RSpec.describe Blog, type: :model do
# it { should have_db_index(:name).unique }
# it { should have_db_index(:name).unique(true) } # if you want to be explicit
# it { should have_db_index(:user_id).unique(false) }
# end
#
# # Minitest (Shoulda)
# class BlogTest < ActiveSupport::TestCase
# should have_db_index(:name).unique
# should have_db_index(:name).unique(true) # if you want to be explicit
# should have_db_index(:user_id).unique(false)
# end
#
# @return [HaveDbIndexMatcher]
#
def have_db_index(columns)
HaveDbIndexMatcher.new(columns)
end
# @private
class HaveDbIndexMatcher
def initialize(columns)
@expected_columns = normalize_columns_to_array(columns)
@qualifiers = {}
end
def unique(unique = true)
@qualifiers[:unique] = unique
self
end
def matches?(subject)
@subject = subject
index_exists? && correct_unique?
end
def failure_message
message =
"Expected #{described_table_name} to #{positive_expectation}"
message <<
if index_exists?
". The index does exist, but #{reason}."
elsif reason
", but #{reason}."
else
', but it does not.'
end
Shoulda::Matchers.word_wrap(message)
end
def failure_message_when_negated
Shoulda::Matchers.word_wrap(
"Expected #{described_table_name} not to " +
"#{negative_expectation}, but it does.",
)
end
def description
String.new('have ').tap do |description|
description <<
if qualifiers.include?(:unique)
"#{Shoulda::Matchers::Util.a_or_an(index_type)} "
else
'an '
end
description << 'index on '
description << inspected_expected_columns
end
end
private
attr_reader :expected_columns, :qualifiers, :subject, :reason
def normalize_columns_to_array(columns)
Array.wrap(columns).map(&:to_s)
end
def index_exists?
!matched_index.nil?
end
def correct_unique?
if qualifiers.include?(:unique)
if qualifiers[:unique] && !matched_index.unique
@reason = 'it is not unique'
false
elsif !qualifiers[:unique] && matched_index.unique
@reason = 'it is unique'
false
else
true
end
else
true
end
end
def matched_index
@_matched_index ||=
if expected_columns.one?
sorted_indexes.detect do |index|
Array.wrap(index.columns) == expected_columns
end
else
sorted_indexes.detect do |index|
index.columns == expected_columns
end
end
end
def sorted_indexes
if qualifiers.include?(:unique)
# return indexes with unique matching the qualifier first
unsorted_indexes.sort_by do |index|
index.unique == qualifiers[:unique] ? 0 : 1
end
else
unsorted_indexes
end
end
def unsorted_indexes
model.connection.indexes(table_name)
end
def described_table_name
if model
"the #{table_name} table"
else
'a table'
end
end
def table_name
model.table_name
end
def positive_expectation
if index_exists?
expectation = "have an index on #{inspected_expected_columns}"
if qualifiers.include?(:unique)
expectation << " and for it to be #{index_type}"
end
expectation
else
description
end
end
def negative_expectation
description
end
def inspected_expected_columns
if formatted_expected_columns.one?
formatted_expected_columns.first.inspect
else
formatted_expected_columns.inspect
end
end
def index_type
if qualifiers[:unique]
'unique'
else
'non-unique'
end
end
def formatted_expected_columns
expected_columns.map do |column|
if column.match?(/^\w+$/)
column.to_sym
else
column
end
end
end
def model
subject&.class
end
end
end
end
end