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
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
def assert_queries(expected_count)
|
||||
def assert_queries(expected_count, &block)
|
||||
ActiveRecord::Base.connection.materialize_transactions
|
||||
|
||||
queries = []
|
||||
|
@ -30,9 +30,9 @@ class ActiveSupport::TestCase
|
|||
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
|
||||
end
|
||||
|
||||
yield.tap do
|
||||
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
|
||||
end
|
||||
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}"
|
||||
result
|
||||
end
|
||||
|
||||
def assert_no_queries(&block)
|
||||
|
|
|
@ -25,7 +25,7 @@ class RelationCacheTest < ActionView::TestCase
|
|||
|
||||
def view_cache_dependencies; []; end
|
||||
|
||||
def assert_queries(num)
|
||||
def assert_queries(num, &block)
|
||||
ActiveRecord::Base.connection.materialize_transactions
|
||||
count = 0
|
||||
|
||||
|
@ -33,7 +33,7 @@ class RelationCacheTest < ActionView::TestCase
|
|||
count += 1 unless ["SCHEMA", "TRANSACTION"].include? payload[:name]
|
||||
end
|
||||
|
||||
result = yield
|
||||
result = _assert_nothing_raised_or_warn("assert_queries", &block)
|
||||
assert_equal num, count, "#{count} instead of #{num} queries were executed."
|
||||
result
|
||||
end
|
||||
|
|
|
@ -489,9 +489,11 @@ class QueryCacheTest < ActiveRecord::TestCase
|
|||
payload[:sql].downcase!
|
||||
end
|
||||
|
||||
assert_raises FrozenError do
|
||||
ActiveRecord::Base.cache do
|
||||
assert_queries(1) { Task.find(1); Task.find(1) }
|
||||
ActiveRecord::Base.cache do
|
||||
assert_queries(1) do
|
||||
assert_raises FrozenError do
|
||||
Task.find(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
ensure
|
||||
|
|
|
@ -38,8 +38,8 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def assert_sql(*patterns_to_match, &block)
|
||||
capture_sql(&block)
|
||||
ensure
|
||||
_assert_nothing_raised_or_warn("assert_sql") { capture_sql(&block) }
|
||||
|
||||
failed_patterns = []
|
||||
patterns_to_match.each do |pattern|
|
||||
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")}"}"
|
||||
end
|
||||
|
||||
def assert_queries(num = 1, options = {})
|
||||
def assert_queries(num = 1, options = {}, &block)
|
||||
ignore_none = options.fetch(:ignore_none) { num == :any }
|
||||
ActiveRecord::Base.connection.materialize_transactions
|
||||
SQLCounter.clear_log
|
||||
x = yield
|
||||
x = _assert_nothing_raised_or_warn("assert_queries", &block)
|
||||
the_log = ignore_none ? SQLCounter.log_all : SQLCounter.log
|
||||
if num == :any
|
||||
assert_operator the_log.size, :>=, 1, "1 or more queries expected, but none were executed."
|
||||
|
|
|
@ -1121,9 +1121,9 @@ class TransactionTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_raising_does_not_materialize_transaction
|
||||
assert_raise(RuntimeError) do
|
||||
assert_no_queries do
|
||||
Topic.transaction { raise }
|
||||
assert_no_queries do
|
||||
assert_raise(RuntimeError) do
|
||||
Topic.transaction { raise "Expected" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -61,7 +61,7 @@ class ActiveSupport::TestCase
|
|||
ActiveStorage::Current.reset
|
||||
end
|
||||
|
||||
def assert_queries(expected_count)
|
||||
def assert_queries(expected_count, &block)
|
||||
ActiveRecord::Base.connection.materialize_transactions
|
||||
|
||||
queries = []
|
||||
|
@ -69,9 +69,9 @@ class ActiveSupport::TestCase
|
|||
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
|
||||
end
|
||||
|
||||
yield.tap do
|
||||
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
|
||||
end
|
||||
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}"
|
||||
result
|
||||
end
|
||||
|
||||
def assert_no_queries(&block)
|
||||
|
|
Loading…
Reference in a new issue