query_matchers 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +1 -0
- data/lib/query_matchers.rb +16 -0
- data/lib/query_matchers/query_counter.rb +34 -0
- data/lib/query_matchers/query_execution_matcher.rb +27 -0
- data/lib/query_matchers/version.rb +3 -0
- data/query_matchers.gemspec +25 -0
- data/spec/query_counter_spec.rb +58 -0
- data/spec/query_execution_matcher_spec.rb +45 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 35b67c78575fc9084086e8370d01dd4260a222f1
|
4
|
+
data.tar.gz: 021976b186936028ddb85da274c7d18796b36fb2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 870b37b067ab66a308f4b3be5e7b5b8f3c974e10125e5662a6a7fe3028ceda654bb7702d9680d8f71f511734cefa3b1b4010a686d12ad63890a21d4f2f3220f4
|
7
|
+
data.tar.gz: 498c380b1fbb2e19b27687f668b9216cd584a61d8f21d3adaf883e8e4cacdb27f6e05e5781df409bed789f2d5d9aea91fb408fadcc2c0ac34cea538926986ab7
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Daniel Schierbeck
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# QueryMatchers
|
2
|
+
|
3
|
+
Match the number of queries performed in any block of code. Allows setting in place regression tests for database performance.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'query_matchers'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install query_matchers
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
expect { magician.magic! }.to execute_queries(43)
|
23
|
+
expect { does_not_hit_the_database }.to execute_no_queries
|
24
|
+
expect { hits_the_database_once }.to execute_one_query
|
25
|
+
```
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'query_matchers/version'
|
2
|
+
require 'query_matchers/query_execution_matcher'
|
3
|
+
|
4
|
+
module QueryMatchers
|
5
|
+
def execute_queries(n)
|
6
|
+
QueryExecutionMatcher.new(n)
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute_one_query
|
10
|
+
QueryExecutionMatcher.new(1)
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute_no_queries
|
14
|
+
QueryExecutionMatcher.new(0)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module QueryMatchers
|
2
|
+
class QueryCounter
|
3
|
+
OPERATIONS = %w(SELECT INSERT UPDATE DELETE)
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@events = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute!(target)
|
10
|
+
ActiveSupport::Notifications.subscribed(subscriber, 'sql.active_record', &target)
|
11
|
+
end
|
12
|
+
|
13
|
+
def query_count
|
14
|
+
@events.size
|
15
|
+
end
|
16
|
+
|
17
|
+
def queries
|
18
|
+
@events.map {|event| event.payload[:sql] }
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def subscriber
|
24
|
+
lambda do |*args|
|
25
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
26
|
+
@events << event if count_query?(event.payload[:sql])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def count_query?(sql)
|
31
|
+
OPERATIONS.any? {|op| sql.start_with?(op) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support/notifications'
|
2
|
+
require 'query_matchers/query_counter'
|
3
|
+
|
4
|
+
module QueryMatchers
|
5
|
+
class QueryExecutionMatcher
|
6
|
+
def initialize(expected, counter = QueryCounter.new)
|
7
|
+
@expected = expected
|
8
|
+
@counter = counter
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(target)
|
12
|
+
@counter.execute!(target)
|
13
|
+
|
14
|
+
@counter.query_count == @expected
|
15
|
+
end
|
16
|
+
|
17
|
+
def failure_message
|
18
|
+
"expected block to execute #{@expected} SQL queries, " <<
|
19
|
+
"but executed #{@counter.query_count}: \n\n" <<
|
20
|
+
@counter.queries.map {|q| " - #{q}" }.join("\n")
|
21
|
+
end
|
22
|
+
|
23
|
+
def negative_failure_message
|
24
|
+
"expected block not to execute #{@expected} SQL queries, but did"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'query_matchers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "query_matchers"
|
8
|
+
spec.version = QueryMatchers::VERSION
|
9
|
+
spec.authors = ["Daniel Schierbeck"]
|
10
|
+
spec.email = ["dasch@zendesk.com"]
|
11
|
+
spec.summary = %q{Match the number of queries performed in any block of code}
|
12
|
+
spec.homepage = "https://github.com/dasch/query_matchers"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency "activesupport", ["> 3.0", "<= 4.1"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.14.0"
|
25
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'active_support/notifications'
|
2
|
+
require 'query_matchers/query_counter'
|
3
|
+
|
4
|
+
describe QueryMatchers::QueryCounter do
|
5
|
+
let(:counter) { described_class.new }
|
6
|
+
|
7
|
+
it "records all relevant SQL operations performed in the target block" do
|
8
|
+
query = "SELECT * FROM somewhere"
|
9
|
+
counter.execute!(sql_target(query))
|
10
|
+
|
11
|
+
counter.queries.should == [query]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "counts the number of queries performed in the target block" do
|
15
|
+
target = proc { 3.times { perform_sql("INSERT INTO jokes") } }
|
16
|
+
counter.execute!(target)
|
17
|
+
|
18
|
+
counter.query_count.should == 3
|
19
|
+
end
|
20
|
+
|
21
|
+
it "counts INSERT queries" do
|
22
|
+
counter.execute!(sql_target("INSERT INTO jokes"))
|
23
|
+
|
24
|
+
counter.query_count.should == 1
|
25
|
+
end
|
26
|
+
|
27
|
+
it "counts UPDATE queries" do
|
28
|
+
counter.execute!(sql_target("UPDATE mood SET laughing = 0"))
|
29
|
+
|
30
|
+
counter.query_count.should == 1
|
31
|
+
end
|
32
|
+
|
33
|
+
it "counts DELETE queries" do
|
34
|
+
counter.execute!(sql_target("DELETE FROM goodwill"))
|
35
|
+
|
36
|
+
counter.query_count.should == 1
|
37
|
+
end
|
38
|
+
|
39
|
+
it "counts SELECT queries" do
|
40
|
+
counter.execute!(sql_target("SELECT FROM inventory"))
|
41
|
+
|
42
|
+
counter.query_count.should == 1
|
43
|
+
end
|
44
|
+
|
45
|
+
it "doesn't count any other type of query" do
|
46
|
+
counter.execute!(sql_target("BREAKDANCE"))
|
47
|
+
|
48
|
+
counter.query_count.should == 0
|
49
|
+
end
|
50
|
+
|
51
|
+
def sql_target(sql)
|
52
|
+
proc { perform_sql(sql) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def perform_sql(sql)
|
56
|
+
ActiveSupport::Notifications.instrument('sql.active_record', sql: sql)
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/core_ext/string/strip'
|
2
|
+
require 'query_matchers/query_execution_matcher'
|
3
|
+
|
4
|
+
describe QueryMatchers::QueryExecutionMatcher do
|
5
|
+
let(:counter) { double("counter", query_count: 0) }
|
6
|
+
let(:matcher) { described_class.new(4, counter) }
|
7
|
+
|
8
|
+
describe "#matches?" do
|
9
|
+
before do
|
10
|
+
counter.stub(:execute!)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "executes the target" do
|
14
|
+
matcher.matches?(:whatever)
|
15
|
+
counter.should have_received(:execute!).with(:whatever)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns true if the number of queries performed matched the expectation" do
|
19
|
+
counter.stub(:query_count) { 4 }
|
20
|
+
matcher.matches?(:whatever).should == true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns false if the number of queries performed doesn't match the expectation" do
|
24
|
+
counter.stub(:query_count) { 3 }
|
25
|
+
matcher.matches?(:whatever).should == false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#failure_message" do
|
30
|
+
it "lists the queries performed in the target" do
|
31
|
+
query1 = "SELECT FROM jokes WHERE puns > 3"
|
32
|
+
query2 = "DELETE FROM jokes WHERE inappropriate = 1"
|
33
|
+
|
34
|
+
counter.stub(:query_count) { 99 }
|
35
|
+
counter.stub(:queries) { [query1, query2] }
|
36
|
+
|
37
|
+
matcher.failure_message.should == <<-MESSAGE.strip_heredoc.chomp
|
38
|
+
expected block to execute 4 SQL queries, but executed 99:
|
39
|
+
|
40
|
+
- SELECT FROM jokes WHERE puns > 3
|
41
|
+
- DELETE FROM jokes WHERE inappropriate = 1
|
42
|
+
MESSAGE
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: query_matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Schierbeck
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>'
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
- - <=
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '4.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>'
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- - <=
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '4.1'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ~>
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.5'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.5'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 2.14.0
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.14.0
|
75
|
+
description:
|
76
|
+
email:
|
77
|
+
- dasch@zendesk.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- .gitignore
|
83
|
+
- Gemfile
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- lib/query_matchers.rb
|
88
|
+
- lib/query_matchers/query_counter.rb
|
89
|
+
- lib/query_matchers/query_execution_matcher.rb
|
90
|
+
- lib/query_matchers/version.rb
|
91
|
+
- query_matchers.gemspec
|
92
|
+
- spec/query_counter_spec.rb
|
93
|
+
- spec/query_execution_matcher_spec.rb
|
94
|
+
homepage: https://github.com/dasch/query_matchers
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.0.14
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Match the number of queries performed in any block of code
|
118
|
+
test_files:
|
119
|
+
- spec/query_counter_spec.rb
|
120
|
+
- spec/query_execution_matcher_spec.rb
|
121
|
+
has_rdoc:
|