require_relative '../../../spec_helper' require 'flydata/fluent-plugins/mysql/alter_table_query_handler' module Mysql describe AlterTableQueryHandler do let(:sync_fm) do r = double('sync_fm') allow(r).to receive(:get_table_binlog_pos).and_return("mysql-bin.000065\t120") r end let(:database) { "testdb" } let(:table) { "foo" } let(:context) do r = double('context') allow(r).to receive(:sync_fm).and_return(sync_fm) allow(r).to receive(:database).and_return([database]) allow(r).to receive(:tables).and_return([table]) r end subject { described_class.new(context) } describe '#process' do let(:query) { "a_query" } let(:record) do r = double('record') allow(r).to receive(:[]).with("db_name").and_return(database) allow(r).to receive(:[]).with("query").and_return(query) allow(r).to receive(:[]).with("table_name").and_return(table) r end let(:normalized_query) { double('normalized_query') } 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 allow(MysqlAlterTableParser).to receive(:new).and_return(parser) allow(subject).to receive(:acceptable_table?).and_return(true) allow(subject).to receive(:acceptable_db?).and_return(true) allow(subject).to receive(:check_empty_binlog) 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, normalized_query)).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, normalized_query)).to eq(nil) end end end end end