Skip to content

Commit

Permalink
Merge pull request #44484 from rails/sql-assertions
Browse files Browse the repository at this point in the history
Implicitly assert no exception is raised in `assert_queries` & al
  • Loading branch information
byroot committed Feb 19, 2022
2 parents 15a2ff1 + e26372b commit ce1806d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
8 changes: 4 additions & 4 deletions actiontext/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
end

class ActiveSupport::TestCase
def assert_queries(expected_count)
def assert_queries(expected_count, &block)
ActiveRecord::Base.connection.materialize_transactions

queries = []
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
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)
Expand Down
4 changes: 2 additions & 2 deletions actionview/test/activerecord/relation_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def test_cache_relation_other

def view_cache_dependencies; []; end

def assert_queries(num)
def assert_queries(num, &block)
ActiveRecord::Base.connection.materialize_transactions
count = 0

ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start, _finish, _id, payload|
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
Expand Down
8 changes: 5 additions & 3 deletions activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,11 @@ def test_query_cache_does_not_allow_sql_key_mutation
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
Expand Down
8 changes: 4 additions & 4 deletions activerecord/test/cases/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ def capture_sql
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 }
end
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."
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1121,9 +1121,9 @@ def test_savepoint_does_not_materialize_transaction
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
Expand Down
8 changes: 4 additions & 4 deletions activestorage/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ class ActiveSupport::TestCase
ActiveStorage::Current.reset
end

def assert_queries(expected_count)
def assert_queries(expected_count, &block)
ActiveRecord::Base.connection.materialize_transactions

queries = []
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
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)
Expand Down

0 comments on commit ce1806d

Please sign in to comment.