forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rails#50281 from p8/activerecord/assert-queries
Expose `assert_queries` and `assert_no_queries` assertions
- Loading branch information
Showing
7 changed files
with
85 additions
and
48 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
24 changes: 24 additions & 0 deletions
24
activerecord/lib/active_record/testing/query_assertions.rb
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module Assertions | ||
module QueryAssertions | ||
def assert_queries(expected_count, matcher: nil, &block) | ||
ActiveRecord::Base.connection.materialize_transactions | ||
|
||
queries = [] | ||
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload| | ||
queries << payload[:sql] if %w[ SCHEMA TRANSACTION ].exclude?(payload[:name]) && (matcher.nil? || payload[:sql].match(matcher)) | ||
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: #{queries.join("\n\n")}" | ||
result | ||
end | ||
|
||
def assert_no_queries(&block) | ||
assert_queries(0, &block) | ||
end | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
activerecord/test/cases/assertions/query_assertions_test.rb
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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "models/post" | ||
require "active_record/testing/query_assertions" | ||
|
||
module ActiveRecord | ||
module Assertions | ||
class QueryAssertionsTest < ActiveSupport::TestCase | ||
include QueryAssertions | ||
|
||
def test_assert_queries | ||
assert_queries(1) { Post.first } | ||
|
||
error = assert_raises(Minitest::Assertion) { | ||
assert_queries(2) { Post.first } | ||
} | ||
assert_match(/1 instead of 2 queries/, error.message) | ||
|
||
error = assert_raises(Minitest::Assertion) { | ||
assert_queries(0) { Post.first } | ||
} | ||
assert_match(/1 instead of 0 queries/, error.message) | ||
end | ||
end | ||
|
||
def test_assert_no_queries | ||
assert_no_queries { Post.none } | ||
|
||
error = assert_raises(Minitest::Assertion) { | ||
assert_no_queries { Post.first } | ||
} | ||
assert_match(/1 .* instead of 2/, error.message) | ||
end | ||
end | ||
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