require 'fluent_plugins_spec_helper' require 'flydata/fluent-plugins/mysql/truncate_table_query_handler' require 'flydata/fluent-plugins/mysql/shared_query_handler_context' module Mysql describe TruncateTableQueryHandler do include_context "query handler context" describe '#process' do let(:truncate_query) { "TRUNCATE table foo" } before do record['query'] = truncate_query record['normalized_query'] = truncate_query record.delete('table_name') end shared_examples "process truncate queries correctly" do let(:expected_record) do { table_name: table, query: truncate_query, type: :truncate_table, respect_order: true, src_pos: "#{current_binlog_file}\t#{next_position - event_length}", table_rev: table_rev, seq: seq, v: flydata_record_version } end context "for a non append only table" do it "should call Fluent's emit with appropriate params" do expect(Fluent::Engine).to receive(:emit).with(tag, timestamp, expected_record) expect(subject.process(record)) end end context "for an append only table" do before do allow(context).to receive(:omit_events).and_return({ table => [:delete, :truncate_table] }) end it "should not call Fluent's emit" do expect(Fluent::Engine).to receive(:emit).never expect(subject.process(record)) end end end shared_examples "skip processing queries" do it "should not call Fluent's emit" do expect(Fluent::Engine).to receive(:emit).never subject.process(record) end end context "truncate queries with table keyword" do let(:truncate_query) { "TRUNCATE table foo" } include_examples "process truncate queries correctly" end context "truncate queries without table keyword" do let(:truncate_query) { "TRUNCATE foo" } include_examples "process truncate queries correctly" end context "truncate queries with db name" do let(:table) { "var" } let(:database) { "testdb" } context 'when db exists' do let(:truncate_query) { "TRUNCATE #{database}.#{table}" } include_examples "process truncate queries correctly" end context 'when db doesn not exists' do let(:truncate_query) { "TRUNCATE dummydb.#{table}" } include_examples "skip processing queries" end end context 'when per-table binlog pos exists' do let(:truncate_query) { "TRUNCATE #{table}" } before do allow(sync_fm).to receive(:get_table_binlog_pos).and_return("mysql-bin.000067\t120") end include_examples "skip processing queries" end end end end