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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ExceedQueryLimitHelpers do
before do
stub_const('TestQueries', Class.new(ActiveRecord::Base))
stub_const('TestMatcher', Class.new)
TestQueries.class_eval do
self.table_name = 'schema_migrations'
end
TestMatcher.class_eval do
include ExceedQueryLimitHelpers
def expected
ActiveRecord::QueryRecorder.new do
2.times { TestQueries.count }
end
end
end
end
describe '#diff_query_group_message' do
it 'prints a group helpfully' do
test_matcher = TestMatcher.new
suffixes = {
'WHERE x = z' => [1, 1],
'WHERE x = y' => [1, 2],
'LIMIT 1' => [1, 0]
}
message = test_matcher.diff_query_group_message('SELECT * FROM foo', suffixes)
expect(message).to eq(<<~MSG.chomp)
SELECT * FROM foo...
-- (expected: 1, got: 1)
WHERE x = z
-- (expected: 1, got: 2)
WHERE x = y
-- (expected: 1, got: 0)
LIMIT 1
MSG
end
end
describe '#diff_query_counts' do
let(:expected) do
ActiveRecord::QueryRecorder.new do
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'also foobar and baz').to_a
TestQueries.count
TestQueries.first
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'x').update_all(version: 'y')
TestQueries.where(version: 'foobar').count
TestQueries.where(version: 'z').delete_all
Project.where(id: 1).pluck(:title)
end
end
let(:actual) do
ActiveRecord::QueryRecorder.new do
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'also foobar and baz').to_a
TestQueries.count
TestQueries.create!(version: 'x')
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'x').update_all(version: 'y')
TestQueries.where(version: 'foobar').count
TestQueries.count
TestQueries.where(version: 'y').update_all(version: 'z')
TestQueries.where(version: 'z').delete_all
Project.where(id: 2).pluck(:title)
end
end
it 'merges two query counts, showing only diffs' do
test_matcher = TestMatcher.new
diff = test_matcher.diff_query_counts(
test_matcher.count_queries(expected),
test_matcher.count_queries(actual)
)
expect(diff).to eq({
"SELECT \"schema_migrations\".* FROM \"schema_migrations\"" => {
"ORDER BY \"schema_migrations\".\"version\" ASC LIMIT 1" => [1, 0]
},
"SELECT COUNT(*) FROM \"schema_migrations\"" => { "" => [1, 2] },
"UPDATE \"schema_migrations\"" => {
"SET \"version\" = 'z' WHERE \"schema_migrations\".\"version\" = 'y'" => [0, 1]
},
"SAVEPOINT active_record_1" => { "" => [0, 1] },
"INSERT INTO \"schema_migrations\" (\"version\")" => {
"VALUES ('x') RETURNING \"version\"" => [0, 1]
},
"RELEASE SAVEPOINT active_record_1" => { "" => [0, 1] }
})
end
it 'can show common queries if so desired' do
test_matcher = TestMatcher.new.show_common_queries
diff = test_matcher.diff_query_counts(
test_matcher.count_queries(expected),
test_matcher.count_queries(actual)
)
expect(diff).to eq({
"SELECT \"schema_migrations\".* FROM \"schema_migrations\"" => {
"WHERE \"schema_migrations\".\"version\" = 'foobar'" => [2, 2],
"WHERE \"schema_migrations\".\"version\" = 'also foobar and baz'" => [1, 1],
"ORDER BY \"schema_migrations\".\"version\" ASC LIMIT 1" => [1, 0]
},
"SELECT COUNT(*) FROM \"schema_migrations\"" => {
"" => [1, 2],
"WHERE \"schema_migrations\".\"version\" = 'foobar'" => [1, 1]
},
"UPDATE \"schema_migrations\"" => {
"SET \"version\" = 'y' WHERE \"schema_migrations\".\"version\" = 'x'" => [1, 1],
"SET \"version\" = 'z' WHERE \"schema_migrations\".\"version\" = 'y'" => [0, 1]
},
"DELETE FROM \"schema_migrations\"" => {
"WHERE \"schema_migrations\".\"version\" = 'z'" => [1, 1]
},
"SAVEPOINT active_record_1" => {
"" => [0, 1]
},
"INSERT INTO \"schema_migrations\" (\"version\")" => {
"VALUES ('x') RETURNING \"version\"" => [0, 1]
},
"RELEASE SAVEPOINT active_record_1" => {
"" => [0, 1]
},
"SELECT \"projects\".\"name\" FROM \"projects\"" => {
"WHERE \"projects\".\"id\" = 1" => [1, 0],
"WHERE \"projects\".\"id\" = 2" => [0, 1]
}
})
end
end
describe '#count_queries' do
it 'handles queries with suffixes over multiple lines' do
test_matcher = TestMatcher.new
recorder = ActiveRecord::QueryRecorder.new do
TestQueries.find_by(version: %w(foo bar baz).join("\n"))
TestQueries.find_by(version: %w(foo biz baz).join("\n"))
TestQueries.find_by(version: %w(foo bar baz).join("\n"))
end
recorder.count
expect(test_matcher.count_queries(recorder)).to eq({
'SELECT "schema_migrations".* FROM "schema_migrations"' => {
%Q[WHERE "schema_migrations"."version" = 'foo\nbar\nbaz' LIMIT 1] => 2,
%Q[WHERE "schema_migrations"."version" = 'foo\nbiz\nbaz' LIMIT 1] => 1
}
})
end
it 'can aggregate queries' do
test_matcher = TestMatcher.new
recorder = ActiveRecord::QueryRecorder.new do
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'also foobar and baz').to_a
TestQueries.count
TestQueries.create!(version: 'x')
TestQueries.first
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'x').update_all(version: 'y')
TestQueries.where(version: 'foobar').count
TestQueries.count
TestQueries.where(version: 'y').update_all(version: 'z')
TestQueries.where(version: 'z').delete_all
end
recorder.count
expect(test_matcher.count_queries(recorder)).to eq({
'SELECT "schema_migrations".* FROM "schema_migrations"' => {
%q[WHERE "schema_migrations"."version" = 'foobar'] => 2,
%q[WHERE "schema_migrations"."version" = 'also foobar and baz'] => 1,
%q[ORDER BY "schema_migrations"."version" ASC LIMIT 1] => 1
},
'SELECT COUNT(*) FROM "schema_migrations"' => {
"" => 2,
%q[WHERE "schema_migrations"."version" = 'foobar'] => 1
},
'SAVEPOINT active_record_1' => { "" => 1 },
'INSERT INTO "schema_migrations" ("version")' => {
%q[VALUES ('x') RETURNING "version"] => 1
},
'RELEASE SAVEPOINT active_record_1' => { "" => 1 },
'UPDATE "schema_migrations"' => {
%q[SET "version" = 'y' WHERE "schema_migrations"."version" = 'x'] => 1,
%q[SET "version" = 'z' WHERE "schema_migrations"."version" = 'y'] => 1
},
'DELETE FROM "schema_migrations"' => {
%q[WHERE "schema_migrations"."version" = 'z'] => 1
}
})
end
end
it 'can count queries' do
test_matcher = TestMatcher.new
test_matcher.verify_count do
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'also foobar and baz').to_a
TestQueries.first
TestQueries.count
end
expect(test_matcher.actual_count).to eq(4)
end
it 'can select specific queries' do
test_matcher = TestMatcher.new.for_query(/foobar/)
test_matcher.verify_count do
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'also foobar and baz').to_a
TestQueries.first
TestQueries.count
end
expect(test_matcher.actual_count).to eq(2)
end
it 'can filter specific models' do
test_matcher = TestMatcher.new.for_model(TestQueries)
test_matcher.verify_count do
TestQueries.first
TestQueries.connection.execute('select 1')
end
expect(test_matcher.actual_count).to eq(1)
end
it 'can ignore specific queries' do
test_matcher = TestMatcher.new.ignoring(/foobar/)
test_matcher.verify_count do
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'also foobar and baz').to_a
TestQueries.first
end
expect(test_matcher.actual_count).to eq(1)
end
it 'can perform inclusion and exclusion' do
test_matcher = TestMatcher.new.for_query(/foobar/).ignoring(/baz/)
test_matcher.verify_count do
TestQueries.where(version: 'foobar').to_a
TestQueries.where(version: 'also foobar and baz').to_a
TestQueries.first
TestQueries.count
end
expect(test_matcher.actual_count).to eq(1)
end
it 'does not contain marginalia annotations' do
test_matcher = TestMatcher.new
test_matcher.verify_count do
2.times { TestQueries.count }
TestQueries.first
end
aggregate_failures do
expect(test_matcher.log_message)
.to match(%r{ORDER BY.*#{TestQueries.table_name}.*LIMIT 1})
expect(test_matcher.log_message)
.not_to match(%r{\/\*.*correlation_id.*\*\/})
end
end
end
|