File: dbref_spec.rb

package info (click to toggle)
ruby-bson 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,828 kB
  • sloc: ruby: 11,712; ansic: 1,427; java: 514; makefile: 8
file content (487 lines) | stat: -rw-r--r-- 12,399 bytes parent folder | download
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'
require 'json'

describe BSON::DBRef do

  let(:object_id) do
    BSON::ObjectId.new
  end

  describe '#as_json' do

    context 'when the database is not provided' do

      let(:dbref) do
        described_class.new({ '$ref' => 'users', '$id' => object_id })
      end

      it 'returns the json document without database' do
        expect(dbref.as_json).to eq({ '$ref' => 'users', '$id' => object_id })
      end
    end

    context 'when the database is provided' do

      let(:dbref) do
        described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
      end

      it 'returns the json document with database' do
        expect(dbref.as_json).to eq({
          '$ref' => 'users',
          '$id' => object_id,
          '$db' => 'database'
        })
      end
    end

    context 'when other keys are provided' do

      let(:dbref) do
        described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database', 'x' => 'y' })
      end

      it 'returns the json document with the other keys' do
        expect(dbref.as_json).to eq({
          '$ref' => 'users',
          '$id' => object_id,
          '$db' => 'database',
          'x' => 'y'
        })
      end
    end
  end

  describe '#initialize' do

    let(:dbref) do
      described_class.new(hash)
    end

    let(:hash) do
      { '$ref' => 'users', '$id' => object_id }
    end

    it 'sets the collection' do
      expect(dbref.collection).to eq('users')
    end

    it 'sets the id' do
      expect(dbref.id).to eq(object_id)
    end

    context 'when first argument is a hash and two arguments are provided' do

      let(:dbref) do
        described_class.new({:$ref => 'users', :$id => object_id}, object_id)
      end

      it 'raises InvalidDBRefArgument' do
        lambda do
          dbref
        end.should raise_error(BSON::Error::InvalidDBRefArgument)
      end
    end

    context 'when first argument is a hash and three arguments are provided' do

      let(:dbref) do
        described_class.new({:$ref => 'users', :$id => object_id}, object_id, 'db')
      end

      it 'raises InvalidDBRefArgument' do
        lambda do
          dbref
        end.should raise_error(BSON::Error::InvalidDBRefArgument)
      end
    end

    context 'when a database is provided' do

      let(:hash) do
        { '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
      end

      it 'sets the database' do
        expect(dbref.database).to eq('db')
      end
    end

    context 'when not providing a collection' do
      let(:hash) do
        { '$id' => object_id, '$db' => 'db' }
      end

      it 'raises an error' do
        expect do
          dbref
        end.to raise_error(BSON::Error::InvalidDBRefArgument, /DBRef must have \$ref/)
      end
    end

    context 'when not providing an id' do
      let(:hash) do
        { '$ref' => 'users', '$db' => 'db' }
      end

      it 'raises an error' do
        expect do
          dbref
        end.to raise_error(BSON::Error::InvalidDBRefArgument, /DBRef must have \$id/)
      end
    end

    context 'when providing an invalid type for ref' do
      let(:hash) do
        { '$ref' => 1, '$id' => object_id }
      end

      it 'raises an error' do
        expect do
          dbref
        end.to raise_error(BSON::Error::InvalidDBRefArgument, /The value for key \$ref must be a string/)
      end
    end

    context 'when providing an invalid type for database' do
      let(:hash) do
        { '$ref' => 'users', '$id' => object_id, '$db' => 1 }
      end

      it 'raises an error' do
        expect do
          dbref
        end.to raise_error(BSON::Error::InvalidDBRefArgument, /The value for key \$db must be a string/)
      end
    end

    context 'when providing the fieds as symbols' do
      let(:hash) do
        { :$ref => 'users', :$id => object_id, :$db => 'db' }
      end

      it 'does not raise an error' do
        expect do
          dbref
        end.to_not raise_error
      end
    end

    context 'when testing the ordering of the fields' do
      context 'when the fields are in order' do
        let(:hash) do
          { '$ref' => 'users', '$id' => object_id, '$db' => 'db' }
        end

        it 'has the correct order' do
          expect(dbref.keys).to eq(['$ref', '$id', '$db'])
        end
      end

      context 'when the fields are out of order' do
        let(:hash) do
          { '$db' => 'db', '$id' => object_id, '$ref' => 'users' }
        end

        it 'has the correct order' do
          expect(dbref.keys).to eq(['$ref', '$id', '$db'])
        end
      end

      context 'when there is no db' do
        let(:hash) do
          { '$id' => object_id, '$ref' => 'users' }
        end

        it 'has the correct order' do
          expect(dbref.keys).to eq(['$ref', '$id'])
        end
      end

      context 'when the there are other fields in order' do
        let(:hash) do
          { '$ref' => 'users', '$id' => object_id, '$db' => 'db', 'x' => 'y', 'y' => 'z' }
        end

        it 'has the correct order' do
          expect(dbref.keys).to eq(['$ref', '$id', '$db', 'x', 'y'])
        end
      end

      context 'when the there are other fields out of order' do
        let(:hash) do
          { 'y' => 'z', '$db' => 'db', '$id' => object_id, 'x' => 'y', '$ref' => 'users' }
        end

        it 'has the correct order' do
          expect(dbref.keys).to eq(['$ref', '$id', '$db', 'y', 'x'])
        end
      end
    end
  end

  describe '#to_bson' do

    let(:dbref) do
      described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
    end

    it 'converts the underlying document to bson' do
      expect(dbref.to_bson.to_s).to eq(dbref.as_json.to_bson.to_s)
    end
  end

  describe '#to_json' do

    context 'when the database is not provided' do

      let(:dbref) do
        described_class.new({ '$ref' => 'users', '$id' => object_id })
      end

      it 'returns the json document without database' do
        expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json}}")
      end
    end

    context 'when the database is provided' do

      let(:dbref) do
        described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database' })
      end

      it 'returns the json document with database' do
        expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\"}")
      end
    end

    context 'when other keys are provided' do

      let(:dbref) do
        described_class.new({ '$ref' => 'users', '$id' => object_id, '$db' => 'database', 'x' => 'y' })
      end

      it 'returns the json document with the other keys' do
        expect(dbref.to_json).to eq("{\"$ref\":\"users\",\"$id\":#{object_id.to_json},\"$db\":\"database\",\"x\":\"y\"}")
      end
    end
  end

  describe '#from_bson' do

    let(:buffer) do
      hash.to_bson
    end

    let(:decoded) do
      BSON::Document.from_bson(BSON::ByteBuffer.new(buffer.to_s))
    end

    context 'when a database exists' do

      let(:hash) do
        { '$ref' => 'users', '$id' => object_id, '$db' => 'database' }
      end

      it 'decodes the ref' do
        expect(decoded.collection).to eq('users')
      end

      it 'decodes the id' do
        expect(decoded.id).to eq(object_id)
      end

      it 'decodes the database' do
        expect(decoded.database).to eq('database')
      end

      it 'is of class DBRef' do
        expect(decoded).to be_a described_class
      end
    end

    context 'when no database exists' do

      let(:hash) do
        { '$ref' => 'users', '$id' => object_id }
      end

      it 'decodes the ref' do
        expect(decoded.collection).to eq('users')
      end

      it 'decodes the id' do
        expect(decoded.id).to eq(object_id)
      end

      it 'sets the database to nil' do
        expect(decoded.database).to be_nil
      end

      it 'is of class DBRef' do
        expect(decoded).to be_a described_class
      end
    end

    context 'when other keys exist' do

      let(:hash) do
        { '$ref' => 'users', '$id' => object_id, 'x' => 'y' }
      end

      it 'decodes the key' do
        expect(decoded['x']).to eq('y')
      end

      it 'is of class DBRef' do
        expect(decoded).to be_a described_class
      end
    end

    context 'when it is an invalid dbref' do

      shared_examples 'bson document' do
        it 'should not raise' do
          expect do
            decoded
          end.to_not raise_error
        end

        it 'has the correct class' do
          expect(decoded).to be_a BSON::Document
          expect(decoded).to_not be_a described_class
        end
      end

      context 'when the hash has invalid collection type' do
        let(:hash) do
          { '$ref' => 1, '$id' => object_id }
        end
        include_examples 'bson document'
      end

      context 'when the hash has an invalid database type' do
        let(:hash) do
          { '$ref' => 'users', '$id' => object_id, '$db' => 1 }
        end
        include_examples 'bson document'
      end

      context 'when the hash is missing a collection' do
        let(:hash) do
          { '$id' => object_id }
        end
        include_examples 'bson document'
      end

      context 'when the hash is missing an id' do
        let(:hash) do
          { '$ref' => 'users' }
        end
        include_examples 'bson document'
      end
    end

    context 'when nesting the dbref' do

      context 'when it is a valid dbref' do
        let(:hash) do
          { 'dbref' => { '$ref' => 'users', '$id' => object_id } }
        end

        it 'should not raise' do
          expect do
            buffer
          end.to_not raise_error
        end

        it 'has the correct class' do
          expect(decoded['dbref']).to be_a described_class
        end
      end

      context 'when it is an invalid dbref' do

        shared_examples 'nested bson document' do
          it 'should not raise' do
            expect do
              decoded
            end.to_not raise_error
          end

          it 'has the correct class' do
            expect(decoded['dbref']).to be_a BSON::Document
            expect(decoded['dbref']).to_not be_a described_class
          end
        end

        context 'when the hash has invalid collection type' do
          let(:hash) do
            { 'dbref' => { '$ref' => 1, '$id' => object_id } }
          end
          include_examples 'nested bson document'
        end

        context 'when the hash has an invalid database type' do
          let(:hash) do
            { 'dbref' => { '$ref' => 'users', '$id' => object_id, '$db' => 1 } }
          end
          include_examples 'nested bson document'
        end

        context 'when the hash is missing a collection' do
          let(:hash) do
            { 'dbref' => { '$id' => object_id } }
          end
          include_examples 'nested bson document'
        end

        context 'when the hash is missing an id' do
          let(:hash) do
            { 'dbref' => { '$ref' => 'users' } }
          end
          include_examples 'nested bson document'
        end
      end
    end

    context 'when nesting a dbref inside a dbref' do
      context 'when it is a valid dbref' do
        let(:hash) do
          { 'dbref1' => { '$ref' => 'users', '$id' => object_id, 'dbref2' => { '$ref' => 'users', '$id' => object_id } } }
        end

        it 'should not raise' do
          expect do
            buffer
          end.to_not raise_error
        end

        it 'has the correct class' do
          expect(decoded['dbref1']).to be_a described_class
          expect(decoded['dbref1']['dbref2']).to be_a described_class
        end
      end

      context 'when it is an invalid dbref' do
        let(:hash) do
          { 'dbref' => { '$ref' => 'users', '$id' => object_id, 'dbref' => { '$ref' => 1, '$id' => object_id } } }
        end

        it 'should not raise' do
          expect do
            decoded
          end.to_not raise_error
        end

        it 'has the correct class' do
          expect(decoded['dbref']).to be_a described_class
          expect(decoded['dbref']['dbref']).to be_a BSON::Document
        end
      end
    end
  end
end