require 'fluent_plugins_spec_helper' require 'flydata/source_mysql/table_meta' module Flydata::SourceMysql describe TableMeta do let(:db_opts) { { host: 'test-host', port: 3306, username: 'test-user', password: 'test-pswd', database: 'test-db' } } let(:database) { 'test-db' } let(:tables) { %w(a_table b_table c_table) } let(:conn) { double('conn') } let(:table_meta) { TableMeta.new(db_opts.merge(database: database, tables: tables)) } let(:utf8) { 'utf8' } let(:latin1) { 'latin1' } let(:cp932) { 'cp932' } before do allow(conn).to receive(:close) allow(Mysql2::Client).to receive(:new).and_return(conn) end describe '.reload' do subject { table_meta.reload } let(:query_result) {[]} before do allow(conn).to receive(:query).and_return(query_result) end context 'when character_set_name is supported' do context 'when encoding is not necessary' do let(:query_result) {[ {'table_name' => 'a_table', 'character_set_name' => utf8 } ]} it 'set nil for encoding' do subject expect(table_meta['a_table'][:encoding]).to eq Encoding::UTF_8 expect(table_meta['a_table'][:mysql_charset]).to eq utf8 end end context 'when encoding is needed' do let(:query_result) {[ {'table_name' => 'a_table', 'character_set_name' => latin1 }, {'table_name' => 'b_table', 'character_set_name' => cp932 } ]} it 'set ruby encoding encoding' do subject expect(table_meta['a_table'][:encoding]).to eq Encoding::ISO_8859_1 expect(table_meta['a_table'][:mysql_charset]).to eq latin1 expect(table_meta['b_table'][:encoding]).to eq Encoding::CP932 expect(table_meta['b_table'][:mysql_charset]).to eq cp932 end end end context 'when character_set_name is not supported' do let(:query_result) {[ {'table_name' => 'a_table', 'character_set_name' => 'xxxx' } ]} it 'raise an error' do expect{subject}.to raise_error('Unsupported charset:xxxx.') end end end end end