require 'flydata/source_mysql/sync_generate_table_ddl' module Flydata module SourceMysql describe SyncGenerateTableDdl do let(:subject_object) { described_class.new(source, dp, options) } let(:dp) { double('dp') } let(:de) { { 'mysql_data_entry_preference' => { 'host' => 'localhost', 'port' => 3306, 'username' => 'masashi', 'password' => 'welcomes', 'database' => 'sync_test', } } } let(:options) { {} } let(:source) { double('source') } before do allow(source).to receive(:de).and_return de end describe '#run_compatibility_check' do subject { subject_object.run_compatibility_check } let(:checker) { double("checker") } it do expect(MysqlCompatibilityCheck).to receive(:new).and_return checker expect(checker).to receive(:check) subject end end describe '#generate_flydata_tabledef' do subject { subject_object.generate_flydata_tabledef(tables, options) } let(:tables) { %w| table1 table2 table4 table3 | } context 'with full options' do it 'issues mysqldump command with expected parameters' do expect(Open3).to receive(:popen3).with( 'mysqldump -h localhost -P 3306 -umasashi -pwelcomes --default-character-set=utf8 --protocol=tcp -d sync_test table1 table2 table4 table3') subject end end context 'without_host' do before do de['mysql_data_entry_preference'].delete('host') end it { expect { subject }.to raise_error } end context 'with empty host' do before do de['mysql_data_entry_preference']['host'] = "" end it { expect { subject }.to raise_error } end context 'without port' do before do de['mysql_data_entry_preference'].delete('port') end it "uses the default port" do expect(Open3).to receive(:popen3).with( 'mysqldump -h localhost -umasashi -pwelcomes --default-character-set=utf8 --protocol=tcp -d sync_test table1 table2 table4 table3') subject end end context 'with port override' do before do de['mysql_data_entry_preference']['port'] = 1234 end it "uses the specified port" do expect(Open3).to receive(:popen3).with( 'mysqldump -h localhost -P 1234 -umasashi -pwelcomes --default-character-set=utf8 --protocol=tcp -d sync_test table1 table2 table4 table3') subject end end context 'without username' do before do de['mysql_data_entry_preference'].delete('username') end it { expect { subject }.to raise_error } end context 'with empty username' do before do de['mysql_data_entry_preference']['username'] = "" end it { expect { subject }.to raise_error } end context 'without_password' do before do de['mysql_data_entry_preference'].delete('password') end it "call mysqldump without MYSQL_PW set" do expect(Open3).to receive(:popen3).with( 'mysqldump -h localhost -P 3306 -umasashi --default-character-set=utf8 --protocol=tcp -d sync_test table1 table2 table4 table3') subject end end context 'with password containing symbols' do before do de['mysql_data_entry_preference']['password']="welcome&!@^@#^" end it "call mysqldump with MYSQL_PW set with correct symbols" do expect(Open3).to receive(:popen3).with( 'mysqldump -h localhost -P 3306 -umasashi -pwelcome\\&\\!@\\^@\\#\\^ --default-character-set=utf8 --protocol=tcp -d sync_test table1 table2 table4 table3') subject end end context 'without_database' do before do de['mysql_data_entry_preference'].delete('database') end it { expect { subject }.to raise_error } end context 'with empty database' do before do de['mysql_data_entry_preference']['database'] = "" end it { expect { subject }.to raise_error } end context 'with empty tables' do before do de['mysql_data_entry_preference']['tables'] = [] end it { expect { subject }.to raise_error } end end end end end