db-query-matchers 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4211e9d9ce47b20a844dd9609372ba7f75aa6072
|
4
|
+
data.tar.gz: a1edf04492e31903f92813058cb8ce4fe68b9568
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00d22a2caf4c12a289c904f70765dc698c6d489c9ac5f27453d7e91b7fa84e1c2a8178a050cccc524b52117acd9321748e08cc48d2bf7571815293a1e7afad57
|
7
|
+
data.tar.gz: 340c16be98d92f6f29251a699407f16d6c3f9da0aab50377964cd1201563846c102e75c7973a473b3981649cdb0a6432bfcb73c4852adb05e120c8f8c1e29681
|
data/lib/db_query_matchers.rb
CHANGED
@@ -1,5 +1,35 @@
|
|
1
1
|
require 'db_query_matchers/version'
|
2
2
|
require 'db_query_matchers/make_database_queries'
|
3
3
|
require 'db_query_matchers/query_counter'
|
4
|
+
require 'db_query_matchers/configuration'
|
4
5
|
require 'active_support'
|
5
6
|
require 'active_record'
|
7
|
+
|
8
|
+
# Main module that holds global configuration.
|
9
|
+
module DBQueryMatchers
|
10
|
+
class << self
|
11
|
+
attr_writer :configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
# Gets the current configuration
|
15
|
+
# @return [DBQueryMatchers::Configuration] the active configuration
|
16
|
+
def self.configuration
|
17
|
+
@configuration ||= Configuration.new
|
18
|
+
end
|
19
|
+
|
20
|
+
# Resets the current configuration.
|
21
|
+
# @return [DBQueryMatchers::Configuration] the active configuration
|
22
|
+
def self.reset_configuration
|
23
|
+
@configuration = Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
# Updates the current configuration.
|
27
|
+
# @example
|
28
|
+
# DBQueryMatchers.configure do |config|
|
29
|
+
# config.ignores = [/SELECT.*FROM.*users/]
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
def self.configure
|
33
|
+
yield(configuration)
|
34
|
+
end
|
35
|
+
end
|
@@ -8,7 +8,7 @@ require 'rspec'
|
|
8
8
|
# @example
|
9
9
|
# expect { subject }.to make_database_queries(count: 1)
|
10
10
|
#
|
11
|
-
# @see QueryCounter
|
11
|
+
# @see DBQueryMatchers::QueryCounter
|
12
12
|
RSpec::Matchers.define :make_database_queries do |options = {}|
|
13
13
|
supports_block_expectations
|
14
14
|
|
@@ -24,7 +24,7 @@ RSpec::Matchers.define :make_database_queries do |options = {}|
|
|
24
24
|
end
|
25
25
|
|
26
26
|
match do |block|
|
27
|
-
@counter = QueryCounter.new
|
27
|
+
@counter = DBQueryMatchers::QueryCounter.new
|
28
28
|
ActiveSupport::Notifications.subscribed(@counter.to_proc,
|
29
29
|
'sql.active_record',
|
30
30
|
&block)
|
@@ -1,43 +1,48 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# puts counter.
|
13
|
-
#
|
14
|
-
#
|
15
|
-
|
16
|
-
|
1
|
+
module DBQueryMatchers
|
2
|
+
# Counter to keep track of the number of queries caused by running a piece of
|
3
|
+
# code. Closely tied to the `:make_database_queries` matcher, this class is
|
4
|
+
# designed to be a consumer of `sql.active_record` events.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# counter = DBQueryMatchers::QueryCounter.new
|
8
|
+
# ActiveSupport::Notifications.subscribed(counter.to_proc,
|
9
|
+
# 'sql.active_record') do
|
10
|
+
# # run code here
|
11
|
+
# end
|
12
|
+
# puts counter.count # prints the number of queries made
|
13
|
+
# puts counter.log.join(', ') # prints all queries made
|
14
|
+
#
|
15
|
+
# @see http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html#module-ActiveSupport::Notifications-label-Temporary+Subscriptions
|
16
|
+
class QueryCounter
|
17
|
+
attr_reader :count, :log
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
def initialize
|
20
|
+
@count = 0
|
21
|
+
@log = []
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
# Turns a QueryCounter instance into a lambda. Designed to be used when
|
25
|
+
# subscribing to events through the ActiveSupport::Notifications module.
|
26
|
+
#
|
27
|
+
# @return [Proc]
|
28
|
+
def to_proc
|
29
|
+
lambda(&method(:callback))
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
32
|
+
# Method called from the ActiveSupport::Notifications module (through the
|
33
|
+
# lambda created by `to_proc`) when an SQL query is made.
|
34
|
+
#
|
35
|
+
# @param _name [String] name of the event
|
36
|
+
# @param _start [Time] when the instrumented block started execution
|
37
|
+
# @param _finish [Time] when the instrumented block ended execution
|
38
|
+
# @param _message_id [String] unique ID for this notification
|
39
|
+
# @param payload [Hash] the payload
|
40
|
+
def callback(_name, _start, _finish, _message_id, payload)
|
41
|
+
return if DBQueryMatchers.configuration.ignores.any? do |pattern|
|
42
|
+
payload[:sql] =~ pattern
|
43
|
+
end
|
44
|
+
@count += 1
|
45
|
+
@log << payload[:sql]
|
46
|
+
end
|
42
47
|
end
|
43
48
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db-query-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Causes Engineering
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-07-02 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -79,6 +79,7 @@ extra_rdoc_files: []
|
|
79
79
|
files:
|
80
80
|
- lib/db-query-matchers.rb
|
81
81
|
- lib/db_query_matchers.rb
|
82
|
+
- lib/db_query_matchers/configuration.rb
|
82
83
|
- lib/db_query_matchers/make_database_queries.rb
|
83
84
|
- lib/db_query_matchers/query_counter.rb
|
84
85
|
- lib/db_query_matchers/version.rb
|