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