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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
require 'spec_helper'
describe Mongo::Operation::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
describe '#acknowledged?' do
context 'when the reply is for a read command' do
let(:documents) do
[{ 'ismaster' => true, 'ok' => 1.0 }]
end
it 'returns true' do
expect(result).to be_acknowledged
end
end
context 'when the reply is for a write command' do
context 'when the command was acknowledged' do
let(:documents) do
[{ "ok" => 1, "n" => 2 }]
end
it 'returns true' do
expect(result).to be_acknowledged
end
end
context 'when the command was not acknowledged' do
let(:reply) { nil }
it 'returns false' do
expect(result).to_not be_acknowledged
end
end
end
end
describe '#cursor_id' do
context 'when the reply exists' do
let(:cursor_id) { 5 }
it 'delegates to the reply' do
expect(result.cursor_id).to eq(5)
end
end
context 'when the reply does not exist' do
let(:reply) { nil }
it 'returns zero' do
expect(result.cursor_id).to eq(0)
end
end
end
describe '#documents' do
context 'when the result is for a command' do
context 'when a reply is received' do
let(:documents) do
[{ "ok" => 1, "n" => 2 }]
end
it 'returns the documents' do
expect(result.documents).to eq(documents)
end
end
context 'when a reply is not received' do
let(:reply) { nil }
it 'returns an empty array' do
expect(result.documents).to be_empty
end
end
end
end
describe '#each' do
let(:documents) do
[{ "ok" => 1, "n" => 2 }]
end
context 'when a block is given' do
it 'yields to each document' do
result.each do |document|
expect(document).to eq(documents.first)
end
end
end
context 'when no block is given' do
it 'returns an enumerator' do
expect(result.each).to be_a(Enumerator)
end
end
end
describe '#initialize' do
it 'sets the replies' do
expect(result.replies).to eq([ reply ])
end
end
describe '#returned_count' do
context 'when the reply is for a read command' do
let(:documents) do
[{ 'ismaster' => true, 'ok' => 1.0 }]
end
it 'returns the number returned' do
expect(result.returned_count).to eq(1)
end
end
context 'when the reply is for a write command' do
context 'when the write is acknowledged' do
let(:documents) do
[{ "ok" => 1, "n" => 2 }]
end
it 'returns the number returned' do
expect(result.returned_count).to eq(1)
end
end
context 'when the write is not acknowledged' do
let(:reply) { nil }
it 'returns zero' do
expect(result.returned_count).to eq(0)
end
end
end
end
describe '#successful?' do
context 'when the reply is for a read command' do
let(:documents) do
[{ 'ismaster' => true, 'ok' => 1.0 }]
end
it 'returns true' do
expect(result).to be_successful
end
end
context 'when the reply is for a query' do
context 'when the query has no errors' do
let(:documents) do
[{ 'field' => 'name' }]
end
it 'returns true' do
expect(result).to be_successful
end
end
context 'when the query has errors' do
let(:documents) do
[{ '$err' => 'not authorized for query on test.system.namespaces', 'code'=> 16550 }]
end
it 'returns false' do
expect(result).to_not be_successful
end
end
context 'when the query reply has the cursor_not_found flag set' do
let(:flags) do
[ :cursor_not_found ]
end
let(:documents) do
[]
end
it 'returns false' do
expect(result).to_not be_successful
end
end
end
context 'when the reply is for a write command' do
context 'when the write is acknowledged' do
context 'when ok is 1' do
let(:documents) do
[{ "ok" => 1, "n" => 2 }]
end
it 'returns true' do
expect(result).to be_successful
end
end
context 'when ok is not 1' do
let(:documents) do
[{ "ok" => 0, "n" => 0 }]
end
it 'returns false' do
expect(result).to_not be_successful
end
end
end
context 'when the write is not acknowledged' do
let(:reply) { nil }
it 'returns true' do
expect(result).to be_successful
end
end
end
end
describe '#written_count' do
context 'when the reply is for a read command' do
let(:documents) do
[{ 'ismaster' => true, 'ok' => 1.0 }]
end
it 'returns the number written' do
expect(result.written_count).to eq(0)
end
end
context 'when the reply is for a write command' do
let(:documents) do
[{ "ok" => 1, "n" => 2 }]
end
it 'returns the number written' do
expect(result.written_count).to eq(2)
end
end
end
context 'when there is a top-level Result class defined' do
before do
class Result
def get_result(address)
Mongo::Client.new([address], TEST_OPTIONS).database.command(:ping => 1)
end
end
end
let(:result) do
Result.new.get_result(default_address.to_s)
end
it 'uses the Result class of the operation' do
expect(result).to be_a(Mongo::Operation::Result)
end
end
end
|