require 'fluent_plugins_spec_helper' require 'flydata/fluent-plugins/mysql/table_meta' module Mysql 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)) } before do allow(conn).to receive(:close) allow(Mysql2::Client).to receive(:new).and_return(conn) end describe '.update' do 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 table_meta.update expect(table_meta['a_table'][:encoding]).to be_nil 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 table_meta.update expect(table_meta['a_table'][:encoding]).to eq('ISO-8859-1') expect(table_meta['b_table'][:encoding]).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{table_meta.update}.to raise_error('Unsupported charset:xxxx.') end end end end end