rspec-activerecord-expectations 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8090634964dbd7f2f5f8a386cfd0de07257d6f08cd6f9b14606982f983489815
4
+ data.tar.gz: 007eef50aa9f1eb61da08fba2a6e7eb6a4f4114c5e0b4fa68f2cb629d4ef86c6
5
+ SHA512:
6
+ metadata.gz: 6c7072fc9e686e66c750e0e117515094d4b6cde5e764a8b07c750f3d8fa64cfd210f010c8816bbef6a6e0edd6c5af7983f6886e1c0fefe05de19b9a4bf2cf0af
7
+ data.tar.gz: 8196d1a03e6e7b1c73ab3b8524a759cadf554db7fee528a8a350df601661b0b21b7bb1cc3078ea852634aea64c5b6ba9c755ba9ce0232bcef46735e959dd57b1
@@ -0,0 +1,27 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ test:
11
+ strategy:
12
+ matrix:
13
+ os: [ubuntu-latest, macos-latest]
14
+ ruby-version: [3.0, 2.7, 2.6, 2.5]
15
+ runs-on: ${{ matrix.os }}
16
+
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - name: Set up Ruby ${{ matrix.ruby-version }}
20
+ uses: ruby/setup-ruby@v1
21
+ with:
22
+ ruby-version: ${{ matrix.ruby-version }}
23
+ bundler-cache: true
24
+ - name: Install dependencies
25
+ run: bundle install
26
+ - name: Run tests
27
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # this is a gem, don't track the specific deps
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.7.3
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.3
6
+ before_install: gem install bundler -v 2.1.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## [1.0.0] - 2020-12-30
4
+ - Basic gem w/ all the fixins and README and such.
5
+ - Add `less_than` comparison, and `queries` type.
6
+ - Basic tests in place, and not bad tbh.
7
+
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at 302011+jmmastey@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake", "~> 12.0"
6
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Joseph Mastey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,158 @@
1
+ # RSpec Activerecord Expectations
2
+
3
+ Rspec::ActiveRecord::Expectations is a library for testing that your code
4
+ doesn't execute too many queries during execution. In development mode, it's
5
+ common to use gems like [bullet](https://github.com/flyerhzm/bullet), but no
6
+ similar functionality exists in RSpec.
7
+
8
+ Tests of this sort are useful when trying to write regression tests for code
9
+ that frequently has N+1s introduced accidentally by new feature development.
10
+
11
+ While it's possible to use
12
+ [Benchmark.measure](https://ruby-doc.org/stdlib-2.7.3/libdoc/benchmark/rdoc/Benchmark.html#method-c-measure)
13
+ to assert that runtime hasn't changed, tests typically run with a very small
14
+ number of records. This can make it difficult to ascertain whether new problems
15
+ have been introduced. That's because the natural variance in execution time for
16
+ queries takes over the majority of runtime, so benchmarked times are no longer
17
+ valid.
18
+
19
+ This gem introduces a number of new expectation types for your tests that
20
+ should make it easier to express the kinds of verification you're looking for.
21
+
22
+ ## Setup
23
+
24
+ Setup is fairly straightforward. Add this gem to your `Gemfile` in the test
25
+ group.
26
+
27
+ ```ruby
28
+ group 'test' do
29
+ gem 'rspec-activerecord-expectations', '~> 1.0'
30
+ end
31
+ ```
32
+
33
+ Then, include the functionality within your spec helper (or in a support file).
34
+
35
+ ```ruby
36
+ RSpec.configure do |config|
37
+ include RSpec::ActiveRecord::Expectations
38
+ end
39
+ ```
40
+
41
+ That's it! Easy peasy.
42
+
43
+ ## Basic Usage
44
+
45
+ At its core, `Rspec::ActiveRecord::Expectations` is just a series of new
46
+ matchers that you can apply to your tests. Let's take an example piece of app
47
+ functionality.
48
+
49
+ ```ruby
50
+ class WastefulnessReport
51
+ def perform
52
+ (1..100).each do |n|
53
+ ResourceType.find(n).summarize
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ This report obviously needs some refactoring. Let's add a test.
60
+
61
+ ```ruby
62
+ # spec/reports/wastefulness_report_spec.rb
63
+
64
+ RSpec.describe WastefulnessReport do
65
+
66
+ it "is reasonably efficient" do
67
+ expect {
68
+ WastefulnessReport.new.perform
69
+ }.to execute.fewer_than(10).queries
70
+ end
71
+
72
+ end
73
+ ```
74
+
75
+ Running this example, we'll see a usefully failing test!
76
+
77
+ ```ruby
78
+ Failures:
79
+
80
+ 1) WastefulnessReport is reasonably efficient
81
+ Failure/Error:
82
+ expect {
83
+ WastefulnessReport.new.perform
84
+ }.to execute.fewer_than(10).queries
85
+
86
+ expected block to execute fewer than 10 queries, but it executed 100
87
+ # ./spec/reports/wastefulness_report_spec.rb:30:in `block (2 levels) in <top (required)>'
88
+ ```
89
+
90
+ That's it! Refactor your report to be more efficient and then leave the test in
91
+ place to make sure that future developers don't accidentally cause a
92
+ performance regression.
93
+
94
+ ## Supported Matchers
95
+
96
+ ```ruby
97
+ expect {}.to execute.fewer_than(20).queries
98
+ expect {}.to execute.less_than(20).queries # alias for fewer_than
99
+ ```
100
+
101
+ ## Future Planned Functionality
102
+
103
+ This gem still has lots of future functionality. See below.
104
+
105
+ ```ruby
106
+ expect {}.to execute.more_than(20).activerecord_queries
107
+ expect {}.to execute.fewer_than(20).insert_queries
108
+ expect {}.to execute.exactly(5).queries
109
+ expect {}.to execute.at_least(5).handrolled_queries
110
+
111
+ expect {}.to execute.less_than(20).delete_queries
112
+ expect {}.to execute.less_than(20).load_queries
113
+ expect {}.to execute.less_than(20).schema_queries
114
+ expect {}.to execute.less_than(20).exists_queries
115
+ expect {}.to execute.less_than(20).queries
116
+ expect {}.to execute.less_than(20).queries_of_type("Audited::Audit Load")
117
+
118
+ expect {}.not_to rollback_transaction.exactly(5).times
119
+ expect {}.not_to commit_transaction.once
120
+ expect {}.to run_a_transaction
121
+
122
+ expect {}.to create.exactly(5).of_type(User)
123
+ expect {}.to insert.exactly(5).subscription_changes
124
+ expect {}.to update.exactly(2).of_any_type
125
+ expect {}.to delete.exactly(2).of_any_type
126
+
127
+ expect {}.not_to repeatedly_load(Audited::Audit)
128
+
129
+ at_least, more_than, at_most, less_than, lteq?
130
+ exactly(1) => once?
131
+ ```
132
+
133
+ - ignore transactionals (begin / rollback)
134
+ - `name: Foo Load`
135
+ - differentiate AR queries from generic ones? arbitrary execution somehow?
136
+ - warn about warmup
137
+ - make sure we don't smite any built in methods (or from other libs)
138
+
139
+ ## Development
140
+
141
+ After checking out the repo, run `bundle install` to install dependencies.
142
+ Then, run `rake spec` to run the tests.
143
+
144
+ Bug reports and PRs are welcome at
145
+ https://github.com/jmmastey/rspec-activerecord-expectations.
146
+
147
+ ## Code of Conduct
148
+
149
+ Everyone interacting in the Rspec::Activerecord::Expectations project's
150
+ codebases, issue trackers, chat rooms and mailing lists is expected to follow
151
+ the [code of
152
+ conduct](https://github.com/jmmastey/rspec-activerecord-expectations/blob/master/CODE_OF_CONDUCT.md).
153
+
154
+ ## Thanks
155
+
156
+ This gem was heavily patterned after
157
+ [shoulda-matchers](https://github.com/thoughtbot/shoulda-matchers), a project
158
+ by Thoughtbot that has some seriously complex matchers. Thanks y'all.
@@ -0,0 +1,23 @@
1
+ module RSpec::ActiveRecord::Expectations
2
+ class Collector
3
+ def initialize
4
+ @counts = {
5
+ queries: 0
6
+ }
7
+
8
+ ActiveSupport::Notifications.subscribe("sql.active_record", method(:record_query))
9
+ end
10
+
11
+ def queries_of_type(type)
12
+ @counts.fetch(type)
13
+ end
14
+
15
+ def valid_type?(type)
16
+ @counts.include? type
17
+ end
18
+
19
+ def record_query(*_unused, data)
20
+ @counts[:queries] += 1
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module RSpec::ActiveRecord::Expectations
2
+ class NoComparisonError < ArgumentError
3
+ def message
4
+ "You must provide an entire expectation " \
5
+ "(e.g. expect {}.to execute.less_than(n).queries). " \
6
+ "Try appending `less_than` to your expectation."
7
+ end
8
+ end
9
+
10
+ class NoQueryTypeError < ArgumentError
11
+ def message
12
+ "You must provide an entire expectation " \
13
+ "(e.g. expect {}.to execute.less_than(n).queries). " \
14
+ "Try appending `queries` to your expectation."
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,57 @@
1
+ module RSpec::ActiveRecord::Expectations
2
+ module Matchers
3
+ class QueryCountMatcher
4
+ attr_reader :failure_message, :failure_message_when_negated
5
+
6
+ def initialize
7
+ @collector = Collector.new
8
+
9
+ @match_method = nil
10
+ @comparison = nil
11
+ @query_type = nil
12
+ end
13
+
14
+ def supports_block_expectations?
15
+ true
16
+ end
17
+
18
+ def matches?(block)
19
+ raise NoComparisonError unless @match_method
20
+ raise NoQueryTypeError unless @collector.valid_type?(@query_type)
21
+
22
+ result = block.call
23
+
24
+ !!@match_method.call
25
+ end
26
+
27
+ # COMPARISON TYPES
28
+
29
+ def fewer_than(n)
30
+ @comparison = n
31
+ @match_method = method(:match_fewer_than)
32
+ self
33
+ end
34
+ alias_method :less_than, :fewer_than
35
+
36
+ # TARGET QUERY TYPES
37
+
38
+ def queries
39
+ @query_type = :queries
40
+ self
41
+ end
42
+
43
+ private
44
+
45
+ # MATCHERS
46
+
47
+ def match_fewer_than
48
+ count = @collector.queries_of_type(@query_type)
49
+
50
+ @failure_message = "expected block to execute fewer than #{@comparison} queries, but it executed #{count}"
51
+ @failure_message_when_negated = "expected block not to execute fewer than #{@comparison} queries, but it executed #{count}"
52
+
53
+ count < @comparison
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ module RSpec
2
+ module ActiveRecord
3
+ module Expectations
4
+ def execute
5
+ Matchers::QueryCountMatcher.new
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ require_relative 'expectations/errors'
12
+ require_relative 'expectations/collector'
13
+ require_relative 'expectations/matchers/query_count'
@@ -0,0 +1 @@
1
+ require_relative 'activerecord/expectations'
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "rspec-activerecord-expectations"
3
+ spec.version = '1.0.0'
4
+ spec.authors = ["Joseph Mastey"]
5
+ spec.email = ["hello@joemastey.com"]
6
+
7
+ spec.summary = %q{A gem to test how many activerecord queries your code executes.}
8
+ spec.description = %q{Adds new matchers to rspec to help you test whether your code is executing an unreasonable number of queries.}
9
+ spec.homepage = "https://github.com/jmmastey/rspec-activerecord-expectations"
10
+ spec.license = "MIT"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.metadata["source_code_uri"] = spec.homepage
15
+ spec.metadata["changelog_uri"] = "https://github.com/jmmastey/rspec-activerecord-expectations/blob/master/CHANGELOG.md"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "activerecord", ">= 5.0", "< 7.0" # TODO run tests to loosen me
26
+ spec.add_dependency "sqlite3", "~> 1.0"
27
+
28
+ spec.add_development_dependency "pry", "~> 0.0"
29
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-activerecord-expectations
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Mastey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.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.0'
61
+ description: Adds new matchers to rspec to help you test whether your code is executing
62
+ an unreasonable number of queries.
63
+ email:
64
+ - hello@joemastey.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - ".github/workflows/main.yml"
70
+ - ".gitignore"
71
+ - ".rspec"
72
+ - ".ruby-version"
73
+ - ".travis.yml"
74
+ - CHANGELOG.md
75
+ - CODE_OF_CONDUCT.md
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - lib/rspec/activerecord-expectations.rb
80
+ - lib/rspec/activerecord/expectations.rb
81
+ - lib/rspec/activerecord/expectations/collector.rb
82
+ - lib/rspec/activerecord/expectations/errors.rb
83
+ - lib/rspec/activerecord/expectations/matchers/query_count.rb
84
+ - rspec-activerecord-expectations.gemspec
85
+ homepage: https://github.com/jmmastey/rspec-activerecord-expectations
86
+ licenses:
87
+ - MIT
88
+ metadata:
89
+ homepage_uri: https://github.com/jmmastey/rspec-activerecord-expectations
90
+ source_code_uri: https://github.com/jmmastey/rspec-activerecord-expectations
91
+ changelog_uri: https://github.com/jmmastey/rspec-activerecord-expectations/blob/master/CHANGELOG.md
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 2.3.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.1.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A gem to test how many activerecord queries your code executes.
111
+ test_files: []