Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/n_plus_one_control/rspec/matchers/perform_linear_number_of_queries.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Metrics/BlockLength | ||
::RSpec::Matchers.define :perform_linear_number_of_queries do |slope: 1, intercept: 1| | ||
supports_block_expectations | ||
|
||
match do |actual, *_args| | ||
raise ArgumentError, "Block is required" unless actual.is_a? Proc | ||
|
||
raise "Missing tag :n_plus_one" unless | ||
@matcher_execution_context.respond_to?(:n_plus_one_populate) | ||
|
||
populate = @matcher_execution_context.n_plus_one_populate | ||
|
||
@matcher_execution_context.executor = NPlusOneControl::Executor.new( | ||
population: populate, | ||
matching: nil, | ||
scale_factors: @factors | ||
) | ||
|
||
@queries = @matcher_execution_context.executor.call(&actual) | ||
|
||
@queries.all? do |scale, queries| | ||
queries.size <= slope * scale + intercept | ||
end | ||
end | ||
|
||
match_when_negated do |_actual| | ||
raise "This matcher doesn't support negation" | ||
end | ||
|
||
# TODO: failure message for linear queries | ||
# failure_message { |_actual| NPlusOneControl.failure_message(@queries) } | ||
end | ||
# rubocop:enable Metrics/BlockLength |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
spec/n_plus_one_control/rspec/matchers/perform_linear_number_of_queries_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe NPlusOneControl::RSpec do | ||
describe 'perform_linear_number_of_queries' do | ||
context "when constant queries", :n_plus_one do | ||
populate { |n| create_list(:post, n) } | ||
|
||
specify do | ||
expect { Post.preload(:user).find_each { |p| p.user.name } } | ||
.not_to raise_error(RSpec::Expectations::ExpectationNotMetError) | ||
end | ||
end | ||
|
||
context "when has linear query", :n_plus_one do | ||
populate { |n| create_list(:post, n) } | ||
|
||
specify do | ||
expect { Post.find_each { |p| p.user.name } } | ||
.to perform_linear_number_of_queries | ||
end | ||
end | ||
|
||
context "when has linear query larger than expected slope", :n_plus_one do | ||
populate { |n| create_list(:post, n) } | ||
|
||
specify do | ||
expect do | ||
expect { Post.find_each { |p| "#{p.user.name} #{p.category.name}" } } | ||
.to perform_linear_number_of_queries | ||
end.to raise_error(RSpec::Expectations::ExpectationNotMetError) | ||
end | ||
end | ||
|
||
context "when context is missing" do | ||
specify do | ||
expect do | ||
expect { subject }.to perform_linear_number_of_queries | ||
end.to raise_error(/missing tag/i) | ||
end | ||
end | ||
|
||
context "when negated" do | ||
specify do | ||
expect do | ||
expect { subject }.not_to perform_linear_number_of_queries | ||
end.to raise_error(/support negation/i) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
ActiveRecord::Schema.define do | ||
create_table :categories do |t| | ||
t.string :name | ||
end | ||
end | ||
|
||
class Category < ActiveRecord::Base | ||
has_many :posts | ||
end | ||
|
||
FactoryGirl.define do | ||
factory :category do | ||
name "Category" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters