require 'spec_helper' require 'flydata-core/mysql/binlog_pos.rb' module FlydataCore module Mysql describe BinlogPos do let(:subject_object) { described_class.new(binlog_str) } let(:binlog_str) { "#{filename}\t#{pos}" } let(:filename) { 'mysql-bin.000064' } let(:pos) { 104 } describe '#pos' do subject { subject_object.pos } it { is_expected.to eq(pos) } end describe '#filename' do subject { subject_object.filename } it { is_expected.to eq(filename) } end describe '#to_s' do subject { subject_object.to_s } it { is_expected.to eq(binlog_str) } end describe '.new' do subject { described_class.new(*args) } shared_examples "properly constructed" do it do expect(subject.filename).to eq(filename) expect(subject.pos).to eq(pos) end end context 'when given two arguments' do let(:args) { [filename, pos] } it_behaves_like "properly constructed" end let(:args) { [ arg1 ] } context 'when given a String' do let(:arg1) { binlog_str } it_behaves_like "properly constructed" end context 'when given a Hash' do let(:arg1) { { filename: filename, pos: pos } } it_behaves_like "properly constructed" end end let(:arg) { described_class.new (arg_str) } let(:arg_str) { arg_binlog_str } let(:arg_binlog_str) { "#{arg_filename}\t#{arg_pos}" } let(:older_filename) { 'mysql-bin.000061' } let(:newer_filename) { 'mysql-bin.000065' } shared_examples 'returning the same result no matter what pos value is' do context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to eq expected_result } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to eq expected_result } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to eq expected_result } end end describe '#<=>' do subject { subject_object <=> arg } context 'when the target is nil' do let(:arg) { nil } it { expect { subject }.to raise_error(ArgumentError) } end context 'when the target has the same value' do let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to eq 0 } end context 'when the target binlog filename is the same' do let(:arg_filename) { filename } context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to eq 1 } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to eq 0 } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to eq -1 } end end context 'when the target binlog filename is older' do let(:arg_filename) { older_filename } let(:expected_result) { 1 } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target binlog filename is newer' do let(:arg_filename) { newer_filename } let(:expected_result) { -1 } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target is empty' do let(:arg_str) { "-" } it { expect { subject }.to raise_error(ArgumentError) } end context 'when self is empty' do let(:binlog_str) { "-" } context 'when target is empty' do let(:arg_str) { "-" } it { is_expected.to eq 0 } end context 'when target is not empty' do let(:arg) { double('arg') } before do allow(arg).to receive(:kind_of?).with(BinlogPos).and_return(true) allow(arg).to receive(:empty?).and_return(false) end it { expect { subject }.to raise_error(ArgumentError) } end end end describe '#<' do subject { subject_object < arg } context 'when the target has the same value' do let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_falsey } end context 'when the target binlog filename is the same' do let(:arg_filename) { filename } context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to be_falsey } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to be_falsey } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to be_truthy } end end context 'when the target binlog filename is older' do let(:arg_filename) { older_filename } let(:expected_result) { false } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target binlog filename is newer' do let(:arg_filename) { newer_filename } let(:expected_result) { true } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target is empty' do let(:arg_str) { "-" } it { expect { subject }.to raise_error(ArgumentError) } end context 'when self is empty' do let(:binlog_str) { "-" } context 'when target is empty' do let(:arg_str) { "-" } it { is_expected.to be_falsey } end context 'when target is not empty' do let(:arg_str) { arg_binlog_str } let(:arg_filename) { filename } let(:arg_pos) { pos } it { expect { subject }.to raise_error(ArgumentError) } end end end describe '#<=' do subject { subject_object <= arg } context 'when the target has the same value' do let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_truthy } end context 'when the target binlog filename is the same' do let(:arg_filename) { filename } context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to be_falsey } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to be_truthy } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to be_truthy } end end context 'when the target binlog filename is older' do let(:arg_filename) { older_filename } let(:expected_result) { false } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target binlog filename is newer' do let(:arg_filename) { newer_filename } let(:expected_result) { true } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target is empty' do let(:arg_str) { "-" } it { expect { subject }.to raise_error(ArgumentError) } end context 'when self is empty' do let(:binlog_str) { "-" } context 'when target is empty' do let(:arg_str) { "-" } it { is_expected.to be_truthy } end context 'when target is not empty' do let(:arg_str) { arg_binlog_str } let(:arg_filename) { filename } let(:arg_pos) { pos } it { expect { subject }.to raise_error(ArgumentError) } end end end describe '#>=' do subject { subject_object >= arg } context 'when the target has the same value' do let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_truthy } end context 'when the target binlog filename is the same' do let(:arg_filename) { filename } context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to be_truthy } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to be_truthy } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to be_falsey } end end context 'when the target binlog filename is older' do let(:arg_filename) { older_filename } let(:expected_result) { true } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target binlog filename is newer' do let(:arg_filename) { newer_filename } let(:expected_result) { false } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target is empty' do let(:arg_str) { "-" } it { expect { subject }.to raise_error(ArgumentError) } end context 'when self is empty' do let(:binlog_str) { "-" } context 'when target is empty' do let(:arg_str) { "-" } it { is_expected.to be_truthy } end context 'when target is not empty' do let(:arg_str) { arg_binlog_str } let(:arg_filename) { filename } let(:arg_pos) { pos } it { expect { subject }.to raise_error(ArgumentError) } end end end describe '#>' do subject { subject_object > arg } context 'when the target has the same value' do let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_falsey } end context 'when the target binlog filename is the same' do let(:arg_filename) { filename } context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to be_truthy } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to be_falsey } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to be_falsey } end end context 'when the target binlog filename is older' do let(:arg_filename) { older_filename } let(:expected_result) { true } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target binlog filename is newer' do let(:arg_filename) { newer_filename } let(:expected_result) { false } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target is empty' do let(:arg_str) { "-" } it { expect { subject }.to raise_error(ArgumentError) } end context 'when self is empty' do let(:binlog_str) { "-" } context 'when target is empty' do let(:arg_str) { "-" } it { is_expected.to be_falsey } end context 'when target is not empty' do let(:arg_str) { arg_binlog_str } let(:arg_filename) { filename } let(:arg_pos) { pos } it { expect { subject }.to raise_error(ArgumentError) } end end end describe '#==' do subject { subject_object == arg } context 'when comparing with nil' do let(:arg) { nil } it { is_expected.to be_falsey } end context 'when the target has the same value' do let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_truthy } end context 'when the target binlog filename is the same' do let(:arg_filename) { filename } context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to be_falsey } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to be_truthy } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to be_falsey } end end context 'when the target binlog filename is older' do let(:arg_filename) { older_filename } let(:expected_result) { false } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target binlog filename is newer' do let(:arg_filename) { newer_filename } let(:expected_result) { false } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target is empty' do let(:arg_str) { "-" } it { is_expected.to be_falsey } end context 'when self is empty' do let(:binlog_str) { "-" } context 'when target is empty' do let(:arg_str) { "-" } it { is_expected.to be_truthy } end context 'when target is not empty' do let(:arg_str) { arg_binlog_str } let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_falsey } end end end describe '#!=' do subject { subject_object != arg } context 'when comparing with nil' do let(:arg) { nil } it { is_expected.to be_truthy } end context 'when the target has the same value' do let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_falsey } end context 'when the target binlog filename is the same' do let(:arg_filename) { filename } context 'when the target pos is smaller' do let(:arg_pos) { pos - 10 } it { is_expected.to be_truthy } end context 'when the target pos is the same' do let(:arg_pos) { pos } it { is_expected.to be_falsey } end context 'when the target pos is larger' do let(:arg_pos) { pos + 10 } it { is_expected.to be_truthy } end end context 'when the target binlog filename is older' do let(:arg_filename) { older_filename } let(:expected_result) { true } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target binlog filename is newer' do let(:arg_filename) { newer_filename } let(:expected_result) { true } it_behaves_like 'returning the same result no matter what pos value is' end context 'when the target is empty' do let(:arg_str) { "-" } it { is_expected.to be_truthy } end context 'when self is empty' do let(:binlog_str) { "-" } context 'when target is empty' do let(:arg_str) { "-" } it { is_expected.to be_falsey } end context 'when target is not empty' do let(:arg_str) { arg_binlog_str } let(:arg_filename) { filename } let(:arg_pos) { pos } it { is_expected.to be_truthy } end end end end end end