spec/flydata/command/sync_spec.rb in flydata-0.1.8 vs spec/flydata/command/sync_spec.rb in flydata-0.1.9
- old
+ new
@@ -1,8 +1,7 @@
# coding: utf-8
require 'spec_helper'
-require 'socket'
module Flydata
module Command
describe Sync do
let(:default_mysqldump_dir) do
@@ -47,12 +46,12 @@
describe '#do_generate_table_ddl' do
subject { Sync.new }
context 'with full options' do
it 'issues mysqldump command with expected parameters' do
- expect(IO).to receive(:popen).with(
- 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi -pwelcome sync_test table1 table2', 'r').and_call_original
+ expect(Open3).to receive(:popen3).with(
+ 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi -pwelcome sync_test table1 table2').and_call_original
subject.send(:do_generate_table_ddl, default_data_entry)
end
end
context 'without_host' do
before do
@@ -67,22 +66,22 @@
context 'without_port' do
before do
default_data_entry['mysql_data_entry_preference'].delete('port')
end
it "uses the default port" do
- expect(IO).to receive(:popen).with(
- 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi -pwelcome sync_test table1 table2', 'r').and_call_original
+ expect(Open3).to receive(:popen3).with(
+ 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi -pwelcome sync_test table1 table2').and_call_original
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(IO).to receive(:popen).with(
- 'mysqldump --protocol=tcp -d -h localhost -P 1234 -u masashi -pwelcome sync_test table1 table2', 'r').and_call_original
+ expect(Open3).to receive(:popen3).with(
+ 'mysqldump --protocol=tcp -d -h localhost -P 1234 -u masashi -pwelcome sync_test table1 table2').and_call_original
subject.send(:do_generate_table_ddl, default_data_entry)
end
end
context 'without_username' do
before do
@@ -97,12 +96,12 @@
context 'without_password' do
before do
default_data_entry['mysql_data_entry_preference'].delete('password')
end
it "call mysqldump without -p option" do
- expect(IO).to receive(:popen).with(
- 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi sync_test table1 table2', 'r').and_call_original
+ expect(Open3).to receive(:popen3).with(
+ 'mysqldump --protocol=tcp -d -h localhost -P 3306 -u masashi sync_test table1 table2').and_call_original
subject.send(:do_generate_table_ddl, default_data_entry)
end
end
context 'without_database' do
before do
@@ -1113,39 +1112,55 @@
expect(subject).to eq([['1','foo','2014-04-15 13:49:14'],['2','var','2014-04-15 14:21:05']])
end
end
context 'when data includes carriage return' do
- let(:target_line) { "INSERT INTO `test_table` VALUES (1,'abcd\\refgh','2014-04-15 13:49:14');" }
+ let(:target_line) { %q|INSERT INTO `test_table` VALUES (1,'abcd\refgh','2014-04-15 13:49:14');| }
it 'should escape return carriage' do
expect(subject).to eq([['1',"abcd\refgh",'2014-04-15 13:49:14']])
end
end
context 'when data includes new line' do
- let(:target_line) { "INSERT INTO `test_table` VALUES (1,'abcd\\nefgh','2014-04-15 13:49:14');" }
+ let(:target_line) { %q|INSERT INTO `test_table` VALUES (1,'abcd\nefgh','2014-04-15 13:49:14');| }
it 'should escape new line' do
expect(subject).to eq([['1',"abcd\nefgh",'2014-04-15 13:49:14']])
end
end
context 'when data includes escaped single quotation' do
- let(:target_line) { "INSERT INTO `test_table` VALUES (1,'abcd\\',\\'efgh','2014-04-15 13:49:14');" }
+ let(:target_line) { %q|INSERT INTO `test_table` VALUES (1,'abcd\',\'efgh','2014-04-15 13:49:14');| }
it 'should escape single quotation' do
expect(subject).to eq([['1',"abcd','efgh",'2014-04-15 13:49:14']])
end
end
context 'when data includes back slash' do
- let(:target_line) { "INSERT INTO `test_table` VALUES (1,'abcd\\\\efgh','2014-04-15 13:49:14');" }
+ let(:target_line) { %q|INSERT INTO `test_table` VALUES (1,'abcd\\efgh','2014-04-15 13:49:14');| }
it 'should escape back slash' do
expect(subject).to eq([['1',"abcd\\efgh",'2014-04-15 13:49:14']])
end
end
+ context 'when data includes back slash with double quote' do
+ # \"\'\' value on insert query shoulde be "''
+ let(:target_line) { %q|INSERT INTO `tast_table` VALUES (1,'\"\'\'','2014-04-15 13:49:14');| }
+ it 'should escape back slash' do
+ expect(subject).to eq([['1',%q|"''| ,'2014-04-15 13:49:14']])
+ end
+ end
+
+ context 'when data includes back slash with double quote another case' do
+ # \"\"\"\"\"''\"'' value on insert query shoulde be """""''"''
+ let(:target_line) { %q|INSERT INTO `test_table` VALUES (1,'\"\"\"\"\"\'\'\"\'\'','2014-04-15 13:49:14');| }
+ it 'should escape back slash' do
+ expect(subject).to eq([['1',%q|"""""''"''|,'2014-04-15 13:49:14']])
+ end
+ end
+
context 'when data includes mixed escaped characters' do
- let(:target_line) { "INSERT INTO `test_table` VALUES (1,'ab\\rcd\\\\e\\nf\\',\\'gh','2014-04-15 13:49:14');" }
+ let(:target_line) { %q|INSERT INTO `test_table` VALUES (1,'ab\rcd\\e\nf\',\'gh','2014-04-15 13:49:14');| }
it 'should escape all' do
expect(subject).to eq([['1',"ab\rcd\\e\nf','gh",'2014-04-15 13:49:14']])
end
end
@@ -1207,9 +1222,65 @@
context 'when there is a white space before the closing bracket' do
let(:target_line) { "INSERT INTO `test_table` VALUES (1,'aa','2014-04-15 13:49:14' );" }
it 'should fail to parse. This is intentional for performance reason' do
expect{subject}.to raise_error
+ end
+ end
+
+ context 'when an integer that has leading zeros is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,00013);"}
+ it 'should remove the leading zeros' do
+ expect(subject).to eq([['1', '13']])
+ end
+ end
+
+ context 'when a decimal that has leading zeros is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,00013.40);"}
+ it 'should remove the leading zeros' do
+ expect(subject).to eq([['1', '13.40']])
+ end
+ end
+
+ context 'when a timestamp that has leading zeros is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,'0004-04-15 13:49:14');"}
+ it 'should not remove the leading zeros' do
+ expect(subject).to eq([['1', '0004-04-15 13:49:14']])
+ end
+ end
+
+ context 'when a string that has leading zeros is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,'00000aa');"}
+ it 'should not remove the leading zeros' do
+ expect(subject).to eq([['1', '00000aa']])
+ end
+ end
+
+ context 'when a string that has leading zeros, numbers and a comma in between is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,'0003,00033');"}
+ it 'should not remove the leading zeros' do
+ expect(subject).to eq([['1', '0003,00033']])
+ end
+ end
+
+ context 'when a string that has leading zeros, has only numbers is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,'00033');"}
+ it 'should not remove the leading zeros' do
+ expect(subject).to eq([['1', '00033']])
+ end
+ end
+
+ context 'when 0 is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,0);"}
+ it 'should not remove the leading zeros' do
+ expect(subject).to eq([['1', '0']])
+ end
+ end
+
+ context 'when 0.00 is given' do
+ let(:target_line) {"INSERT INTO `test_table` VALUES (1,0.00);"}
+ it 'should not remove the leading zeros' do
+ expect(subject).to eq([['1', '0.00']])
end
end
end
end
end