File: collection_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (849 lines) | stat: -rw-r--r-- 23,571 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Collection do

  let(:subscriber) { Mrss::EventSubscriber.new }

  let(:client) do
    authorized_client.tap do |client|
      client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
    end
  end

  let(:authorized_collection) { client['collection_spec'] }

  before do
    authorized_client['collection_spec'].drop
  end

  describe '#==' do

    let(:database) do
      Mongo::Database.new(authorized_client, :test)
    end

    let(:collection) do
      described_class.new(database, :users)
    end

    context 'when the names are the same' do

      context 'when the databases are the same' do

        let(:other) do
          described_class.new(database, :users)
        end

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

      context 'when the databases are not the same' do

        let(:other_db) do
          Mongo::Database.new(authorized_client, :testing)
        end

        let(:other) do
          described_class.new(other_db, :users)
        end

        it 'returns false' do
          expect(collection).to_not eq(other)
        end
      end

      context 'when the options are the same' do

        let(:other) do
          described_class.new(database, :users)
        end

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

      context 'when the options are not the same' do

        let(:other) do
          described_class.new(database, :users, :capped => true)
        end

        it 'returns false' do
          expect(collection).to_not eq(other)
        end
      end
    end

    context 'when the names are not the same' do

      let(:other) do
        described_class.new(database, :sounds)
      end

      it 'returns false' do
        expect(collection).to_not eq(other)
      end
    end

    context 'when the object is not a collection' do

      it 'returns false' do
        expect(collection).to_not eq('test')
      end
    end
  end

  describe '#initialize' do

    let(:client) do
      new_local_client(SpecConfig.instance.addresses,
        SpecConfig.instance.test_options.merge(monitoring_io: false))
    end

    let(:database) { client.database }

    context 'write concern given in :write option' do
      let(:collection) do
        Mongo::Collection.new(database, 'foo', write: {w: 1})
      end

      it 'stores write concern' do
        expect(collection.write_concern).to be_a(Mongo::WriteConcern::Acknowledged)
        expect(collection.write_concern.options).to eq(w: 1)
      end

      it 'stores write concern under :write' do
        expect(collection.options[:write]).to eq(w: 1)
        expect(collection.options[:write_concern]).to be nil
      end
    end

    context 'write concern given in :write_concern option' do
      let(:collection) do
        Mongo::Collection.new(database, 'foo', write_concern: {w: 1})
      end

      it 'stores write concern' do
        expect(collection.write_concern).to be_a(Mongo::WriteConcern::Acknowledged)
        expect(collection.write_concern.options).to eq(w: 1)
      end

      it 'stores write concern under :write_concern' do
        expect(collection.options[:write_concern]).to eq(w: 1)
        expect(collection.options[:write]).to be nil
      end
    end

    context 'write concern given in both :write and :write_concern options' do
      context 'identical values' do

        let(:collection) do
          Mongo::Collection.new(database, 'foo',
            write: {w: 1}, write_concern: {w: 1})
        end

        it 'stores write concern' do
          expect(collection.write_concern).to be_a(Mongo::WriteConcern::Acknowledged)
          expect(collection.write_concern.options).to eq(w: 1)
        end

        it 'stores write concern under both options' do
          expect(collection.options[:write]).to eq(w: 1)
          expect(collection.options[:write_concern]).to eq(w: 1)
        end
      end

      context 'different values' do

        let(:collection) do
          Mongo::Collection.new(database, 'foo',
            write: {w: 1}, write_concern: {w: 2})
        end

        it 'raises an exception' do
          expect do
            collection
          end.to raise_error(ArgumentError, /If :write and :write_concern are both given, they must be identical/)
        end
      end
    end

=begin WriteConcern object support
    context 'when write concern is provided via a WriteConcern object' do

      let(:collection) do
        Mongo::Collection.new(database, 'foo', write_concern: wc)
      end

      let(:wc) { Mongo::WriteConcern.get(w: 2) }

      it 'stores write concern options in collection options' do
        expect(collection.options[:write_concern]).to eq(w: 2)
      end

      it 'caches write concern object' do
        expect(collection.write_concern).to be wc
      end
    end
=end
  end

  describe '#with' do

    let(:client) do
      new_local_client_nmio(SpecConfig.instance.addresses,
        SpecConfig.instance.test_options.merge(
          SpecConfig.instance.auth_options
      ))
    end

    let(:database) do
      Mongo::Database.new(client, SpecConfig.instance.test_db)
    end

    let(:collection) do
      database.collection('test-collection')
    end

    let(:new_collection) do
      collection.with(new_options)
    end

    context 'when new read options are provided' do

      let(:new_options) do
        { read: { mode: :secondary } }
      end

      it 'returns a new collection' do
        expect(new_collection).not_to be(collection)
      end

      it 'sets the new read options on the new collection' do
        expect(new_collection.read_preference).to eq(new_options[:read])
      end

      context 'when the client has a server selection timeout setting' do

        let(:client) do
          new_local_client(SpecConfig.instance.addresses,
            SpecConfig.instance.test_options.merge(server_selection_timeout: 2, monitoring_io: false))
        end

        it 'passes the the server_selection_timeout to the cluster' do
          expect(client.cluster.options[:server_selection_timeout]).to eq(client.options[:server_selection_timeout])
        end
      end

      context 'when the client has a read preference set' do

        let(:client) do
          authorized_client.with(client_options).tap do |client|
            expect(client.options[:read]).to eq(Mongo::Options::Redacted.new(
              mode: :primary_preferred))
            client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
          end
        end

        let(:client_options) do
          {
            read: { mode: :primary_preferred },
            monitoring_io: false,
          }
        end

        let(:new_options) do
          { read: { mode: :secondary } }
        end

        it 'sets the new read options on the new collection' do
          # This is strictly a Hash, not a BSON::Document like the client's
          # read preference.
          expect(new_collection.read_preference).to eq(mode: :secondary)
        end

        it 'duplicates the read option' do
          expect(new_collection.read_preference).not_to eql(client.read_preference)
        end

        context 'when reading from collection' do
          # Since we are requesting a secondary read, we need a replica set.
          require_topology :replica_set

          let(:client_options) do
            {read: { mode: :primary_preferred }}
          end

          shared_examples_for "uses collection's read preference when reading" do
            it "uses collection's read preference when reading" do
              expect do
                new_collection.find.to_a.count
              end.not_to raise_error

              event = subscriber.started_events.detect do |event|
                event.command['find']
              end
              actual_rp = event.command['$readPreference']
              expect(actual_rp).to eq(expected_read_preference)
            end
          end

          context 'post-OP_MSG server' do
            min_server_fcv '3.6'

            context 'standalone' do
              require_topology :single

              let(:expected_read_preference) do
                nil
              end

              it_behaves_like "uses collection's read preference when reading"
            end

            context 'RS, sharded' do
              require_topology :replica_set, :sharded

              let(:expected_read_preference) do
                {'mode' => 'secondary'}
              end

              it_behaves_like "uses collection's read preference when reading"
            end
          end

          context 'pre-OP-MSG server' do
            max_server_version '3.4'

            let(:expected_read_preference) do
              nil
            end

            it_behaves_like "uses collection's read preference when reading"
          end
        end
      end

      context 'when the client has a read preference and server selection timeout set' do

        let(:client) do
          new_local_client(SpecConfig.instance.addresses,
            SpecConfig.instance.test_options.merge(
              read: { mode: :primary_preferred },
              server_selection_timeout: 2,
              monitoring_io: false
          ))
        end

        it 'sets the new read options on the new collection' do
          expect(new_collection.read_preference).to eq(new_options[:read])
        end

        it 'passes the server_selection_timeout setting to the cluster' do
          expect(client.cluster.options[:server_selection_timeout]).to eq(client.options[:server_selection_timeout])
        end
      end
    end

    context 'when new write options are provided' do

      let(:new_options) do
        { write: { w: 5 } }
      end

      it 'returns a new collection' do
        expect(new_collection).not_to be(collection)
      end

      it 'sets the new write options on the new collection' do
        expect(new_collection.write_concern.options).to eq(Mongo::WriteConcern.get(new_options[:write]).options)
      end

      context 'when the client has a write concern set' do

        let(:client) do
          new_local_client(SpecConfig.instance.addresses,
            SpecConfig.instance.test_options.merge(
              write: INVALID_WRITE_CONCERN,
              monitoring_io: false,
          ))
        end

        it 'sets the new write options on the new collection' do
          expect(new_collection.write_concern.options).to eq(Mongo::WriteConcern.get(new_options[:write]).options)
        end

        context 'when client uses :write_concern and collection uses :write' do

          let(:client) do
            new_local_client(SpecConfig.instance.addresses,
              SpecConfig.instance.test_options.merge(
                write_concern: {w: 1},
                monitoring_io: false,
            ))
          end

          it 'uses :write from collection options only' do
            expect(new_collection.options[:write]).to eq(w: 5)
            expect(new_collection.options[:write_concern]).to be nil
          end
        end

        context 'when client uses :write and collection uses :write_concern' do

          let(:client) do
            new_local_client(SpecConfig.instance.addresses,
              SpecConfig.instance.test_options.merge(
                write: {w: 1},
                monitoring_io: false,
            ))
          end

          let(:new_options) do
            { write_concern: { w: 5 } }
          end

          it 'uses :write_concern from collection options only' do
            expect(new_collection.options[:write_concern]).to eq(w: 5)
            expect(new_collection.options[:write]).to be nil
          end
        end

        context 'when collection previously had :wrte_concern and :write is used with a different value' do

          let(:collection) do
            database.collection(:users, write_concern: {w: 2})
          end

          let(:new_options) do
            { write: { w: 5 } }
          end

          it 'uses the new option' do
            expect(new_collection.options[:write]).to eq(w: 5)
            expect(new_collection.options[:write_concern]).to be nil
          end
        end

        context 'when collection previously had :wrte and :write_concern is used with a different value' do

          let(:collection) do
            database.collection(:users, write: {w: 2})
          end

          let(:new_options) do
            { write_concern: { w: 5 } }
          end

          it 'uses the new option' do
            expect(new_collection.options[:write_concern]).to eq(w: 5)
            expect(new_collection.options[:write]).to be nil
          end
        end

        context 'when collection previously had :wrte_concern and :write is used with the same value' do

          let(:collection) do
            database.collection(:users, write_concern: {w: 2})
          end

          let(:new_options) do
            { write: { w: 2 } }
          end

          it 'uses the new option' do
            expect(new_collection.options[:write]).to eq(w: 2)
            expect(new_collection.options[:write_concern]).to be nil
          end
        end

        context 'when collection previously had :wrte and :write_concern is used with the same value' do

          let(:collection) do
            database.collection(:users, write: {w: 2})
          end

          let(:new_options) do
            { write_concern: { w: 2 } }
          end

          it 'uses the new option' do
            expect(new_collection.options[:write]).to be nil
            expect(new_collection.options[:write_concern]).to eq(w: 2)
          end
        end
      end
    end

    context 'when new read and write options are provided' do

      let(:new_options) do
        {
          read: { mode: :secondary },
          write: { w: 4}
        }
      end

      it 'returns a new collection' do
        expect(new_collection).not_to be(collection)
      end

      it 'sets the new read options on the new collection' do
        expect(new_collection.read_preference).to eq(new_options[:read])
      end

      it 'sets the new write options on the new collection' do
        expect(new_collection.write_concern.options).to eq(Mongo::WriteConcern.get(new_options[:write]).options)
      end

      context 'when the client has a server selection timeout setting' do

        let(:client) do
          new_local_client(SpecConfig.instance.addresses,
            SpecConfig.instance.test_options.merge(
              server_selection_timeout: 2,
              monitoring_io: false,
          ))
        end

        it 'passes the server_selection_timeout setting to the cluster' do
          expect(client.cluster.options[:server_selection_timeout]).to eq(client.options[:server_selection_timeout])
        end
      end

      context 'when the client has a read preference set' do

        let(:client) do
          new_local_client(SpecConfig.instance.addresses,
            SpecConfig.instance.test_options.merge(
              read: { mode: :primary_preferred },
              monitoring_io: false,
          ))
        end

        it 'sets the new read options on the new collection' do
          expect(new_collection.read_preference).to eq(new_options[:read])
          expect(new_collection.read_preference).not_to be(client.read_preference)
        end
      end
    end

    context 'when neither read nor write options are provided' do

      let(:new_options) do
        { some_option: 'invalid' }
      end

      it 'raises an error' do
        expect {
          new_collection
        }.to raise_exception(Mongo::Error::UnchangeableCollectionOption)
      end
    end
  end

  describe '#read_preference' do

    let(:collection) do
      described_class.new(authorized_client.database, :users, options)
    end

    let(:options) { {} }

    context 'when a read preference is set in the options' do

      let(:options) do
        { read: { mode: :secondary } }
      end

      it 'returns the read preference' do
        expect(collection.read_preference).to eq(options[:read])
      end
    end

    context 'when a read preference is not set in the options' do

      context 'when the database has a read preference set' do

        let(:client) do
          authorized_client.with(read: { mode: :secondary_preferred })
        end

        let(:collection) do
          described_class.new(client.database, :users, options)
        end

        it 'returns the database read preference' do
          expect(collection.read_preference).to eq(BSON::Document.new({ mode: :secondary_preferred }))
        end
      end

      context 'when the database does not have a read preference' do

        it 'returns nil' do
          expect(collection.read_preference).to be_nil
        end
      end
    end
  end

  describe '#server_selector' do

    let(:collection) do
      described_class.new(authorized_client.database, :users, options)
    end

    let(:options) { {} }

    context 'when a read preference is set in the options' do

      let(:options) do
        { read: { mode: :secondary } }
      end

      it 'returns the server selector for that read preference' do
        expect(collection.server_selector).to be_a(Mongo::ServerSelector::Secondary)
      end
    end

    context 'when a read preference is not set in the options' do

      context 'when the database has a read preference set' do

        let(:client) do
          authorized_client.with(read: { mode: :secondary_preferred })
        end

        let(:collection) do
          described_class.new(client.database, :users, options)
        end

        it 'returns the server selector for that read preference' do
          expect(collection.server_selector).to be_a(Mongo::ServerSelector::SecondaryPreferred)
        end
      end

      context 'when the database does not have a read preference' do

        it 'returns a primary server selector' do
          expect(collection.server_selector).to be_a(Mongo::ServerSelector::Primary)
        end
      end
    end
  end

  describe '#capped?' do

    let(:database) do
      authorized_client.database
    end

    context 'when the collection is capped' do

      let(:collection) do
        described_class.new(database, :specs, :capped => true, :size => 4096, :max => 512)
      end

      let(:collstats) do
        collection.aggregate([ {'$collStats' => { 'storageStats' => {} }} ]).first
      end

      let(:storage_stats) do
        collstats.fetch('storageStats', {})
      end

      before do
        authorized_client[:specs].drop
        collection.create
      end

      it 'returns true' do
        expect(collection).to be_capped
      end

      it "applies the options" do
        expect(storage_stats["capped"]).to be true
        expect(storage_stats["max"]).to eq(512)
        expect(storage_stats["maxSize"]).to eq(4096)
      end
    end

    context 'when the collection is not capped' do

      let(:collection) do
        described_class.new(database, :specs)
      end

      before do
        authorized_client[:specs].drop
        collection.create
      end

      it 'returns false' do
        expect(collection).to_not be_capped
      end
    end
  end

  describe '#inspect' do

    it 'includes the object id' do
      expect(authorized_collection.inspect).to include(authorized_collection.object_id.to_s)
    end

    it 'includes the namespace' do
      expect(authorized_collection.inspect).to include(authorized_collection.namespace)
    end
  end

  describe '#watch' do

    context 'when change streams can be tested' do
      require_wired_tiger
      min_server_fcv '3.6'
      require_topology :replica_set

      let(:change_stream) do
        authorized_collection.watch
      end

      let(:enum) do
        change_stream.to_enum
      end

      before do
        change_stream
        authorized_collection.insert_one(a: 1)
      end

      context 'when no options are provided' do

        context 'when the operation type is an insert' do

          it 'returns the change' do
            expect(enum.next[:fullDocument][:a]).to eq(1)
          end
        end

        context 'when the operation type is an update' do

          before do
            authorized_collection.update_one({ a: 1 }, { '$set' => { a: 2 } })
          end

          let(:change_doc) do
            enum.next
            enum.next
          end

          it 'returns the change' do
            expect(change_doc[:operationType]).to eq('update')
            expect(change_doc[:updateDescription][:updatedFields]).to eq('a' => 2)
          end
        end
      end

      context 'when options are provided' do

        context 'when full_document is updateLookup' do

          let(:change_stream) do
            authorized_collection.watch([], full_document: 'updateLookup').to_enum
          end

          before do
            authorized_collection.update_one({ a: 1 }, { '$set' => { a: 2 } })
          end

          let(:change_doc) do
            enum.next
            enum.next
          end

          it 'returns the change' do
            expect(change_doc[:operationType]).to eq('update')
            expect(change_doc[:fullDocument][:a]).to eq(2)
          end
        end

        context 'when batch_size is provided' do

          before do
            Thread.new do
              sleep 1
              authorized_collection.insert_one(a: 2)
              authorized_collection.insert_one(a: 3)
            end
          end

          let(:change_stream) do
            authorized_collection.watch([], batch_size: 2)
          end

          it 'returns the documents in the batch size specified' do
            expect(change_stream.instance_variable_get(:@cursor)).to receive(:get_more).once.and_call_original
            enum.next
          end
        end

        context 'when collation is provided' do

          before do
            authorized_collection.update_one({ a: 1 }, { '$set' => { a: 2 } })
          end

          let(:change_doc) do
            enum.next
          end

          let(:change_stream) do
            authorized_collection.watch([ { '$match' => { operationType: 'UPDATE'}}],
                                        collation: { locale: 'en_US', strength: 2 } ).to_enum
          end

          it 'returns the change' do
            expect(change_doc['operationType']).to eq('update')
            expect(change_doc['updateDescription']['updatedFields']['a']).to eq(2)
          end
        end
      end
    end

    context 'when the change stream is empty' do
      require_wired_tiger
      min_server_fcv '3.6'
      require_topology :replica_set

      context 'when setting the max_await_time_ms' do

        let(:change_stream) do
          authorized_collection.watch([], max_await_time_ms: 3000)
        end

        let(:enum) { change_stream.to_enum }

        let(:get_more) { subscriber.started_events.detect { |e| e.command['getMore'] }.command }

        it 'sets the option correctly' do
          enum.try_next
          expect(get_more).not_to be_nil
          expect(get_more['maxTimeMS']).to be == 3000
        end

        it "waits the appropriate amount of time" do
          start_time = Mongo::Utils.monotonic_time
          enum.try_next
          end_time = Mongo::Utils.monotonic_time

          expect(end_time-start_time).to be >= 3
        end
      end
    end
  end
end