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_column(model, column_name, msg = nil) ⇒ Object



68
69
70
# File 'lib/active_record/test_case.rb', line 68

def assert_column(model, column_name, msg=nil)
  assert has_column?(model, column_name), msg
end

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



12
13
14
# File 'lib/active_record/test_case.rb', line 12

def assert_date_from_db(expected, actual, message = nil)
  assert_equal expected.to_s, actual.to_s, message
end

#assert_no_column(model, column_name, msg = nil) ⇒ Object



72
73
74
# File 'lib/active_record/test_case.rb', line 72

def assert_no_column(model, column_name, msg=nil)
  assert_not has_column?(model, column_name), msg
end

#assert_no_queries(options = {}, &block) ⇒ Object



63
64
65
66
# File 'lib/active_record/test_case.rb', line 63

def assert_no_queries(options = {}, &block)
  options.reverse_merge! ignore_none: true
  assert_queries(0, options, &block)
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_record/test_case.rb', line 49

def assert_queries(num = 1, options = {})
  ignore_none = options.fetch(:ignore_none) { num == :any }
  SQLCounter.clear_log
  x = yield
  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
  x
end

#assert_sql(*patterns_to_match) ⇒ Object



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

def assert_sql(*patterns_to_match)
  capture_sql { yield }
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

#capture(stream) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_record/test_case.rb', line 16

def capture(stream)
  stream = stream.to_s
  captured_stream = Tempfile.new(stream)
  stream_io = eval("$#{stream}")
  origin_stream = stream_io.dup
  stream_io.reopen(captured_stream)

  yield

  stream_io.rewind
  return captured_stream.read
ensure
  captured_stream.close
  captured_stream.unlink
  stream_io.reopen(origin_stream)
end

#capture_sqlObject



33
34
35
36
37
# File 'lib/active_record/test_case.rb', line 33

def capture_sql
  SQLCounter.clear_log
  yield
  SQLCounter.log_all.dup
end

#has_column?(model, column_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/active_record/test_case.rb', line 76

def has_column?(model, column_name)
  model.reset_column_information
  model.column_names.include?(column_name.to_s)
end

#teardownObject

:nodoc:



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

def teardown
  SQLCounter.clear_log
end