mongo 2.0.6 → 2.1.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/mongo.rb +2 -0
  5. data/lib/mongo/bulk_write.rb +1 -0
  6. data/lib/mongo/bulk_write/bulk_writable.rb +87 -31
  7. data/lib/mongo/bulk_write/deletable.rb +8 -7
  8. data/lib/mongo/bulk_write/insertable.rb +4 -3
  9. data/lib/mongo/bulk_write/ordered_bulk_write.rb +6 -6
  10. data/lib/mongo/bulk_write/replacable.rb +4 -3
  11. data/lib/mongo/bulk_write/result.rb +138 -0
  12. data/lib/mongo/bulk_write/unordered_bulk_write.rb +5 -8
  13. data/lib/mongo/bulk_write/updatable.rb +8 -7
  14. data/lib/mongo/client.rb +36 -4
  15. data/lib/mongo/cluster.rb +39 -4
  16. data/lib/mongo/cluster/topology/replica_set.rb +20 -4
  17. data/lib/mongo/cluster/topology/sharded.rb +1 -1
  18. data/lib/mongo/collection.rb +282 -29
  19. data/lib/mongo/collection/view/aggregation.rb +32 -4
  20. data/lib/mongo/collection/view/iterable.rb +2 -1
  21. data/lib/mongo/collection/view/map_reduce.rb +3 -1
  22. data/lib/mongo/collection/view/readable.rb +89 -14
  23. data/lib/mongo/collection/view/writable.rb +11 -5
  24. data/lib/mongo/cursor.rb +11 -3
  25. data/lib/mongo/dbref.rb +113 -0
  26. data/lib/mongo/error.rb +6 -2
  27. data/lib/mongo/error/parser.rb +1 -1
  28. data/lib/mongo/event/description_changed.rb +1 -1
  29. data/lib/mongo/grid/file.rb +1 -1
  30. data/lib/mongo/grid/fs.rb +2 -5
  31. data/lib/mongo/monitoring.rb +199 -0
  32. data/lib/mongo/monitoring/command_log_subscriber.rb +88 -0
  33. data/lib/mongo/monitoring/event.rb +17 -0
  34. data/lib/mongo/monitoring/event/command_failed.rb +96 -0
  35. data/lib/mongo/monitoring/event/command_started.rb +88 -0
  36. data/lib/mongo/monitoring/event/command_succeeded.rb +96 -0
  37. data/lib/mongo/monitoring/publishable.rb +96 -0
  38. data/lib/mongo/operation.rb +1 -0
  39. data/lib/mongo/operation/executable.rb +1 -1
  40. data/lib/mongo/operation/parallel_scan.rb +76 -0
  41. data/lib/mongo/operation/parallel_scan/result.rb +72 -0
  42. data/lib/mongo/operation/specifiable.rb +18 -0
  43. data/lib/mongo/operation/write/bulk/bulk_delete.rb +1 -1
  44. data/lib/mongo/operation/write/bulk/bulk_insert.rb +1 -1
  45. data/lib/mongo/operation/write/bulk/bulk_mergable.rb +2 -2
  46. data/lib/mongo/operation/write/bulk/bulk_update.rb +1 -1
  47. data/lib/mongo/operation/write/bulk/bulk_update/result.rb +13 -1
  48. data/lib/mongo/protocol/delete.rb +8 -13
  49. data/lib/mongo/protocol/get_more.rb +13 -13
  50. data/lib/mongo/protocol/insert.rb +8 -13
  51. data/lib/mongo/protocol/kill_cursors.rb +7 -11
  52. data/lib/mongo/protocol/query.rb +58 -20
  53. data/lib/mongo/protocol/reply.rb +12 -0
  54. data/lib/mongo/protocol/update.rb +13 -14
  55. data/lib/mongo/server.rb +23 -2
  56. data/lib/mongo/server/connectable.rb +0 -22
  57. data/lib/mongo/server/connection.rb +29 -0
  58. data/lib/mongo/server/description.rb +23 -1
  59. data/lib/mongo/server/monitor.rb +17 -1
  60. data/lib/mongo/server/monitor/connection.rb +24 -0
  61. data/lib/mongo/socket/ssl.rb +28 -16
  62. data/lib/mongo/socket/tcp.rb +1 -1
  63. data/lib/mongo/socket/unix.rb +1 -1
  64. data/lib/mongo/uri.rb +12 -5
  65. data/lib/mongo/version.rb +1 -1
  66. data/spec/mongo/auth/cr_spec.rb +9 -1
  67. data/spec/mongo/auth/ldap_spec.rb +9 -1
  68. data/spec/mongo/auth/scram_spec.rb +9 -1
  69. data/spec/mongo/auth/x509_spec.rb +9 -1
  70. data/spec/mongo/{bulk/bulk_write_spec.rb → bulk_write_spec.rb} +15 -15
  71. data/spec/mongo/client_spec.rb +42 -0
  72. data/spec/mongo/cluster/topology/replica_set_spec.rb +16 -9
  73. data/spec/mongo/cluster/topology/sharded_spec.rb +11 -3
  74. data/spec/mongo/cluster/topology/single_spec.rb +12 -4
  75. data/spec/mongo/cluster_spec.rb +55 -10
  76. data/spec/mongo/collection/view/aggregation_spec.rb +123 -1
  77. data/spec/mongo/collection/view/explainable_spec.rb +1 -1
  78. data/spec/mongo/collection/view/map_reduce_spec.rb +1 -1
  79. data/spec/mongo/collection/view/readable_spec.rb +251 -6
  80. data/spec/mongo/collection/view/writable_spec.rb +4 -4
  81. data/spec/mongo/collection/view_spec.rb +233 -71
  82. data/spec/mongo/collection_spec.rb +905 -9
  83. data/spec/mongo/crud_spec.rb +2 -2
  84. data/spec/mongo/cursor_spec.rb +3 -3
  85. data/spec/mongo/dbref_spec.rb +149 -0
  86. data/spec/mongo/monitoring_spec.rb +168 -0
  87. data/spec/mongo/operation/map_reduce_spec.rb +1 -1
  88. data/spec/mongo/operation/write/bulk/bulk_delete_spec.rb +1 -1
  89. data/spec/mongo/operation/write/bulk/bulk_insert_spec.rb +2 -2
  90. data/spec/mongo/operation/write/bulk/bulk_update_spec.rb +1 -1
  91. data/spec/mongo/operation/write/delete_spec.rb +1 -1
  92. data/spec/mongo/operation/write/insert_spec.rb +2 -2
  93. data/spec/mongo/operation/write/update_spec.rb +1 -1
  94. data/spec/mongo/protocol/query_spec.rb +0 -29
  95. data/spec/mongo/server/connection_pool_spec.rb +18 -6
  96. data/spec/mongo/server/connection_spec.rb +12 -4
  97. data/spec/mongo/server/description_spec.rb +7 -3
  98. data/spec/mongo/server/monitor_spec.rb +30 -0
  99. data/spec/mongo/server_discovery_and_monitoring_spec.rb +11 -4
  100. data/spec/mongo/server_selection_spec.rb +14 -6
  101. data/spec/mongo/server_spec.rb +27 -8
  102. data/spec/mongo/socket/ssl_spec.rb +94 -8
  103. data/spec/mongo/uri_spec.rb +25 -9
  104. data/spec/spec_helper.rb +29 -20
  105. data/spec/support/authorization.rb +19 -4
  106. data/spec/support/certificates/client.pem +4 -4
  107. data/spec/support/crud/read.rb +9 -10
  108. data/spec/support/crud/write.rb +24 -20
  109. data/spec/support/sdam/rs/equal_electionids.yml +45 -0
  110. data/spec/support/sdam/rs/new_primary_new_electionid.yml +98 -0
  111. data/spec/support/sdam/rs/null_election_id.yml +144 -0
  112. data/spec/support/sdam/rs/primary_disconnect_electionid.yml +124 -0
  113. data/spec/support/sdam/sharded/mongos_disconnect.yml +104 -0
  114. data/spec/support/server_discovery_and_monitoring.rb +19 -2
  115. data/spec/support/shared/bulk_write.rb +26 -22
  116. data/spec/support/shared/server_selector.rb +2 -1
  117. metadata +31 -7
  118. metadata.gz.sig +0 -0
  119. data/lib/mongo/error/invalid_uri_option.rb +0 -38
@@ -2,6 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe Mongo::Collection do
4
4
 
5
+ after do
6
+ authorized_collection.delete_many
7
+ end
8
+
5
9
  describe '#==' do
6
10
 
7
11
  let(:database) do
@@ -282,7 +286,7 @@ describe Mongo::Collection do
282
286
  end
283
287
 
284
288
  after do
285
- authorized_collection.find.delete_many
289
+ authorized_collection.delete_many
286
290
  end
287
291
 
288
292
  let(:view) do
@@ -295,24 +299,148 @@ describe Mongo::Collection do
295
299
  end
296
300
  end
297
301
  end
302
+
303
+ context 'when provided options' do
304
+
305
+ let(:view) do
306
+ authorized_collection.find({}, options)
307
+ end
308
+
309
+ context 'when provided :allow_partial_results' do
310
+
311
+ let(:options) do
312
+ { allow_partial_results: true }
313
+ end
314
+
315
+ it 'returns a view with :allow_partial_results set' do
316
+ expect(view.options[:allow_partial_results]).to be(options[:allow_partial_results])
317
+ end
318
+ end
319
+
320
+ context 'when provided :batch_size' do
321
+
322
+ let(:options) do
323
+ { batch_size: 100 }
324
+ end
325
+
326
+ it 'returns a view with :batch_size set' do
327
+ expect(view.options[:batch_size]).to be(options[:batch_size])
328
+ end
329
+ end
330
+
331
+ context 'when provided :comment' do
332
+
333
+ let(:options) do
334
+ { comment: 'slow query' }
335
+ end
336
+
337
+ it 'returns a view with :comment set' do
338
+ expect(view.options[:comment]).to be(options[:comment])
339
+ end
340
+ end
341
+
342
+ context 'when provided :cursor_type' do
343
+
344
+ let(:options) do
345
+ { cursor_type: :tailable }
346
+ end
347
+
348
+ it 'returns a view with :cursor_type set' do
349
+ expect(view.options[:cursor_type]).to be(options[:cursor_type])
350
+ end
351
+ end
352
+
353
+ context 'when provided :max_time_ms' do
354
+
355
+ let(:options) do
356
+ { max_time_ms: 500 }
357
+ end
358
+
359
+ it 'returns a view with :max_time_ms set' do
360
+ expect(view.options[:max_time_ms]).to be(options[:max_time_ms])
361
+ end
362
+ end
363
+
364
+ context 'when provided :modifiers' do
365
+
366
+ let(:options) do
367
+ { modifiers: { :$orderby => Mongo::Index::ASCENDING } }
368
+ end
369
+
370
+ it 'returns a view with modifiers set' do
371
+ expect(view.options[:modifiers]).to be(options[:modifiers])
372
+ end
373
+ end
374
+
375
+ context 'when provided :no_cursor_timeout' do
376
+
377
+ let(:options) do
378
+ { no_cursor_timeout: true }
379
+ end
380
+
381
+ it 'returns a view with :no_cursor_timeout set' do
382
+ expect(view.options[:no_cursor_timeout]).to be(options[:no_cursor_timeout])
383
+ end
384
+ end
385
+
386
+ context 'when provided :oplog_replay' do
387
+
388
+ let(:options) do
389
+ { oplog_replay: false }
390
+ end
391
+
392
+ it 'returns a view with :oplog_replay set' do
393
+ expect(view.options[:oplog_replay]).to be(options[:oplog_replay])
394
+ end
395
+ end
396
+
397
+ context 'when provided :projection' do
398
+
399
+ let(:options) do
400
+ { projection: { 'x' => 1 } }
401
+ end
402
+
403
+ it 'returns a view with :projection set' do
404
+ expect(view.options[:projection]).to be(options[:projection])
405
+ end
406
+ end
407
+
408
+ context 'when provided :skip' do
409
+
410
+ let(:options) do
411
+ { skip: 5 }
412
+ end
413
+
414
+ it 'returns a view with :skip set' do
415
+ expect(view.options[:skip]).to be(options[:skip])
416
+ end
417
+ end
418
+
419
+ context 'when provided :sort' do
420
+
421
+ let(:options) do
422
+ { sort: { 'x' => Mongo::Index::ASCENDING } }
423
+ end
424
+
425
+ it 'returns a view with :sort set' do
426
+ expect(view.options[:sort]).to be(options[:sort])
427
+ end
428
+ end
429
+ end
298
430
  end
299
431
 
300
432
  describe '#insert_many' do
301
433
 
302
434
  after do
303
- authorized_collection.find.delete_many
435
+ authorized_collection.delete_many
304
436
  end
305
437
 
306
438
  let(:result) do
307
439
  authorized_collection.insert_many([{ name: 'test1' }, { name: 'test2' }])
308
440
  end
309
441
 
310
- it 'inserts the documents into the collection', if: write_command_enabled? do
311
- expect(result.written_count).to eq(2)
312
- end
313
-
314
- it 'inserts the documents into the collection', unless: write_command_enabled? do
315
- expect(result.written_count).to eq(0)
442
+ it 'inserts the documents into the collection' do
443
+ expect(result.inserted_count).to eq(2)
316
444
  end
317
445
 
318
446
  it 'contains the ids in the result' do
@@ -323,7 +451,7 @@ describe Mongo::Collection do
323
451
  describe '#insert_one' do
324
452
 
325
453
  after do
326
- authorized_collection.find.delete_many
454
+ authorized_collection.delete_many
327
455
  end
328
456
 
329
457
  let(:result) do
@@ -387,4 +515,772 @@ describe Mongo::Collection do
387
515
  end
388
516
  end
389
517
  end
518
+
519
+ describe '#aggregate' do
520
+
521
+ it 'returns an Aggregation object' do
522
+ expect(authorized_collection.aggregate([])).to be_a(Mongo::Collection::View::Aggregation)
523
+ end
524
+
525
+ context 'when options are provided' do
526
+
527
+ let(:options) do
528
+ { :allow_disk_use => true }
529
+ end
530
+
531
+ it 'sets the options on the Aggregation object' do
532
+ expect(authorized_collection.aggregate([], options).options).to eq(options)
533
+ end
534
+ end
535
+ end
536
+
537
+ describe '#count' do
538
+
539
+ let(:documents) do
540
+ (1..10).map{ |i| { field: "test#{i}" }}
541
+ end
542
+
543
+ before do
544
+ authorized_collection.insert_many(documents)
545
+ end
546
+
547
+ after do
548
+ authorized_collection.delete_many
549
+ end
550
+
551
+ it 'returns an integer count' do
552
+ expect(authorized_collection.count).to eq(10)
553
+ end
554
+
555
+ context 'when options are provided' do
556
+
557
+ it 'passes the options to the count' do
558
+ expect(authorized_collection.count({}, limit: 5)).to eq(5)
559
+ end
560
+ end
561
+ end
562
+
563
+ describe '#distinct' do
564
+
565
+ let(:documents) do
566
+ (1..3).map{ |i| { field: "test#{i}" }}
567
+ end
568
+
569
+ before do
570
+ authorized_collection.insert_many(documents)
571
+ end
572
+
573
+ after do
574
+ authorized_collection.delete_many
575
+ end
576
+
577
+ it 'returns the distinct values' do
578
+ expect(authorized_collection.distinct(:field)).to eq([ 'test1', 'test2', 'test3' ])
579
+ end
580
+
581
+ context 'when a selector is provided' do
582
+
583
+ it 'returns the distinct values' do
584
+ expect(authorized_collection.distinct(:field, field: 'test1')).to eq([ 'test1' ])
585
+ end
586
+ end
587
+
588
+ context 'when options are provided' do
589
+
590
+ it 'passes the options to the distinct command' do
591
+ expect(authorized_collection.distinct(:field, {}, max_time_ms: 100)).to eq([ 'test1', 'test2', 'test3' ])
592
+ end
593
+ end
594
+ end
595
+
596
+ describe '#delete_one' do
597
+
598
+ context 'when a selector was provided' do
599
+
600
+ let(:selector) do
601
+ { field: 'test1' }
602
+ end
603
+
604
+ before do
605
+ authorized_collection.insert_many([
606
+ { field: 'test1' },
607
+ { field: 'test1' },
608
+ { field: 'test1' }
609
+ ])
610
+ end
611
+
612
+ after do
613
+ authorized_collection.delete_many
614
+ end
615
+
616
+ let(:response) do
617
+ authorized_collection.delete_one(selector)
618
+ end
619
+
620
+ it 'deletes the first matching document in the collection' do
621
+ expect(response.deleted_count).to eq(1)
622
+ end
623
+ end
624
+
625
+ context 'when no selector was provided' do
626
+
627
+ before do
628
+ authorized_collection.insert_many([{ field: 'test1' }, { field: 'test2' }])
629
+ end
630
+
631
+ let(:response) do
632
+ authorized_collection.delete_one
633
+ end
634
+
635
+ it 'deletes the first document in the collection' do
636
+ expect(response.deleted_count).to eq(1)
637
+ end
638
+ end
639
+ end
640
+
641
+ describe '#delete_many' do
642
+
643
+ before do
644
+ authorized_collection.insert_many([{ field: 'test1' }, { field: 'test2' }])
645
+ end
646
+
647
+ after do
648
+ authorized_collection.delete_many
649
+ end
650
+
651
+ context 'when a selector was provided' do
652
+
653
+ let(:selector) do
654
+ { field: 'test1' }
655
+ end
656
+
657
+ it 'deletes the matching documents in the collection' do
658
+ expect(authorized_collection.delete_many(selector).deleted_count).to eq(1)
659
+ end
660
+ end
661
+
662
+ context 'when no selector was provided' do
663
+
664
+ it 'deletes all the documents in the collection' do
665
+ expect(authorized_collection.delete_many.deleted_count).to eq(2)
666
+ end
667
+ end
668
+ end
669
+
670
+ describe '#replace_one' do
671
+
672
+ let(:selector) do
673
+ { field: 'test1' }
674
+ end
675
+
676
+ context 'when a selector was provided' do
677
+
678
+ before do
679
+ authorized_collection.insert_many([{ field: 'test1' }, { field: 'test1' }])
680
+ end
681
+
682
+ after do
683
+ authorized_collection.delete_many
684
+ end
685
+
686
+ let!(:response) do
687
+ authorized_collection.replace_one(selector, { field: 'testing' })
688
+ end
689
+
690
+ let(:updated) do
691
+ authorized_collection.find(field: 'testing').first
692
+ end
693
+
694
+ it 'updates the first matching document in the collection' do
695
+ expect(response.modified_count).to eq(1)
696
+ end
697
+
698
+ it 'updates the documents in the collection' do
699
+ expect(updated[:field]).to eq('testing')
700
+ end
701
+ end
702
+
703
+ context 'when upsert is false' do
704
+
705
+ let!(:response) do
706
+ authorized_collection.replace_one(selector, { field: 'test1' }, upsert: false)
707
+ end
708
+
709
+ let(:updated) do
710
+ authorized_collection.find(field: 'test1').to_a
711
+ end
712
+
713
+ it 'reports that no documents were written' do
714
+ expect(response.modified_count).to eq(0)
715
+ end
716
+
717
+ it 'does not insert the document' do
718
+ expect(updated).to be_empty
719
+ end
720
+ end
721
+
722
+ context 'when upsert is true' do
723
+
724
+ let!(:response) do
725
+ authorized_collection.replace_one(selector, { field: 'test1' }, upsert: true)
726
+ end
727
+
728
+ let(:updated) do
729
+ authorized_collection.find(field: 'test1').first
730
+ end
731
+
732
+ after do
733
+ authorized_collection.delete_many
734
+ end
735
+
736
+ it 'reports that a document was written' do
737
+ expect(response.written_count).to eq(1)
738
+ end
739
+
740
+ it 'inserts the document' do
741
+ expect(updated[:field]).to eq('test1')
742
+ end
743
+ end
744
+
745
+ context 'when upsert is not specified' do
746
+
747
+ let!(:response) do
748
+ authorized_collection.replace_one(selector, { field: 'test1' })
749
+ end
750
+
751
+ let(:updated) do
752
+ authorized_collection.find(field: 'test1').to_a
753
+ end
754
+
755
+ it 'reports that no documents were written' do
756
+ expect(response.modified_count).to eq(0)
757
+ end
758
+
759
+ it 'does not insert the document' do
760
+ expect(updated).to be_empty
761
+ end
762
+ end
763
+ end
764
+
765
+ describe '#update_many' do
766
+
767
+ let(:selector) do
768
+ { field: 'test' }
769
+ end
770
+
771
+ after do
772
+ authorized_collection.delete_many
773
+ end
774
+
775
+ context 'when a selector was provided' do
776
+
777
+ before do
778
+ authorized_collection.insert_many([{ field: 'test' }, { field: 'test' }])
779
+ end
780
+
781
+ let!(:response) do
782
+ authorized_collection.update_many(selector, '$set'=> { field: 'testing' })
783
+ end
784
+
785
+ let(:updated) do
786
+ authorized_collection.find(field: 'testing').to_a.last
787
+ end
788
+
789
+ it 'returns the number updated' do
790
+ expect(response.modified_count).to eq(2)
791
+ end
792
+
793
+ it 'updates the documents in the collection' do
794
+ expect(updated[:field]).to eq('testing')
795
+ end
796
+ end
797
+
798
+ context 'when upsert is false' do
799
+
800
+ let(:response) do
801
+ authorized_collection.update_many(selector, { '$set'=> { field: 'testing' } },
802
+ upsert: false)
803
+ end
804
+
805
+ let(:updated) do
806
+ authorized_collection.find.to_a
807
+ end
808
+
809
+ it 'reports that no documents were updated' do
810
+ expect(response.modified_count).to eq(0)
811
+ end
812
+
813
+ it 'updates no documents in the collection' do
814
+ expect(updated).to be_empty
815
+ end
816
+ end
817
+
818
+ context 'when upsert is true' do
819
+
820
+ let!(:response) do
821
+ authorized_collection.update_many(selector, { '$set'=> { field: 'testing' } },
822
+ upsert: true)
823
+ end
824
+
825
+ let(:updated) do
826
+ authorized_collection.find.to_a.last
827
+ end
828
+
829
+ it 'reports that a document was written' do
830
+ expect(response.written_count).to eq(1)
831
+ end
832
+
833
+ it 'inserts a document into the collection' do
834
+ expect(updated[:field]).to eq('testing')
835
+ end
836
+ end
837
+
838
+ context 'when upsert is not specified' do
839
+
840
+ let(:response) do
841
+ authorized_collection.update_many(selector, { '$set'=> { field: 'testing' } })
842
+ end
843
+
844
+ let(:updated) do
845
+ authorized_collection.find.to_a
846
+ end
847
+
848
+ it 'reports that no documents were updated' do
849
+ expect(response.modified_count).to eq(0)
850
+ end
851
+
852
+ it 'updates no documents in the collection' do
853
+ expect(updated).to be_empty
854
+ end
855
+ end
856
+ end
857
+
858
+ describe '#update_one' do
859
+
860
+ let(:selector) do
861
+ { field: 'test1' }
862
+ end
863
+
864
+ context 'when a selector was provided' do
865
+
866
+ before do
867
+ authorized_collection.insert_many([{ field: 'test1' }, { field: 'test1' }])
868
+ end
869
+
870
+ let!(:response) do
871
+ authorized_collection.update_one(selector, '$set'=> { field: 'testing' })
872
+ end
873
+
874
+ let(:updated) do
875
+ authorized_collection.find(field: 'testing').first
876
+ end
877
+
878
+ it 'updates the first matching document in the collection' do
879
+ expect(response.modified_count).to eq(1)
880
+ end
881
+
882
+ it 'updates the documents in the collection' do
883
+ expect(updated[:field]).to eq('testing')
884
+ end
885
+ end
886
+
887
+ context 'when upsert is false' do
888
+
889
+ let(:response) do
890
+ authorized_collection.update_one(selector, { '$set'=> { field: 'testing' } },
891
+ upsert: false)
892
+ end
893
+
894
+ let(:updated) do
895
+ authorized_collection.find.to_a
896
+ end
897
+
898
+ it 'reports that no documents were updated' do
899
+ expect(response.modified_count).to eq(0)
900
+ end
901
+
902
+ it 'updates no documents in the collection' do
903
+ expect(updated).to be_empty
904
+ end
905
+ end
906
+
907
+ context 'when upsert is true' do
908
+
909
+ let!(:response) do
910
+ authorized_collection.update_one(selector, { '$set'=> { field: 'testing' } },
911
+ upsert: true)
912
+ end
913
+
914
+ let(:updated) do
915
+ authorized_collection.find.first
916
+ end
917
+
918
+ it 'reports that a document was written' do
919
+ expect(response.written_count).to eq(1)
920
+ end
921
+
922
+ it 'inserts a document into the collection' do
923
+ expect(updated[:field]).to eq('testing')
924
+ end
925
+ end
926
+
927
+ context 'when upsert is not specified' do
928
+
929
+ let(:response) do
930
+ authorized_collection.update_one(selector, { '$set'=> { field: 'testing' } })
931
+ end
932
+
933
+ let(:updated) do
934
+ authorized_collection.find.to_a
935
+ end
936
+
937
+ it 'reports that no documents were updated' do
938
+ expect(response.modified_count).to eq(0)
939
+ end
940
+
941
+ it 'updates no documents in the collection' do
942
+ expect(updated).to be_empty
943
+ end
944
+ end
945
+ end
946
+
947
+ describe '#find_one_and_delete' do
948
+
949
+ before do
950
+ authorized_collection.insert_many([{ field: 'test1' }])
951
+ end
952
+
953
+ context 'when a matching document is found' do
954
+
955
+ let(:selector) do
956
+ { field: 'test1' }
957
+ end
958
+
959
+ context 'when no options are provided' do
960
+
961
+ let!(:document) do
962
+ authorized_collection.find_one_and_delete(selector)
963
+ end
964
+
965
+ it 'deletes the document from the database' do
966
+ expect(authorized_collection.find.to_a).to be_empty
967
+ end
968
+
969
+ it 'returns the document' do
970
+ expect(document['field']).to eq('test1')
971
+ end
972
+ end
973
+
974
+ context 'when a projection is provided' do
975
+
976
+ let!(:document) do
977
+ authorized_collection.find_one_and_delete(selector, projection: { _id: 1 })
978
+ end
979
+
980
+ it 'deletes the document from the database' do
981
+ expect(authorized_collection.find.to_a).to be_empty
982
+ end
983
+
984
+ it 'returns the document with limited fields' do
985
+ expect(document['field']).to be_nil
986
+ expect(document['_id']).to_not be_nil
987
+ end
988
+ end
989
+
990
+ context 'when a sort is provided' do
991
+
992
+ let!(:document) do
993
+ authorized_collection.find_one_and_delete(selector, sort: { field: 1 })
994
+ end
995
+
996
+ it 'deletes the document from the database' do
997
+ expect(authorized_collection.find.to_a).to be_empty
998
+ end
999
+
1000
+ it 'returns the document with limited fields' do
1001
+ expect(document['field']).to eq('test1')
1002
+ end
1003
+ end
1004
+
1005
+ context 'when max_time_ms is provided', if: write_command_enabled? do
1006
+
1007
+ it 'includes the max_time_ms value in the command' do
1008
+ expect {
1009
+ authorized_collection.find_one_and_delete(selector, max_time_ms: 0.1)
1010
+ }.to raise_error(Mongo::Error::OperationFailure)
1011
+ end
1012
+ end
1013
+ end
1014
+
1015
+ context 'when no matching document is found' do
1016
+
1017
+ let(:selector) do
1018
+ { field: 'test5' }
1019
+ end
1020
+
1021
+ let!(:document) do
1022
+ authorized_collection.find_one_and_delete(selector)
1023
+ end
1024
+
1025
+ it 'returns nil' do
1026
+ expect(document).to be_nil
1027
+ end
1028
+ end
1029
+ end
1030
+
1031
+ describe '#find_one_and_update' do
1032
+
1033
+ before do
1034
+ authorized_collection.insert_many([{ field: 'test1' }])
1035
+ end
1036
+
1037
+ let(:selector) do
1038
+ { field: 'test1' }
1039
+ end
1040
+
1041
+ context 'when a matching document is found' do
1042
+
1043
+ context 'when no options are provided' do
1044
+
1045
+ let(:document) do
1046
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }})
1047
+ end
1048
+
1049
+ it 'returns the original document' do
1050
+ expect(document['field']).to eq('test1')
1051
+ end
1052
+ end
1053
+
1054
+ context 'when no options are provided' do
1055
+
1056
+ let(:document) do
1057
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }})
1058
+ end
1059
+
1060
+ it 'returns the original document' do
1061
+ expect(document['field']).to eq('test1')
1062
+ end
1063
+ end
1064
+
1065
+ context 'when return_document options are provided' do
1066
+
1067
+ context 'when return_document is :after' do
1068
+
1069
+ let(:document) do
1070
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }}, :return_document => :after)
1071
+ end
1072
+
1073
+ it 'returns the new document' do
1074
+ expect(document['field']).to eq('testing')
1075
+ end
1076
+ end
1077
+
1078
+ context 'when return_document is :before' do
1079
+
1080
+ let(:document) do
1081
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }}, :return_document => :before)
1082
+ end
1083
+
1084
+ it 'returns the original document' do
1085
+ expect(document['field']).to eq('test1')
1086
+ end
1087
+ end
1088
+ end
1089
+
1090
+ context 'when a projection is provided' do
1091
+
1092
+ let(:document) do
1093
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }}, projection: { _id: 1 })
1094
+ end
1095
+
1096
+ it 'returns the document with limited fields' do
1097
+ expect(document['field']).to be_nil
1098
+ expect(document['_id']).to_not be_nil
1099
+ end
1100
+ end
1101
+
1102
+ context 'when a sort is provided' do
1103
+
1104
+ let(:document) do
1105
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }}, sort: { field: 1 })
1106
+ end
1107
+
1108
+ it 'returns the original document' do
1109
+ expect(document['field']).to eq('test1')
1110
+ end
1111
+ end
1112
+ end
1113
+
1114
+ context 'when max_time_ms is provided' do
1115
+
1116
+ it 'includes the max_time_ms value in the command', if: write_command_enabled? do
1117
+ expect {
1118
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }}, max_time_ms: 0.1)
1119
+ }.to raise_error(Mongo::Error::OperationFailure)
1120
+ end
1121
+ end
1122
+
1123
+ context 'when no matching document is found' do
1124
+
1125
+ let(:selector) do
1126
+ { field: 'test5' }
1127
+ end
1128
+
1129
+ let(:document) do
1130
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }})
1131
+ end
1132
+
1133
+ it 'returns nil' do
1134
+ expect(document).to be_nil
1135
+ end
1136
+ end
1137
+
1138
+ context 'when no matching document is found' do
1139
+
1140
+ context 'when no upsert options are provided' do
1141
+
1142
+ let(:selector) do
1143
+ { field: 'test5' }
1144
+ end
1145
+
1146
+ let(:document) do
1147
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }})
1148
+ end
1149
+
1150
+ it 'returns nil' do
1151
+ expect(document).to be_nil
1152
+ end
1153
+ end
1154
+
1155
+ context 'when upsert options are provided' do
1156
+
1157
+ let(:selector) do
1158
+ { field: 'test5' }
1159
+ end
1160
+
1161
+ let(:document) do
1162
+ authorized_collection.find_one_and_update(selector, { '$set' => { field: 'testing' }}, :upsert => true, :return_document => :after)
1163
+ end
1164
+
1165
+ it 'returns the new document' do
1166
+ expect(document['field']).to eq('testing')
1167
+ end
1168
+ end
1169
+ end
1170
+ end
1171
+
1172
+ describe '#find_one_and_replace' do
1173
+
1174
+ before do
1175
+ authorized_collection.insert_many([{ field: 'test1', other: 'sth' }])
1176
+ end
1177
+
1178
+ let(:selector) do
1179
+ { field: 'test1' }
1180
+ end
1181
+
1182
+ context 'when a matching document is found' do
1183
+
1184
+ context 'when no options are provided' do
1185
+
1186
+ let(:document) do
1187
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' })
1188
+ end
1189
+
1190
+ it 'returns the original document' do
1191
+ expect(document['field']).to eq('test1')
1192
+ end
1193
+ end
1194
+
1195
+ context 'when return_document options are provided' do
1196
+
1197
+ context 'when return_document is :after' do
1198
+
1199
+ let(:document) do
1200
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' }, :return_document => :after)
1201
+ end
1202
+
1203
+ it 'returns the new document' do
1204
+ expect(document['field']).to eq('testing')
1205
+ end
1206
+ end
1207
+
1208
+ context 'when return_document is :before' do
1209
+
1210
+ let(:document) do
1211
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' }, :return_document => :before)
1212
+ end
1213
+
1214
+ it 'returns the original document' do
1215
+ expect(document['field']).to eq('test1')
1216
+ end
1217
+ end
1218
+ end
1219
+
1220
+ context 'when a projection is provided' do
1221
+
1222
+ let(:document) do
1223
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' }, projection: { _id: 1 })
1224
+ end
1225
+
1226
+ it 'returns the document with limited fields' do
1227
+ expect(document['field']).to be_nil
1228
+ expect(document['_id']).to_not be_nil
1229
+ end
1230
+ end
1231
+
1232
+ context 'when a sort is provided' do
1233
+
1234
+ let(:document) do
1235
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' }, :sort => { field: 1 })
1236
+ end
1237
+
1238
+ it 'returns the original document' do
1239
+ expect(document['field']).to eq('test1')
1240
+ end
1241
+ end
1242
+ end
1243
+
1244
+ context 'when no matching document is found' do
1245
+
1246
+ context 'when no upsert options are provided' do
1247
+
1248
+ let(:selector) do
1249
+ { field: 'test5' }
1250
+ end
1251
+
1252
+ let(:document) do
1253
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' })
1254
+ end
1255
+
1256
+ it 'returns nil' do
1257
+ expect(document).to be_nil
1258
+ end
1259
+ end
1260
+
1261
+ context 'when upsert options are provided' do
1262
+
1263
+ let(:selector) do
1264
+ { field: 'test5' }
1265
+ end
1266
+
1267
+ let(:document) do
1268
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' }, :upsert => true, :return_document => :after)
1269
+ end
1270
+
1271
+ it 'returns the new document' do
1272
+ expect(document['field']).to eq('testing')
1273
+ end
1274
+ end
1275
+ end
1276
+
1277
+ context 'when max_time_ms is provided', if: write_command_enabled? do
1278
+
1279
+ it 'includes the max_time_ms value in the command' do
1280
+ expect {
1281
+ authorized_collection.find_one_and_replace(selector, { field: 'testing' }, max_time_ms: 0.1)
1282
+ }.to raise_error(Mongo::Error::OperationFailure)
1283
+ end
1284
+ end
1285
+ end
390
1286
  end