Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add assert_sql helper method to check for specific SQL output in Acti…
…ve Record test suite. [#325 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
gtd authored and jeremy committed Jul 15, 2008
1 parent 8a548e4 commit 97fa854
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 13 additions & 2 deletions activerecord/lib/active_record/test_case.rb
Expand Up @@ -22,11 +22,22 @@ def assert_date_from_db(expected, actual, message = nil)
end
end

def assert_sql(*patterns_to_match)
$queries_executed = []
yield
ensure
failed_patterns = []
patterns_to_match.each do |pattern|
failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql }
end
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found."
end

def assert_queries(num = 1)
$query_count = 0
$queries_executed = []
yield
ensure
assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed."
end

def assert_no_queries(&block)
Expand Down
10 changes: 5 additions & 5 deletions activerecord/test/cases/helper.rb
Expand Up @@ -32,13 +32,13 @@ def uses_mocha(description)
ActiveRecord::Base.connection.class.class_eval do
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/]

def execute_with_counting(sql, name = nil, &block)
$query_count ||= 0
$query_count += 1 unless IGNORED_SQL.any? { |r| sql =~ r }
execute_without_counting(sql, name, &block)
def execute_with_query_record(sql, name = nil, &block)
$queries_executed ||= []
$queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
execute_without_query_record(sql, name, &block)
end

alias_method_chain :execute, :counting
alias_method_chain :execute, :query_record
end

# Make with_scope public for tests
Expand Down

0 comments on commit 97fa854

Please sign in to comment.