-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.rb
95 lines (74 loc) · 2.16 KB
/
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
require 'rubygems'
require 'mocha'
require 'active_support'
require 'active_support/test_case'
require 'activerecord'
ActiveRecord.load_all!
module Mysqlplus
class Test
CONNECTION_SPEC = { :adapter => 'mysqlplus',
:username => 'root',
:database => 'mysql',
:pool => 5,
:warmup => true }
MODELS_DIR = "#{File.dirname(__FILE__)}/models".freeze
class << self
def prepare!
require 'test/unit'
connect!
require_models()
end
def setup!
setup_constants!
setup_config!
end
def mysqlplus_connection
"#{File.dirname(__FILE__)}/connections/mysqlplus"
end
def active_record_test_files
returning([]) do |files|
files << glob( "#{AR_TEST_SUITE}/cases/**/*_test{,_mysqlplus}.rb" )
end.sort
end
def test_files
glob( "#{File.dirname(__FILE__)}/**/*_test.rb" )
end
private
def connect!
::ActiveRecord::Base.establish_connection( CONNECTION_SPEC )
end
def require_models
Dir.entries( MODELS_DIR ).grep(/.rb/).each do |model|
require_model( model )
end
end
def require_model( model )
require "#{MODELS_DIR}/#{model}"
end
def setup_constants!
set_constant( 'MYSQL_DB_USER' ){ 'rails' }
set_constant( 'AR_TEST_SUITE' ) do
ENV['AR_TEST_SUITE'] || find_active_record_test_suite()
end
end
def setup_config!
unless Object.const_defined?( 'MIGRATIONS_ROOT' )
require "#{::AR_TEST_SUITE}/config"
end
end
def set_constant( constant )
Object.const_set(constant, yield ) unless Object.const_defined?( constant )
end
def find_active_record_test_suite
returning( ($:).grep( /activerecord/ ).last.split('/') ) do |ar_ts|
ar_ts.pop
ar_ts << 'test'
end.join('/')
end
def glob( pattern )
Dir.glob( pattern )
end
end
end
end
Mysqlplus::Test.setup!