Skip to content

Commit

Permalink
Allow only one regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Dec 19, 2023
1 parent 7746f2b commit a22acbd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
29 changes: 9 additions & 20 deletions activerecord/lib/active_record/testing/query_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,17 @@ def assert_queries(expected_count = nil, matcher: nil, ignore_none: false, &bloc
ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do
result = _assert_nothing_raised_or_warn("assert_queries", &block)
queries = ignore_none ? counter.log_all : counter.log

if expected_count.nil? && matcher
failed_patterns = []
matcher = Array(matcher)
matcher.each do |pattern|
failed_patterns << pattern unless queries.any? { |query| pattern === query }
end
assert_predicate failed_patterns, :empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found.#{queries.empty? ? '' : "\nQueries:\n#{queries.join("\n")}"}"
else
matched_queries =
if matcher
matcher = Array(matcher)
queries.select { |query| matcher.any? { |pattern| pattern === query } }
else
queries
end

if expected_count.nil?
assert_operator matched_queries.size, :>=, 1, "1 or more queries expected, but none were executed."
matched_queries =
if matcher
queries.select { |query| query.match?(matcher) }
else
assert_equal expected_count, matched_queries.size, "#{matched_queries.size} instead of #{expected_count} queries were executed. Queries: #{matched_queries.join("\n\n")}"
queries
end

if expected_count
assert_equal expected_count, matched_queries.size, "#{matched_queries.size} instead of #{expected_count} queries were executed. Queries: #{matched_queries.join("\n\n")}"
else
assert_operator matched_queries.size, :>=, 1, "1 or more queries expected, but none were executed.#{queries.empty? ? '' : "\nQueries:\n#{queries.join("\n")}"}"
end

result
Expand Down
8 changes: 0 additions & 8 deletions activerecord/test/cases/assertions/query_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ def test_assert_queries_with_matcher
assert_match(/0 instead of 1 queries/, error.message)
end

def test_assert_queries_with_multiple_matchers
assert_raises(Minitest::Assertion, match: "Query pattern(s) /GROUP/ not found") do
assert_queries(matcher: [/GROUP/, /ORDER BY/]) do
Post.where(id: 1).first
end
end
end

def test_assert_queries_when_there_are_no_queries
assert_raises(Minitest::Assertion, match: "1 or more queries expected, but none were executed") do
assert_queries { Post.none }
Expand Down
8 changes: 7 additions & 1 deletion activerecord/test/cases/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ def capture_sql
end

def assert_sql(*patterns_to_match, &block)
assert_queries(ignore_none: true, matcher: patterns_to_match, &block)
_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_predicate 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_column(model, column_name, msg = nil)
Expand Down

0 comments on commit a22acbd

Please sign in to comment.