# encoding: utf-8 require 'fluent_plugins_spec_helper' require 'flydata/fluent-plugins/mysql/dml_record_handler' module Mysql describe DmlRecordHandler do let(:subject_object) { described_class.new(context) } let(:table_meta) { double('table_meta') } let(:tables) { ['items', 'orders'] } let(:sync_fm) { sfm = double('sync_fm') allow(sfm).to receive(:get_table_source_raw_pos).and_return nil sfm } let(:context) { c = double('context') allow(c).to receive(:table_meta).and_return(table_meta) allow(c).to receive(:tables).and_return(tables) allow(c).to receive(:sync_fm).and_return(sync_fm) c } describe '#encode_row_value' do subject { subject_object.send(:encode_row_value, value) } let(:normal_utf8_string) { "ABC新年Ã" } let(:valid_utf8_string) { "\x00abc" } # Yes, this is a valid UTF-8 string let(:invalid_utf8_string) { "新年".encode('shift_jis') } let(:latin1_string_happened_to_be_a_valid_utf8_string) { "ã".encode("iso-8859-1") } shared_examples 'encoding values differently based on their contents not based on encoding' do context 'with a normal utf-8 string' do let(:value) { normal_utf8_string } it 'returns a utf8 string' do result = subject expect(result).to eq normal_utf8_string expect(result.encoding).to eq Encoding.find('utf-8') end end context 'with a valid utf-8 string' do let(:value) { valid_utf8_string } it 'returns a utf8 string' do result = subject expect(result).to eq valid_utf8_string expect(result.encoding).to eq Encoding.find('utf-8') end end context 'with an invalid utf-8 string' do let(:value) { invalid_utf8_string } it 'returns a FlyData binary format string as BINARY encoding' do result = subject expect(result).to eq "0x9056944E" expect(result.encoding).to eq Encoding::BINARY end end context 'with a latin1 string which happens to be a valid utf8 character' do let(:value) { latin1_string_happened_to_be_a_valid_utf8_string } it 'returns a utf8 string' do result = subject expect(result).to eq latin1_string_happened_to_be_a_valid_utf8_string expect(result).to eq "ã" # #C3A3 expect(result.encoding).to eq Encoding.find('utf-8') end end end context 'with strings whose encoding is utf-8' do before do normal_utf8_string.force_encoding 'utf-8' valid_utf8_string.force_encoding 'utf-8' invalid_utf8_string.force_encoding 'utf-8' latin1_string_happened_to_be_a_valid_utf8_string.force_encoding 'utf-8' end it_behaves_like 'encoding values differently based on their contents not based on encoding' end context 'with strings whose encoding is binary' do before do normal_utf8_string.force_encoding 'binary' valid_utf8_string.force_encoding 'binary' invalid_utf8_string.force_encoding 'binary' latin1_string_happened_to_be_a_valid_utf8_string.force_encoding 'binary' end it_behaves_like 'encoding values differently based on their contents not based on encoding' end end describe '#convert_to_flydata_row_format' do subject { subject_object.send(:convert_to_flydata_row_format, row) } let(:row) { [ value ] } before do end shared_examples "returning a json hash with no attrs" do it do is_expected.to eq({"1" => value }) end end context 'with a row containing a string value' do let(:value) { "a normal string" } before do expect(subject_object).to receive(:encode_row_value).with(value).and_return(value) end it_behaves_like "returning a json hash with no attrs" end context 'with a row containing a binary value' do let(:value) { "\x00abc".force_encoding('binary') } before do expect(subject_object).to receive(:encode_row_value).with(value).and_return(value) end it "returns a json hash with attrs" do is_expected.to eq({"1" => value, "attrs" => {"1" => {"enc" => "b" }}}) end end context 'with a row containing an integer value' do let(:value) { 4 } it_behaves_like "returning a json hash with no attrs" end end end end