BigW Consortium Gitlab

collection_spec.rb 1.96 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::Sherlock::Collection do
4 5 6 7 8 9 10 11 12 13
  let(:collection) { described_class.new }

  let(:transaction) do
    Gitlab::Sherlock::Transaction.new('POST', '/cat_pictures')
  end

  describe '#add' do
    it 'adds a new transaction' do
      collection.add(transaction)

14
      expect(collection).not_to be_empty
15 16 17 18 19
    end

    it 'is aliased as <<' do
      collection << transaction

20
      expect(collection).not_to be_empty
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    end
  end

  describe '#each' do
    it 'iterates over every transaction' do
      collection.add(transaction)

      expect { |b| collection.each(&b) }.to yield_with_args(transaction)
    end
  end

  describe '#clear' do
    it 'removes all transactions' do
      collection.add(transaction)

      collection.clear

      expect(collection).to be_empty
    end
  end

  describe '#empty?' do
    it 'returns true for an empty collection' do
      expect(collection).to be_empty
    end

    it 'returns false for a collection with a transaction' do
      collection.add(transaction)

50
      expect(collection).not_to be_empty
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    end
  end

  describe '#find_transaction' do
    it 'returns the transaction for the given ID' do
      collection.add(transaction)

      expect(collection.find_transaction(transaction.id)).to eq(transaction)
    end

    it 'returns nil when no transaction could be found' do
      collection.add(transaction)

      expect(collection.find_transaction('cats')).to be_nil
    end
  end

  describe '#newest_first' do
    it 'returns transactions sorted from new to old' do
      trans1 = Gitlab::Sherlock::Transaction.new('POST', '/cat_pictures')
      trans2 = Gitlab::Sherlock::Transaction.new('POST', '/more_cat_pictures')

      allow(trans1).to receive(:finished_at).and_return(Time.utc(2015, 1, 1))
      allow(trans2).to receive(:finished_at).and_return(Time.utc(2015, 1, 2))

      collection.add(trans1)
      collection.add(trans2)

      expect(collection.newest_first).to eq([trans2, trans1])
    end
  end
end