require 'spec_helper' describe Alephant::Sequencer do let(:ident) { :ident } let(:jsonpath) { :jsonpath } describe ".create(table_name, ident, jsonpath)" do it "should return a Sequencer" do Alephant::Sequencer::SequenceTable.any_instance.stub(:create) expect(subject.create(:table_name, ident, jsonpath)).to be_a Alephant::Sequencer::Sequencer end end describe Alephant::Sequencer::Sequencer do let(:data) { double() } let(:last_seen) { 42 } def sequence_table table = double() table.stub(:create) table.stub(:sequence_exists) table.stub(:sequence_for) table.stub(:set_sequence_for) table end describe "#initialize(opts, id)" do it "sets @jsonpath, @ident" do subject = Alephant::Sequencer::Sequencer.new(sequence_table, ident, jsonpath) expect(subject.jsonpath).to eq(jsonpath) expect(subject.ident).to eq(ident) end it "calls create on sequence_table" do table = double() table.stub(:sequence_exists) table.should_receive(:create) Alephant::Sequencer::Sequencer.new(table, ident, jsonpath) end end describe "#get_last_seen" do it "returns sequence_table.sequence_for(ident)" do table = double() table.stub(:sequence_exists) table.stub(:create) table.should_receive(:sequence_for).with(ident).and_return(:expected_value) expect( Alephant::Sequencer::Sequencer.new(table, ident, jsonpath).get_last_seen ).to eq(:expected_value) end end describe "#set_last_seen(data)" do before(:each) do Alephant::Sequencer::Sequencer.any_instance.stub(:sequence_id_from).and_return(last_seen) end it "calls set_sequence_for(ident, last_seen)" do table = double() table.stub(:sequence_exists) table.stub(:create) table.stub(:sequence_for) table.should_receive(:set_sequence_for).with(ident, last_seen, nil) Alephant::Sequencer::Sequencer.new(table, ident, jsonpath).set_last_seen(data) end end describe "#sequence_id_from(data)" do subject { Alephant::Sequencer::Sequencer.new(sequence_table, ident, '$.set_sequence_id') } it "should return the id described by the set jsonpath" do msg = Struct.new(:body).new({ "set_sequence_id" => 1 }) expect(subject.sequence_id_from msg).to eq(1) end end describe "#sequential?(data, jsonpath)" do before(:each) do Alephant::Sequencer::Sequencer.any_instance.stub(:get_last_seen).and_return(1) data.stub(:body).and_return('sequence_id' => id_value) end context "jsonpath = '$.sequence_id'" do let(:jsonpath) { '$.sequence_id' } subject { Alephant::Sequencer::Sequencer.new(sequence_table, :ident, jsonpath) } context "sequential" do let(:id_value) { 2 } it "is true" do expect(subject.sequential?(data)).to be_true end end context "nonsequential" do let(:id_value) { 0 } it "is false" do expect(subject.sequential?(data)).to be_false end end end context "jsonpath = nil" do let(:jsonpath) { '$.sequence_id' } subject { Alephant::Sequencer::Sequencer.new(sequence_table, :ident, jsonpath) } context "sequential" do let(:id_value) { 2 } it "is true" do expect(subject.sequential?(data)).to be_true end end context "nonsequential" do let(:id_value) { 0 } it "is false" do expect(subject.sequential?(data)).to be_false end end end end end end