jmongo 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. data/Gemfile +8 -0
  2. data/Gemfile.lock +43 -0
  3. data/Rakefile +72 -0
  4. data/jmongo.gemspec +84 -6
  5. data/lib/jmongo.rb +6 -14
  6. data/lib/jmongo/collection.rb +196 -114
  7. data/lib/jmongo/connection.rb +39 -13
  8. data/lib/jmongo/cursor.rb +161 -63
  9. data/lib/jmongo/db.rb +119 -30
  10. data/lib/jmongo/exceptions.rb +39 -0
  11. data/lib/jmongo/mongo-2.6.5.gb1.jar +0 -0
  12. data/lib/jmongo/mongo/bson.rb +130 -0
  13. data/lib/jmongo/mongo/collection.rb +185 -0
  14. data/lib/jmongo/mongo/connection.rb +45 -0
  15. data/lib/jmongo/mongo/db.rb +31 -0
  16. data/lib/jmongo/mongo/jmongo.rb +44 -0
  17. data/lib/jmongo/mongo/mongo.rb +98 -0
  18. data/lib/jmongo/mongo/ruby_ext.rb +38 -0
  19. data/lib/jmongo/mongo/utils.rb +136 -0
  20. data/lib/jmongo/version.rb +1 -1
  21. data/test-results.txt +98 -0
  22. data/test/auxillary/1.4_features.rb +166 -0
  23. data/test/auxillary/authentication_test.rb +68 -0
  24. data/test/auxillary/autoreconnect_test.rb +41 -0
  25. data/test/auxillary/fork_test.rb +30 -0
  26. data/test/auxillary/repl_set_auth_test.rb +58 -0
  27. data/test/auxillary/slave_connection_test.rb +36 -0
  28. data/test/auxillary/threaded_authentication_test.rb +101 -0
  29. data/test/bson/binary_test.rb +15 -0
  30. data/test/bson/bson_test.rb +657 -0
  31. data/test/bson/byte_buffer_test.rb +208 -0
  32. data/test/bson/hash_with_indifferent_access_test.rb +38 -0
  33. data/test/bson/json_test.rb +17 -0
  34. data/test/bson/object_id_test.rb +138 -0
  35. data/test/bson/ordered_hash_test.rb +245 -0
  36. data/test/bson/test_helper.rb +46 -0
  37. data/test/bson/timestamp_test.rb +46 -0
  38. data/test/collection_test.rb +933 -0
  39. data/test/connection_test.rb +325 -0
  40. data/test/conversions_test.rb +121 -0
  41. data/test/cursor_fail_test.rb +75 -0
  42. data/test/cursor_message_test.rb +43 -0
  43. data/test/cursor_test.rb +547 -0
  44. data/test/data/empty_data +0 -0
  45. data/test/data/sample_data +0 -0
  46. data/test/data/sample_file.pdf +0 -0
  47. data/test/data/small_data.txt +1 -0
  48. data/test/db_api_test.rb +739 -0
  49. data/test/db_connection_test.rb +15 -0
  50. data/test/db_test.rb +325 -0
  51. data/test/grid_file_system_test.rb +260 -0
  52. data/test/grid_io_test.rb +210 -0
  53. data/test/grid_test.rb +259 -0
  54. data/test/load/thin/config.ru +6 -0
  55. data/test/load/thin/config.yml.template +6 -0
  56. data/test/load/thin/load.rb +24 -0
  57. data/test/load/unicorn/config.ru +6 -0
  58. data/test/load/unicorn/load.rb +23 -0
  59. data/test/load/unicorn/unicorn.rb.template +29 -0
  60. data/test/replica_sets/connect_test.rb +111 -0
  61. data/test/replica_sets/connection_string_test.rb +29 -0
  62. data/test/replica_sets/count_test.rb +36 -0
  63. data/test/replica_sets/insert_test.rb +54 -0
  64. data/test/replica_sets/pooled_insert_test.rb +58 -0
  65. data/test/replica_sets/query_secondaries.rb +109 -0
  66. data/test/replica_sets/query_test.rb +52 -0
  67. data/test/replica_sets/read_preference_test.rb +43 -0
  68. data/test/replica_sets/refresh_test.rb +123 -0
  69. data/test/replica_sets/replication_ack_test.rb +71 -0
  70. data/test/replica_sets/rs_test_helper.rb +27 -0
  71. data/test/safe_test.rb +68 -0
  72. data/test/support/hash_with_indifferent_access.rb +186 -0
  73. data/test/support/keys.rb +45 -0
  74. data/test/support_test.rb +19 -0
  75. data/test/test_helper.rb +111 -0
  76. data/test/threading/threading_with_large_pool_test.rb +90 -0
  77. data/test/threading_test.rb +88 -0
  78. data/test/tools/auth_repl_set_manager.rb +14 -0
  79. data/test/tools/keyfile.txt +1 -0
  80. data/test/tools/repl_set_manager.rb +377 -0
  81. data/test/unit/collection_test.rb +128 -0
  82. data/test/unit/connection_test.rb +85 -0
  83. data/test/unit/cursor_test.rb +127 -0
  84. data/test/unit/db_test.rb +96 -0
  85. data/test/unit/grid_test.rb +51 -0
  86. data/test/unit/node_test.rb +73 -0
  87. data/test/unit/pool_manager_test.rb +47 -0
  88. data/test/unit/pool_test.rb +9 -0
  89. data/test/unit/read_test.rb +101 -0
  90. data/test/unit/safe_test.rb +125 -0
  91. data/test/uri_test.rb +92 -0
  92. metadata +170 -99
  93. data/lib/jmongo/ajrb.rb +0 -189
  94. data/lib/jmongo/jmongo_jext.rb +0 -302
  95. data/lib/jmongo/mongo-2.6.3.jar +0 -0
  96. data/lib/jmongo/utils.rb +0 -61
@@ -0,0 +1,43 @@
1
+ require './test/test_helper'
2
+ require 'logger'
3
+
4
+ class CursorMessageTest < 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_valid_batch_sizes
20
+ assert_raise ArgumentError do
21
+ @@coll.find({}, :batch_size => 1, :limit => 5)
22
+ end
23
+
24
+ assert_raise ArgumentError do
25
+ @@coll.find({}, :batch_size => -1, :limit => 5)
26
+ end
27
+
28
+ assert @@coll.find({}, :batch_size => 0, :limit => 5)
29
+ end
30
+
31
+ def test_batch_size
32
+ @@coll.remove
33
+ 200.times do |n|
34
+ @@coll.insert({:a => n})
35
+ end
36
+
37
+ list = @@coll.find({}, :batch_size => 2, :limit => 6).to_a
38
+ assert_equal 6, list.length
39
+
40
+ list = @@coll.find({}, :batch_size => 100, :limit => 101).to_a
41
+ assert_equal 101, list.length
42
+ end
43
+ end
@@ -0,0 +1,547 @@
1
+ require './test/test_helper'
2
+ require 'logger'
3
+
4
+ CONNECTION ||= Mongo::Connection.new(TEST_HOST, TEST_PORT, :op_timeout => 10)
5
+ $db = CONNECTION.db(MONGO_TEST_DB)
6
+
7
+ VERSION = CONNECTION.server_version
8
+ apr VERSION
9
+
10
+ def clear_collections
11
+ $db.collection_names.each do |n|
12
+ $db.drop_collection(n) unless n =~ /system/
13
+ end
14
+ end
15
+
16
+ clear_collections
17
+
18
+ $coll = $db.collection("test")
19
+ $coll_full_name = "#{MONGO_TEST_DB}.test"
20
+
21
+ class CursorTest < MiniTest::Unit::TestCase
22
+ include Mongo
23
+
24
+ def setup
25
+ clear_collections
26
+ #$coll.insert('a' => 1) # collection not created until it's used
27
+ end
28
+
29
+ def test_alive
30
+ batch = []
31
+ 5000.times do |n|
32
+ batch << {:a => n}
33
+ end
34
+
35
+ $coll.insert(batch)
36
+ cursor = $coll.find
37
+ assert !cursor.alive?
38
+ cursor.next
39
+ assert cursor.alive?
40
+ cursor.close
41
+ assert !cursor.alive?
42
+ end
43
+
44
+ def test_add_and_remove_options
45
+ $coll.insert('a' => 1)
46
+ c = $coll.find
47
+ assert_equal 0, c.options & OP_QUERY_EXHAUST
48
+ c.add_option(OP_QUERY_EXHAUST)
49
+ assert_equal OP_QUERY_EXHAUST, c.options & OP_QUERY_EXHAUST
50
+ c.remove_option(OP_QUERY_EXHAUST)
51
+ assert_equal 0, c.options & OP_QUERY_EXHAUST
52
+
53
+ c.next
54
+ assert_raises Mongo::InvalidOperation do
55
+ c.add_option(OP_QUERY_EXHAUST)
56
+ end
57
+
58
+ assert_raises Mongo::InvalidOperation do
59
+ c.add_option(OP_QUERY_EXHAUST)
60
+ end
61
+ end
62
+
63
+ def test_exhaust
64
+ skip("Mongo Version is not >= 2.0") unless VERSION >= "2.0"
65
+
66
+ data = "1" * 100_000
67
+ 10_000.times do |n|
68
+ $coll.insert({:n => n, :data => data})
69
+ end
70
+
71
+ c = Cursor.new($coll)
72
+ c.add_option(OP_QUERY_EXHAUST)
73
+ assert_equal $coll.count, c.to_a.size
74
+ assert c.closed?
75
+
76
+ c = Cursor.new($coll)
77
+ c.add_option(OP_QUERY_EXHAUST)
78
+ 9999.times do
79
+ c.next
80
+ end
81
+ assert c.has_next?
82
+ assert c.next
83
+ assert !c.has_next?
84
+ assert c.closed?
85
+
86
+ end
87
+
88
+ # def test_inspect
89
+ # selector = {:a => 1}
90
+ # cursor = $coll.find(selector)
91
+ # assert_equal "<Mongo::Cursor:0x#{cursor.object_id.to_s(16)} namespace='#{$db.name}.#{$coll.name}' " +
92
+ # "@selector=#{selector.inspect} @cursor_id=#{cursor.cursor_id}>", cursor.inspect
93
+ # end
94
+
95
+ def test_explain
96
+ $coll.insert('a' => 1)
97
+ cursor = $coll.find('a' => 1)
98
+ explaination = cursor.explain
99
+ assert explaination['cursor']
100
+ assert_kind_of Numeric, explaination['n']
101
+ assert_kind_of Numeric, explaination['millis']
102
+ assert_kind_of Numeric, explaination['nscanned']
103
+ end
104
+
105
+ def test_count
106
+
107
+ assert_equal 0, $coll.find().count()
108
+
109
+ 10.times do |i|
110
+ $coll.save("x" => i)
111
+ end
112
+
113
+ assert_equal 10, $coll.find().count()
114
+ assert_kind_of Integer, $coll.find().count()
115
+ assert_equal 10, $coll.find({}, :limit => 5).count()
116
+ assert_equal 10, $coll.find({}, :skip => 5).count()
117
+
118
+ assert_equal 5, $coll.find({}, :limit => 5).count(true)
119
+ assert_equal 5, $coll.find({}, :skip => 5).count(true)
120
+ assert_equal 2, $coll.find({}, :skip => 5, :limit => 2).count(true)
121
+
122
+ assert_equal 1, $coll.find({"x" => 1}).count()
123
+ assert_equal 5, $coll.find({"x" => {"$lt" => 5}}).count()
124
+
125
+ a = $coll.find()
126
+ b = a.count()
127
+ a.each do |doc|
128
+ break
129
+ end
130
+ assert_equal b, a.count()
131
+
132
+ assert_equal 0, $db['acollectionthatdoesn'].count()
133
+ end
134
+
135
+ def test_sort
136
+
137
+ 5.times{|x| $coll.insert({"age" => x}) }
138
+
139
+ assert_kind_of Cursor, $coll.find().sort(:age, 1)
140
+
141
+ assert_equal 0, $coll.find().sort(:age, 1).next_document["age"]
142
+ assert_equal 4, $coll.find().sort(:age, -1).next_document["age"]
143
+ assert_equal 0, $coll.find().sort([["age", :asc]]).next_document["age"]
144
+
145
+ assert_kind_of Cursor, $coll.find().sort([[:age, -1], [:b, 1]])
146
+
147
+ assert_equal 4, $coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
148
+ assert_equal 0, $coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
149
+
150
+ assert_equal 4, $coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
151
+ assert_equal 0, $coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
152
+
153
+ cursor = $coll.find()
154
+ cursor.next_document
155
+ assert_raises InvalidOperation do
156
+ cursor.sort(["age", 1])
157
+ end
158
+
159
+ assert_raises InvalidSortValueError do
160
+ $coll.find().sort(:age, 25).next_document
161
+ end
162
+
163
+ assert_raises InvalidSortValueError do
164
+ $coll.find().sort(25).next_document
165
+ end
166
+ end
167
+
168
+ def test_sort_date
169
+
170
+ 5.times{|x| $coll.insert({"created_at" => Time.utc(2000 + x)}) }
171
+
172
+ assert_equal 2000, $coll.find().sort(:created_at, :asc).next_document["created_at"].year
173
+ assert_equal 2004, $coll.find().sort(:created_at, :desc).next_document["created_at"].year
174
+
175
+ assert_equal 2000, $coll.find().sort([:created_at, :asc]).next_document["created_at"].year
176
+ assert_equal 2004, $coll.find().sort([:created_at, :desc]).next_document["created_at"].year
177
+
178
+ assert_equal 2000, $coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
179
+ assert_equal 2004, $coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
180
+ end
181
+
182
+ def test_sort_min_max_keys
183
+
184
+ $coll.insert({"n" => 1000000})
185
+ $coll.insert({"n" => -1000000})
186
+ $coll.insert({"n" => MaxKey.new})
187
+ $coll.insert({"n" => MinKey.new})
188
+
189
+ results = $coll.find.sort([:n, :asc]).to_a
190
+
191
+ assert_equal MinKey.new, results[0]['n']
192
+ assert_equal(-1000000, results[1]['n'])
193
+ assert_equal 1000000, results[2]['n']
194
+ assert_equal MaxKey.new, results[3]['n']
195
+ end
196
+
197
+ def test_id_range_queries
198
+
199
+ t1 = Time.now
200
+ t1_id = ObjectId.from_time(t1)
201
+ $coll.save({:t => 't1'})
202
+ $coll.save({:t => 't1'})
203
+ $coll.save({:t => 't1'})
204
+ sleep(2)
205
+ t2 = Time.now
206
+ t2_id = ObjectId.from_time(t2)
207
+ $coll.save({:t => 't2'})
208
+ $coll.save({:t => 't2'})
209
+ $coll.save({:t => 't2'})
210
+
211
+ assert_equal 3, $coll.find({'_id' => {'$gt' => t1_id, '$lt' => t2_id}}).count
212
+ $coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
213
+ assert_equal 't2', doc['t']
214
+ end
215
+ end
216
+
217
+ def test_limit
218
+
219
+ 10.times do |i|
220
+ $coll.save("x" => i)
221
+ end
222
+ assert_equal 10, $coll.find().count()
223
+
224
+ results = $coll.find().limit(5).to_a
225
+ assert_equal 5, results.length
226
+ end
227
+
228
+ def test_timeout_options
229
+ cursor = Cursor.new($coll)
230
+ assert_equal true, cursor.timeout
231
+
232
+ cursor = $coll.find
233
+ assert_equal true, cursor.timeout
234
+
235
+ cursor = $coll.find({}, :timeout => nil)
236
+ assert_equal true, cursor.timeout
237
+
238
+ cursor = Cursor.new($coll, :timeout => false)
239
+ assert_equal false, cursor.timeout
240
+
241
+ $coll.find({}, :timeout => false) do |c|
242
+ assert_equal false, c.timeout
243
+ end
244
+ end
245
+
246
+ def test_timeout
247
+ opts = Cursor.new($coll).query_opts
248
+ assert_equal 0, opts & Mongo::OP_QUERY_NO_CURSOR_TIMEOUT
249
+
250
+ opts = Cursor.new($coll, :timeout => false).query_opts
251
+ assert_equal Mongo::OP_QUERY_NO_CURSOR_TIMEOUT,
252
+ opts & Mongo::OP_QUERY_NO_CURSOR_TIMEOUT
253
+ end
254
+
255
+ def test_limit_exceptions
256
+ cursor = $coll.find()
257
+ firstResult = cursor.next_document
258
+ assert_raises InvalidOperation, "Cannot modify the query once it has been run or closed." do
259
+ cursor.limit(1)
260
+ end
261
+
262
+ cursor = $coll.find()
263
+ cursor.close
264
+ assert_raises InvalidOperation, "Cannot modify the query once it has been run or closed." do
265
+ cursor.limit(1)
266
+ end
267
+ end
268
+
269
+ def test_skip
270
+
271
+
272
+ 10.times do |i|
273
+ $coll.save("x" => i)
274
+ end
275
+ assert_equal 10, $coll.find().count()
276
+
277
+ all_results = $coll.find().to_a
278
+ skip_results = $coll.find().skip(2).to_a
279
+ assert_equal 10, all_results.length
280
+ assert_equal 8, skip_results.length
281
+
282
+ assert_equal all_results.slice(2...10), skip_results
283
+ end
284
+
285
+ def test_skip_exceptions
286
+ cursor = $coll.find()
287
+ firstResult = cursor.next_document
288
+ assert_raises InvalidOperation, "Cannot modify the query once it has been run or closed." do
289
+ cursor.skip(1)
290
+ end
291
+
292
+ cursor = $coll.find()
293
+ cursor.close
294
+ assert_raises InvalidOperation, "Cannot modify the query once it has been run or closed." do
295
+ cursor.skip(1)
296
+ end
297
+ end
298
+
299
+ def test_limit_skip_chaining
300
+
301
+ 10.times do |i|
302
+ $coll.save("x" => i)
303
+ end
304
+
305
+ all_results = $coll.find().to_a
306
+ limited_skip_results = $coll.find().limit(5).skip(3).to_a
307
+
308
+ assert_equal all_results.slice(3...8), limited_skip_results
309
+ end
310
+
311
+ def test_close_no_query_sent
312
+ begin
313
+ cursor = $coll.find('a' => 1)
314
+ cursor.close
315
+ assert cursor.closed?
316
+ rescue => ex
317
+ fail ex.to_s
318
+ end
319
+ end
320
+
321
+ def test_refill_via_get_more
322
+ $coll.insert('a' => 1)
323
+ assert_equal 1, $coll.count
324
+ 1000.times { |i|
325
+ assert_equal 1 + i, $coll.count
326
+ $coll.insert('a' => i)
327
+ }
328
+
329
+ assert_equal 1001, $coll.count
330
+ count = 0
331
+ $coll.find.each { |obj|
332
+ count += obj['a']
333
+ }
334
+ assert_equal 1001, $coll.count
335
+
336
+ # do the same thing again for debugging
337
+ assert_equal 1001, $coll.count
338
+ count2 = 0
339
+ $coll.find.each { |obj|
340
+ count2 += obj['a']
341
+ }
342
+ assert_equal 1001, $coll.count
343
+
344
+ assert_equal count, count2
345
+ assert_equal 499501, count
346
+ end
347
+
348
+ def test_refill_via_get_more_alt_coll
349
+ coll = $db.collection('test-alt-coll')
350
+ coll.remove
351
+ coll.insert('a' => 1) # collection not created until it's used
352
+ assert_equal 1, coll.count
353
+
354
+ 1000.times { |i|
355
+ assert_equal 1 + i, coll.count
356
+ coll.insert('a' => i)
357
+ }
358
+
359
+ assert_equal 1001, coll.count
360
+ count = 0
361
+ coll.find.each { |obj|
362
+ count += obj['a']
363
+ }
364
+ assert_equal 1001, coll.count
365
+
366
+ # do the same thing again for debugging
367
+ assert_equal 1001, coll.count
368
+ count2 = 0
369
+ coll.find.each { |obj|
370
+ count2 += obj['a']
371
+ }
372
+ assert_equal 1001, coll.count
373
+
374
+ assert_equal count, count2
375
+ assert_equal 499501, count
376
+ end
377
+
378
+ def test_close_after_query_sent
379
+ begin
380
+ cursor = $coll.find('a' => 1)
381
+ cursor.next_document
382
+ cursor.close
383
+ assert cursor.closed?
384
+ rescue => ex
385
+ fail ex.to_s
386
+ end
387
+ end
388
+
389
+ def test_kill_cursors
390
+ $coll.drop
391
+
392
+ client_cursors = $db.command("cursorInfo" => 1)["clientCursors_size"]
393
+
394
+ 10000.times do |i|
395
+ $coll.insert("i" => i)
396
+ end
397
+
398
+ assert_equal(client_cursors,
399
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
400
+
401
+ 10.times do |i|
402
+ $coll.find_one()
403
+ end
404
+
405
+ assert_equal(client_cursors,
406
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
407
+
408
+ 10.times do |i|
409
+ a = $coll.find()
410
+ a.next_document
411
+ a.close()
412
+ end
413
+
414
+ assert_equal(client_cursors,
415
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
416
+
417
+ a = $coll.find()
418
+ a.next_document
419
+
420
+ refute_equal(client_cursors,
421
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
422
+
423
+ a.close()
424
+
425
+ assert_equal(client_cursors,
426
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
427
+
428
+ a = $coll.find({}, :limit => 10).next_document
429
+
430
+ assert_equal(client_cursors,
431
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
432
+
433
+ $coll.find() do |cursor|
434
+ cursor.next_document
435
+ end
436
+
437
+ assert_equal(client_cursors,
438
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
439
+
440
+ $coll.find() { |cursor|
441
+ cursor.next_document
442
+ }
443
+
444
+ assert_equal(client_cursors,
445
+ $db.command("cursorInfo" => 1)["clientCursors_size"])
446
+ end
447
+
448
+ def test_count_with_fields
449
+ $coll.save("x" => 1)
450
+
451
+ assert_equal(1, $coll.find({}, :fields => ["a"]).count())
452
+ end
453
+
454
+ def test_has_next
455
+
456
+ 200.times do |n|
457
+ $coll.save("x" => n)
458
+ end
459
+
460
+ cursor = $coll.find
461
+ n = 0
462
+ while cursor.has_next?
463
+ assert cursor.next
464
+ n += 1
465
+ end
466
+
467
+ assert_equal n, 200
468
+ assert_equal false, cursor.has_next?
469
+ end
470
+
471
+ def test_cursor_invalid
472
+
473
+ 10000.times do |n|
474
+ $coll.insert({:a => n})
475
+ end
476
+
477
+ cursor = $coll.find({})
478
+
479
+ # assert_raises_error Mongo::OperationFailure, "CURSOR_NOT_FOUND" do
480
+ # 9999.times do
481
+ # cursor.next_document
482
+ # cursor.instance_variable_set(:@cursor_id, 1234567890)
483
+ # end
484
+ # end
485
+ end
486
+
487
+ def test_enumberables
488
+
489
+ 100.times do |n|
490
+ $coll.insert({:a => n})
491
+ end
492
+
493
+ assert_equal 100, $coll.find.to_a.length
494
+ assert_equal 100, $coll.find.to_set.length
495
+
496
+ cursor = $coll.find
497
+ 50.times { |n| cursor.next_document }
498
+ assert_equal 50, cursor.to_a.length
499
+ end
500
+
501
+ def test_rewind
502
+
503
+ 100.times do |n|
504
+ $coll.insert({:a => n})
505
+ end
506
+
507
+ cursor = $coll.find
508
+ cursor.to_a
509
+ assert_equal false, cursor.has_next?
510
+
511
+ cursor.rewind!
512
+ assert_equal 100, cursor.map {|doc| doc }.length
513
+
514
+ cursor.rewind!
515
+ 5.times { cursor.next_document }
516
+ cursor.rewind!
517
+ assert_equal 100, cursor.map {|doc| doc }.length
518
+ end
519
+
520
+ def test_transformer
521
+ transformer = Proc.new { |doc| doc }
522
+ cursor = Cursor.new($coll, :transformer => transformer)
523
+ assert_equal(transformer, cursor.transformer)
524
+ end
525
+
526
+ def test_instance_transformation_with_next
527
+ $coll.insert('a' => 1)
528
+ klass = Struct.new(:id, :a)
529
+ transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
530
+ cursor = Cursor.new($coll, :transformer => transformer)
531
+ instance = cursor.next
532
+
533
+ assert_instance_of(klass, instance)
534
+ assert_instance_of(BSON::ObjectId, instance.id)
535
+ assert_equal(1, instance.a)
536
+ end
537
+
538
+ def test_instance_transformation_with_each
539
+ klass = Struct.new(:id, :a)
540
+ transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
541
+ cursor = Cursor.new($coll, :transformer => transformer)
542
+
543
+ cursor.each do |instance|
544
+ assert_instance_of(klass, instance)
545
+ end
546
+ end
547
+ end