db-query-matchers 0.1.1 → 0.1.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: 65befe1a407bc1dd9428fe80524abdc717de779d
4
- data.tar.gz: ed7a5d7eb22679bb5318991bf70c06f49fca653e
3
+ metadata.gz: fc48c9f7dac8104c00663ade64d3dba62487e682
4
+ data.tar.gz: d6813610637b71bfa53435f8fc3f45e58470a6b2
5
5
  SHA512:
6
- metadata.gz: 68a653eaa1cc7782db59b75f8779a038060e6086a6959464d05cfa35870b4a99ab57e904f91e1bdd782cffcd30c27dcf473a98c787db7e1eef6df70e6baf7bed
7
- data.tar.gz: 18093d3257903cdb7346e53cda23a79110b11ada6d04292163f757d27a62f0a5a2b7ca6bbbee48e337c016221b07c374276d490fdae9e423e6adc5daa2e7f25a
6
+ metadata.gz: 0b836ebccfaeb3b302cf75768c25dadefcd7e6988435cd81e7c510536e1909aed2a55fb0d6b49005fc81cdd4e8e3990f34931d9c7a8465b990faedae865c2583
7
+ data.tar.gz: aae4118888524878f5793fe2d760a7a8d9c025e8b7859235a0c8349e866250b2d2810cbf8acda815e982c7c523822086d2b504b65c678d236671438da1629e98
@@ -0,0 +1,58 @@
1
+ require 'rspec'
2
+
3
+ # Custom matcher to check for database queries performed by a block of code.
4
+ #
5
+ # @example
6
+ # expect { subject }.to_not make_database_queries
7
+ #
8
+ # @example
9
+ # expect { subject }.to make_database_queries(count: 1)
10
+ #
11
+ # @see QueryCounter
12
+ RSpec::Matchers.define :make_database_queries do |options = {}|
13
+
14
+ # Taken from ActionView::Helpers::TextHelper
15
+ def pluralize(count, singular, plural = nil)
16
+ word = if count == 1 || count =~ /^1(\.0+)?$/
17
+ singular
18
+ else
19
+ plural || singular.pluralize
20
+ end
21
+
22
+ "#{count || 0} #{word}"
23
+ end
24
+
25
+ match do |block|
26
+ @counter = QueryCounter.new
27
+ ActiveSupport::Notifications.subscribed(@counter.to_proc,
28
+ 'sql.active_record',
29
+ &block)
30
+ if absolute_count = options[:count]
31
+ @counter.count == absolute_count
32
+ else
33
+ @counter.count > 0
34
+ end
35
+ end
36
+
37
+ failure_message_for_should_not do |_|
38
+ <<-EOS
39
+ expected no queries, but #{@counter.count} were made:
40
+ #{@counter.log.join("\n")}
41
+ EOS
42
+ end
43
+
44
+ failure_message_for_should do |_|
45
+ if options[:count]
46
+ expected = pluralize(options[:count], 'query')
47
+ actual = pluralize(@counter.count, 'was', 'were')
48
+
49
+ output = "expected #{expected}, but #{actual} made"
50
+ if @counter.count > 0
51
+ output += ":\n#{@counter.log.join("\n")}"
52
+ end
53
+ output
54
+ else
55
+ 'expected queries, but none were made'
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,43 @@
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
17
+
18
+ def initialize
19
+ @count = 0
20
+ @log = []
21
+ end
22
+
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
30
+
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]
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ # Defines the gem version.
2
+ module DBQueryMatchers
3
+ VERSION = '0.1.2'
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.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Causes Engineering
@@ -78,6 +78,9 @@ extensions: []
78
78
  extra_rdoc_files: []
79
79
  files:
80
80
  - lib/db_query_matchers.rb
81
+ - lib/db_query_matchers/version.rb
82
+ - lib/db_query_matchers/make_database_queries.rb
83
+ - lib/db_query_matchers/query_counter.rb
81
84
  homepage: https://github.com/causes/db-query-matchers
82
85
  licenses:
83
86
  - MIT