require 'fluent_plugins_spec_helper' require 'flydata/source_mysql/plugin_support/source_position_file' require 'tempfile' module Flydata::SourceMysql::PluginSupport describe SourcePositionFile do let(:binlog_file) { "mysqlbin-00001" } let(:binlog_pos) { "4" } let(:source_pos_content) { "#{binlog_file}\t#{binlog_pos}" } let(:pos_file) { Tempfile.new('source_pos') } let(:file_path) { pos_file.path } let(:subject_object) { described_class.new(file_path) } before { File.write(file_path, source_pos_content) } after { FileUtils.rm(file_path) if File.exists?(file_path) } describe '#exists?' do subject { subject_object.exists? } context 'when the file exists' do it { is_expected.to eq(true) } end context 'when the file does not exist' do before { FileUtils.rm(file_path) } it { is_expected.to eq(false) } end end describe '#read' do subject { subject_object.read } it { is_expected.to eq(source_pos_content) } end describe '#pos' do subject { subject_object.pos } it { is_expected.to be_kind_of(FlydataCore::Mysql::BinlogPos) } it { expect(subject.to_s).to eq(source_pos_content) } context 'when the file is missing' do before { FileUtils.rm(file_path) } it { is_expected.to be_nil } end end describe '#save' do subject { subject_object.save("mysql-bin.000002", 120)} it { is_expected.to be_kind_of(FlydataCore::Mysql::BinlogPos) } it do subject expect(subject_object.read).to eq("mysql-bin.000002\t120") end end end end