# coding: utf-8 require 'spec_helper' module Flydata module Command describe Sync do let(:default_mysqldump_dir) do File.join('/tmp', "sync_dump_#{Time.now.to_i}") end let(:default_data_entry) do {"id"=>93, "name"=>"flydata_sync_mysql", "data_port_id"=>52, "display_name"=>"flydata_sync_mysql", "enabled"=>true, "heroku_log_type"=>nil, "heroku_resource_id"=>nil, "log_deletion"=>nil, "log_file_delimiter"=>nil, "log_file_type"=>nil, "log_path"=>nil, "redshift_schema_name"=>"", "redshift_table_name"=>nil, "created_at"=>"2014-01-22T18:58:43Z", "updated_at"=>"2014-01-30T02:42:26Z", "type"=>"RedshiftMysqlDataEntry", "tag_name"=>"flydata.a458c641_dp52.flydata_mysql", "tag_name_dev"=>"flydata.a458c641_dp52.flydata_mysql.dev", "data_port_key"=>"a458c641", "mysql_data_entry_preference" => { "host"=>"localhost", "port"=>3306, "username"=>"masashi", "password"=>"welcome", "database"=>"sync_test", "tables"=>"table1, table2", "mysqldump_dir"=>default_mysqldump_dir, "forwarder" => "tcpforwarder", "data_servers"=>"localhost:9905" } } end after :each do if Dir.exists?(default_mysqldump_dir) Dir.delete(default_mysqldump_dir) rescue nil end if File.exists?(default_mysqldump_dir) File.delete(default_mysqldump_dir) rescue nil end end describe '#do_generate_table_ddl' do subject { Sync.new } shared_examples 'throws an error' do it "throws an error" do expect { subject.send(:do_generate_table_ddl, default_data_entry) }.to raise_error end end context 'with full options' do it 'issues mysqldump command with expected parameters' do expect(Open3).to receive(:popen3).with( 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi -pwelcome sync_test table1 table2') subject.send(:do_generate_table_ddl, default_data_entry) end end context 'without_host' do before do default_data_entry['mysql_data_entry_preference'].delete('host') end include_examples 'throws an error' end context 'with empty host' do before do default_data_entry['mysql_data_entry_preference']['host'] = "" end include_examples 'throws an error' end context 'without_port' do before do default_data_entry['mysql_data_entry_preference'].delete('port') end it "uses the default port" do expect(Open3).to receive(:popen3).with( 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi -pwelcome sync_test table1 table2') subject.send(:do_generate_table_ddl, default_data_entry) end end context 'with_port_override' do before do default_data_entry['mysql_data_entry_preference']['port'] = 1234 end it "uses the specified port" do expect(Open3).to receive(:popen3).with( 'mysqldump --protocol=tcp -d -h localhost -P 1234 -u masashi -pwelcome sync_test table1 table2') subject.send(:do_generate_table_ddl, default_data_entry) end end context 'without_username' do before do default_data_entry['mysql_data_entry_preference'].delete('username') end include_examples 'throws an error' end context 'with empty username' do before do default_data_entry['mysql_data_entry_preference']['username'] = "" end include_examples 'throws an error' end context 'without_password' do before do default_data_entry['mysql_data_entry_preference'].delete('password') end it "call mysqldump without -p option" do expect(Open3).to receive(:popen3).with( 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi sync_test table1 table2') subject.send(:do_generate_table_ddl, default_data_entry) end end context 'without_database' do before do default_data_entry['mysql_data_entry_preference'].delete('database') end include_examples 'throws an error' end context 'with empty database' do before do default_data_entry['mysql_data_entry_preference']['database'] = "" end include_examples 'throws an error' end context 'without_tables' do before do default_data_entry['mysql_data_entry_preference'].delete('tables') end include_examples 'throws an error' end context 'with empty tables' do before do default_data_entry['mysql_data_entry_preference']['tables'] = "" end include_examples 'throws an error' end end end end end