forked from rsim/oracle-enhanced
/
spec_helper.rb
200 lines (158 loc) · 5.13 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require 'rubygems'
# Set up gems listed in the Gemfile.
gemfile = File.expand_path('../../Gemfile', __FILE__)
begin
ENV['BUNDLE_GEMFILE'] = gemfile
require 'bundler'
Bundler.setup
rescue Bundler::GemNotFound => e
STDERR.puts e.message
STDERR.puts "Try running `bundle install`."
exit!
end if File.exist?(gemfile)
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'spec'
if !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby'
puts "==> Running specs with MRI version #{RUBY_VERSION}"
require 'oci8'
elsif RUBY_ENGINE == 'jruby'
puts "==> Running specs with JRuby version #{JRUBY_VERSION}"
require 'active_record/connection_adapters/jdbc_adapter'
end
ENV['RAILS_GEM_VERSION'] ||= '3.0.0'
NO_COMPOSITE_PRIMARY_KEYS = true if ENV['RAILS_GEM_VERSION'] >= '2.3.5'
puts "==> Running specs with Rails version #{ENV['RAILS_GEM_VERSION']}"
require 'active_record'
if ENV['RAILS_GEM_VERSION'] >= '3.0'
require 'action_dispatch'
require 'active_support/core_ext/module/attribute_accessors'
if ENV['RAILS_GEM_VERSION'] =~ /^3.0.0.beta/
require "rails/log_subscriber"
require 'active_record/railties/log_subscriber'
else
require "active_support/log_subscriber"
require 'active_record/log_subscriber'
end
require 'logger'
elsif ENV['RAILS_GEM_VERSION'] =~ /^2.3/
require 'action_pack'
require 'action_controller/session/abstract_store'
require 'active_record/session_store'
elsif ENV['RAILS_GEM_VERSION'] <= '2.3'
require 'action_pack'
require 'action_controller/session/active_record_store'
end
require 'active_record/connection_adapters/oracle_enhanced_adapter'
require 'ruby-plsql'
module LoggerSpecHelper
def set_logger
@logger = MockLogger.new
@old_logger = ActiveRecord::Base.logger
if ENV['RAILS_GEM_VERSION'] =~ /^3.0.0.beta/
queue = ActiveSupport::Notifications::Fanout.new
@notifier = ActiveSupport::Notifications::Notifier.new(queue)
Rails::LogSubscriber.colorize_logging = false
ActiveRecord::Base.logger = @logger
@old_notifier = ActiveSupport::Notifications.notifier
ActiveSupport::Notifications.notifier = @notifier
Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new)
elsif ENV['RAILS_GEM_VERSION'] >= '3.0'
@notifier = ActiveSupport::Notifications::Fanout.new
ActiveSupport::LogSubscriber.colorize_logging = false
ActiveRecord::Base.logger = @logger
@old_notifier = ActiveSupport::Notifications.notifier
ActiveSupport::Notifications.notifier = @notifier
ActiveRecord::LogSubscriber.attach_to(:active_record)
else # ActiveRecord 2.x
if ActiveRecord::Base.respond_to?(:connection_pool)
ActiveRecord::Base.connection_pool.clear_reloadable_connections!
else
ActiveRecord::Base.clear_active_connections!
end
ActiveRecord::Base.logger = @logger
ActiveRecord::Base.colorize_logging = false
# ActiveRecord::Base.logger.level = Logger::DEBUG
end
end
class MockLogger
attr_reader :flush_count
def initialize
@flush_count = 0
@logged = Hash.new { |h,k| h[k] = [] }
end
# used in AtiveRecord 2.x
def debug?
true
end
def method_missing(level, message)
@logged[level] << message
end
def logged(level)
@logged[level].compact.map { |l| l.to_s.strip }
end
def output(level)
logged(level).join("\n")
end
def flush
@flush_count += 1
end
def clear(level)
@logged[level] = []
end
end
def clear_logger
ActiveRecord::Base.logger = @old_logger
@logger = nil
if ENV['RAILS_GEM_VERSION'] >= '3.0'
ActiveSupport::Notifications.notifier = @old_notifier
@notifier = nil
end
end
# Wait notifications to be published (for Rails 3.0)
# should not be currently used with sync queues in tests
def wait
@notifier.wait if @notifier
end
end
module SchemaSpecHelper
def schema_define(&block)
ActiveRecord::Schema.define do
suppress_messages do
instance_eval(&block)
end
end
end
end
DATABASE_NAME = ENV['DATABASE_NAME'] || 'orcl'
DATABASE_HOST = ENV['DATABASE_HOST'] || 'localhost'
DATABASE_PORT = ENV['DATABASE_PORT'] || 1521
DATABASE_USER = ENV['DATABASE_USER'] || 'oracle_enhanced'
DATABASE_PASSWORD = ENV['DATABASE_PASSWORD'] || 'oracle_enhanced'
DATABASE_SYS_PASSWORD = ENV['DATABASE_SYS_PASSWORD'] || 'admin'
CONNECTION_PARAMS = {
:adapter => "oracle_enhanced",
:database => DATABASE_NAME,
:host => DATABASE_HOST,
:username => DATABASE_USER,
:password => DATABASE_PASSWORD
}
SYS_CONNECTION_PARAMS = {
:adapter => "oracle_enhanced",
:database => DATABASE_NAME,
:host => DATABASE_HOST,
:username => "sys",
:password => DATABASE_SYS_PASSWORD,
:privilege => "SYSDBA"
}
SYSTEM_CONNECTION_PARAMS = {
:adapter => "oracle_enhanced",
:database => DATABASE_NAME,
:host => DATABASE_HOST,
:username => "system",
:password => DATABASE_SYS_PASSWORD
}
# For JRuby Set default $KCODE to UTF8
$KCODE = "UTF8" if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
# set default time zone in TZ environment variable
# which will be used to set session time zone
ENV['TZ'] ||= 'Europe/Riga'