Skip to content

Commit 9517841

Browse files
authored
Merge pull request rails#50334 from yykamei/add_doc_for_assert_queries
Add doc for `assert_queries` and `assert_no_queries`
2 parents 970126e + ccc512f commit 9517841

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

activerecord/lib/active_record/testing/query_assertions.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33
module ActiveRecord
44
module Assertions
55
module QueryAssertions
6+
# Asserts that the number of SQL queries executed in the given block matches the expected count.
7+
#
8+
# assert_queries(1) { Post.first }
9+
#
10+
# If the +:matcher+ option is provided, only queries that match the matcher are counted.
11+
#
12+
# assert_queries(1, matcher: /LIMIT \?/) { Post.first }
13+
#
614
def assert_queries(expected_count, matcher: nil, &block)
715
ActiveRecord::Base.connection.materialize_transactions
816

917
queries = []
10-
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
18+
callback = lambda do |*, payload|
1119
queries << payload[:sql] if %w[ SCHEMA TRANSACTION ].exclude?(payload[:name]) && (matcher.nil? || payload[:sql].match(matcher))
1220
end
13-
14-
result = _assert_nothing_raised_or_warn("assert_queries", &block)
15-
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. Queries: #{queries.join("\n\n")}"
16-
result
21+
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
22+
result = _assert_nothing_raised_or_warn("assert_queries", &block)
23+
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. Queries: #{queries.join("\n\n")}"
24+
result
25+
end
1726
end
1827

28+
# Asserts that no SQL queries are executed in the given block.
1929
def assert_no_queries(&block)
2030
assert_queries(0, &block)
2131
end

activerecord/test/cases/assertions/query_assertions_test.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ def test_assert_queries
2222
}
2323
assert_match(/1 instead of 0 queries/, error.message)
2424
end
25-
end
2625

27-
def test_assert_no_queries
28-
assert_no_queries { Post.none }
26+
def test_assert_queries_with_matcher
27+
error = assert_raises(Minitest::Assertion) {
28+
assert_queries(1, matcher: /WHERE "posts"."id" = \? LIMIT \?/) do
29+
Post.where(id: 1).first
30+
end
31+
}
32+
assert_match(/0 instead of 1 queries/, error.message)
33+
end
34+
35+
def test_assert_no_queries
36+
assert_no_queries { Post.none }
2937

30-
error = assert_raises(Minitest::Assertion) {
31-
assert_no_queries { Post.first }
32-
}
33-
assert_match(/1 .* instead of 2/, error.message)
38+
error = assert_raises(Minitest::Assertion) {
39+
assert_no_queries { Post.first }
40+
}
41+
assert_match(/1 instead of 0/, error.message)
42+
end
3443
end
3544
end
3645
end

0 commit comments

Comments
 (0)