rspec-query-limit 0.1.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.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in query_limit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013-2014 Monterail.com LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Rspec custom matchers for Rails query counter
2
+
3
+ ## Features
4
+
5
+ - Prevent N+1 problem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rspec-query-limit'
13
+ ```
14
+
15
+ And then execute:
16
+ ```bash
17
+ $ bundle
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Add to your ```spec/spec_helper.rb```
23
+
24
+ ```ruby
25
+ require 'rspec/query_limit'
26
+ ```
27
+
28
+ And then you can use matchers
29
+ ```ruby
30
+ describe UserEmails do
31
+ describe '#primary_emails' do
32
+ users = User.all
33
+ expect { UserEmails.primary_emails(users) }.to query_limit_eq(2)
34
+ end
35
+ end
36
+ ```
37
+
38
+ ## List of query limit matchers
39
+ Numbers of queries must be equal
40
+
41
+ ```
42
+ query_limit_eq(number)
43
+ ```
44
+
45
+ ## Inspiration from
46
+ [Stackoverflow](http://stackoverflow.com/questions/5490411/counting-the-number-of-queries-performed)
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1 @@
1
+ require 'rspec/query_limit'
@@ -0,0 +1,3 @@
1
+ require 'rspec/query_limit/version'
2
+ require 'rspec/query_limit/query_counter'
3
+ require 'rspec/query_limit/matchers'
@@ -0,0 +1,29 @@
1
+ module Rspec
2
+ module QueryLimit
3
+ RSpec::Matchers.define :query_limit_eq do |expected|
4
+
5
+ match do |block|
6
+ query_count(&block) == expected
7
+ end
8
+
9
+ failure_message_for_should do |actual|
10
+ "Expected to run exactly #{expected} queries, got #{@counter.query_count}"
11
+ end
12
+
13
+ failure_message_for_should_not do |actual|
14
+ "Expected to run other than #{expected} queries, got #{@counter.query_count}"
15
+ end
16
+
17
+ def query_count(&block)
18
+ @counter = ActiveRecord::QueryCounter.new
19
+ ActiveSupport::Notifications.subscribed(@counter.to_proc, 'sql.active_record', &block)
20
+ @counter.query_count
21
+ end
22
+
23
+ def supports_block_expectations?
24
+ true
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ module Rspec
2
+ module QueryLimit
3
+ module ActiveRecord
4
+ class QueryCounter
5
+
6
+ attr_reader :query_count
7
+
8
+ def initialize
9
+ @query_count = 0
10
+ end
11
+
12
+ def to_proc
13
+ lambda(&method(:callback))
14
+ end
15
+
16
+ def callback(name, start, finish, message_id, values)
17
+ @query_count += 1 unless %w(CACHE SCHEMA).include?(values[:name])
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module QueryLimit
3
+ VERSION = "0.1.0"
4
+ end
5
+ 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 'rspec/query_limit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rspec-query-limit"
8
+ spec.version = Rspec::QueryLimit::VERSION
9
+ spec.authors = ["Jakub Cieslar"]
10
+ spec.email = ["cieslar.jakub@gmail.com"]
11
+ spec.summary = %q{Rspec custom matchers for Rails query counter}
12
+ spec.description = %q{Rspec custom matchers for Rails query counter}
13
+ spec.homepage = "https://github.com/monterail/rspec-query-limit"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'rspec-rails'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-query-limit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jakub Cieslar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
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
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Rspec custom matchers for Rails query counter
63
+ email:
64
+ - cieslar.jakub@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - lib/rspec-query-limit.rb
74
+ - lib/rspec/query_limit.rb
75
+ - lib/rspec/query_limit/matchers.rb
76
+ - lib/rspec/query_limit/query_counter.rb
77
+ - lib/rspec/query_limit/version.rb
78
+ - rspec-query-limit.gemspec
79
+ homepage: https://github.com/monterail/rspec-query-limit
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23.2
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Rspec custom matchers for Rails query counter
104
+ test_files: []