have_query_count 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/have_query_count/query_counter.rb +17 -0
- data/lib/have_query_count.rb +27 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7148e8f1a06689a3a605acd47fac3baa11ad909861045b4c17f5d0216777dfe
|
4
|
+
data.tar.gz: c9d4ba3962309bc6f98562a96a5685815fe57b4e154c602175e4e1c4d24aa027
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b602c2ad943c2b35e01437a1dee2fe75b7cf4c1b10e55a3396227d3c52c3005000d2e69fbae1a848cf36668b9daa334e3fff444c3bc0f88b78f47e9cd8d6ae2e
|
7
|
+
data.tar.gz: 47e29a075a925499830171c1e2bb35c7977fa09e1529776782fd13b4009c21daf6348c553fdbad5d4927acbf99cbcd35a76cd782403ae7c493b4eeffa9340da4
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class QueryCounter
|
2
|
+
IGNORE = %w(CACHE SCHEMA TRANSACTION).freeze
|
3
|
+
|
4
|
+
attr_reader :query_count
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@query_count = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_proc = lambda(&method(:callback))
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def callback(name, start, finish, message_id, values)
|
15
|
+
IGNORE.include?(values[:name]) || @query_count += 1
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support"
|
4
|
+
require "rspec/expectations"
|
5
|
+
|
6
|
+
require "have_query_count/query_counter"
|
7
|
+
|
8
|
+
module HaveQueryCount
|
9
|
+
RSpec::Matchers.define :have_query_count do |expected|
|
10
|
+
supports_block_expectations
|
11
|
+
|
12
|
+
match { |block| count(&block) == expected }
|
13
|
+
|
14
|
+
failure_message do |_|
|
15
|
+
"expected #{expected} sql queries, but got #{@counter.query_count}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def count(&block)
|
19
|
+
@counter = QueryCounter.new
|
20
|
+
ActiveSupport::Notifications.subscribed(
|
21
|
+
@counter.to_proc,
|
22
|
+
"sql.active_record",
|
23
|
+
&block
|
24
|
+
) && @counter.query_count
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: have_query_count
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Violet Graves
|
@@ -85,7 +85,9 @@ email:
|
|
85
85
|
executables: []
|
86
86
|
extensions: []
|
87
87
|
extra_rdoc_files: []
|
88
|
-
files:
|
88
|
+
files:
|
89
|
+
- lib/have_query_count.rb
|
90
|
+
- lib/have_query_count/query_counter.rb
|
89
91
|
homepage: https://rubygems.org/gems/have_query_count
|
90
92
|
licenses:
|
91
93
|
- MIT
|