mongo 2.14.1 → 2.15.0.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +4 -1
  4. data/Rakefile +8 -15
  5. data/lib/mongo/auth/aws/conversation.rb +1 -4
  6. data/lib/mongo/auth/base.rb +13 -7
  7. data/lib/mongo/auth/conversation_base.rb +32 -0
  8. data/lib/mongo/auth/cr/conversation.rb +6 -29
  9. data/lib/mongo/auth/gssapi/conversation.rb +4 -15
  10. data/lib/mongo/auth/ldap/conversation.rb +3 -14
  11. data/lib/mongo/auth/sasl_conversation_base.rb +1 -13
  12. data/lib/mongo/auth/scram_conversation_base.rb +7 -34
  13. data/lib/mongo/auth/user/view.rb +16 -9
  14. data/lib/mongo/auth/x509/conversation.rb +4 -25
  15. data/lib/mongo/bulk_write.rb +21 -18
  16. data/lib/mongo/client.rb +82 -6
  17. data/lib/mongo/cluster/reapers/cursor_reaper.rb +6 -2
  18. data/lib/mongo/cluster.rb +19 -2
  19. data/lib/mongo/collection/view/aggregation.rb +1 -1
  20. data/lib/mongo/collection/view/change_stream.rb +1 -1
  21. data/lib/mongo/collection/view/iterable.rb +7 -17
  22. data/lib/mongo/collection/view/map_reduce.rb +2 -2
  23. data/lib/mongo/collection/view/readable.rb +42 -20
  24. data/lib/mongo/collection/view/writable.rb +14 -14
  25. data/lib/mongo/collection.rb +6 -6
  26. data/lib/mongo/cursor.rb +2 -12
  27. data/lib/mongo/database/view.rb +1 -1
  28. data/lib/mongo/database.rb +8 -3
  29. data/lib/mongo/error/bulk_write_error.rb +17 -3
  30. data/lib/mongo/error/internal_driver_error.rb +22 -0
  31. data/lib/mongo/error/operation_failure.rb +21 -2
  32. data/lib/mongo/error/parser.rb +65 -12
  33. data/lib/mongo/error/server_api_conflict.rb +23 -0
  34. data/lib/mongo/error/server_api_not_supported.rb +24 -0
  35. data/lib/mongo/error/unmet_dependency.rb +21 -0
  36. data/lib/mongo/error.rb +9 -1
  37. data/lib/mongo/index/view.rb +21 -11
  38. data/lib/mongo/monitoring/event/server_heartbeat_failed.rb +27 -16
  39. data/lib/mongo/monitoring/event/server_heartbeat_succeeded.rb +26 -15
  40. data/lib/mongo/monitoring.rb +13 -4
  41. data/lib/mongo/operation/collections_info/command.rb +2 -2
  42. data/lib/mongo/operation/collections_info.rb +18 -1
  43. data/lib/mongo/operation/context.rb +99 -0
  44. data/lib/mongo/operation/indexes.rb +15 -1
  45. data/lib/mongo/operation/insert/command.rb +2 -2
  46. data/lib/mongo/operation/insert/legacy.rb +2 -2
  47. data/lib/mongo/operation/insert/op_msg.rb +2 -2
  48. data/lib/mongo/operation/list_collections/result.rb +4 -1
  49. data/lib/mongo/operation/parallel_scan/command.rb +2 -1
  50. data/lib/mongo/operation/result.rb +2 -0
  51. data/lib/mongo/operation/shared/executable.rb +24 -14
  52. data/lib/mongo/operation/shared/executable_no_validate.rb +2 -2
  53. data/lib/mongo/operation/shared/op_msg_or_command.rb +1 -7
  54. data/lib/mongo/operation/shared/op_msg_or_find_command.rb +1 -7
  55. data/lib/mongo/operation/shared/polymorphic_operation.rb +39 -0
  56. data/lib/mongo/operation/shared/read_preference_supported.rb +36 -38
  57. data/lib/mongo/operation/shared/response_handling.rb +23 -23
  58. data/lib/mongo/operation/shared/sessions_supported.rb +15 -5
  59. data/lib/mongo/operation/shared/write.rb +8 -18
  60. data/lib/mongo/operation.rb +2 -2
  61. data/lib/mongo/protocol/compressed.rb +51 -5
  62. data/lib/mongo/protocol/message.rb +20 -2
  63. data/lib/mongo/protocol/msg.rb +38 -13
  64. data/lib/mongo/protocol/query.rb +11 -11
  65. data/lib/mongo/query_cache.rb +30 -0
  66. data/lib/mongo/retryable.rb +1 -1
  67. data/lib/mongo/server/app_metadata.rb +52 -18
  68. data/lib/mongo/server/connection.rb +5 -0
  69. data/lib/mongo/server/connection_base.rb +13 -10
  70. data/lib/mongo/server/connection_pool.rb +6 -2
  71. data/lib/mongo/server/description/features.rb +9 -8
  72. data/lib/mongo/server/description.rb +4 -0
  73. data/lib/mongo/server/monitor/app_metadata.rb +1 -1
  74. data/lib/mongo/server/monitor/connection.rb +9 -10
  75. data/lib/mongo/server/monitor.rb +20 -1
  76. data/lib/mongo/server/pending_connection.rb +24 -6
  77. data/lib/mongo/server/push_monitor.rb +11 -1
  78. data/lib/mongo/server.rb +7 -1
  79. data/lib/mongo/server_selector/secondary_preferred.rb +7 -2
  80. data/lib/mongo/session/session_pool.rb +4 -2
  81. data/lib/mongo/session.rb +2 -2
  82. data/lib/mongo/socket/ssl.rb +8 -0
  83. data/lib/mongo/socket.rb +29 -4
  84. data/lib/mongo/uri/options_mapper.rb +38 -0
  85. data/lib/mongo/utils.rb +15 -0
  86. data/lib/mongo/version.rb +1 -1
  87. data/lib/mongo.rb +23 -0
  88. data/spec/README.md +24 -1
  89. data/spec/integration/auth_spec.rb +25 -15
  90. data/spec/integration/bulk_write_error_message_spec.rb +41 -0
  91. data/spec/integration/change_stream_spec.rb +4 -4
  92. data/spec/integration/command_monitoring_spec.rb +2 -2
  93. data/spec/integration/connection_spec.rb +2 -0
  94. data/spec/integration/docs_examples_spec.rb +8 -1
  95. data/spec/integration/fork_reconnect_spec.rb +4 -1
  96. data/spec/integration/ocsp_verifier_spec.rb +13 -7
  97. data/spec/integration/operation_failure_code_spec.rb +1 -1
  98. data/spec/integration/operation_failure_message_spec.rb +90 -0
  99. data/spec/integration/query_cache_spec.rb +0 -45
  100. data/spec/integration/reconnect_spec.rb +1 -1
  101. data/spec/integration/snappy_compression_spec.rb +25 -0
  102. data/spec/integration/srv_monitoring_spec.rb +1 -1
  103. data/spec/integration/transactions_examples_spec.rb +6 -0
  104. data/spec/integration/zlib_compression_spec.rb +1 -1
  105. data/spec/integration/zstd_compression_spec.rb +26 -0
  106. data/spec/lite_spec_helper.rb +7 -1
  107. data/spec/mongo/address_spec.rb +15 -11
  108. data/spec/mongo/auth/ldap/conversation_spec.rb +1 -1
  109. data/spec/mongo/auth/ldap_spec.rb +5 -1
  110. data/spec/mongo/auth/scram_negotiation_spec.rb +1 -1
  111. data/spec/mongo/auth/scram_spec.rb +1 -1
  112. data/spec/mongo/auth/x509/conversation_spec.rb +3 -3
  113. data/spec/mongo/client_construction_spec.rb +207 -33
  114. data/spec/mongo/client_spec.rb +17 -0
  115. data/spec/mongo/cluster_spec.rb +1 -0
  116. data/spec/mongo/collection/view/explainable_spec.rb +1 -1
  117. data/spec/mongo/collection/view/readable_spec.rb +33 -19
  118. data/spec/mongo/collection_crud_spec.rb +4357 -0
  119. data/spec/mongo/collection_ddl_spec.rb +534 -0
  120. data/spec/mongo/collection_spec.rb +5 -4859
  121. data/spec/mongo/database_spec.rb +66 -4
  122. data/spec/mongo/error/bulk_write_error_spec.rb +3 -3
  123. data/spec/mongo/error/parser_spec.rb +37 -6
  124. data/spec/mongo/index/view_spec.rb +4 -0
  125. data/spec/mongo/monitoring/event/server_heartbeat_failed_spec.rb +1 -1
  126. data/spec/mongo/monitoring/event/server_heartbeat_succeeded_spec.rb +1 -1
  127. data/spec/mongo/operation/aggregate_spec.rb +2 -1
  128. data/spec/mongo/operation/collections_info_spec.rb +4 -1
  129. data/spec/mongo/operation/command_spec.rb +6 -3
  130. data/spec/mongo/operation/create_index_spec.rb +6 -3
  131. data/spec/mongo/operation/create_user_spec.rb +6 -3
  132. data/spec/mongo/operation/delete/bulk_spec.rb +9 -6
  133. data/spec/mongo/operation/delete_spec.rb +11 -7
  134. data/spec/mongo/operation/drop_index_spec.rb +6 -2
  135. data/spec/mongo/operation/find/legacy_spec.rb +3 -1
  136. data/spec/mongo/operation/get_more_spec.rb +3 -1
  137. data/spec/mongo/operation/indexes_spec.rb +5 -1
  138. data/spec/mongo/operation/insert/bulk_spec.rb +10 -7
  139. data/spec/mongo/operation/insert_spec.rb +15 -12
  140. data/spec/mongo/operation/map_reduce_spec.rb +5 -2
  141. data/spec/mongo/operation/read_preference_legacy_spec.rb +19 -9
  142. data/spec/mongo/operation/read_preference_op_msg_spec.rb +3 -3
  143. data/spec/mongo/operation/remove_user_spec.rb +6 -3
  144. data/spec/mongo/operation/result_spec.rb +1 -1
  145. data/spec/mongo/operation/update/bulk_spec.rb +9 -6
  146. data/spec/mongo/operation/update_spec.rb +10 -7
  147. data/spec/mongo/operation/update_user_spec.rb +4 -1
  148. data/spec/mongo/protocol/compressed_spec.rb +26 -12
  149. data/spec/mongo/query_cache_middleware_spec.rb +55 -0
  150. data/spec/mongo/retryable_spec.rb +3 -2
  151. data/spec/mongo/server/app_metadata_shared.rb +7 -33
  152. data/spec/mongo/server/app_metadata_spec.rb +2 -0
  153. data/spec/mongo/server/connection_pool/populator_spec.rb +3 -1
  154. data/spec/mongo/server/connection_pool_spec.rb +1 -1
  155. data/spec/mongo/server/connection_spec.rb +24 -17
  156. data/spec/mongo/server/monitor/connection_spec.rb +17 -7
  157. data/spec/mongo/server/monitor_spec.rb +9 -1
  158. data/spec/mongo/server_selector/secondary_preferred_spec.rb +6 -6
  159. data/spec/mongo/server_spec.rb +15 -2
  160. data/spec/mongo/socket/ssl_spec.rb +40 -0
  161. data/spec/mongo/socket_spec.rb +2 -2
  162. data/spec/mongo/tls_context_hooks_spec.rb +37 -0
  163. data/spec/runners/connection_string.rb +0 -4
  164. data/spec/runners/crud/requirement.rb +40 -3
  165. data/spec/runners/crud/verifier.rb +8 -0
  166. data/spec/runners/transactions/operation.rb +1 -1
  167. data/spec/runners/transactions/test.rb +1 -0
  168. data/spec/runners/unified/assertions.rb +249 -0
  169. data/spec/runners/unified/change_stream_operations.rb +26 -0
  170. data/spec/runners/unified/crud_operations.rb +199 -0
  171. data/spec/runners/unified/ddl_operations.rb +96 -0
  172. data/spec/runners/unified/entity_map.rb +39 -0
  173. data/spec/runners/unified/error.rb +25 -0
  174. data/spec/runners/unified/event_subscriber.rb +91 -0
  175. data/spec/runners/unified/exceptions.rb +21 -0
  176. data/spec/runners/unified/grid_fs_operations.rb +55 -0
  177. data/spec/runners/unified/support_operations.rb +250 -0
  178. data/spec/runners/unified/test.rb +393 -0
  179. data/spec/runners/unified/test_group.rb +28 -0
  180. data/spec/runners/unified/using_hash.rb +31 -0
  181. data/spec/runners/unified.rb +96 -0
  182. data/spec/shared/lib/mrss/cluster_config.rb +0 -3
  183. data/spec/shared/lib/mrss/docker_runner.rb +0 -3
  184. data/spec/shared/lib/mrss/lite_constraints.rb +0 -16
  185. data/spec/shared/lib/mrss/server_version_registry.rb +0 -3
  186. data/spec/shared/lib/mrss/spec_organizer.rb +0 -3
  187. data/spec/shared/shlib/server.sh +1 -1
  188. data/spec/spec_helper.rb +4 -1
  189. data/spec/spec_tests/crud_unified_spec.rb +10 -0
  190. data/spec/spec_tests/data/change_streams/change-streams.yml +0 -1
  191. data/spec/spec_tests/data/crud_unified/estimatedDocumentCount.yml +267 -0
  192. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-4.9.yml +60 -0
  193. data/spec/spec_tests/data/retryable_reads/{estimatedDocumentCount.yml → estimatedDocumentCount-pre4.9.yml} +2 -0
  194. data/spec/spec_tests/data/retryable_reads/estimatedDocumentCount-serverErrors-4.9.yml +146 -0
  195. data/spec/spec_tests/data/retryable_reads/{estimatedDocumentCount-serverErrors.yml → estimatedDocumentCount-serverErrors-pre4.9.yml} +2 -0
  196. data/spec/spec_tests/data/retryable_reads/listIndexNames.yml +1 -1
  197. data/spec/spec_tests/data/unified/valid-fail/operation-failure.yml +31 -0
  198. data/spec/spec_tests/data/unified/valid-pass/poc-change-streams.yml +220 -0
  199. data/spec/spec_tests/data/unified/valid-pass/poc-command-monitoring.yml +102 -0
  200. data/spec/spec_tests/data/unified/valid-pass/poc-crud.yml +184 -0
  201. data/spec/spec_tests/data/unified/valid-pass/poc-gridfs.yml +155 -0
  202. data/spec/spec_tests/data/unified/valid-pass/poc-retryable-reads.yml +193 -0
  203. data/spec/spec_tests/data/unified/valid-pass/poc-retryable-writes.yml +210 -0
  204. data/spec/spec_tests/data/unified/valid-pass/poc-sessions.yml +215 -0
  205. data/spec/spec_tests/data/unified/valid-pass/poc-transactions-convenient-api.yml +235 -0
  206. data/spec/spec_tests/data/unified/valid-pass/poc-transactions-mongos-pin-auto.yml +169 -0
  207. data/spec/spec_tests/data/unified/valid-pass/poc-transactions.yml +170 -0
  208. data/spec/spec_tests/data/uri_options/compression-options.yml +1 -1
  209. data/spec/spec_tests/data/versioned_api/crud-api-version-1-strict.yml +416 -0
  210. data/spec/spec_tests/data/versioned_api/crud-api-version-1.yml +409 -0
  211. data/spec/spec_tests/data/versioned_api/runcommand-helper-no-api-version-declared.yml +67 -0
  212. data/spec/spec_tests/data/versioned_api/test-commands-deprecation-errors.yml +47 -0
  213. data/spec/spec_tests/data/versioned_api/test-commands-strict-mode.yml +44 -0
  214. data/spec/spec_tests/data/versioned_api/transaction-handling.yml +180 -0
  215. data/spec/spec_tests/unified_spec.rb +15 -0
  216. data/spec/spec_tests/uri_options_spec.rb +16 -0
  217. data/spec/spec_tests/versioned_api_spec.rb +10 -0
  218. data/spec/support/client_registry.rb +4 -8
  219. data/spec/support/client_registry_macros.rb +4 -4
  220. data/spec/support/common_shortcuts.rb +15 -1
  221. data/spec/support/shared/session.rb +2 -2
  222. data/spec/support/spec_config.rb +42 -11
  223. data/spec/support/utils.rb +64 -3
  224. data.tar.gz.sig +0 -0
  225. metadata +1005 -915
  226. metadata.gz.sig +0 -0
  227. data/lib/mongo/operation/shared/collections_info_or_list_collections.rb +0 -58
  228. data/lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb +0 -47
  229. data/spec/integration/secondary_reads_spec.rb +0 -102
  230. data/spec/support/cluster_config.rb +0 -207
@@ -0,0 +1,534 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongo::Collection do
4
+
5
+ let(:subscriber) { EventSubscriber.new }
6
+
7
+ let(:client) do
8
+ authorized_client.tap do |client|
9
+ client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
10
+ end
11
+ end
12
+
13
+ let(:authorized_collection) { client['collection_spec'] }
14
+
15
+ before do
16
+ authorized_client['collection_spec'].drop
17
+ end
18
+
19
+ describe '#create' do
20
+ before do
21
+ authorized_client[:specs].drop
22
+ end
23
+
24
+ let(:database) do
25
+ authorized_client.database
26
+ end
27
+
28
+ context 'when the collection has no options' do
29
+
30
+ let(:collection) do
31
+ described_class.new(database, :specs)
32
+ end
33
+
34
+ let!(:response) do
35
+ collection.create
36
+ end
37
+
38
+ it 'executes the command' do
39
+ expect(response).to be_successful
40
+ end
41
+
42
+ it 'creates the collection in the database' do
43
+ expect(database.collection_names).to include('specs')
44
+ end
45
+ end
46
+
47
+ context 'when the collection has options' do
48
+
49
+ context 'when the collection is capped' do
50
+
51
+ shared_examples 'a capped collection command' do
52
+
53
+ let!(:response) do
54
+ collection.create
55
+ end
56
+
57
+ let(:options) do
58
+ { :capped => true, :size => 1024 }
59
+ end
60
+
61
+ it 'executes the command' do
62
+ expect(response).to be_successful
63
+ end
64
+
65
+ it 'sets the collection as capped' do
66
+ expect(collection).to be_capped
67
+ end
68
+
69
+ it 'creates the collection in the database' do
70
+ expect(database.collection_names).to include('specs')
71
+ end
72
+ end
73
+
74
+ shared_examples 'a validated collection command' do
75
+
76
+ let!(:response) do
77
+ collection.create
78
+ end
79
+
80
+ let(:options) do
81
+ { :validator => { fieldName: { '$gte' => 1024 } },
82
+ :validationLevel => 'strict' }
83
+ end
84
+
85
+ let(:collection_info) do
86
+ database.list_collections.find { |i| i['name'] == 'specs' }
87
+ end
88
+
89
+ it 'executes the command' do
90
+ expect(response).to be_successful
91
+ end
92
+
93
+ it 'sets the collection with validators' do
94
+ expect(collection_info['options']['validator']).to eq({ 'fieldName' => { '$gte' => 1024 } })
95
+ end
96
+
97
+ it 'creates the collection in the database' do
98
+ expect(database.collection_names).to include('specs')
99
+ end
100
+ end
101
+
102
+ context 'when instantiating a collection directly' do
103
+
104
+ let(:collection) do
105
+ described_class.new(database, :specs, options)
106
+ end
107
+
108
+ it_behaves_like 'a capped collection command'
109
+
110
+ context 'when validators can be set' do
111
+ min_server_fcv '3.2'
112
+ it_behaves_like 'a validated collection command'
113
+ end
114
+ end
115
+
116
+ context 'when instantiating a collection through the database' do
117
+
118
+ let(:collection) do
119
+ authorized_client[:specs, options]
120
+ end
121
+
122
+ it_behaves_like 'a capped collection command'
123
+
124
+ context 'when validators can be set' do
125
+ min_server_fcv '3.2'
126
+ it_behaves_like 'a validated collection command'
127
+ end
128
+ end
129
+ end
130
+
131
+ context 'when the collection has a write concern' do
132
+
133
+ before do
134
+ database[:specs].drop
135
+ end
136
+
137
+ let(:options) do
138
+ {
139
+ write: INVALID_WRITE_CONCERN
140
+ }
141
+ end
142
+
143
+ let(:collection) do
144
+ described_class.new(database, :specs, options)
145
+ end
146
+
147
+ context 'when the server supports write concern on the create command' do
148
+ min_server_fcv '3.4'
149
+ require_topology :replica_set
150
+
151
+ it 'applies the write concern' do
152
+ expect{
153
+ collection.create
154
+ }.to raise_exception(Mongo::Error::OperationFailure)
155
+ end
156
+ end
157
+
158
+ context 'when write concern passed in as an option' do
159
+ min_server_fcv '3.4'
160
+ require_topology :replica_set
161
+
162
+ before do
163
+ database['collection_spec'].drop
164
+ end
165
+
166
+ let(:events) do
167
+ subscriber.command_started_events('create')
168
+ end
169
+
170
+ let(:options) do
171
+ { write_concern: {w: 1} }
172
+ end
173
+
174
+ let!(:collection) do
175
+ authorized_collection.with(options)
176
+ end
177
+
178
+ let!(:command) do
179
+ Utils.get_command_event(authorized_client, 'create') do |client|
180
+ collection.create({ write_concern: {w: 2} })
181
+ end.command
182
+ end
183
+
184
+ it 'applies the write concern passed in as an option' do
185
+ expect(events.length).to eq(1)
186
+ expect(command[:writeConcern][:w]).to eq(2)
187
+ end
188
+ end
189
+
190
+ context 'when the server does not support write concern on the create command' do
191
+ max_server_version '3.2'
192
+
193
+ it 'does not apply the write concern' do
194
+ expect(collection.create).to be_successful
195
+ end
196
+ end
197
+ end
198
+
199
+ context 'when the collection has a collation' do
200
+
201
+ shared_examples 'a collection command with a collation option' do
202
+
203
+ let(:response) do
204
+ collection.create
205
+ end
206
+
207
+ let(:options) do
208
+ { :collation => { locale: 'fr' } }
209
+ end
210
+
211
+ let(:collection_info) do
212
+ database.list_collections.find { |i| i['name'] == 'specs' }
213
+ end
214
+
215
+ before do
216
+ collection.drop
217
+ end
218
+
219
+ context 'when the server supports collations' do
220
+ min_server_fcv '3.4'
221
+
222
+ it 'executes the command' do
223
+ expect(response).to be_successful
224
+ end
225
+
226
+ it 'sets the collection with a collation' do
227
+ response
228
+ expect(collection_info['options']['collation']['locale']).to eq('fr')
229
+ end
230
+
231
+ it 'creates the collection in the database' do
232
+ response
233
+ expect(database.collection_names).to include('specs')
234
+ end
235
+ end
236
+
237
+ context 'when the server does not support collations' do
238
+ max_server_version '3.2'
239
+
240
+ it 'raises an error' do
241
+ expect {
242
+ response
243
+ }.to raise_exception(Mongo::Error::UnsupportedCollation)
244
+ end
245
+
246
+ context 'when a String key is used' do
247
+
248
+ let(:options) do
249
+ { 'collation' => { locale: 'fr' } }
250
+ end
251
+
252
+ it 'raises an exception' do
253
+ expect {
254
+ response
255
+ }.to raise_exception(Mongo::Error::UnsupportedCollation)
256
+ end
257
+ end
258
+ end
259
+ end
260
+
261
+ context 'when instantiating a collection directly' do
262
+
263
+ let(:collection) do
264
+ described_class.new(database, :specs, options)
265
+ end
266
+
267
+ it_behaves_like 'a collection command with a collation option'
268
+ end
269
+
270
+ context 'when instantiating a collection through the database' do
271
+
272
+ let(:collection) do
273
+ authorized_client[:specs, options]
274
+ end
275
+
276
+ it_behaves_like 'a collection command with a collation option'
277
+ end
278
+ end
279
+
280
+ context 'when a session is provided' do
281
+
282
+ let(:collection) do
283
+ authorized_client[:specs]
284
+ end
285
+
286
+ let(:operation) do
287
+ collection.create(session: session)
288
+ end
289
+
290
+ let(:session) do
291
+ authorized_client.start_session
292
+ end
293
+
294
+ let(:client) do
295
+ authorized_client
296
+ end
297
+
298
+ let(:failed_operation) do
299
+ authorized_client[:specs, invalid: true].create(session: session)
300
+ end
301
+
302
+ before do
303
+ collection.drop
304
+ end
305
+
306
+ it_behaves_like 'an operation using a session'
307
+ it_behaves_like 'a failed operation using a session'
308
+ end
309
+ end
310
+
311
+ context 'when collation has a strength' do
312
+ min_server_fcv '3.4'
313
+
314
+ let(:band_collection) do
315
+ described_class.new(database, :bands)
316
+ end
317
+
318
+ before do
319
+ band_collection.delete_many
320
+ band_collection.insert_many([{ name: "Depeche Mode" }, { name: "New Order" }])
321
+ end
322
+
323
+ let(:options) do
324
+ { collation: { locale: 'en_US', strength: 2 } }
325
+ end
326
+ let(:band_result) do
327
+ band_collection.find({ name: 'DEPECHE MODE' }, options)
328
+ end
329
+
330
+ it 'finds Capitalize from UPPER CASE' do
331
+ expect(band_result.count_documents).to eq(1)
332
+ end
333
+ end
334
+ end
335
+
336
+ describe '#drop' do
337
+
338
+ let(:database) do
339
+ authorized_client.database
340
+ end
341
+
342
+ let(:collection) do
343
+ described_class.new(database, :specs)
344
+ end
345
+
346
+ context 'when the collection exists' do
347
+
348
+ before do
349
+ authorized_client[:specs].drop
350
+ collection.create
351
+ # wait for the collection to be created
352
+ sleep 0.4
353
+ end
354
+
355
+ context 'when a session is provided' do
356
+
357
+ let(:operation) do
358
+ collection.drop(session: session)
359
+ end
360
+
361
+ let(:failed_operation) do
362
+ collection.with(write: INVALID_WRITE_CONCERN).drop(session: session)
363
+ end
364
+
365
+ let(:session) do
366
+ authorized_client.start_session
367
+ end
368
+
369
+ let(:client) do
370
+ authorized_client
371
+ end
372
+
373
+ it_behaves_like 'an operation using a session'
374
+
375
+ context 'can set write concern' do
376
+ require_set_write_concern
377
+
378
+ it_behaves_like 'a failed operation using a session'
379
+ end
380
+ end
381
+
382
+ context 'when the collection does not have a write concern set' do
383
+
384
+ let!(:response) do
385
+ collection.drop
386
+ end
387
+
388
+ it 'executes the command' do
389
+ expect(response).to be_successful
390
+ end
391
+
392
+ it 'drops the collection from the database' do
393
+ expect(database.collection_names).to_not include('specs')
394
+ end
395
+
396
+ context 'when the collection does not exist' do
397
+ require_set_write_concern
398
+
399
+ it 'does not raise an error' do
400
+ expect(database['non-existent-coll'].drop).to be(false)
401
+ end
402
+ end
403
+ end
404
+
405
+ context 'when the collection has a write concern' do
406
+
407
+ let(:write_options) do
408
+ {
409
+ write: INVALID_WRITE_CONCERN
410
+ }
411
+ end
412
+
413
+ let(:collection_with_write_options) do
414
+ collection.with(write_options)
415
+ end
416
+
417
+ context 'when the server supports write concern on the drop command' do
418
+ min_server_fcv '3.4'
419
+ require_set_write_concern
420
+
421
+ it 'applies the write concern' do
422
+ expect{
423
+ collection_with_write_options.drop
424
+ }.to raise_exception(Mongo::Error::OperationFailure)
425
+ end
426
+ end
427
+
428
+ context 'when write concern passed in as an option' do
429
+ min_server_fcv '3.4'
430
+ require_set_write_concern
431
+
432
+ let(:events) do
433
+ subscriber.command_started_events('drop')
434
+ end
435
+
436
+ let(:options) do
437
+ { write_concern: {w: 1} }
438
+ end
439
+
440
+ let!(:collection) do
441
+ authorized_collection.with(options)
442
+ end
443
+
444
+ let!(:command) do
445
+ Utils.get_command_event(authorized_client, 'drop') do |client|
446
+ collection.drop({ write_concern: {w: 0} })
447
+ end.command
448
+ end
449
+
450
+ it 'applies the write concern passed in as an option' do
451
+ expect(events.length).to eq(1)
452
+ expect(command[:writeConcern][:w]).to eq(0)
453
+ end
454
+ end
455
+
456
+ context 'when the server does not support write concern on the drop command' do
457
+ max_server_version '3.2'
458
+
459
+ it 'does not apply the write concern' do
460
+ expect(collection_with_write_options.drop).to be_successful
461
+ end
462
+ end
463
+ end
464
+ end
465
+
466
+ context 'when the collection does not exist' do
467
+ require_set_write_concern
468
+
469
+ before do
470
+ begin
471
+ collection.drop
472
+ rescue Mongo::Error::OperationFailure
473
+ end
474
+ end
475
+
476
+ it 'returns false' do
477
+ expect(collection.drop).to be(false)
478
+ end
479
+ end
480
+ end
481
+
482
+ describe '#indexes' do
483
+
484
+ let(:index_spec) do
485
+ { name: 1 }
486
+ end
487
+
488
+ let(:batch_size) { nil }
489
+
490
+ let(:index_names) do
491
+ authorized_collection.indexes(batch_size: batch_size).collect { |i| i['name'] }
492
+ end
493
+
494
+ before do
495
+ authorized_collection.indexes.create_one(index_spec, unique: true)
496
+ end
497
+
498
+ it 'returns a list of indexes' do
499
+ expect(index_names).to include(*'name_1', '_id_')
500
+ end
501
+
502
+ context 'when a session is provided' do
503
+ require_wired_tiger
504
+
505
+ let(:session) do
506
+ authorized_client.start_session
507
+ end
508
+
509
+ let(:operation) do
510
+ authorized_collection.indexes(batch_size: batch_size, session: session).collect { |i| i['name'] }
511
+ end
512
+
513
+ let(:failed_operation) do
514
+ authorized_collection.indexes(batch_size: -100, session: session).collect { |i| i['name'] }
515
+ end
516
+
517
+ let(:client) do
518
+ authorized_client
519
+ end
520
+
521
+ it_behaves_like 'an operation using a session'
522
+ it_behaves_like 'a failed operation using a session'
523
+ end
524
+
525
+ context 'when batch size is specified' do
526
+
527
+ let(:batch_size) { 1 }
528
+
529
+ it 'returns a list of indexes' do
530
+ expect(index_names).to include(*'name_1', '_id_')
531
+ end
532
+ end
533
+ end
534
+ end