Sha256: 4810d82d8e703a62af60504494d6f6e5b66505d5af7152e218d37d6a801640cf

Contents?: true

Size: 1.62 KB

Versions: 18

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require 'spec_helper'

describe Mongo::CachingCursor do

  around do |spec|
    Mongo::QueryCache.clear
    Mongo::QueryCache.cache { spec.run }
  end

  let(:authorized_collection) do
    authorized_client['caching_cursor']
  end

  before do
    authorized_collection.drop
  end

  let(:server) do
    view.send(:server_selector).select_server(authorized_client.cluster)
  end

  let(:reply) do
    view.send(:send_initial_query, server)
  end

  let(:cursor) do
    described_class.new(view, reply, server)
  end

  let(:view) do
    Mongo::Collection::View.new(authorized_collection)
  end

  before do
    authorized_collection.delete_many
    3.times { |i| authorized_collection.insert_one(_id: i) }
  end

  describe '#cached_docs' do
    context 'when no query has been performed' do
      it 'returns nil' do
        expect(cursor.cached_docs).to be_nil
      end
    end

    context 'when a query has been performed' do
      it 'returns the number of documents' do
        cursor.to_a
        expect(cursor.cached_docs.length).to eq(3)
        expect(cursor.cached_docs).to eq([{ '_id' => 0 }, { '_id' => 1 }, { '_id' => 2 }])
      end
    end
  end

  describe '#try_next' do
    it 'fetches the next document' do
      expect(cursor.try_next).to eq('_id' => 0)
      expect(cursor.try_next).to eq('_id' => 1)
      expect(cursor.try_next).to eq('_id' => 2)
    end
  end

  describe '#each' do
    it 'iterates the cursor' do
      result = cursor.each.to_a
      expect(result.length).to eq(3)
      expect(result).to eq([{ '_id' => 0 }, { '_id' => 1 }, { '_id' => 2 }])
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
mongo-2.16.4 spec/mongo/caching_cursor_spec.rb
mongo-2.17.4 spec/mongo/caching_cursor_spec.rb
mongo-2.18.3 spec/mongo/caching_cursor_spec.rb
mongo-2.18.2 spec/mongo/caching_cursor_spec.rb
mongo-2.17.3 spec/mongo/caching_cursor_spec.rb
mongo-2.18.1 spec/mongo/caching_cursor_spec.rb
mongo-2.18.0 spec/mongo/caching_cursor_spec.rb
mongo-2.17.2 spec/mongo/caching_cursor_spec.rb
mongo-2.16.3 spec/mongo/caching_cursor_spec.rb
mongo-2.18.0.beta1 spec/mongo/caching_cursor_spec.rb
mongo-2.16.2 spec/mongo/caching_cursor_spec.rb
mongo-2.17.1 spec/mongo/caching_cursor_spec.rb
mongo-2.16.1 spec/mongo/caching_cursor_spec.rb
mongo-2.17.0 spec/mongo/caching_cursor_spec.rb
mongo-2.16.0 spec/mongo/caching_cursor_spec.rb
mongo-2.15.1 spec/mongo/caching_cursor_spec.rb
mongo-2.16.0.alpha1 spec/mongo/caching_cursor_spec.rb
mongo-2.15.0 spec/mongo/caching_cursor_spec.rb