Skip to content

Commit

Permalink
Implicitly assert no exception is raised in assert_queries & al
Browse files Browse the repository at this point in the history
Fix: #44397
Ref: #37313
Ref: #42459

This avoid mistakes such as:

```ruby
assert_raise Something do
  assert_queries(1) do
    raise Something
  end
end
```

Co-Authored-By: Alex Coomans <alexc@squareup.com>
  • Loading branch information
byroot and drcapulet committed Feb 19, 2022
1 parent 15a2ff1 commit 196558c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 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: 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
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 196558c

Please sign in to comment.