File: reply_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 (195 lines) | stat: -rw-r--r-- 5,045 bytes parent folder | download | duplicates (3)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require 'spec_helper'

describe Mongo::Protocol::Reply do

  let(:length)      { 78 }
  let(:request_id)  { 0 }
  let(:response_to) { 0 }
  let(:op_code)     { 1 }
  let(:flags)       { 0 }
  let(:start)       { 0 }
  let(:n_returned)  { 2 }
  let(:cursor_id)   { 999_999 }
  let(:doc)         { { 'name' => 'Tyler' } }
  let(:documents)   { [doc] * 2 }

  let(:header) do
    [length, request_id, response_to, op_code].pack('l<l<l<l<')
  end

  let(:data) do
    data =  [flags].pack('l<')
    data << [cursor_id].pack('q<')
    data << [start].pack('l<')
    data << [n_returned].pack('l<')
    data << documents.map(&:to_bson).join
  end

  let(:io)    { StringIO.new(header + data) }
  let(:reply) { described_class.deserialize(io) }

  describe '#==' do

    context 'when the other is a reply' do

      context 'when the fields are equal' do
        let(:other) do
          other = described_class.allocate
          other.instance_variable_set(:@flags, [])
          other.instance_variable_set(:@number_returned, n_returned)
          other.instance_variable_set(:@cursor_id, cursor_id)
          other.instance_variable_set(:@documents, documents)
          other.instance_variable_set(:@starting_from, start)
          other
        end

        it 'returns true' do
          expect(reply).to eq(other)
        end
      end

      context 'when fields are not equal' do
        let(:other) do
          other = described_class.allocate
          other.instance_variable_set(:@flags, [:await_capable])
          other.instance_variable_set(:@number_returned, n_returned)
          other.instance_variable_set(:@cursor_id, cursor_id)
          other.instance_variable_set(:@documents, documents)
          other.instance_variable_set(:@starting_from, start)
          other
        end

        it 'returns false' do
          expect(reply).not_to eq(other)
        end
      end
    end

    context 'when the other is not a reply' do
      let(:other) do
        expect(message).not_to eq('test')
      end
    end
  end

  describe '#hash' do
    let(:values) do
      reply.send(:fields).map do |field|
        reply.instance_variable_get(field[:name])
      end
    end

    it 'returns a hash of the field values' do
      expect(reply.hash).to eq(values.hash)
    end
  end

  describe '#deserialize' do

    context 'when the message size is greater than the max message size' do

      let(:length) { Mongo::Protocol::Message::MAX_MESSAGE_SIZE + 1 }

      it 'raises a max message size error' do
        expect {
          reply
        }.to raise_error(Mongo::Error::MaxMessageSize)
      end
    end

    context 'when the max message size is nil' do

      let(:reply) { described_class.deserialize(io, nil) }

      let(:length) { Mongo::Protocol::Message::MAX_MESSAGE_SIZE + 1 }

      it 'uses the default max message size for comparison' do
        expect {
          reply
        }.to raise_error(Mongo::Error::MaxMessageSize)
      end
    end

    describe 'response flags' do

      context 'no flags' do
        it 'sets no flags' do
          expect(reply.flags).to be_empty
        end
      end

      context 'cursor not found' do
        let(:flags) { 1 }
        it 'sets the cursor not found flag' do
          expect(reply.flags).to eq([:cursor_not_found])
        end
      end

      context 'query failure' do
        let(:flags) { 2 }
        it 'sets the query failure flag' do
          expect(reply.flags).to eq([:query_failure])
        end
      end

      context 'shard config stale' do
        let(:flags) { 4 }
        it 'sets the shard config stale flag' do
          expect(reply.flags).to eq([:shard_config_stale])
        end
      end

      context 'await capable' do
        let(:flags) { 8 }
        it 'sets the await capable flag' do
          expect(reply.flags).to eq([:await_capable])
        end
      end

      context 'multiple flags' do
        let(:flags) { 10 }
        it 'sets multiple flags' do
          expect(reply.flags).to include(:query_failure, :await_capable)
        end
      end
    end

    describe 'cursor id' do
      it 'sets the cursor id attribute' do
        expect(reply.cursor_id).to eq(cursor_id)
      end
    end

    describe 'starting from' do
      it 'sets the starting from attribute' do
        expect(reply.starting_from).to eq(start)
      end
    end

    describe 'number returned' do
      it 'sets the number returned attribute' do
        expect(reply.number_returned).to eq(n_returned)
      end
    end

    describe 'documents' do
      it 'sets the documents attribute' do
        expect(reply.documents).to eq(documents)
      end
    end
  end

  describe '#registry' do

    context 'when the class is loaded' do

      it 'registers the op code in the Protocol Registry' do
        expect(Mongo::Protocol::Registry.get(described_class::OP_CODE)).to be(described_class)
      end

      it 'creates an #op_code instance method' do
        expect(reply.op_code).to eq(described_class::OP_CODE)
      end
    end
  end
end