require 'fluent_plugins_spec_helper' require 'flydata/source_mysql/plugin_support/alter_table_query_handler' require 'flydata/source_mysql/plugin_support/shared_query_handler_context' module Flydata::SourceMysql::PluginSupport describe AlterTableQueryHandler do include_context "query handler context" describe '#process' do let(:parse_result) do r = double('parse_result') r end let(:parser) do r = double('parser') allow(r).to receive(:parse).and_return(parse_result) r end before do parser_class = Flydata::Parser::ParserProvider.parser(:mysql, :mysql_alter_table) allow(parser_class).to receive(:new).and_return(parser) allow_any_instance_of(described_class).to receive(:check_empty_binlog) record.delete('table_name') record['normalized_query'] = "truncate table #{table};" end shared_examples "a process method receiving an exception" do let(:an_error) { "an error" } it "returns nil with an error log" do expect($log).to receive(:error).with(/Received unsupported alter table query.*Caused by error '#{an_error}'/m) expect(subject.process(record)).to eq(nil) end end context "when the parser#parse throws an exception" do before do expect(parser).to receive(:parse).and_raise(RuntimeError.new(an_error)) end it_behaves_like "a process method receiving an exception" end context "when the parse result throws an exception" do before do expect(parse_result).to receive(:tree).and_raise(RuntimeError.new(an_error)) end it_behaves_like "a process method receiving an exception" end context "when the parse result is nil" do before do expect(parser).to receive(:parse).and_return(nil) end it "returns nil with a warn log" do expect($log).to receive(:error).with(/Received unsupported alter table query\./) expect(subject.process(record)).to eq(nil) end end context "when event binlog is older than table binlog.pos" do it 'skip sending event' do expect(sync_fm).to receive(:get_table_source_raw_pos).and_return("mysql-bin.000067\t120").once expect(Flydata::Parser::ParserProvider).not_to receive(:parser) expect(subject.process(record)).to eq(nil) end end end end end