merged cont.
[opensuse:yast-rest-service.git] / webyast / vendor / plugins / static_record_cache / test / test_helper.rb
1
2
3 require 'ostruct'
4
5 require File.dirname(__FILE__)+"/../../../../config/boot"
6
7 begin ; require 'active_record' ; rescue LoadError;  require 'rubygems'; require 'active_record'; end
8
9 require 'active_support'
10 require 'active_record'
11 require 'active_record/version'
12 require 'active_record/fixtures'
13 require 'mocha'
14 require 'test/unit'
15 require 'fileutils'
16
17
18 dir = File.dirname(__FILE__)
19
20 require File.join(dir, '/../init')
21
22 ActiveRecord::Base.logger = Logger.new("debug.log")
23
24 config = ActiveRecord::Base.configurations['test'] = {
25   :adapter  => "mysql",
26   :username => "root",
27   :encoding => "utf8",
28   :host => '127.0.0.1',
29   :database => 'static_record_cache' }
30
31 ActiveRecord::Base.establish_connection( config )
32
33
34 if File.exists?(active_record_context_dir = File.join(dir, '../../active_record_context'))
35   require File.join(active_record_context_dir, '/lib/technoweenie/active_record_context.rb')
36   require File.join(active_record_context_dir, '/init')
37 else
38   raise LoadError.new("Cannot load active record context please install plugin")
39 end
40
41 require File.join( dir, '/db/schema.rb' )
42
43 models_dir = File.join( dir, 'models' )
44 $: << models_dir
45 Dir[ models_dir + '/*.rb'].each { |m| require m }
46
47
48
49
50
51 if ActiveRecord::VERSION::STRING < '2.3.1'
52   
53   TestCaseSuperClass = Test::Unit::TestCase
54   class Test::Unit::TestCase #:nodoc:
55     self.use_transactional_fixtures = true
56     self.use_instantiated_fixtures = false
57     
58     def assert_queries(num = 1)
59       $queries_executed = []
60       yield
61     ensure
62       %w{ BEGIN COMMIT }.each { |x| $queries_executed.delete(x) }
63       assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}"
64     end
65
66   end
67
68   Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
69 else
70
71
72
73   TestCaseSuperClass = ActiveRecord::TestCase
74   require 'active_record/test_case'
75   class ActiveRecord::TestCase #:nodoc:
76     include ActiveRecord::TestFixtures
77     self.use_transactional_fixtures = true
78     self.use_instantiated_fixtures = false
79     self.fixtures :all
80   end
81   ActiveRecord::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
82 end
83
84 ActiveRecord::Base.connection.class.class_eval do
85   IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
86
87   def execute_with_query_record(sql, name = nil, &block)
88     $queries_executed ||= []
89     $queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r }
90     execute_without_query_record(sql, name, &block)
91   end
92
93   alias_method_chain :execute, :query_record
94 end
95
96 class TestCaseSuperClass
97   def logger; ActiveRecord::Base.logger; end
98   def self.logger; ActiveRecord::Base.logger; end
99 end