mongo 2.13.0.beta1 → 2.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (339) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +1 -5
  4. data/Rakefile +50 -9
  5. data/lib/mongo.rb +13 -2
  6. data/lib/mongo/address.rb +1 -1
  7. data/lib/mongo/address/ipv4.rb +1 -1
  8. data/lib/mongo/address/ipv6.rb +1 -1
  9. data/lib/mongo/auth/aws/request.rb +31 -5
  10. data/lib/mongo/bulk_write.rb +18 -0
  11. data/lib/mongo/caching_cursor.rb +74 -0
  12. data/lib/mongo/client.rb +238 -31
  13. data/lib/mongo/cluster.rb +56 -20
  14. data/lib/mongo/cluster/sdam_flow.rb +13 -10
  15. data/lib/mongo/cluster/topology/replica_set_no_primary.rb +3 -2
  16. data/lib/mongo/cluster/topology/sharded.rb +1 -1
  17. data/lib/mongo/cluster/topology/single.rb +2 -2
  18. data/lib/mongo/collection.rb +66 -24
  19. data/lib/mongo/collection/view.rb +24 -20
  20. data/lib/mongo/collection/view/aggregation.rb +25 -4
  21. data/lib/mongo/collection/view/builder/find_command.rb +38 -18
  22. data/lib/mongo/collection/view/explainable.rb +27 -8
  23. data/lib/mongo/collection/view/iterable.rb +72 -12
  24. data/lib/mongo/collection/view/readable.rb +19 -3
  25. data/lib/mongo/collection/view/writable.rb +55 -5
  26. data/lib/mongo/crypt/encryption_io.rb +6 -6
  27. data/lib/mongo/cursor.rb +16 -3
  28. data/lib/mongo/database.rb +37 -4
  29. data/lib/mongo/database/view.rb +18 -3
  30. data/lib/mongo/distinguishing_semaphore.rb +55 -0
  31. data/lib/mongo/error.rb +5 -0
  32. data/lib/mongo/error/invalid_read_concern.rb +28 -0
  33. data/lib/mongo/error/invalid_server_auth_host.rb +22 -0
  34. data/lib/mongo/error/invalid_session.rb +2 -1
  35. data/lib/mongo/error/operation_failure.rb +11 -5
  36. data/lib/mongo/error/server_certificate_revoked.rb +22 -0
  37. data/lib/mongo/error/sessions_not_supported.rb +35 -0
  38. data/lib/mongo/error/unsupported_option.rb +14 -12
  39. data/lib/mongo/event/base.rb +6 -0
  40. data/lib/mongo/grid/file.rb +5 -0
  41. data/lib/mongo/grid/file/chunk.rb +2 -0
  42. data/lib/mongo/grid/fs_bucket.rb +15 -13
  43. data/lib/mongo/grid/stream/write.rb +9 -3
  44. data/lib/mongo/index/view.rb +3 -0
  45. data/lib/mongo/lint.rb +2 -1
  46. data/lib/mongo/logger.rb +3 -3
  47. data/lib/mongo/monitoring.rb +38 -0
  48. data/lib/mongo/monitoring/command_log_subscriber.rb +10 -2
  49. data/lib/mongo/monitoring/event/command_failed.rb +11 -0
  50. data/lib/mongo/monitoring/event/command_started.rb +37 -2
  51. data/lib/mongo/monitoring/event/command_succeeded.rb +11 -0
  52. data/lib/mongo/monitoring/event/server_closed.rb +1 -1
  53. data/lib/mongo/monitoring/event/server_description_changed.rb +27 -4
  54. data/lib/mongo/monitoring/event/server_heartbeat_failed.rb +9 -2
  55. data/lib/mongo/monitoring/event/server_heartbeat_started.rb +9 -2
  56. data/lib/mongo/monitoring/event/server_heartbeat_succeeded.rb +9 -2
  57. data/lib/mongo/monitoring/event/server_opening.rb +1 -1
  58. data/lib/mongo/monitoring/event/topology_changed.rb +1 -1
  59. data/lib/mongo/monitoring/event/topology_closed.rb +1 -1
  60. data/lib/mongo/monitoring/event/topology_opening.rb +1 -1
  61. data/lib/mongo/monitoring/publishable.rb +6 -3
  62. data/lib/mongo/monitoring/server_description_changed_log_subscriber.rb +9 -1
  63. data/lib/mongo/monitoring/topology_changed_log_subscriber.rb +1 -1
  64. data/lib/mongo/operation.rb +2 -0
  65. data/lib/mongo/operation/aggregate/result.rb +9 -8
  66. data/lib/mongo/operation/collections_info/command.rb +5 -0
  67. data/lib/mongo/operation/collections_info/result.rb +18 -1
  68. data/lib/mongo/operation/delete/bulk_result.rb +2 -0
  69. data/lib/mongo/operation/delete/result.rb +3 -0
  70. data/lib/mongo/operation/explain/command.rb +4 -0
  71. data/lib/mongo/operation/explain/legacy.rb +4 -0
  72. data/lib/mongo/operation/explain/op_msg.rb +6 -0
  73. data/lib/mongo/operation/explain/result.rb +3 -0
  74. data/lib/mongo/operation/find/legacy/result.rb +2 -0
  75. data/lib/mongo/operation/find/result.rb +13 -0
  76. data/lib/mongo/operation/get_more/result.rb +3 -0
  77. data/lib/mongo/operation/indexes/result.rb +5 -0
  78. data/lib/mongo/operation/insert/bulk_result.rb +5 -0
  79. data/lib/mongo/operation/insert/result.rb +5 -0
  80. data/lib/mongo/operation/list_collections/result.rb +5 -0
  81. data/lib/mongo/operation/map_reduce/result.rb +10 -0
  82. data/lib/mongo/operation/parallel_scan/result.rb +4 -0
  83. data/lib/mongo/operation/result.rb +35 -6
  84. data/lib/mongo/operation/shared/bypass_document_validation.rb +1 -0
  85. data/lib/mongo/operation/shared/causal_consistency_supported.rb +1 -0
  86. data/lib/mongo/operation/shared/collections_info_or_list_collections.rb +2 -0
  87. data/lib/mongo/operation/shared/executable.rb +1 -0
  88. data/lib/mongo/operation/shared/idable.rb +2 -1
  89. data/lib/mongo/operation/shared/limited.rb +1 -0
  90. data/lib/mongo/operation/shared/object_id_generator.rb +1 -0
  91. data/lib/mongo/operation/shared/result/aggregatable.rb +1 -0
  92. data/lib/mongo/operation/shared/sessions_supported.rb +1 -0
  93. data/lib/mongo/operation/shared/specifiable.rb +1 -0
  94. data/lib/mongo/operation/shared/write.rb +1 -0
  95. data/lib/mongo/operation/shared/write_concern_supported.rb +1 -0
  96. data/lib/mongo/operation/update/legacy/result.rb +7 -0
  97. data/lib/mongo/operation/update/result.rb +8 -0
  98. data/lib/mongo/operation/users_info/result.rb +3 -0
  99. data/lib/mongo/protocol/message.rb +47 -10
  100. data/lib/mongo/protocol/msg.rb +34 -1
  101. data/lib/mongo/protocol/query.rb +36 -0
  102. data/lib/mongo/protocol/serializers.rb +5 -2
  103. data/lib/mongo/query_cache.rb +242 -0
  104. data/lib/mongo/retryable.rb +8 -1
  105. data/lib/mongo/server.rb +15 -4
  106. data/lib/mongo/server/app_metadata.rb +27 -3
  107. data/lib/mongo/server/connection.rb +4 -4
  108. data/lib/mongo/server/connection_base.rb +38 -12
  109. data/lib/mongo/server/connection_common.rb +2 -2
  110. data/lib/mongo/server/connection_pool.rb +3 -0
  111. data/lib/mongo/server/description.rb +13 -1
  112. data/lib/mongo/server/monitor.rb +76 -44
  113. data/lib/mongo/server/monitor/connection.rb +57 -9
  114. data/lib/mongo/server/pending_connection.rb +14 -4
  115. data/lib/mongo/server/push_monitor.rb +173 -0
  116. data/{spec/runners/transactions/context.rb → lib/mongo/server/push_monitor/connection.rb} +9 -14
  117. data/lib/mongo/server_selector.rb +0 -1
  118. data/lib/mongo/server_selector/base.rb +583 -1
  119. data/lib/mongo/server_selector/nearest.rb +1 -6
  120. data/lib/mongo/server_selector/primary.rb +1 -6
  121. data/lib/mongo/server_selector/primary_preferred.rb +7 -10
  122. data/lib/mongo/server_selector/secondary.rb +1 -6
  123. data/lib/mongo/server_selector/secondary_preferred.rb +1 -7
  124. data/lib/mongo/session.rb +7 -1
  125. data/lib/mongo/socket.rb +26 -12
  126. data/lib/mongo/socket/ocsp_cache.rb +97 -0
  127. data/lib/mongo/socket/ocsp_verifier.rb +368 -0
  128. data/lib/mongo/socket/ssl.rb +46 -25
  129. data/lib/mongo/socket/tcp.rb +1 -1
  130. data/lib/mongo/srv/monitor.rb +7 -13
  131. data/lib/mongo/srv/resolver.rb +14 -10
  132. data/lib/mongo/timeout.rb +2 -0
  133. data/lib/mongo/topology_version.rb +9 -0
  134. data/lib/mongo/uri.rb +21 -390
  135. data/lib/mongo/uri/options_mapper.rb +582 -0
  136. data/lib/mongo/uri/srv_protocol.rb +3 -2
  137. data/lib/mongo/utils.rb +73 -0
  138. data/lib/mongo/version.rb +1 -1
  139. data/spec/NOTES.aws-auth.md +12 -7
  140. data/spec/README.aws-auth.md +2 -2
  141. data/spec/README.md +63 -1
  142. data/spec/integration/awaited_ismaster_spec.rb +28 -0
  143. data/spec/integration/bson_symbol_spec.rb +4 -2
  144. data/spec/integration/bulk_write_spec.rb +67 -0
  145. data/spec/integration/change_stream_examples_spec.rb +6 -2
  146. data/spec/integration/change_stream_spec.rb +1 -1
  147. data/spec/integration/check_clean_slate_spec.rb +16 -0
  148. data/spec/integration/client_authentication_options_spec.rb +92 -28
  149. data/spec/integration/client_construction_spec.rb +1 -0
  150. data/spec/integration/client_side_encryption/auto_encryption_bulk_writes_spec.rb +9 -5
  151. data/spec/integration/connect_single_rs_name_spec.rb +5 -2
  152. data/spec/integration/connection_pool_populator_spec.rb +4 -2
  153. data/spec/integration/connection_spec.rb +7 -4
  154. data/spec/integration/crud_spec.rb +4 -4
  155. data/spec/integration/cursor_reaping_spec.rb +54 -18
  156. data/spec/integration/docs_examples_spec.rb +6 -0
  157. data/spec/integration/fork_reconnect_spec.rb +56 -1
  158. data/spec/integration/grid_fs_bucket_spec.rb +48 -0
  159. data/spec/integration/heartbeat_events_spec.rb +4 -23
  160. data/spec/integration/ocsp_connectivity_spec.rb +26 -0
  161. data/spec/integration/ocsp_verifier_cache_spec.rb +188 -0
  162. data/spec/integration/ocsp_verifier_spec.rb +334 -0
  163. data/spec/integration/query_cache_spec.rb +1045 -0
  164. data/spec/integration/query_cache_transactions_spec.rb +190 -0
  165. data/spec/integration/read_concern_spec.rb +1 -1
  166. data/spec/integration/retryable_errors_spec.rb +1 -1
  167. data/spec/integration/retryable_writes/retryable_writes_40_and_newer_spec.rb +1 -0
  168. data/spec/integration/retryable_writes/shared/performs_legacy_retries.rb +4 -2
  169. data/spec/integration/retryable_writes/shared/performs_modern_retries.rb +3 -3
  170. data/spec/integration/retryable_writes/shared/performs_no_retries.rb +2 -2
  171. data/spec/integration/sdam_error_handling_spec.rb +122 -15
  172. data/spec/integration/sdam_events_spec.rb +80 -6
  173. data/spec/integration/sdam_prose_spec.rb +64 -0
  174. data/spec/integration/server_monitor_spec.rb +25 -1
  175. data/spec/integration/server_selection_spec.rb +36 -0
  176. data/spec/integration/size_limit_spec.rb +23 -5
  177. data/spec/integration/srv_monitoring_spec.rb +38 -3
  178. data/spec/integration/srv_spec.rb +56 -0
  179. data/spec/integration/ssl_uri_options_spec.rb +2 -2
  180. data/spec/integration/transactions_examples_spec.rb +17 -7
  181. data/spec/integration/zlib_compression_spec.rb +25 -0
  182. data/spec/lite_spec_helper.rb +20 -9
  183. data/spec/mongo/address_spec.rb +1 -1
  184. data/spec/mongo/auth/aws/request_region_spec.rb +42 -0
  185. data/spec/mongo/auth/aws/request_spec.rb +76 -0
  186. data/spec/mongo/auth/scram_spec.rb +1 -1
  187. data/spec/mongo/auth/user_spec.rb +1 -1
  188. data/spec/mongo/bulk_write_spec.rb +2 -2
  189. data/spec/mongo/caching_cursor_spec.rb +70 -0
  190. data/spec/mongo/client_construction_spec.rb +386 -3
  191. data/spec/mongo/client_encryption_spec.rb +16 -10
  192. data/spec/mongo/client_spec.rb +85 -3
  193. data/spec/mongo/cluster/topology/replica_set_spec.rb +53 -10
  194. data/spec/mongo/cluster/topology/sharded_spec.rb +1 -1
  195. data/spec/mongo/cluster/topology/single_spec.rb +19 -8
  196. data/spec/mongo/cluster/topology/unknown_spec.rb +1 -1
  197. data/spec/mongo/cluster/topology_spec.rb +1 -1
  198. data/spec/mongo/cluster_spec.rb +37 -35
  199. data/spec/mongo/collection/view/change_stream_resume_spec.rb +7 -7
  200. data/spec/mongo/collection/view/explainable_spec.rb +87 -4
  201. data/spec/mongo/collection/view/map_reduce_spec.rb +2 -0
  202. data/spec/mongo/collection/view/readable_spec.rb +36 -0
  203. data/spec/mongo/collection_spec.rb +572 -0
  204. data/spec/mongo/crypt/auto_decryption_context_spec.rb +1 -1
  205. data/spec/mongo/crypt/auto_encryption_context_spec.rb +1 -1
  206. data/spec/mongo/crypt/binary_spec.rb +1 -6
  207. data/spec/mongo/crypt/binding/binary_spec.rb +1 -6
  208. data/spec/mongo/crypt/binding/context_spec.rb +2 -7
  209. data/spec/mongo/crypt/binding/helpers_spec.rb +1 -6
  210. data/spec/mongo/crypt/binding/mongocrypt_spec.rb +2 -7
  211. data/spec/mongo/crypt/binding/status_spec.rb +1 -6
  212. data/spec/mongo/crypt/binding/version_spec.rb +1 -6
  213. data/spec/mongo/crypt/data_key_context_spec.rb +1 -1
  214. data/spec/mongo/crypt/explicit_decryption_context_spec.rb +1 -1
  215. data/spec/mongo/crypt/explicit_encryption_context_spec.rb +1 -1
  216. data/spec/mongo/crypt/status_spec.rb +1 -6
  217. data/spec/mongo/database_spec.rb +353 -8
  218. data/spec/mongo/distinguishing_semaphore_spec.rb +63 -0
  219. data/spec/mongo/error/no_server_available_spec.rb +1 -1
  220. data/spec/mongo/error/operation_failure_spec.rb +40 -0
  221. data/spec/mongo/index/view_spec.rb +148 -2
  222. data/spec/mongo/logger_spec.rb +13 -11
  223. data/spec/mongo/monitoring/event/server_closed_spec.rb +1 -1
  224. data/spec/mongo/monitoring/event/server_description_changed_spec.rb +1 -4
  225. data/spec/mongo/monitoring/event/server_opening_spec.rb +1 -1
  226. data/spec/mongo/monitoring/event/topology_changed_spec.rb +1 -1
  227. data/spec/mongo/monitoring/event/topology_closed_spec.rb +1 -1
  228. data/spec/mongo/monitoring/event/topology_opening_spec.rb +1 -1
  229. data/spec/mongo/operation/delete/op_msg_spec.rb +3 -3
  230. data/spec/mongo/operation/insert/command_spec.rb +2 -2
  231. data/spec/mongo/operation/insert/op_msg_spec.rb +3 -3
  232. data/spec/mongo/operation/read_preference_op_msg_spec.rb +1 -1
  233. data/spec/mongo/operation/update/command_spec.rb +2 -2
  234. data/spec/mongo/operation/update/op_msg_spec.rb +3 -3
  235. data/spec/mongo/protocol/msg_spec.rb +10 -0
  236. data/spec/mongo/query_cache_spec.rb +280 -0
  237. data/spec/mongo/semaphore_spec.rb +51 -0
  238. data/spec/mongo/server/app_metadata_shared.rb +82 -2
  239. data/spec/mongo/server/connection_auth_spec.rb +2 -2
  240. data/spec/mongo/server/connection_pool_spec.rb +7 -3
  241. data/spec/mongo/server/connection_spec.rb +15 -8
  242. data/spec/mongo/server/description_spec.rb +18 -0
  243. data/spec/mongo/server_selector/nearest_spec.rb +23 -23
  244. data/spec/mongo/server_selector/primary_preferred_spec.rb +26 -26
  245. data/spec/mongo/server_selector/primary_spec.rb +9 -9
  246. data/spec/mongo/server_selector/secondary_preferred_spec.rb +22 -22
  247. data/spec/mongo/server_selector/secondary_spec.rb +18 -18
  248. data/spec/mongo/server_selector_spec.rb +6 -6
  249. data/spec/mongo/session_spec.rb +35 -0
  250. data/spec/mongo/socket/ssl_spec.rb +4 -4
  251. data/spec/mongo/socket_spec.rb +1 -1
  252. data/spec/mongo/uri/srv_protocol_spec.rb +64 -33
  253. data/spec/mongo/uri_option_parsing_spec.rb +11 -11
  254. data/spec/mongo/uri_spec.rb +68 -41
  255. data/spec/mongo/utils_spec.rb +39 -0
  256. data/spec/runners/auth.rb +3 -0
  257. data/spec/runners/change_streams/test.rb +3 -3
  258. data/spec/runners/cmap.rb +1 -1
  259. data/spec/runners/command_monitoring.rb +3 -34
  260. data/spec/runners/connection_string.rb +35 -124
  261. data/spec/runners/crud/context.rb +9 -5
  262. data/spec/runners/crud/operation.rb +59 -27
  263. data/spec/runners/crud/spec.rb +0 -8
  264. data/spec/runners/crud/test.rb +1 -1
  265. data/spec/runners/crud/test_base.rb +0 -19
  266. data/spec/runners/sdam.rb +2 -2
  267. data/spec/runners/server_selection.rb +242 -28
  268. data/spec/runners/transactions.rb +12 -12
  269. data/spec/runners/transactions/operation.rb +151 -25
  270. data/spec/runners/transactions/test.rb +62 -18
  271. data/spec/shared/LICENSE +20 -0
  272. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  273. data/spec/shared/lib/mrss/constraints.rb +303 -0
  274. data/spec/shared/lib/mrss/lite_constraints.rb +175 -0
  275. data/spec/shared/lib/mrss/spec_organizer.rb +149 -0
  276. data/spec/spec_helper.rb +3 -1
  277. data/spec/spec_tests/cmap_spec.rb +7 -3
  278. data/spec/spec_tests/command_monitoring_spec.rb +22 -12
  279. data/spec/spec_tests/crud_spec.rb +1 -1
  280. data/spec/spec_tests/data/change_streams/change-streams-errors.yml +4 -9
  281. data/spec/spec_tests/data/change_streams/change-streams-resume-whitelist.yml +66 -0
  282. data/spec/spec_tests/data/change_streams/change-streams.yml +0 -1
  283. data/spec/spec_tests/data/cmap/pool-checkout-connection.yml +6 -2
  284. data/spec/spec_tests/data/cmap/pool-create-min-size.yml +3 -0
  285. data/spec/spec_tests/data/connection_string/valid-warnings.yml +24 -0
  286. data/spec/spec_tests/data/max_staleness/ReplicaSetNoPrimary/MaxStalenessTooSmall.yml +15 -0
  287. data/spec/spec_tests/data/max_staleness/ReplicaSetNoPrimary/NoKnownServers.yml +4 -3
  288. data/spec/spec_tests/data/max_staleness/Unknown/SmallMaxStaleness.yml +1 -0
  289. data/spec/spec_tests/data/sdam_integration/cancel-server-check.yml +96 -0
  290. data/spec/spec_tests/data/sdam_integration/connectTimeoutMS.yml +88 -0
  291. data/spec/spec_tests/data/sdam_integration/find-network-error.yml +83 -0
  292. data/spec/spec_tests/data/sdam_integration/find-shutdown-error.yml +116 -0
  293. data/spec/spec_tests/data/sdam_integration/insert-network-error.yml +86 -0
  294. data/spec/spec_tests/data/sdam_integration/insert-shutdown-error.yml +115 -0
  295. data/spec/spec_tests/data/sdam_integration/isMaster-command-error.yml +168 -0
  296. data/spec/spec_tests/data/sdam_integration/isMaster-network-error.yml +162 -0
  297. data/spec/spec_tests/data/sdam_integration/isMaster-timeout.yml +229 -0
  298. data/spec/spec_tests/data/sdam_integration/rediscover-quickly-after-step-down.yml +87 -0
  299. data/spec/spec_tests/data/sdam_monitoring/discovered_standalone.yml +1 -3
  300. data/spec/spec_tests/data/sdam_monitoring/standalone.yml +2 -2
  301. data/spec/spec_tests/data/sdam_monitoring/standalone_repeated.yml +2 -2
  302. data/spec/spec_tests/data/sdam_monitoring/standalone_suppress_equal_description_changes.yml +2 -2
  303. data/spec/spec_tests/data/sdam_monitoring/standalone_to_rs_with_me_mismatch.yml +2 -2
  304. data/spec/spec_tests/data/uri_options/auth-options.yml +25 -0
  305. data/spec/spec_tests/data/uri_options/compression-options.yml +6 -3
  306. data/spec/spec_tests/data/uri_options/read-preference-options.yml +24 -0
  307. data/spec/spec_tests/data/uri_options/ruby-connection-options.yml +1 -0
  308. data/spec/spec_tests/data/uri_options/tls-options.yml +160 -4
  309. data/spec/spec_tests/dns_seedlist_discovery_spec.rb +9 -1
  310. data/spec/spec_tests/max_staleness_spec.rb +4 -142
  311. data/spec/spec_tests/retryable_reads_spec.rb +2 -2
  312. data/spec/spec_tests/sdam_integration_spec.rb +13 -0
  313. data/spec/spec_tests/sdam_monitoring_spec.rb +1 -2
  314. data/spec/spec_tests/server_selection_spec.rb +4 -116
  315. data/spec/spec_tests/uri_options_spec.rb +31 -33
  316. data/spec/stress/cleanup_spec.rb +17 -2
  317. data/spec/stress/connection_pool_stress_spec.rb +10 -8
  318. data/spec/stress/fork_reconnect_stress_spec.rb +1 -1
  319. data/spec/support/certificates/atlas-ocsp-ca.crt +28 -0
  320. data/spec/support/certificates/atlas-ocsp.crt +41 -0
  321. data/spec/support/client_registry.rb +1 -0
  322. data/spec/support/client_registry_macros.rb +11 -2
  323. data/spec/support/cluster_config.rb +4 -0
  324. data/spec/support/common_shortcuts.rb +45 -0
  325. data/spec/support/constraints.rb +6 -253
  326. data/spec/support/event_subscriber.rb +123 -33
  327. data/spec/support/keyword_struct.rb +26 -0
  328. data/spec/support/matchers.rb +16 -0
  329. data/spec/support/ocsp +1 -0
  330. data/spec/support/session_registry.rb +52 -0
  331. data/spec/support/shared/server_selector.rb +13 -1
  332. data/spec/support/spec_config.rb +60 -13
  333. data/spec/support/spec_setup.rb +1 -1
  334. data/spec/support/utils.rb +84 -1
  335. metadata +1027 -937
  336. metadata.gz.sig +0 -0
  337. data/lib/mongo/server_selector/selectable.rb +0 -560
  338. data/spec/runners/sdam_monitoring.rb +0 -89
  339. data/spec/support/lite_constraints.rb +0 -141
@@ -183,7 +183,7 @@ describe Mongo::Collection::View::ChangeStream do
183
183
 
184
184
  context 'when a killCursors command is issued for the cursor' do
185
185
  context 'using Enumerable' do
186
- only_mri
186
+ require_mri
187
187
 
188
188
  before do
189
189
  change_stream
@@ -196,7 +196,7 @@ describe Mongo::Collection::View::ChangeStream do
196
196
  change_stream.to_enum
197
197
  end
198
198
 
199
- it 'propagates cursor not found error' do
199
+ it 'resumes on a cursor not found error' do
200
200
  original_cursor_id = cursor.id
201
201
 
202
202
  client.use(:admin).command({
@@ -204,9 +204,9 @@ describe Mongo::Collection::View::ChangeStream do
204
204
  cursors: [cursor.id]
205
205
  })
206
206
 
207
- lambda do
207
+ expect do
208
208
  enum.next
209
- end.should raise_error(Mongo::Error::OperationFailure, /cursor.*not found/)
209
+ end.not_to raise_error
210
210
  end
211
211
  end
212
212
 
@@ -218,7 +218,7 @@ describe Mongo::Collection::View::ChangeStream do
218
218
  collection.insert_one(a:2)
219
219
  end
220
220
 
221
- it 'propagates cursor not found error' do
221
+ it 'resumes on a cursor not found error' do
222
222
  original_cursor_id = cursor.id
223
223
 
224
224
  client.use(:admin).command({
@@ -226,9 +226,9 @@ describe Mongo::Collection::View::ChangeStream do
226
226
  cursors: [cursor.id]
227
227
  })
228
228
 
229
- lambda do
229
+ expect do
230
230
  change_stream.try_next
231
- end.should raise_error(Mongo::Error::OperationFailure, /cursor.*not found/)
231
+ end.not_to raise_error
232
232
  end
233
233
  end
234
234
  end
@@ -20,12 +20,95 @@ describe Mongo::Collection::View::Explainable do
20
20
 
21
21
  describe '#explain' do
22
22
 
23
- let(:explain) do
24
- view.explain
23
+ shared_examples 'executes the explain' do
24
+ context '3.0+ server' do
25
+ min_server_fcv '3.0'
26
+
27
+ context 'not sharded' do
28
+ require_topology :single, :replica_set
29
+
30
+ it 'executes the explain' do
31
+ explain[:queryPlanner][:namespace].should == authorized_collection.namespace
32
+ end
33
+ end
34
+
35
+ context 'sharded' do
36
+ require_topology :sharded
37
+
38
+ context 'pre-3.2 server' do
39
+ max_server_version '3.0'
40
+
41
+ it 'executes the explain' do
42
+ explain[:queryPlanner][:parsedQuery].should be_a(Hash)
43
+ end
44
+ end
45
+
46
+ context '3.2+ server' do
47
+ min_server_fcv '3.2'
48
+
49
+ it 'executes the explain' do
50
+ explain[:queryPlanner][:mongosPlannerVersion].should == 1
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ context '2.6 server' do
57
+ max_server_version '2.6'
58
+
59
+ it 'executes the explain' do
60
+ explain[:cursor].should == 'BasicCursor'
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'without arguments' do
66
+ let(:explain) do
67
+ view.explain
68
+ end
69
+
70
+ include_examples 'executes the explain'
25
71
  end
26
72
 
27
- it 'executes an explain' do
28
- expect(explain[:cursor] == 'BasicCursor' || explain[:queryPlanner]).to be_truthy
73
+ context 'with verbosity argument' do
74
+ let(:explain) do
75
+ view.explain(verbosity: verbosity)
76
+ end
77
+
78
+ shared_examples 'triggers server error' do
79
+ # 3.0 does not produce the error.
80
+ min_server_fcv '3.2'
81
+
82
+ it 'triggers server error' do
83
+ lambda do
84
+ explain
85
+ end.should raise_error(Mongo::Error::OperationFailure, /verbosity string must be/)
86
+ end
87
+ end
88
+
89
+ context 'valid symbol value' do
90
+ let(:verbosity) { :query_planner }
91
+
92
+ include_examples 'executes the explain'
93
+ end
94
+
95
+ context 'valid string value' do
96
+ let(:verbosity) { 'executionStats' }
97
+
98
+ include_examples 'executes the explain'
99
+ end
100
+
101
+ context 'invalid symbol value' do
102
+ let(:verbosity) { :bogus }
103
+
104
+ include_examples 'triggers server error'
105
+ end
106
+
107
+ context 'invalid string value' do
108
+ let(:verbosity) { 'bogus' }
109
+
110
+ include_examples 'triggers server error'
111
+ end
29
112
  end
30
113
  end
31
114
  end
@@ -581,6 +581,7 @@ describe Mongo::Collection::View::MapReduce do
581
581
 
582
582
  context 'when the server is not valid for writing' do
583
583
  clean_slate
584
+ require_warning_clean
584
585
 
585
586
  before do
586
587
  stop_monitoring(authorized_client)
@@ -661,6 +662,7 @@ describe Mongo::Collection::View::MapReduce do
661
662
 
662
663
  context 'when the server is a valid for writing' do
663
664
  clean_slate
665
+ require_warning_clean
664
666
 
665
667
  before do
666
668
  stop_monitoring(authorized_client)
@@ -602,6 +602,42 @@ describe Mongo::Collection::View::Readable do
602
602
  end
603
603
  end
604
604
 
605
+ describe "#estimated_document_count" do
606
+
607
+ let(:documents) do
608
+ (1..10).map{ |i| { field: "test#{i}" }}
609
+ end
610
+
611
+ before do
612
+ authorized_collection.delete_many
613
+ authorized_collection.insert_many(documents)
614
+ end
615
+
616
+ let(:result) do
617
+ view.estimated_document_count(options)
618
+ end
619
+
620
+ context 'when a selector is provided' do
621
+
622
+ let(:selector) do
623
+ { field: 'test1' }
624
+ end
625
+
626
+ it 'raises an error' do
627
+ expect {
628
+ result
629
+ }.to raise_error(ArgumentError, "Cannot call estimated_document_count when querying with a filter")
630
+ end
631
+ end
632
+
633
+ context 'when no selector is provided' do
634
+
635
+ it 'returns the estimated count of matching documents' do
636
+ expect(view.estimated_document_count).to eq(10)
637
+ end
638
+ end
639
+ end
640
+
605
641
  describe '#distinct' do
606
642
 
607
643
  context 'when incorporating read concern' do
@@ -821,6 +821,38 @@ describe Mongo::Collection do
821
821
  end
822
822
  end
823
823
 
824
+ context 'when write concern passed in as an option' do
825
+ min_server_fcv '3.4'
826
+ require_topology :replica_set
827
+
828
+ before do
829
+ database['collection_spec'].drop
830
+ end
831
+
832
+ let(:events) do
833
+ subscriber.command_started_events('create')
834
+ end
835
+
836
+ let(:options) do
837
+ { write_concern: {w: 1} }
838
+ end
839
+
840
+ let!(:collection) do
841
+ authorized_collection.with(options)
842
+ end
843
+
844
+ let!(:command) do
845
+ Utils.get_command_event(authorized_client, 'create') do |client|
846
+ collection.create({ write_concern: {w: 2} })
847
+ end.command
848
+ end
849
+
850
+ it 'applies the write concern passed in as an option' do
851
+ expect(events.length).to eq(1)
852
+ expect(command[:writeConcern][:w]).to eq(2)
853
+ end
854
+ end
855
+
824
856
  context 'when the server does not support write concern on the create command' do
825
857
  max_server_version '3.2'
826
858
 
@@ -941,6 +973,30 @@ describe Mongo::Collection do
941
973
  it_behaves_like 'a failed operation using a session'
942
974
  end
943
975
  end
976
+
977
+ context 'when collation has a strength' do
978
+ min_server_fcv '3.4'
979
+
980
+ let(:band_collection) do
981
+ described_class.new(database, :bands)
982
+ end
983
+
984
+ before do
985
+ band_collection.delete_many
986
+ band_collection.insert_many([{ name: "Depeche Mode" }, { name: "New Order" }])
987
+ end
988
+
989
+ let(:options) do
990
+ { collation: { locale: 'en_US', strength: 2 } }
991
+ end
992
+ let(:band_result) do
993
+ band_collection.find({ name: 'DEPECHE MODE' }, options)
994
+ end
995
+
996
+ it 'finds Capitalize from UPPER CASE' do
997
+ expect(band_result.count_documents).to eq(1)
998
+ end
999
+ end
944
1000
  end
945
1001
 
946
1002
  describe '#drop' do
@@ -1035,6 +1091,34 @@ describe Mongo::Collection do
1035
1091
  end
1036
1092
  end
1037
1093
 
1094
+ context 'when write concern passed in as an option' do
1095
+ min_server_fcv '3.4'
1096
+ require_set_write_concern
1097
+
1098
+ let(:events) do
1099
+ subscriber.command_started_events('drop')
1100
+ end
1101
+
1102
+ let(:options) do
1103
+ { write_concern: {w: 1} }
1104
+ end
1105
+
1106
+ let!(:collection) do
1107
+ authorized_collection.with(options)
1108
+ end
1109
+
1110
+ let!(:command) do
1111
+ Utils.get_command_event(authorized_client, 'drop') do |client|
1112
+ collection.drop({ write_concern: {w: 0} })
1113
+ end.command
1114
+ end
1115
+
1116
+ it 'applies the write concern passed in as an option' do
1117
+ expect(events.length).to eq(1)
1118
+ expect(command[:writeConcern][:w]).to eq(0)
1119
+ end
1120
+ end
1121
+
1038
1122
  context 'when the server does not support write concern on the drop command' do
1039
1123
  max_server_version '3.2'
1040
1124
 
@@ -1601,6 +1685,40 @@ describe Mongo::Collection do
1601
1685
  expect(result.inserted_count).to be(0)
1602
1686
  end
1603
1687
  end
1688
+
1689
+ context 'when various options passed in' do
1690
+ # w: 2 requires a replica set
1691
+ require_topology :replica_set
1692
+
1693
+ # https://jira.mongodb.org/browse/RUBY-2306
1694
+ min_server_fcv '3.6'
1695
+
1696
+ let(:session) do
1697
+ authorized_client.start_session
1698
+ end
1699
+
1700
+ let(:events) do
1701
+ subscriber.command_started_events('insert')
1702
+ end
1703
+
1704
+ let(:collection) do
1705
+ authorized_collection.with(write_concern: {w: 2})
1706
+ end
1707
+
1708
+ let!(:command) do
1709
+ Utils.get_command_event(authorized_client, 'insert') do |client|
1710
+ collection.insert_many([{ name: 'test1' }, { name: 'test2' }], session: session,
1711
+ write_concern: {w: 1}, bypass_document_validation: true)
1712
+ end.command
1713
+ end
1714
+
1715
+ it 'inserts many successfully with correct options sent to server' do
1716
+ expect(events.length).to eq(1)
1717
+ expect(command[:writeConcern]).to_not be_nil
1718
+ expect(command[:writeConcern][:w]).to eq(1)
1719
+ expect(command[:bypassDocumentValidation]).to be(true)
1720
+ end
1721
+ end
1604
1722
  end
1605
1723
 
1606
1724
  describe '#insert_one' do
@@ -1683,6 +1801,37 @@ describe Mongo::Collection do
1683
1801
  it_behaves_like 'an implicit session with an unacknowledged write'
1684
1802
  end
1685
1803
 
1804
+ context 'when various options passed in' do
1805
+ # https://jira.mongodb.org/browse/RUBY-2306
1806
+ min_server_fcv '3.6'
1807
+
1808
+ let(:session) do
1809
+ authorized_client.start_session
1810
+ end
1811
+
1812
+ let(:events) do
1813
+ subscriber.command_started_events('insert')
1814
+ end
1815
+
1816
+ let(:collection) do
1817
+ authorized_collection.with(write_concern: {w: 3})
1818
+ end
1819
+
1820
+ let!(:command) do
1821
+ Utils.get_command_event(authorized_client, 'insert') do |client|
1822
+ collection.insert_one({name: 'test1'}, session: session, write_concern: {w: 1},
1823
+ bypass_document_validation: true)
1824
+ end.command
1825
+ end
1826
+
1827
+ it 'inserts one successfully with correct options sent to server' do
1828
+ expect(events.length).to eq(1)
1829
+ expect(command[:writeConcern]).to_not be_nil
1830
+ expect(command[:writeConcern][:w]).to eq(1)
1831
+ expect(command[:bypassDocumentValidation]).to be(true)
1832
+ end
1833
+ end
1834
+
1686
1835
  context 'when the document contains invalid keys' do
1687
1836
 
1688
1837
  let(:doc) do
@@ -1696,6 +1845,18 @@ describe Mongo::Collection do
1696
1845
  end
1697
1846
  end
1698
1847
 
1848
+ context 'when the document is nil' do
1849
+ let(:result) do
1850
+ authorized_collection.insert_one(nil)
1851
+ end
1852
+
1853
+ it 'raises an ArgumentError' do
1854
+ expect {
1855
+ result
1856
+ }.to raise_error(ArgumentError, "Document to be inserted cannot be nil")
1857
+ end
1858
+ end
1859
+
1699
1860
  context 'when the insert fails' do
1700
1861
 
1701
1862
  let(:result) do
@@ -1791,6 +1952,52 @@ describe Mongo::Collection do
1791
1952
  end
1792
1953
  end
1793
1954
 
1955
+ describe '#bulk_write' do
1956
+
1957
+ context 'when various options passed in' do
1958
+ min_server_fcv '3.2'
1959
+ require_topology :replica_set
1960
+
1961
+ # https://jira.mongodb.org/browse/RUBY-2306
1962
+ min_server_fcv '3.6'
1963
+
1964
+ let(:requests) do
1965
+ [
1966
+ { insert_one: { name: "anne" }},
1967
+ { insert_one: { name: "bob" }},
1968
+ { insert_one: { name: "charlie" }}
1969
+ ]
1970
+ end
1971
+
1972
+ let(:session) do
1973
+ authorized_client.start_session
1974
+ end
1975
+
1976
+ let!(:command) do
1977
+ Utils.get_command_event(authorized_client, 'insert') do |client|
1978
+ collection.bulk_write(requests, session: session, write_concern: {w: 1},
1979
+ bypass_document_validation: true)
1980
+ end.command
1981
+ end
1982
+
1983
+ let(:events) do
1984
+ subscriber.command_started_events('insert')
1985
+ end
1986
+
1987
+ let(:collection) do
1988
+ authorized_collection.with(write_concern: {w: 2})
1989
+ end
1990
+
1991
+ it 'inserts successfully with correct options sent to server' do
1992
+ expect(collection.count).to eq(3)
1993
+ expect(events.length).to eq(1)
1994
+ expect(command[:writeConcern]).to_not be_nil
1995
+ expect(command[:writeConcern][:w]).to eq(1)
1996
+ expect(command[:bypassDocumentValidation]).to eq(true)
1997
+ end
1998
+ end
1999
+ end
2000
+
1794
2001
  describe '#inspect' do
1795
2002
 
1796
2003
  it 'includes the object id' do
@@ -2000,6 +2207,39 @@ describe Mongo::Collection do
2000
2207
  end
2001
2208
 
2002
2209
  describe '#count_documents' do
2210
+
2211
+ before do
2212
+ authorized_collection.delete_many
2213
+ end
2214
+
2215
+ context 'no argument provided' do
2216
+
2217
+ context 'when collection is empty' do
2218
+ it 'returns 0 matching documents' do
2219
+ expect(authorized_collection.count_documents).to eq(0)
2220
+ end
2221
+ end
2222
+
2223
+ context 'when collection is not empty' do
2224
+
2225
+ let(:documents) do
2226
+ documents = []
2227
+ 1.upto(10) do |index|
2228
+ documents << { key: 'a', _id: "in#{index}" }
2229
+ end
2230
+ documents
2231
+ end
2232
+
2233
+ before do
2234
+ authorized_collection.insert_many(documents)
2235
+ end
2236
+
2237
+ it 'returns 10 matching documents' do
2238
+ expect(authorized_collection.count_documents).to eq(10)
2239
+ end
2240
+ end
2241
+ end
2242
+
2003
2243
  context 'when transactions are enabled' do
2004
2244
  require_wired_tiger
2005
2245
  require_transaction_support
@@ -2478,6 +2718,48 @@ describe Mongo::Collection do
2478
2718
  expect(authorized_collection.find(name: 'bang').count).to eq(1)
2479
2719
  end
2480
2720
  end
2721
+
2722
+ context 'when various options passed in' do
2723
+ # w: 2 requires a replica set
2724
+ require_topology :replica_set
2725
+
2726
+ # https://jira.mongodb.org/browse/RUBY-2306
2727
+ min_server_fcv '3.6'
2728
+
2729
+ before do
2730
+ authorized_collection.insert_many([{ name: 'test1' }, { name: 'test2' }])
2731
+ end
2732
+
2733
+ let(:selector) do
2734
+ {name: 'test2'}
2735
+ end
2736
+
2737
+ let(:session) do
2738
+ authorized_client.start_session
2739
+ end
2740
+
2741
+ let(:events) do
2742
+ subscriber.command_started_events('delete')
2743
+ end
2744
+
2745
+ let(:collection) do
2746
+ authorized_collection.with(write_concern: {w: 2})
2747
+ end
2748
+
2749
+ let!(:command) do
2750
+ Utils.get_command_event(authorized_client, 'delete') do |client|
2751
+ collection.delete_one(selector, session: session, write_concern: {w: 1},
2752
+ bypass_document_validation: true)
2753
+ end.command
2754
+ end
2755
+
2756
+ it 'deletes one successfully with correct options sent to server' do
2757
+ expect(events.length).to eq(1)
2758
+ expect(command[:writeConcern]).to_not be_nil
2759
+ expect(command[:writeConcern][:w]).to eq(1)
2760
+ expect(command[:bypassDocumentValidation]).to eq(true)
2761
+ end
2762
+ end
2481
2763
  end
2482
2764
 
2483
2765
  describe '#delete_many' do
@@ -2668,6 +2950,48 @@ describe Mongo::Collection do
2668
2950
  expect(authorized_collection.find(name: 'bang').count).to eq(2)
2669
2951
  end
2670
2952
  end
2953
+
2954
+ context 'when various options passed in' do
2955
+ # w: 2 requires a replica set
2956
+ require_topology :replica_set
2957
+
2958
+ # https://jira.mongodb.org/browse/RUBY-2306
2959
+ min_server_fcv '3.6'
2960
+
2961
+ before do
2962
+ collection.insert_many([{ name: 'test1' }, { name: 'test2' }, { name: 'test3'}])
2963
+ end
2964
+
2965
+ let(:selector) do
2966
+ {name: 'test1'}
2967
+ end
2968
+
2969
+ let(:session) do
2970
+ authorized_client.start_session
2971
+ end
2972
+
2973
+ let(:events) do
2974
+ subscriber.command_started_events('delete')
2975
+ end
2976
+
2977
+ let(:collection) do
2978
+ authorized_collection.with(write_concern: {w: 1})
2979
+ end
2980
+
2981
+ let!(:command) do
2982
+ Utils.get_command_event(authorized_client, 'delete') do |client|
2983
+ collection.delete_many(selector, session: session, write_concern: {w: 2},
2984
+ bypass_document_validation: true)
2985
+ end.command
2986
+ end
2987
+
2988
+ it 'deletes many successfully with correct options sent to server' do
2989
+ expect(events.length).to eq(1)
2990
+ expect(command[:writeConcern]).to_not be_nil
2991
+ expect(command[:writeConcern][:w]).to eq(2)
2992
+ expect(command[:bypassDocumentValidation]).to be(true)
2993
+ end
2994
+ end
2671
2995
  end
2672
2996
 
2673
2997
  describe '#parallel_scan' do
@@ -3174,6 +3498,51 @@ describe Mongo::Collection do
3174
3498
 
3175
3499
  it_behaves_like 'an implicit session with an unacknowledged write'
3176
3500
  end
3501
+
3502
+ context 'when various options passed in' do
3503
+ # w: 2 requires a replica set
3504
+ require_topology :replica_set
3505
+
3506
+ # https://jira.mongodb.org/browse/RUBY-2306
3507
+ min_server_fcv '3.6'
3508
+
3509
+ before do
3510
+ authorized_collection.insert_one({field: 'test1'})
3511
+ end
3512
+
3513
+ let(:session) do
3514
+ authorized_client.start_session
3515
+ end
3516
+
3517
+ let(:events) do
3518
+ subscriber.command_started_events('update')
3519
+ end
3520
+
3521
+ let(:collection) do
3522
+ authorized_collection.with(write_concern: {w: 3})
3523
+ end
3524
+
3525
+ let(:updated) do
3526
+ collection.find(field: 'test4').first
3527
+ end
3528
+
3529
+ let!(:command) do
3530
+ Utils.get_command_event(authorized_client, 'update') do |client|
3531
+ collection.replace_one(selector, { field: 'test4'},
3532
+ session: session, :return_document => :after, write_concern: {w: 2},
3533
+ upsert: true, bypass_document_validation: true)
3534
+ end.command
3535
+ end
3536
+
3537
+ it 'replaced one successfully with correct options sent to server' do
3538
+ expect(updated[:field]).to eq('test4')
3539
+ expect(events.length).to eq(1)
3540
+ expect(command[:writeConcern]).to_not be_nil
3541
+ expect(command[:writeConcern][:w]).to eq(2)
3542
+ expect(command[:bypassDocumentValidation]).to be(true)
3543
+ expect(command[:updates][0][:upsert]).to be(true)
3544
+ end
3545
+ end
3177
3546
  end
3178
3547
 
3179
3548
  describe '#update_many' do
@@ -3596,6 +3965,45 @@ describe Mongo::Collection do
3596
3965
 
3597
3966
  it_behaves_like 'an implicit session with an unacknowledged write'
3598
3967
  end
3968
+
3969
+ context 'when various options passed in' do
3970
+ # w: 2 requires a replica set
3971
+ require_topology :replica_set
3972
+
3973
+ # https://jira.mongodb.org/browse/RUBY-2306
3974
+ min_server_fcv '3.6'
3975
+
3976
+ before do
3977
+ collection.insert_many([{ field: 'test' }, { field: 'test2' }], session: session)
3978
+ end
3979
+
3980
+ let(:session) do
3981
+ authorized_client.start_session
3982
+ end
3983
+
3984
+ let(:collection) do
3985
+ authorized_collection.with(write_concern: {w: 1})
3986
+ end
3987
+
3988
+ let(:events) do
3989
+ subscriber.command_started_events('update')
3990
+ end
3991
+
3992
+ let!(:command) do
3993
+ Utils.get_command_event(authorized_client, 'update') do |client|
3994
+ collection.update_many(selector, {'$set'=> { field: 'testing' }}, session: session,
3995
+ write_concern: {w: 2}, bypass_document_validation: true, upsert: true)
3996
+ end.command
3997
+ end
3998
+
3999
+ it 'updates many successfully with correct options sent to server' do
4000
+ expect(events.length).to eq(1)
4001
+ expect(collection.options[:write_concern]).to eq(w: 1)
4002
+ expect(command[:writeConcern][:w]).to eq(2)
4003
+ expect(command[:bypassDocumentValidation]).to be(true)
4004
+ expect(command[:updates][0][:upsert]).to be(true)
4005
+ end
4006
+ end
3599
4007
  end
3600
4008
 
3601
4009
  describe '#update_one' do
@@ -4014,6 +4422,47 @@ describe Mongo::Collection do
4014
4422
 
4015
4423
  it_behaves_like 'an implicit session with an unacknowledged write'
4016
4424
  end
4425
+
4426
+ context 'when various options passed in' do
4427
+ # w: 2 requires a replica set
4428
+ require_topology :replica_set
4429
+
4430
+ # https://jira.mongodb.org/browse/RUBY-2306
4431
+ min_server_fcv '3.6'
4432
+
4433
+ before do
4434
+ collection.insert_many([{ field: 'test1' }, { field: 'test2' }], session: session)
4435
+ end
4436
+
4437
+ let(:session) do
4438
+ authorized_client.start_session
4439
+ end
4440
+
4441
+ let(:collection) do
4442
+ authorized_collection.with(write_concern: {w: 1})
4443
+ end
4444
+
4445
+ let(:events) do
4446
+ subscriber.command_started_events('update')
4447
+ end
4448
+
4449
+ let!(:command) do
4450
+ Utils.get_command_event(authorized_client, 'update') do |client|
4451
+ collection.update_one(selector, { '$set'=> { field: 'testing' } }, session: session,
4452
+ write_concern: {w: 2}, bypass_document_validation: true, :return_document => :after,
4453
+ upsert: true)
4454
+ end.command
4455
+ end
4456
+
4457
+ it 'updates one successfully with correct options sent to server' do
4458
+ expect(events.length).to eq(1)
4459
+ expect(command[:writeConcern]).to_not be_nil
4460
+ expect(command[:writeConcern][:w]).to eq(2)
4461
+ expect(collection.options[:write_concern]).to eq(w:1)
4462
+ expect(command[:bypassDocumentValidation]).to be(true)
4463
+ expect(command[:updates][0][:upsert]).to be(true)
4464
+ end
4465
+ end
4017
4466
  end
4018
4467
 
4019
4468
  describe '#find_one_and_delete' do
@@ -4231,6 +4680,46 @@ describe Mongo::Collection do
4231
4680
  expect(result).to be_nil
4232
4681
  end
4233
4682
  end
4683
+
4684
+ context 'when various options passed in' do
4685
+ # w: 2 requires a replica set
4686
+ require_topology :replica_set
4687
+
4688
+ # https://jira.mongodb.org/browse/RUBY-2306
4689
+ min_server_fcv '3.6'
4690
+
4691
+ before do
4692
+ authorized_collection.delete_many
4693
+ authorized_collection.insert_many([{ name: 'test1' }, { name: 'test2' }])
4694
+ end
4695
+
4696
+ let(:collection) do
4697
+ authorized_collection.with(write_concern: {w: 2})
4698
+ end
4699
+
4700
+ let(:session) do
4701
+ authorized_client.start_session
4702
+ end
4703
+
4704
+ let!(:command) do
4705
+ Utils.get_command_event(authorized_client, 'findAndModify') do |client|
4706
+ collection.find_one_and_delete(selector, session: session, write_concern: {w: 2},
4707
+ bypass_document_validation: true, max_time_ms: 300)
4708
+ end.command
4709
+ end
4710
+
4711
+ let(:events) do
4712
+ subscriber.command_started_events('findAndModify')
4713
+ end
4714
+
4715
+ it 'finds and deletes successfully with correct options sent to server' do
4716
+ expect(events.length).to eq(1)
4717
+ expect(command[:writeConcern]).to_not be_nil
4718
+ expect(command[:writeConcern][:w]).to eq(2)
4719
+ expect(command[:bypassDocumentValidation]).to eq(true)
4720
+ expect(command[:maxTimeMS]).to eq(300)
4721
+ end
4722
+ end
4234
4723
  end
4235
4724
 
4236
4725
  describe '#find_one_and_update' do
@@ -4652,6 +5141,51 @@ describe Mongo::Collection do
4652
5141
  end
4653
5142
  end
4654
5143
  end
5144
+
5145
+ context 'when various options passed in' do
5146
+ # w: 2 requires a replica set
5147
+ require_topology :replica_set
5148
+
5149
+ # https://jira.mongodb.org/browse/RUBY-2306
5150
+ min_server_fcv '3.6'
5151
+
5152
+ let(:session) do
5153
+ authorized_client.start_session
5154
+ end
5155
+
5156
+ let(:events) do
5157
+ subscriber.command_started_events('findAndModify')
5158
+ end
5159
+
5160
+ let(:collection) do
5161
+ authorized_collection.with(write_concern: {w: 2})
5162
+ end
5163
+
5164
+ let(:selector) do
5165
+ {field: 'test1'}
5166
+ end
5167
+
5168
+ before do
5169
+ collection.insert_one({field: 'test1'}, session: session)
5170
+ end
5171
+
5172
+ let!(:command) do
5173
+ Utils.get_command_event(authorized_client, 'findAndModify') do |client|
5174
+ collection.find_one_and_update(selector, { '$set' => {field: 'testing'}},
5175
+ :return_document => :after, write_concern: {w: 1}, upsert: true,
5176
+ bypass_document_validation: true, max_time_ms: 100, session: session)
5177
+ end.command
5178
+ end
5179
+
5180
+ it 'find and updates successfully with correct options sent to server' do
5181
+ expect(events.length).to eq(1)
5182
+ expect(command[:writeConcern]).to_not be_nil
5183
+ expect(command[:writeConcern][:w]).to eq(1)
5184
+ expect(command[:upsert]).to eq(true)
5185
+ expect(command[:bypassDocumentValidation]).to be(true)
5186
+ expect(command[:maxTimeMS]).to eq(100)
5187
+ end
5188
+ end
4655
5189
  end
4656
5190
 
4657
5191
  describe '#find_one_and_replace' do
@@ -4965,6 +5499,44 @@ describe Mongo::Collection do
4965
5499
  expect(result).to be_nil
4966
5500
  end
4967
5501
  end
5502
+
5503
+ context 'when various options passed in' do
5504
+ # https://jira.mongodb.org/browse/RUBY-2306
5505
+ min_server_fcv '3.6'
5506
+
5507
+ before do
5508
+ authorized_collection.insert_one({field: 'test1'})
5509
+ end
5510
+
5511
+ let(:session) do
5512
+ authorized_client.start_session
5513
+ end
5514
+
5515
+ let(:events) do
5516
+ subscriber.command_started_events('findAndModify')
5517
+ end
5518
+
5519
+ let(:collection) do
5520
+ authorized_collection.with(write_concern: { w: 2 })
5521
+ end
5522
+
5523
+ let!(:command) do
5524
+ Utils.get_command_event(authorized_client, 'findAndModify') do |client|
5525
+ collection.find_one_and_replace(selector, { '$set' => {field: 'test5'}},
5526
+ :return_document => :after, write_concern: {w: 1}, session: session,
5527
+ upsert: true, bypass_document_validation: false, max_time_ms: 200)
5528
+ end.command
5529
+ end
5530
+
5531
+ it 'find and replaces successfully with correct options sent to server' do
5532
+ expect(events.length).to eq(1)
5533
+ expect(command[:writeConcern]).to_not be_nil
5534
+ expect(command[:writeConcern][:w]).to eq(1)
5535
+ expect(command[:upsert]).to be(true)
5536
+ expect(command[:bypassDocumentValidation]).to be_nil
5537
+ expect(command[:maxTimeMS]).to eq(200)
5538
+ end
5539
+ end
4968
5540
  end
4969
5541
 
4970
5542
  describe '#watch' do