mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #44484 from rails/sql-assertions
Implicitly assert no exception is raised in `assert_queries` & al
This commit is contained in:
commit
ce1806d945
6 changed files with 22 additions and 20 deletions
|
@ -22,7 +22,7 @@ if ActiveSupport::TestCase.respond_to?(:fixture_path=)
|
||||||
end
|
end
|
||||||
|
|
||||||
class ActiveSupport::TestCase
|
class ActiveSupport::TestCase
|
||||||
def assert_queries(expected_count)
|
def assert_queries(expected_count, &block)
|
||||||
ActiveRecord::Base.connection.materialize_transactions
|
ActiveRecord::Base.connection.materialize_transactions
|
||||||
|
|
||||||
queries = []
|
queries = []
|
||||||
|
@ -30,9 +30,9 @@ class ActiveSupport::TestCase
|
||||||
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
|
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
|
||||||
end
|
end
|
||||||
|
|
||||||
yield.tap do
|
result = _assert_nothing_raised_or_warn("assert_queries", &block)
|
||||||
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
|
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
|
||||||
end
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_no_queries(&block)
|
def assert_no_queries(&block)
|
||||||
|
|
|
@ -25,7 +25,7 @@ class RelationCacheTest < ActionView::TestCase
|
||||||
|
|
||||||
def view_cache_dependencies; []; end
|
def view_cache_dependencies; []; end
|
||||||
|
|
||||||
def assert_queries(num)
|
def assert_queries(num, &block)
|
||||||
ActiveRecord::Base.connection.materialize_transactions
|
ActiveRecord::Base.connection.materialize_transactions
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ class RelationCacheTest < ActionView::TestCase
|
||||||
count += 1 unless ["SCHEMA", "TRANSACTION"].include? payload[:name]
|
count += 1 unless ["SCHEMA", "TRANSACTION"].include? payload[:name]
|
||||||
end
|
end
|
||||||
|
|
||||||
result = yield
|
result = _assert_nothing_raised_or_warn("assert_queries", &block)
|
||||||
assert_equal num, count, "#{count} instead of #{num} queries were executed."
|
assert_equal num, count, "#{count} instead of #{num} queries were executed."
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
|
@ -489,9 +489,11 @@ class QueryCacheTest < ActiveRecord::TestCase
|
||||||
payload[:sql].downcase!
|
payload[:sql].downcase!
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_raises FrozenError do
|
ActiveRecord::Base.cache do
|
||||||
ActiveRecord::Base.cache do
|
assert_queries(1) do
|
||||||
assert_queries(1) { Task.find(1); Task.find(1) }
|
assert_raises FrozenError do
|
||||||
|
Task.find(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
|
|
|
@ -38,8 +38,8 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_sql(*patterns_to_match, &block)
|
def assert_sql(*patterns_to_match, &block)
|
||||||
capture_sql(&block)
|
_assert_nothing_raised_or_warn("assert_sql") { capture_sql(&block) }
|
||||||
ensure
|
|
||||||
failed_patterns = []
|
failed_patterns = []
|
||||||
patterns_to_match.each do |pattern|
|
patterns_to_match.each do |pattern|
|
||||||
failed_patterns << pattern unless SQLCounter.log_all.any? { |sql| pattern === sql }
|
failed_patterns << pattern unless SQLCounter.log_all.any? { |sql| pattern === sql }
|
||||||
|
@ -47,11 +47,11 @@ module ActiveRecord
|
||||||
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found.#{SQLCounter.log.size == 0 ? '' : "\nQueries:\n#{SQLCounter.log.join("\n")}"}"
|
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found.#{SQLCounter.log.size == 0 ? '' : "\nQueries:\n#{SQLCounter.log.join("\n")}"}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_queries(num = 1, options = {})
|
def assert_queries(num = 1, options = {}, &block)
|
||||||
ignore_none = options.fetch(:ignore_none) { num == :any }
|
ignore_none = options.fetch(:ignore_none) { num == :any }
|
||||||
ActiveRecord::Base.connection.materialize_transactions
|
ActiveRecord::Base.connection.materialize_transactions
|
||||||
SQLCounter.clear_log
|
SQLCounter.clear_log
|
||||||
x = yield
|
x = _assert_nothing_raised_or_warn("assert_queries", &block)
|
||||||
the_log = ignore_none ? SQLCounter.log_all : SQLCounter.log
|
the_log = ignore_none ? SQLCounter.log_all : SQLCounter.log
|
||||||
if num == :any
|
if num == :any
|
||||||
assert_operator the_log.size, :>=, 1, "1 or more queries expected, but none were executed."
|
assert_operator the_log.size, :>=, 1, "1 or more queries expected, but none were executed."
|
||||||
|
|
|
@ -1121,9 +1121,9 @@ class TransactionTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_raising_does_not_materialize_transaction
|
def test_raising_does_not_materialize_transaction
|
||||||
assert_raise(RuntimeError) do
|
assert_no_queries do
|
||||||
assert_no_queries do
|
assert_raise(RuntimeError) do
|
||||||
Topic.transaction { raise }
|
Topic.transaction { raise "Expected" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -61,7 +61,7 @@ class ActiveSupport::TestCase
|
||||||
ActiveStorage::Current.reset
|
ActiveStorage::Current.reset
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_queries(expected_count)
|
def assert_queries(expected_count, &block)
|
||||||
ActiveRecord::Base.connection.materialize_transactions
|
ActiveRecord::Base.connection.materialize_transactions
|
||||||
|
|
||||||
queries = []
|
queries = []
|
||||||
|
@ -69,9 +69,9 @@ class ActiveSupport::TestCase
|
||||||
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
|
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
|
||||||
end
|
end
|
||||||
|
|
||||||
yield.tap do
|
result = _assert_nothing_raised_or_warn("assert_queries", &block)
|
||||||
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
|
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
|
||||||
end
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_no_queries(&block)
|
def assert_no_queries(&block)
|
||||||
|
|
Loading…
Reference in a new issue