File: result_spec.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (80 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
50
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
require 'spec_helper'

describe Mongo::Operation::Commands::Aggregate::Result do

  let(:result) do
    described_class.new(reply)
  end

  let(:cursor_id) { 0 }
  let(:documents) { [] }
  let(:flags) { [] }
  let(:starting_from) { 0 }

  let(:reply) do
    Mongo::Protocol::Reply.new.tap do |reply|
      reply.instance_variable_set(:@flags, flags)
      reply.instance_variable_set(:@cursor_id, cursor_id)
      reply.instance_variable_set(:@starting_from, starting_from)
      reply.instance_variable_set(:@number_returned, documents.size)
      reply.instance_variable_set(:@documents, documents)
    end
  end

  let(:aggregate) do
    [
      { '_id' => 'New York', 'totalpop' => 40270 },
      { '_id' => 'Berlin', 'totalpop' => 103056 }
    ]
  end

  describe '#cursor_id' do

    context 'when the result is not using a cursor' do

      let(:documents) do
        [{ 'result' => aggregate, 'ok' => 1.0 }]
      end

      it 'returns zero' do
        expect(result.cursor_id).to eq(0)
      end
    end

    context 'when the result is using a cursor' do

      let(:documents) do
        [{ 'cursor' => { 'id' => 15, 'ns' => 'test', 'firstBatch' => aggregate }, 'ok' => 1.0 }]
      end

      it 'returns the cursor id' do
        expect(result.cursor_id).to eq(15)
      end
    end
  end

  describe '#documents' do

    context 'when the result is not using a cursor' do

      let(:documents) do
        [{ 'result' => aggregate, 'ok' => 1.0 }]
      end

      it 'returns the documents' do
        expect(result.documents).to eq(aggregate)
      end
    end

    context 'when the result is using a cursor' do

      let(:documents) do
        [{ 'cursor' => { 'id' => 15, 'ns' => 'test', 'firstBatch' => aggregate }, 'ok' => 1.0 }]
      end

      it 'returns the documents' do
        expect(result.documents).to eq(aggregate)
      end
    end
  end
end