jonbell-mongo 1.3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +190 -0
- data/README.md +333 -0
- data/Rakefile +215 -0
- data/bin/mongo_console +21 -0
- data/docs/CREDITS.md +123 -0
- data/docs/FAQ.md +116 -0
- data/docs/GridFS.md +158 -0
- data/docs/HISTORY.md +263 -0
- data/docs/RELEASES.md +33 -0
- data/docs/REPLICA_SETS.md +72 -0
- data/docs/TUTORIAL.md +247 -0
- data/docs/WRITE_CONCERN.md +28 -0
- data/lib/mongo.rb +97 -0
- data/lib/mongo/collection.rb +895 -0
- data/lib/mongo/connection.rb +926 -0
- data/lib/mongo/cursor.rb +474 -0
- data/lib/mongo/db.rb +617 -0
- data/lib/mongo/exceptions.rb +71 -0
- data/lib/mongo/gridfs/grid.rb +107 -0
- data/lib/mongo/gridfs/grid_ext.rb +57 -0
- data/lib/mongo/gridfs/grid_file_system.rb +146 -0
- data/lib/mongo/gridfs/grid_io.rb +485 -0
- data/lib/mongo/gridfs/grid_io_fix.rb +38 -0
- data/lib/mongo/repl_set_connection.rb +356 -0
- data/lib/mongo/util/conversions.rb +89 -0
- data/lib/mongo/util/core_ext.rb +60 -0
- data/lib/mongo/util/pool.rb +177 -0
- data/lib/mongo/util/server_version.rb +71 -0
- data/lib/mongo/util/support.rb +82 -0
- data/lib/mongo/util/uri_parser.rb +185 -0
- data/mongo.gemspec +34 -0
- data/test/auxillary/1.4_features.rb +166 -0
- data/test/auxillary/authentication_test.rb +68 -0
- data/test/auxillary/autoreconnect_test.rb +41 -0
- data/test/auxillary/fork_test.rb +30 -0
- data/test/auxillary/repl_set_auth_test.rb +58 -0
- data/test/auxillary/slave_connection_test.rb +36 -0
- data/test/auxillary/threaded_authentication_test.rb +101 -0
- data/test/bson/binary_test.rb +15 -0
- data/test/bson/bson_test.rb +654 -0
- data/test/bson/byte_buffer_test.rb +208 -0
- data/test/bson/hash_with_indifferent_access_test.rb +38 -0
- data/test/bson/json_test.rb +17 -0
- data/test/bson/object_id_test.rb +154 -0
- data/test/bson/ordered_hash_test.rb +210 -0
- data/test/bson/timestamp_test.rb +24 -0
- data/test/collection_test.rb +910 -0
- data/test/connection_test.rb +324 -0
- data/test/conversions_test.rb +119 -0
- data/test/cursor_fail_test.rb +75 -0
- data/test/cursor_message_test.rb +43 -0
- data/test/cursor_test.rb +483 -0
- data/test/db_api_test.rb +738 -0
- data/test/db_connection_test.rb +15 -0
- data/test/db_test.rb +315 -0
- data/test/grid_file_system_test.rb +259 -0
- data/test/grid_io_test.rb +209 -0
- data/test/grid_test.rb +258 -0
- data/test/load/thin/load.rb +24 -0
- data/test/load/unicorn/load.rb +23 -0
- data/test/replica_sets/connect_test.rb +112 -0
- data/test/replica_sets/connection_string_test.rb +32 -0
- data/test/replica_sets/count_test.rb +35 -0
- data/test/replica_sets/insert_test.rb +53 -0
- data/test/replica_sets/pooled_insert_test.rb +55 -0
- data/test/replica_sets/query_secondaries.rb +108 -0
- data/test/replica_sets/query_test.rb +51 -0
- data/test/replica_sets/replication_ack_test.rb +66 -0
- data/test/replica_sets/rs_test_helper.rb +27 -0
- data/test/safe_test.rb +68 -0
- data/test/support/hash_with_indifferent_access.rb +186 -0
- data/test/support/keys.rb +45 -0
- data/test/support_test.rb +18 -0
- data/test/test_helper.rb +102 -0
- data/test/threading/threading_with_large_pool_test.rb +90 -0
- data/test/threading_test.rb +87 -0
- data/test/tools/auth_repl_set_manager.rb +14 -0
- data/test/tools/repl_set_manager.rb +266 -0
- data/test/unit/collection_test.rb +130 -0
- data/test/unit/connection_test.rb +85 -0
- data/test/unit/cursor_test.rb +109 -0
- data/test/unit/db_test.rb +94 -0
- data/test/unit/grid_test.rb +49 -0
- data/test/unit/pool_test.rb +9 -0
- data/test/unit/repl_set_connection_test.rb +59 -0
- data/test/unit/safe_test.rb +125 -0
- data/test/uri_test.rb +91 -0
- metadata +224 -0
@@ -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
|
data/test/cursor_test.rb
ADDED
@@ -0,0 +1,483 @@
|
|
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
|