Class: ActiveRecord::TestCase

Inherits:
ActiveSupport::TestCase
  • Object
show all
Defined in:
lib/active_record/test_case.rb

Overview

Active Record Test Case

Defines some test assertions to test against SQL queries.

Instance Method Summary collapse

Instance Method Details

#assert_date_from_db(expected, actual, message = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/active_record/test_case.rb', line 13

def assert_date_from_db(expected, actual, message = nil)
  # SybaseAdapter doesn't have a separate column type just for dates,
  # so the time is in the string and incorrectly formatted
  if current_adapter?(:SybaseAdapter)
    assert_equal expected.to_s, actual.to_date.to_s, message
  else
    assert_equal expected.to_s, actual.to_s, message
  end
end

#assert_no_queries(&block) ⇒ Object



49
50
51
# File 'lib/active_record/test_case.rb', line 49

def assert_no_queries(&block)
  assert_queries(0, :ignore_none => true, &block)
end

#assert_queries(num = 1, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_record/test_case.rb', line 35

def assert_queries(num = 1, options = {})
  ignore_none = options.fetch(:ignore_none) { num == :any }
  SQLCounter.clear_log
  yield
ensure
  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."
  else
    mesg = "#{the_log.size} instead of #{num} queries were executed.#{the_log.size == 0 ? '' : "\nQueries:\n#{the_log.join("\n")}"}"
    assert_equal num, the_log.size, mesg
  end
end

#assert_sql(*patterns_to_match) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_record/test_case.rb', line 23

def assert_sql(*patterns_to_match)
  SQLCounter.clear_log
  yield
  SQLCounter.log_all
ensure
  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{ |p| p.inspect }.join(', ')} not found.#{SQLCounter.log.size == 0 ? '' : "\nQueries:\n#{SQLCounter.log.join("\n")}"}"
end

#teardownObject

:nodoc:



9
10
11
# File 'lib/active_record/test_case.rb', line 9

def teardown
  SQLCounter.clear_log
end