/
mysql_spec.rb
171 lines (141 loc) · 5.81 KB
/
mysql_spec.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
require 'spec_helper'
module Dbmanager
module Adapters
module Mysql
describe Dumper do
before { Dbmanager.stub :output => STDStub.new }
let :source do
Environment.new(
:username => 'root',
:ignoretables => ['a_view', 'another_view'],
:database => 'database',
:password => 'secret',
:port => 42,
:host => '0.0.0.0'
)
end
subject { Dumper.new(source, '/tmp/dump_file.sql') }
describe '#ignoretables' do
context 'when there are tables to be ignored' do
it 'returns a string containing ignore-table flags' do
string = '--ignore-table=database.a_view --ignore-table=database.another_view'
subject.ignoretables.should == string
end
end
context 'when there are no tables to be ignored' do
it 'returns nil' do
source.stub!(:ignoretables => nil)
subject.ignoretables.should be_nil
end
end
end
describe '#dump_command' do
it 'returns expected command' do
[
'mysqldump --ignore-table=database.a_view',
'--ignore-table=database.another_view',
'-uroot -psecret -h0.0.0.0 -P42 database',
'> \'/tmp/dump_file.sql\''
].each do |command_part|
subject.dump_command.should include command_part
end
end
context 'when mysqldump version is >= than 5.6' do
before do
subject.stub(:get_mysqldump_version => 'mysqldump Ver 10.13 Distrib 5.6.0, for osx10.8 (i386)')
end
it {subject.mysqldump_version.should == 5.6 }
it 'adds a flag that sets off gtid-purged' do
subject.dump_command.should include '--set-gtid-purged=OFF'
end
end
context 'when mysqldump version is < than 5.6' do
before do
subject.stub(:get_mysqldump_version => 'mysqldump Ver 10.13 Distrib 5.5.28, for osx10.8 (i386)')
end
it {subject.mysqldump_version.should == 5.5 }
it 'adds a flag that sets off gtid-purged' do
subject.dump_command.should_not include '--set-gtid-purged=OFF'
end
end
end
end
describe Loader do
before { Dbmanager.stub :output => STDStub.new }
describe 'an importer instance' do
before { Time.stub! :now => Time.parse('2012/03/23 12:30:32') }
let(:source) { Environment.new :protected => false, :name => 'development', :username => 'root' }
let(:target) { Environment.new :protected => false, :name => 'beta', :username => 'beta_user' }
let(:tmp_file) { '/some/arbitrary/path' }
subject { Loader.new target, tmp_file }
it 'has target and tmp_file attribute methods' do
%w[target tmp_file].each { |m| subject.should respond_to m }
end
describe '#load_command' do
it 'returns expected command' do
subject.load_command.should == 'mysql -ubeta_user < \'/some/arbitrary/path\''
end
end
describe '#create_db_if_missing_command' do
it 'returns expected command' do
subject.create_db_if_missing_command.should == 'bundle exec rake db:create RAILS_ENV=beta'
end
end
describe '#bundle' do
it 'returns "bundle exec" when bundler is present' do
Dbmanager.should_receive(:execute).and_return true
subject.bundle.should == 'bundle exec'
end
it 'returns nil when bundler is missing' do
Dbmanager.should_receive(:execute).and_return false
subject.bundle.should be_nil
end
end
describe '#run' do
xit 'creates the db if missing and then imports the db' do
subject.stub!(:remove_tmp_file => true)
Dbmanager.should_receive(:execute!).with(subject.create_db_if_missing_command)
Dbmanager.should_receive(:execute!).with(subject.load_command)
subject.run
end
end
end
end
describe Importer do
def environment(opts={})
opts = {:protected => false}.merge(opts)
Environment.new opts
end
before { Dbmanager.stub :output => STDStub.new }
describe 'an importer instance' do
before { Time.stub! :now => Time.parse('2012/03/23 12:30:32') }
subject { Importer.new source, target, tmp_file }
let(:source) { environment(:name => 'development', :username => 'root') }
let(:target) { environment(:name => 'beta', :username => 'beta_user') }
let(:tmp_file) { '/some/arbitrary/path' }
it 'has target, source and tmp_file attribute methods' do
%w[source target tmp_file].each { |m| subject.should respond_to m }
end
describe '#remove_tmp_file' do
it 'tries to remove the temporary file' do
Dbmanager.should_receive(:execute).with('rm \'/some/arbitrary/path\'')
subject.remove_tmp_file
end
end
describe '#run' do
it 'create ad Dumper that will dump the db' do
Dbmanager.stub!(:execute! => nil)
Dumper.should_receive(:new).and_return(mock.as_null_object)
subject.run
end
it 'create ad Loader that will dump the db' do
Dbmanager.stub!(:execute! => nil)
Loader.should_receive(:new).and_return(mock.as_null_object)
subject.run
end
end
end
end
end
end
end