-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
107 additions
and
6 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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Keypairs | ||
VERSION = '2.0.0.develop' | ||
VERSION = '1.3.4.develop' | ||
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
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
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
# Based on https://stackoverflow.com/a/13423584 | ||
RSpec::Matchers.define :exceed_query_limit do |expected| | ||
match do |block| | ||
# For now we will need to exclude the keypairs since its value is not cached | ||
@excluded_queries = [/SELECT "public"\."keypairs"\.\* FROM "public"\."keypairs"/] | ||
|
||
query_count(&block) > expected | ||
end | ||
|
||
failure_message_when_negated do |_actual| | ||
queries = @queries.map do |query| | ||
if query[:location] | ||
<<~TEXT | ||
#{query[:name]}: #{query[:sql]} | ||
↳ #{query[:location]} | ||
TEXT | ||
else | ||
<<~TEXT | ||
#{query[:name]}: #{query[:sql]} | ||
TEXT | ||
end | ||
end.join.indent(4) | ||
|
||
<<~TEXT | ||
Expected to run maximum #{expected} queries, got #{@query_count}: | ||
#{queries} | ||
TEXT | ||
end | ||
|
||
chain(:with_excluded_query) do |*excluded_queries| | ||
@excluded_queries.push(*excluded_queries) | ||
end | ||
|
||
def query_count(&block) | ||
@query_count = 0 | ||
@queries = [] | ||
ActiveSupport::Notifications.subscribed(method(:query_callback), 'sql.active_record', &block) | ||
@query_count | ||
end | ||
|
||
def query_callback(_name, _start, _finish, _message_id, values) | ||
return if %w[CACHE SCHEMA].include?(values[:name]) | ||
|
||
@excluded_queries&.each do |excluded_query| | ||
return if excluded_query.match?(values[:sql]) | ||
end | ||
|
||
@query_count += 1 | ||
@queries << { sql: values[:sql], name: values[:name], location: Rails.backtrace_cleaner.clean(caller).first } | ||
end | ||
|
||
def supports_block_expectations? | ||
true | ||
end | ||
end |