mongo 1.3.0 → 1.12.5

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 (185) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/{LICENSE.txt → LICENSE} +1 -1
  4. data/README.md +122 -271
  5. data/Rakefile +25 -209
  6. data/VERSION +1 -0
  7. data/bin/mongo_console +31 -9
  8. data/lib/mongo/bulk_write_collection_view.rb +387 -0
  9. data/lib/mongo/collection.rb +576 -269
  10. data/lib/mongo/collection_writer.rb +364 -0
  11. data/lib/mongo/connection/node.rb +249 -0
  12. data/lib/mongo/connection/pool.rb +340 -0
  13. data/lib/mongo/connection/pool_manager.rb +320 -0
  14. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  15. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  16. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  17. data/lib/mongo/connection/socket/tcp_socket.rb +87 -0
  18. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection.rb +7 -875
  21. data/lib/mongo/cursor.rb +403 -117
  22. data/lib/mongo/db.rb +444 -243
  23. data/lib/mongo/exception.rb +145 -0
  24. data/lib/mongo/functional/authentication.rb +455 -0
  25. data/lib/mongo/functional/logging.rb +85 -0
  26. data/lib/mongo/functional/read_preference.rb +183 -0
  27. data/lib/mongo/functional/scram.rb +556 -0
  28. data/lib/mongo/functional/uri_parser.rb +409 -0
  29. data/lib/mongo/functional/write_concern.rb +66 -0
  30. data/lib/mongo/functional.rb +20 -0
  31. data/lib/mongo/gridfs/grid.rb +30 -24
  32. data/lib/mongo/gridfs/grid_ext.rb +6 -10
  33. data/lib/mongo/gridfs/grid_file_system.rb +38 -20
  34. data/lib/mongo/gridfs/grid_io.rb +84 -75
  35. data/lib/mongo/gridfs.rb +18 -0
  36. data/lib/mongo/legacy.rb +140 -0
  37. data/lib/mongo/mongo_client.rb +697 -0
  38. data/lib/mongo/mongo_replica_set_client.rb +535 -0
  39. data/lib/mongo/mongo_sharded_client.rb +159 -0
  40. data/lib/mongo/networking.rb +372 -0
  41. data/lib/mongo/{util → utils}/conversions.rb +29 -8
  42. data/lib/mongo/{util → utils}/core_ext.rb +28 -18
  43. data/lib/mongo/{util → utils}/server_version.rb +4 -6
  44. data/lib/mongo/{util → utils}/support.rb +29 -31
  45. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  46. data/lib/mongo/utils.rb +19 -0
  47. data/lib/mongo.rb +51 -50
  48. data/mongo.gemspec +29 -32
  49. data/test/functional/authentication_test.rb +39 -0
  50. data/test/functional/bulk_api_stress_test.rb +133 -0
  51. data/test/functional/bulk_write_collection_view_test.rb +1198 -0
  52. data/test/functional/client_test.rb +627 -0
  53. data/test/functional/collection_test.rb +2175 -0
  54. data/test/functional/collection_writer_test.rb +83 -0
  55. data/test/{conversions_test.rb → functional/conversions_test.rb} +47 -3
  56. data/test/functional/cursor_fail_test.rb +57 -0
  57. data/test/functional/cursor_message_test.rb +56 -0
  58. data/test/functional/cursor_test.rb +683 -0
  59. data/test/functional/db_api_test.rb +835 -0
  60. data/test/functional/db_connection_test.rb +25 -0
  61. data/test/functional/db_test.rb +348 -0
  62. data/test/functional/grid_file_system_test.rb +285 -0
  63. data/test/{grid_io_test.rb → functional/grid_io_test.rb} +72 -11
  64. data/test/{grid_test.rb → functional/grid_test.rb} +88 -15
  65. data/test/functional/pool_test.rb +136 -0
  66. data/test/functional/safe_test.rb +98 -0
  67. data/test/functional/ssl_test.rb +29 -0
  68. data/test/functional/support_test.rb +62 -0
  69. data/test/functional/timeout_test.rb +60 -0
  70. data/test/functional/uri_test.rb +446 -0
  71. data/test/functional/write_concern_test.rb +118 -0
  72. data/test/helpers/general.rb +50 -0
  73. data/test/helpers/test_unit.rb +476 -0
  74. data/test/replica_set/authentication_test.rb +37 -0
  75. data/test/replica_set/basic_test.rb +189 -0
  76. data/test/replica_set/client_test.rb +393 -0
  77. data/test/replica_set/connection_test.rb +138 -0
  78. data/test/replica_set/count_test.rb +66 -0
  79. data/test/replica_set/cursor_test.rb +220 -0
  80. data/test/replica_set/insert_test.rb +157 -0
  81. data/test/replica_set/max_values_test.rb +151 -0
  82. data/test/replica_set/pinning_test.rb +105 -0
  83. data/test/replica_set/query_test.rb +73 -0
  84. data/test/replica_set/read_preference_test.rb +219 -0
  85. data/test/replica_set/refresh_test.rb +211 -0
  86. data/test/replica_set/replication_ack_test.rb +95 -0
  87. data/test/replica_set/ssl_test.rb +32 -0
  88. data/test/sharded_cluster/basic_test.rb +203 -0
  89. data/test/shared/authentication/basic_auth_shared.rb +260 -0
  90. data/test/shared/authentication/bulk_api_auth_shared.rb +249 -0
  91. data/test/shared/authentication/gssapi_shared.rb +176 -0
  92. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  93. data/test/shared/authentication/scram_shared.rb +92 -0
  94. data/test/shared/ssl_shared.rb +235 -0
  95. data/test/test_helper.rb +53 -94
  96. data/test/threading/basic_test.rb +120 -0
  97. data/test/tools/mongo_config.rb +708 -0
  98. data/test/tools/mongo_config_test.rb +160 -0
  99. data/test/unit/client_test.rb +381 -0
  100. data/test/unit/collection_test.rb +89 -53
  101. data/test/unit/connection_test.rb +282 -32
  102. data/test/unit/cursor_test.rb +206 -8
  103. data/test/unit/db_test.rb +55 -13
  104. data/test/unit/grid_test.rb +43 -16
  105. data/test/unit/mongo_sharded_client_test.rb +48 -0
  106. data/test/unit/node_test.rb +93 -0
  107. data/test/unit/pool_manager_test.rb +111 -0
  108. data/test/unit/read_pref_test.rb +406 -0
  109. data/test/unit/read_test.rb +159 -0
  110. data/test/unit/safe_test.rb +69 -36
  111. data/test/unit/sharding_pool_manager_test.rb +84 -0
  112. data/test/unit/write_concern_test.rb +175 -0
  113. data.tar.gz.sig +3 -0
  114. metadata +227 -216
  115. metadata.gz.sig +0 -0
  116. data/docs/CREDITS.md +0 -123
  117. data/docs/FAQ.md +0 -116
  118. data/docs/GridFS.md +0 -158
  119. data/docs/HISTORY.md +0 -244
  120. data/docs/RELEASES.md +0 -33
  121. data/docs/REPLICA_SETS.md +0 -72
  122. data/docs/TUTORIAL.md +0 -247
  123. data/docs/WRITE_CONCERN.md +0 -28
  124. data/lib/mongo/exceptions.rb +0 -71
  125. data/lib/mongo/gridfs/grid_io_fix.rb +0 -38
  126. data/lib/mongo/repl_set_connection.rb +0 -342
  127. data/lib/mongo/test.rb +0 -20
  128. data/lib/mongo/util/pool.rb +0 -177
  129. data/lib/mongo/util/uri_parser.rb +0 -185
  130. data/test/async/collection_test.rb +0 -224
  131. data/test/async/connection_test.rb +0 -24
  132. data/test/async/cursor_test.rb +0 -162
  133. data/test/async/worker_pool_test.rb +0 -99
  134. data/test/auxillary/1.4_features.rb +0 -166
  135. data/test/auxillary/authentication_test.rb +0 -68
  136. data/test/auxillary/autoreconnect_test.rb +0 -41
  137. data/test/auxillary/fork_test.rb +0 -30
  138. data/test/auxillary/repl_set_auth_test.rb +0 -58
  139. data/test/auxillary/slave_connection_test.rb +0 -36
  140. data/test/auxillary/threaded_authentication_test.rb +0 -101
  141. data/test/bson/binary_test.rb +0 -15
  142. data/test/bson/bson_test.rb +0 -649
  143. data/test/bson/byte_buffer_test.rb +0 -208
  144. data/test/bson/hash_with_indifferent_access_test.rb +0 -38
  145. data/test/bson/json_test.rb +0 -17
  146. data/test/bson/object_id_test.rb +0 -154
  147. data/test/bson/ordered_hash_test.rb +0 -204
  148. data/test/bson/timestamp_test.rb +0 -24
  149. data/test/collection_test.rb +0 -910
  150. data/test/connection_test.rb +0 -309
  151. data/test/cursor_fail_test.rb +0 -75
  152. data/test/cursor_message_test.rb +0 -43
  153. data/test/cursor_test.rb +0 -483
  154. data/test/db_api_test.rb +0 -726
  155. data/test/db_connection_test.rb +0 -15
  156. data/test/db_test.rb +0 -287
  157. data/test/grid_file_system_test.rb +0 -243
  158. data/test/load/resque/load.rb +0 -21
  159. data/test/load/resque/processor.rb +0 -26
  160. data/test/load/thin/load.rb +0 -24
  161. data/test/load/unicorn/load.rb +0 -23
  162. data/test/load/unicorn/unicorn.rb +0 -29
  163. data/test/replica_sets/connect_test.rb +0 -94
  164. data/test/replica_sets/connection_string_test.rb +0 -32
  165. data/test/replica_sets/count_test.rb +0 -35
  166. data/test/replica_sets/insert_test.rb +0 -53
  167. data/test/replica_sets/pooled_insert_test.rb +0 -55
  168. data/test/replica_sets/query_secondaries.rb +0 -96
  169. data/test/replica_sets/query_test.rb +0 -51
  170. data/test/replica_sets/replication_ack_test.rb +0 -66
  171. data/test/replica_sets/rs_test_helper.rb +0 -27
  172. data/test/safe_test.rb +0 -68
  173. data/test/support/hash_with_indifferent_access.rb +0 -186
  174. data/test/support/keys.rb +0 -45
  175. data/test/support_test.rb +0 -18
  176. data/test/threading/threading_with_large_pool_test.rb +0 -90
  177. data/test/threading_test.rb +0 -87
  178. data/test/tools/auth_repl_set_manager.rb +0 -14
  179. data/test/tools/load.rb +0 -58
  180. data/test/tools/repl_set_manager.rb +0 -266
  181. data/test/tools/sharding_manager.rb +0 -202
  182. data/test/tools/test.rb +0 -4
  183. data/test/unit/pool_test.rb +0 -9
  184. data/test/unit/repl_set_connection_test.rb +0 -59
  185. data/test/uri_test.rb +0 -91
data/test/cursor_test.rb DELETED
@@ -1,483 +0,0 @@
1
- require './test/test_helper'
2
- require 'logger'
3
-
4
- class CursorTest < Test::Unit::TestCase
5
-
6
- include Mongo
7
-
8
- @@connection = standard_connection
9
- @@db = @@connection.db(MONGO_TEST_DB)
10
- @@coll = @@db.collection('test')
11
- @@version = @@connection.server_version
12
-
13
- def setup
14
- @@coll.remove
15
- @@coll.insert('a' => 1) # collection not created until it's used
16
- @@coll_full_name = "#{MONGO_TEST_DB}.test"
17
- end
18
-
19
- def test_inspect
20
- selector = {:a => 1}
21
- cursor = @@coll.find(selector)
22
- assert_equal "<Mongo::Cursor:0x#{cursor.object_id.to_s(16)} namespace='#{@@db.name}.#{@@coll.name}' " +
23
- "@selector=#{selector.inspect}>", cursor.inspect
24
- end
25
-
26
- def test_explain
27
- cursor = @@coll.find('a' => 1)
28
- explaination = cursor.explain
29
- assert_not_nil explaination['cursor']
30
- assert_kind_of Numeric, explaination['n']
31
- assert_kind_of Numeric, explaination['millis']
32
- assert_kind_of Numeric, explaination['nscanned']
33
- end
34
-
35
- def test_count
36
- @@coll.remove
37
-
38
- assert_equal 0, @@coll.find().count()
39
-
40
- 10.times do |i|
41
- @@coll.save("x" => i)
42
- end
43
-
44
- assert_equal 10, @@coll.find().count()
45
- assert_kind_of Integer, @@coll.find().count()
46
- assert_equal 10, @@coll.find({}, :limit => 5).count()
47
- assert_equal 10, @@coll.find({}, :skip => 5).count()
48
-
49
- assert_equal 5, @@coll.find({}, :limit => 5).count(true)
50
- assert_equal 5, @@coll.find({}, :skip => 5).count(true)
51
- assert_equal 2, @@coll.find({}, :skip => 5, :limit => 2).count(true)
52
-
53
- assert_equal 1, @@coll.find({"x" => 1}).count()
54
- assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()
55
-
56
- a = @@coll.find()
57
- b = a.count()
58
- a.each do |doc|
59
- break
60
- end
61
- assert_equal b, a.count()
62
-
63
- assert_equal 0, @@db['acollectionthatdoesn'].count()
64
- end
65
-
66
- def test_sort
67
- @@coll.remove
68
- 5.times{|x| @@coll.insert({"age" => x}) }
69
-
70
- assert_kind_of Cursor, @@coll.find().sort(:age, 1)
71
-
72
- assert_equal 0, @@coll.find().sort(:age, 1).next_document["age"]
73
- assert_equal 4, @@coll.find().sort(:age, -1).next_document["age"]
74
- assert_equal 0, @@coll.find().sort([["age", :asc]]).next_document["age"]
75
-
76
- assert_kind_of Cursor, @@coll.find().sort([[:age, -1], [:b, 1]])
77
-
78
- assert_equal 4, @@coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
79
- assert_equal 0, @@coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
80
-
81
- assert_equal 4, @@coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
82
- assert_equal 0, @@coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
83
-
84
- cursor = @@coll.find()
85
- cursor.next_document
86
- assert_raise InvalidOperation do
87
- cursor.sort(["age"])
88
- end
89
-
90
- assert_raise InvalidSortValueError do
91
- @@coll.find().sort(:age, 25).next_document
92
- end
93
-
94
- assert_raise InvalidSortValueError do
95
- @@coll.find().sort(25).next_document
96
- end
97
- end
98
-
99
- def test_sort_date
100
- @@coll.remove
101
- 5.times{|x| @@coll.insert({"created_at" => Time.utc(2000 + x)}) }
102
-
103
- assert_equal 2000, @@coll.find().sort(:created_at, :asc).next_document["created_at"].year
104
- assert_equal 2004, @@coll.find().sort(:created_at, :desc).next_document["created_at"].year
105
-
106
- assert_equal 2000, @@coll.find().sort([:created_at, :asc]).next_document["created_at"].year
107
- assert_equal 2004, @@coll.find().sort([:created_at, :desc]).next_document["created_at"].year
108
-
109
- assert_equal 2000, @@coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
110
- assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
111
- end
112
-
113
- def test_sort_min_max_keys
114
- @@coll.remove
115
- @@coll.insert({"n" => 1000000})
116
- @@coll.insert({"n" => -1000000})
117
- @@coll.insert({"n" => MaxKey.new})
118
- @@coll.insert({"n" => MinKey.new})
119
-
120
- results = @@coll.find.sort([:n, :asc]).to_a
121
-
122
- assert_equal MinKey.new, results[0]['n']
123
- assert_equal(-1000000, results[1]['n'])
124
- assert_equal 1000000, results[2]['n']
125
- assert_equal MaxKey.new, results[3]['n']
126
- end
127
-
128
- def test_id_range_queries
129
- @@coll.remove
130
-
131
- t1 = Time.now
132
- t1_id = ObjectId.from_time(t1)
133
- @@coll.save({:t => 't1'})
134
- @@coll.save({:t => 't1'})
135
- @@coll.save({:t => 't1'})
136
- sleep(2)
137
- t2 = Time.now
138
- t2_id = ObjectId.from_time(t2)
139
- @@coll.save({:t => 't2'})
140
- @@coll.save({:t => 't2'})
141
- @@coll.save({:t => 't2'})
142
-
143
- assert_equal 3, @@coll.find({'_id' => {'$gt' => t1_id, '$lt' => t2_id}}).count
144
- @@coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
145
- assert_equal 't2', doc['t']
146
- end
147
- end
148
-
149
- def test_limit
150
- @@coll.remove
151
-
152
- 10.times do |i|
153
- @@coll.save("x" => i)
154
- end
155
- assert_equal 10, @@coll.find().count()
156
-
157
- results = @@coll.find().limit(5).to_a
158
- assert_equal 5, results.length
159
- end
160
-
161
- def test_timeout_options
162
- cursor = Cursor.new(@@coll)
163
- assert_equal true, cursor.timeout
164
-
165
- cursor = @@coll.find
166
- assert_equal true, cursor.timeout
167
-
168
- cursor = @@coll.find({}, :timeout => nil)
169
- assert_equal true, cursor.timeout
170
-
171
- cursor = Cursor.new(@@coll, :timeout => false)
172
- assert_equal false, cursor.timeout
173
-
174
- @@coll.find({}, :timeout => false) do |c|
175
- assert_equal false, c.timeout
176
- end
177
- end
178
-
179
- def test_timeout
180
- opts = Cursor.new(@@coll).query_opts
181
- assert_equal 0, opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
182
-
183
- opts = Cursor.new(@@coll, :timeout => false).query_opts
184
- assert_equal Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT,
185
- opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
186
- end
187
-
188
- def test_limit_exceptions
189
- cursor = @@coll.find()
190
- firstResult = cursor.next_document
191
- assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
192
- cursor.limit(1)
193
- end
194
-
195
- cursor = @@coll.find()
196
- cursor.close
197
- assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
198
- cursor.limit(1)
199
- end
200
- end
201
-
202
- def test_skip
203
- @@coll.remove
204
-
205
- 10.times do |i|
206
- @@coll.save("x" => i)
207
- end
208
- assert_equal 10, @@coll.find().count()
209
-
210
- all_results = @@coll.find().to_a
211
- skip_results = @@coll.find().skip(2).to_a
212
- assert_equal 10, all_results.length
213
- assert_equal 8, skip_results.length
214
-
215
- assert_equal all_results.slice(2...10), skip_results
216
- end
217
-
218
- def test_skip_exceptions
219
- cursor = @@coll.find()
220
- firstResult = cursor.next_document
221
- assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
222
- cursor.skip(1)
223
- end
224
-
225
- cursor = @@coll.find()
226
- cursor.close
227
- assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
228
- cursor.skip(1)
229
- end
230
- end
231
-
232
- def test_limit_skip_chaining
233
- @@coll.remove
234
- 10.times do |i|
235
- @@coll.save("x" => i)
236
- end
237
-
238
- all_results = @@coll.find().to_a
239
- limited_skip_results = @@coll.find().limit(5).skip(3).to_a
240
-
241
- assert_equal all_results.slice(3...8), limited_skip_results
242
- end
243
-
244
- def test_close_no_query_sent
245
- begin
246
- cursor = @@coll.find('a' => 1)
247
- cursor.close
248
- assert cursor.closed?
249
- rescue => ex
250
- fail ex.to_s
251
- end
252
- end
253
-
254
- def test_refill_via_get_more
255
- assert_equal 1, @@coll.count
256
- 1000.times { |i|
257
- assert_equal 1 + i, @@coll.count
258
- @@coll.insert('a' => i)
259
- }
260
-
261
- assert_equal 1001, @@coll.count
262
- count = 0
263
- @@coll.find.each { |obj|
264
- count += obj['a']
265
- }
266
- assert_equal 1001, @@coll.count
267
-
268
- # do the same thing again for debugging
269
- assert_equal 1001, @@coll.count
270
- count2 = 0
271
- @@coll.find.each { |obj|
272
- count2 += obj['a']
273
- }
274
- assert_equal 1001, @@coll.count
275
-
276
- assert_equal count, count2
277
- assert_equal 499501, count
278
- end
279
-
280
- def test_refill_via_get_more_alt_coll
281
- coll = @@db.collection('test-alt-coll')
282
- coll.remove
283
- coll.insert('a' => 1) # collection not created until it's used
284
- assert_equal 1, coll.count
285
-
286
- 1000.times { |i|
287
- assert_equal 1 + i, coll.count
288
- coll.insert('a' => i)
289
- }
290
-
291
- assert_equal 1001, coll.count
292
- count = 0
293
- coll.find.each { |obj|
294
- count += obj['a']
295
- }
296
- assert_equal 1001, coll.count
297
-
298
- # do the same thing again for debugging
299
- assert_equal 1001, coll.count
300
- count2 = 0
301
- coll.find.each { |obj|
302
- count2 += obj['a']
303
- }
304
- assert_equal 1001, coll.count
305
-
306
- assert_equal count, count2
307
- assert_equal 499501, count
308
- end
309
-
310
- def test_close_after_query_sent
311
- begin
312
- cursor = @@coll.find('a' => 1)
313
- cursor.next_document
314
- cursor.close
315
- assert cursor.closed?
316
- rescue => ex
317
- fail ex.to_s
318
- end
319
- end
320
-
321
- def test_kill_cursors
322
- @@coll.drop
323
-
324
- client_cursors = @@db.command("cursorInfo" => 1)["clientCursors_size"]
325
-
326
- 10000.times do |i|
327
- @@coll.insert("i" => i)
328
- end
329
-
330
- assert_equal(client_cursors,
331
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
332
-
333
- 10.times do |i|
334
- @@coll.find_one()
335
- end
336
-
337
- assert_equal(client_cursors,
338
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
339
-
340
- 10.times do |i|
341
- a = @@coll.find()
342
- a.next_document
343
- a.close()
344
- end
345
-
346
- assert_equal(client_cursors,
347
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
348
-
349
- a = @@coll.find()
350
- a.next_document
351
-
352
- assert_not_equal(client_cursors,
353
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
354
-
355
- a.close()
356
-
357
- assert_equal(client_cursors,
358
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
359
-
360
- a = @@coll.find({}, :limit => 10).next_document
361
-
362
- assert_equal(client_cursors,
363
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
364
-
365
- @@coll.find() do |cursor|
366
- cursor.next_document
367
- end
368
-
369
- assert_equal(client_cursors,
370
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
371
-
372
- @@coll.find() { |cursor|
373
- cursor.next_document
374
- }
375
-
376
- assert_equal(client_cursors,
377
- @@db.command("cursorInfo" => 1)["clientCursors_size"])
378
- end
379
-
380
- def test_count_with_fields
381
- @@coll.remove
382
- @@coll.save("x" => 1)
383
-
384
- if @@version < "1.1.3"
385
- assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
386
- else
387
- assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
388
- end
389
- end
390
-
391
- def test_has_next
392
- @@coll.remove
393
- 200.times do |n|
394
- @@coll.save("x" => n)
395
- end
396
-
397
- cursor = @@coll.find
398
- n = 0
399
- while cursor.has_next?
400
- assert cursor.next
401
- n += 1
402
- end
403
-
404
- assert_equal n, 200
405
- assert_equal false, cursor.has_next?
406
- end
407
-
408
- def test_cursor_invalid
409
- @@coll.remove
410
- 10000.times do |n|
411
- @@coll.insert({:a => n})
412
- end
413
-
414
- cursor = @@coll.find({})
415
-
416
- assert_raise_error Mongo::OperationFailure, "CURSOR_NOT_FOUND" do
417
- 9999.times do
418
- cursor.next_document
419
- cursor.instance_variable_set(:@cursor_id, 1234567890)
420
- end
421
- end
422
- end
423
-
424
- def test_enumberables
425
- @@coll.remove
426
- 100.times do |n|
427
- @@coll.insert({:a => n})
428
- end
429
-
430
- assert_equal 100, @@coll.find.to_a.length
431
- assert_equal 100, @@coll.find.to_set.length
432
-
433
- cursor = @@coll.find
434
- 50.times { |n| cursor.next_document }
435
- assert_equal 50, cursor.to_a.length
436
- end
437
-
438
- def test_rewind
439
- @@coll.remove
440
- 100.times do |n|
441
- @@coll.insert({:a => n})
442
- end
443
-
444
- cursor = @@coll.find
445
- cursor.to_a
446
- assert_equal [], cursor.map {|doc| doc }
447
-
448
- cursor.rewind!
449
- assert_equal 100, cursor.map {|doc| doc }.length
450
-
451
- cursor.rewind!
452
- 5.times { cursor.next_document }
453
- cursor.rewind!
454
- assert_equal 100, cursor.map {|doc| doc }.length
455
- end
456
-
457
- def test_transformer
458
- transformer = Proc.new { |doc| doc }
459
- cursor = Cursor.new(@@coll, :transformer => transformer)
460
- assert_equal(transformer, cursor.transformer)
461
- end
462
-
463
- def test_instance_transformation_with_next
464
- klass = Struct.new(:id, :a)
465
- transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
466
- cursor = Cursor.new(@@coll, :transformer => transformer)
467
- instance = cursor.next
468
-
469
- assert_instance_of(klass, instance)
470
- assert_instance_of(BSON::ObjectId, instance.id)
471
- assert_equal(1, instance.a)
472
- end
473
-
474
- def test_instance_transformation_with_each
475
- klass = Struct.new(:id, :a)
476
- transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
477
- cursor = Cursor.new(@@coll, :transformer => transformer)
478
-
479
- cursor.each do |instance|
480
- assert_instance_of(klass, instance)
481
- end
482
- end
483
- end