require 'spec_helper' require 'flydata-core/redshift/string' module FlydataCore module Redshift describe String do let(:subject_object) { described_class } describe '.encode' do subject { subject_object.encode(string, options) } let(:result) { string.encode(Encoding::UTF_8) } shared_examples "converting string to a Redshift friendly string" do before do string.encode!(src_encoding) end it do is_expected.to eq result end end shared_examples "converting an invalid string to raise an error" do before do string.encode!(src_encoding) end it do expect{subject}.to raise_error error end end context "with replace option" do let(:options) { { invalid: :replace, undef: :replace } } context 'UTF-8 string' do let(:src_encoding) { Encoding::UTF_8 } context 'valid char' do let(:string) { "Straße" } it_behaves_like "converting string to a Redshift friendly string" end context 'invalid char' do let(:string) { "\xeb\x13\x00" } let(:result) { "\uFFFD\u0013\u0000".encode(Encoding::UTF_8) } it_behaves_like "converting string to a Redshift friendly string" end context 'invalid char for Redshift (error 6)' do let(:string) { "\xef\xb7\x91" } let(:result) { "\uFFFD".encode(Encoding::UTF_8) } it_behaves_like "converting string to a Redshift friendly string" end context 'invalid char for Redshift (error 7)' do let(:string) { "\xef\xbf\xbf" } let(:result) { "\uFFFD".encode(Encoding::UTF_8) } it_behaves_like "converting string to a Redshift friendly string" end end context 'Shift JIS string' do let(:src_encoding) { Encoding::SHIFT_JIS } let(:string) { "テスト" } it_behaves_like "converting string to a Redshift friendly string" end end context 'without replace option' do let(:options) { {} } context 'UTF-8 string' do let(:src_encoding) { Encoding::UTF_8 } context 'valid char' do let(:string) { "Straße" } it_behaves_like "converting string to a Redshift friendly string" end context 'invalid char' do let(:string) { "\xeb\x13\x00" } let(:error) { Encoding::InvalidByteSequenceError } it_behaves_like "converting an invalid string to raise an error" end context 'invalid char for Redshift (error 6)' do let(:string) { "\xef\xb7\x91" } let(:result) { "\uFFFD".encode(Encoding::UTF_8) } # Redshift invalid characters are replaced regardless of options it_behaves_like "converting string to a Redshift friendly string" end context 'invalid char for Redshift (error 7)' do let(:string) { "\xef\xbf\xbf" } let(:result) { "\uFFFD".encode(Encoding::UTF_8) } # Redshift invalid characters are replaced regardless of options it_behaves_like "converting string to a Redshift friendly string" end end context 'Shift JIS string' do let(:src_encoding) { Encoding::SHIFT_JIS } let(:string) { "テスト" } it_behaves_like "converting string to a Redshift friendly string" end end end end end end