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: 1ee8041578d3286a7bd09fc7f2aba92c797aeb6f
4
- data.tar.gz: 08cd262fd6ee889e0db92fb9bc8347c3b5f23864
3
+ metadata.gz: 4211e9d9ce47b20a844dd9609372ba7f75aa6072
4
+ data.tar.gz: a1edf04492e31903f92813058cb8ce4fe68b9568
5
5
  SHA512:
6
- metadata.gz: 2b6e5c04b7f43ac11bbd84290087031bab478fdb00e24b248dfa77f81b2495d92a17bb7246c1bb25d6a680cf19371f3547b9ea73312188ec27333c3ff8fbb329
7
- data.tar.gz: 122b88bf16eee4bf4502f4216c323dc833a182b110a3f08674a47f34573952ff5435de814bada2d4c44d4cbfd5ab11e2509fe51e0d366d8d9a1b5b18f06a5fdd
6
+ metadata.gz: 00d22a2caf4c12a289c904f70765dc698c6d489c9ac5f27453d7e91b7fa84e1c2a8178a050cccc524b52117acd9321748e08cc48d2bf7571815293a1e7afad57
7
+ data.tar.gz: 340c16be98d92f6f29251a699407f16d6c3f9da0aab50377964cd1201563846c102e75c7973a473b3981649cdb0a6432bfcb73c4852adb05e120c8f8c1e29681
@@ -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
@@ -0,0 +1,10 @@
1
+ module DBQueryMatchers
2
+ # Configuration for the DBQueryMatcher module.
3
+ class Configuration
4
+ attr_accessor :ignores
5
+
6
+ def initialize
7
+ @ignores = []
8
+ end
9
+ end
10
+ 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
- # Counter to keep track of the number of queries caused by running a piece of
2
- # code. Closely tied to the `:make_database_queries` matcher, this class is
3
- # designed to be a consumer of `sql.active_record` events.
4
- #
5
- # @example
6
- # counter = QueryCounter.new
7
- # ActiveSupport::Notifications.subscribed(counter.to_proc,
8
- # 'sql.active_record') do
9
- # # run code here
10
- # end
11
- # puts counter.count # prints the number of queries made
12
- # puts counter.log.join(', ') # prints all queries made
13
- #
14
- # @see http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html#module-ActiveSupport::Notifications-label-Temporary+Subscriptions
15
- class QueryCounter
16
- attr_reader :count, :log
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
- def initialize
19
- @count = 0
20
- @log = []
21
- end
19
+ def initialize
20
+ @count = 0
21
+ @log = []
22
+ end
22
23
 
23
- # Turns a QueryCounter instance into a lambda. Designed to be used when
24
- # subscribing to events through the ActiveSupport::Notifications module.
25
- #
26
- # @return [Proc]
27
- def to_proc
28
- lambda(&method(:callback))
29
- end
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
- # Method called from the ActiveSupport::Notifications module (through the
32
- # lambda created by `to_proc`) when an SQL query is made.
33
- #
34
- # @param name [String] name of the event
35
- # @param start [Time] when the instrumented block started execution
36
- # @param finish [Time] when the instrumented block ended execution
37
- # @param message_id [String] unique ID for this notification
38
- # @param payload [Hash] the payload
39
- def callback(name, start, finish, message_id, payload)
40
- @count += 1
41
- @log << payload[:sql]
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
@@ -1,4 +1,4 @@
1
1
  # Defines the gem version.
2
2
  module DBQueryMatchers
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  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.1
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-06-25 00:00:00.000000000 Z
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