Skip to content

Commit 564c4ff

Browse files
authored
Merge pull request #15 from tyok/fix-and-ignore-cached-queries
Ignore cached queries
2 parents 0c62feb + a0090da commit 564c4ff

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/rspec/sqlimit/counter.rb

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def count
3131
def callback
3232
@callback ||= lambda do |_name, start, finish, _message_id, values|
3333
return if %w(CACHE SCHEMA).include? values[:name]
34+
return if cached_query?(values)
3435
queries << {
3536
sql: values[:sql],
3637
duration: (finish - start) * 1_000,
@@ -42,5 +43,9 @@ def callback
4243
def type_cast(binds)
4344
binds.map { |column, value| ActiveRecord::Base.connection.type_cast(value, column) }
4445
end
46+
47+
def cached_query?(values)
48+
values[:type_casted_binds].respond_to?(:call)
49+
end
4550
end
4651
end

spec/rspec/sqlimit/reporter_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@
4242
end
4343
end
4444
end
45+
46+
context 'activerecord query caching was enabled' do
47+
let(:counter) { RSpec::SQLimit::Counter[nil, Proc.new{ queries }] }
48+
49+
let(:queries) do
50+
User.cache do
51+
User.where(id: 1).to_a
52+
User.where(id: 1).to_a
53+
User.where(id: [2, 3]).to_a
54+
User.where(id: [2, 3]).to_a
55+
end
56+
end
57+
58+
it 'ignores cached queries' do
59+
expect(subject.call).to include("2 queries were invoked")
60+
end
61+
end
4562
end

0 commit comments

Comments
 (0)