Skip to content
This repository
Newer
Older
100644 95 lines (78 sloc) 3.499 kb
6fe02f9a » tenderlove
2012-01-24 deprecated AR::TestCase in favor of AS::TestCase
1 require 'active_support/test_case'
2
3 ActiveSupport::Deprecation.warn('ActiveRecord::TestCase is deprecated, please use ActiveSupport::TestCase')
b17eb65d » jeremy
2008-11-12 Move fixtures settings from AR::TestCase to railties test_help
4 module ActiveRecord
4e3eec3d » rizwanreza
2010-06-16 Adds title and proper formatting to docs.
5 # = Active Record Test Case
b451de0d » spastorino
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
6 #
4e3eec3d » rizwanreza
2010-06-16 Adds title and proper formatting to docs.
7 # Defines some test assertions to test against SQL queries.
39814fcc » jeremy
2008-01-21 Merge branch 'ar-test-cleanup' of git://git.geeksomnia.com/rails
8 class TestCase < ActiveSupport::TestCase #:nodoc:
39489d13 » tenderlove
2012-01-04 clear the sql counter on teardown so the GC can do it's thing
9 def teardown
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
10 SQLCounter.clear_log
39489d13 » tenderlove
2012-01-04 clear the sql counter on teardown so the GC can do it's thing
11 end
12
39814fcc » jeremy
2008-01-21 Merge branch 'ar-test-cleanup' of git://git.geeksomnia.com/rails
13 def assert_date_from_db(expected, actual, message = nil)
8e4624be » metaskills
2008-11-19 Remove SQL Server cases from tests for latest adapter work to pass ra…
14 # SybaseAdapter doesn't have a separate column type just for dates,
39814fcc » jeremy
2008-01-21 Merge branch 'ar-test-cleanup' of git://git.geeksomnia.com/rails
15 # so the time is in the string and incorrectly formatted
8e4624be » metaskills
2008-11-19 Remove SQL Server cases from tests for latest adapter work to pass ra…
16 if current_adapter?(:SybaseAdapter)
39814fcc » jeremy
2008-01-21 Merge branch 'ar-test-cleanup' of git://git.geeksomnia.com/rails
17 assert_equal expected.to_s, actual.to_date.to_s, message
18 else
19 assert_equal expected.to_s, actual.to_s, message
20 end
21 end
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
22
23 def assert_sql(*patterns_to_match)
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
24 SQLCounter.clear_log
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
25 yield
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
26 SQLCounter.log_all
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
27 ensure
28 failed_patterns = []
29 patterns_to_match.each do |pattern|
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
30 failed_patterns << pattern unless SQLCounter.log_all.any?{ |sql| pattern === sql }
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
31 end
32 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")}"}"
33 end
34
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
35 def assert_queries(num = 1, options = {})
36 ignore_none = options.fetch(:ignore_none) { num == :any }
37 SQLCounter.clear_log
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
38 yield
39 ensure
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
40 the_log = ignore_none ? SQLCounter.log_all : SQLCounter.log
41 if num == :any
42 assert_operator the_log.size, :>=, 1, "1 or more queries expected, but none were executed."
43 else
44 mesg = "#{the_log.size} instead of #{num} queries were executed.#{the_log.size == 0 ? '' : "\nQueries:\n#{the_log.join("\n")}"}"
45 assert_equal num, the_log.size, mesg
46 end
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
47 end
48
49 def assert_no_queries(&block)
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
50 assert_queries(0, :ignore_none => true, &block)
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
51 end
52
53 end
54
55 class SQLCounter
56 class << self
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
57 attr_accessor :ignored_sql, :log, :log_all
58 def clear_log; self.log = []; self.log_all = []; end
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
59 end
60
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
61 self.clear_log
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
62
63 self.ignored_sql = [/^PRAGMA (?!(table_info))/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /^SHOW max_identifier_length/, /^BEGIN/, /^COMMIT/]
64
65 # FIXME: this needs to be refactored so specific database can add their own
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
66 # ignored SQL, or better yet, use a different notification for the queries
67 # instead examining the SQL content.
68 oracle_ignored = [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO/, /^\s*select .* from all_triggers/im]
69 mysql_ignored = [/^SHOW TABLES/i, /^SHOW FULL FIELDS/]
70 postgresql_ignored = [/^\s*select\b.*\bfrom\b.*pg_namespace\b/im, /^\s*select\b.*\battname\b.*\bfrom\b.*\bpg_attribute\b/im]
71
72 [oracle_ignored, mysql_ignored, postgresql_ignored].each do |db_ignored_sql|
73 ignored_sql.concat db_ignored_sql
74 end
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
75
76 attr_reader :ignore
77
78 def initialize(ignore = Regexp.union(self.class.ignored_sql))
79 @ignore = ignore
80 end
81
82 def call(name, start, finish, message_id, values)
83 sql = values[:sql]
84
85 # FIXME: this seems bad. we should probably have a better way to indicate
86 # the query was cached
88d6ae30 » Steve Jorgensen
2012-06-08 Fix fragile #assert_queries implementation and usages.
87 return if 'CACHE' == values[:name]
88
89 self.class.log_all << sql
90 self.class.log << sql unless ignore =~ sql
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
91 end
39814fcc » jeremy
2008-01-21 Merge branch 'ar-test-cleanup' of git://git.geeksomnia.com/rails
92 end
33514a17 » jeremy
2012-02-05 Get ActiveRecord::TestCase back on its feet, despite deprecation. It …
93
94 ActiveSupport::Notifications.subscribe('sql.active_record', SQLCounter.new)
39814fcc » jeremy
2008-01-21 Merge branch 'ar-test-cleanup' of git://git.geeksomnia.com/rails
95 end
Something went wrong with that request. Please try again.