2012-04-11 15:08:04 +00:00
|
|
|
require File.expand_path("../test_helper", __FILE__)
|
2010-01-20 23:50:07 +00:00
|
|
|
require 'logger'
|
2009-01-13 19:02:16 +00:00
|
|
|
|
|
|
|
class CursorTest < Test::Unit::TestCase
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2011-08-05 15:52:45 +00:00
|
|
|
include Mongo::Constants
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2010-10-13 21:09:23 +00:00
|
|
|
@@connection = standard_connection
|
2010-04-05 19:48:35 +00:00
|
|
|
@@db = @@connection.db(MONGO_TEST_DB)
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll = @@db.collection('test')
|
2009-10-26 18:54:33 +00:00
|
|
|
@@version = @@connection.server_version
|
2009-02-05 15:10:41 +00:00
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
def setup
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll.insert('a' => 1) # collection not created until it's used
|
2010-04-05 19:48:35 +00:00
|
|
|
@@coll_full_name = "#{MONGO_TEST_DB}.test"
|
2009-01-13 19:02:16 +00:00
|
|
|
end
|
|
|
|
|
2011-08-05 21:54:38 +00:00
|
|
|
def test_alive
|
|
|
|
batch = []
|
|
|
|
5000.times do |n|
|
|
|
|
batch << {:a => n}
|
|
|
|
end
|
|
|
|
|
|
|
|
@@coll.insert(batch)
|
|
|
|
cursor = @@coll.find
|
|
|
|
assert !cursor.alive?
|
|
|
|
cursor.next
|
|
|
|
assert cursor.alive?
|
|
|
|
cursor.close
|
|
|
|
assert !cursor.alive?
|
|
|
|
@@coll.remove
|
|
|
|
end
|
|
|
|
|
2011-08-05 22:15:48 +00:00
|
|
|
def test_add_and_remove_options
|
2011-08-05 15:52:45 +00:00
|
|
|
c = @@coll.find
|
2011-08-05 22:15:48 +00:00
|
|
|
assert_equal 0, c.options & OP_QUERY_EXHAUST
|
2011-08-05 15:52:45 +00:00
|
|
|
c.add_option(OP_QUERY_EXHAUST)
|
2011-08-05 22:15:48 +00:00
|
|
|
assert_equal OP_QUERY_EXHAUST, c.options & OP_QUERY_EXHAUST
|
|
|
|
c.remove_option(OP_QUERY_EXHAUST)
|
|
|
|
assert_equal 0, c.options & OP_QUERY_EXHAUST
|
2011-08-05 22:22:09 +00:00
|
|
|
|
|
|
|
c.next
|
|
|
|
assert_raise Mongo::InvalidOperation do
|
|
|
|
c.add_option(OP_QUERY_EXHAUST)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise Mongo::InvalidOperation do
|
|
|
|
c.add_option(OP_QUERY_EXHAUST)
|
|
|
|
end
|
2011-08-05 15:52:45 +00:00
|
|
|
end
|
|
|
|
|
2011-09-07 14:02:10 +00:00
|
|
|
def test_exhaust
|
|
|
|
if @@version >= "2.0"
|
|
|
|
@@coll.remove
|
2011-11-04 19:12:58 +00:00
|
|
|
data = "1" * 10_000
|
2011-10-17 18:41:09 +00:00
|
|
|
5000.times do |n|
|
2011-09-07 14:02:10 +00:00
|
|
|
@@coll.insert({:n => n, :data => data})
|
|
|
|
end
|
|
|
|
|
|
|
|
c = Cursor.new(@@coll)
|
|
|
|
c.add_option(OP_QUERY_EXHAUST)
|
|
|
|
assert_equal @@coll.count, c.to_a.size
|
|
|
|
assert c.closed?
|
|
|
|
|
|
|
|
c = Cursor.new(@@coll)
|
|
|
|
c.add_option(OP_QUERY_EXHAUST)
|
2011-10-17 18:41:09 +00:00
|
|
|
4999.times do
|
2011-09-07 14:02:10 +00:00
|
|
|
c.next
|
|
|
|
end
|
|
|
|
assert c.has_next?
|
|
|
|
assert c.next
|
|
|
|
assert !c.has_next?
|
|
|
|
assert c.closed?
|
|
|
|
|
|
|
|
@@coll.remove
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-02 21:24:45 +00:00
|
|
|
def test_inspect
|
|
|
|
selector = {:a => 1}
|
|
|
|
cursor = @@coll.find(selector)
|
|
|
|
assert_equal "<Mongo::Cursor:0x#{cursor.object_id.to_s(16)} namespace='#{@@db.name}.#{@@coll.name}' " +
|
2011-09-07 14:17:24 +00:00
|
|
|
"@selector=#{selector.inspect} @cursor_id=#{cursor.cursor_id}>", cursor.inspect
|
2010-07-02 21:24:45 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
def test_explain
|
2009-02-05 15:10:41 +00:00
|
|
|
cursor = @@coll.find('a' => 1)
|
2009-01-13 19:02:16 +00:00
|
|
|
explaination = cursor.explain
|
|
|
|
assert_not_nil explaination['cursor']
|
|
|
|
assert_kind_of Numeric, explaination['n']
|
|
|
|
assert_kind_of Numeric, explaination['millis']
|
|
|
|
assert_kind_of Numeric, explaination['nscanned']
|
|
|
|
end
|
|
|
|
|
2009-08-18 15:26:58 +00:00
|
|
|
def test_count
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-08-18 15:26:58 +00:00
|
|
|
|
|
|
|
assert_equal 0, @@coll.find().count()
|
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
@@coll.save("x" => i)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 10, @@coll.find().count()
|
|
|
|
assert_kind_of Integer, @@coll.find().count()
|
|
|
|
assert_equal 10, @@coll.find({}, :limit => 5).count()
|
2009-09-17 20:45:03 +00:00
|
|
|
assert_equal 10, @@coll.find({}, :skip => 5).count()
|
2009-08-18 15:26:58 +00:00
|
|
|
|
2010-11-15 14:44:33 +00:00
|
|
|
assert_equal 5, @@coll.find({}, :limit => 5).count(true)
|
|
|
|
assert_equal 5, @@coll.find({}, :skip => 5).count(true)
|
|
|
|
assert_equal 2, @@coll.find({}, :skip => 5, :limit => 2).count(true)
|
|
|
|
|
2009-08-18 15:26:58 +00:00
|
|
|
assert_equal 1, @@coll.find({"x" => 1}).count()
|
|
|
|
assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()
|
|
|
|
|
|
|
|
a = @@coll.find()
|
|
|
|
b = a.count()
|
|
|
|
a.each do |doc|
|
|
|
|
break
|
|
|
|
end
|
|
|
|
assert_equal b, a.count()
|
|
|
|
|
|
|
|
assert_equal 0, @@db['acollectionthatdoesn'].count()
|
|
|
|
end
|
2009-09-17 19:07:18 +00:00
|
|
|
|
2009-09-04 23:04:11 +00:00
|
|
|
def test_sort
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-12-28 18:43:20 +00:00
|
|
|
5.times{|x| @@coll.insert({"age" => x}) }
|
2009-09-17 19:07:18 +00:00
|
|
|
|
2009-12-28 18:43:20 +00:00
|
|
|
assert_kind_of Cursor, @@coll.find().sort(:age, 1)
|
2009-09-17 19:07:18 +00:00
|
|
|
|
2009-12-28 18:43:20 +00:00
|
|
|
assert_equal 0, @@coll.find().sort(:age, 1).next_document["age"]
|
|
|
|
assert_equal 4, @@coll.find().sort(:age, -1).next_document["age"]
|
|
|
|
assert_equal 0, @@coll.find().sort([["age", :asc]]).next_document["age"]
|
2009-09-17 19:07:18 +00:00
|
|
|
|
2009-12-28 18:43:20 +00:00
|
|
|
assert_kind_of Cursor, @@coll.find().sort([[:age, -1], [:b, 1]])
|
2009-09-17 19:07:18 +00:00
|
|
|
|
2009-12-28 18:43:20 +00:00
|
|
|
assert_equal 4, @@coll.find().sort(:age, 1).sort(:age, -1).next_document["age"]
|
|
|
|
assert_equal 0, @@coll.find().sort(:age, -1).sort(:age, 1).next_document["age"]
|
|
|
|
|
|
|
|
assert_equal 4, @@coll.find().sort([:age, :asc]).sort(:age, -1).next_document["age"]
|
|
|
|
assert_equal 0, @@coll.find().sort([:age, :desc]).sort(:age, 1).next_document["age"]
|
2009-09-17 19:07:18 +00:00
|
|
|
|
|
|
|
cursor = @@coll.find()
|
2009-12-16 19:03:15 +00:00
|
|
|
cursor.next_document
|
2009-09-17 19:07:18 +00:00
|
|
|
assert_raise InvalidOperation do
|
2009-12-28 18:43:20 +00:00
|
|
|
cursor.sort(["age"])
|
2009-09-17 19:07:18 +00:00
|
|
|
end
|
2009-10-08 14:02:54 +00:00
|
|
|
|
|
|
|
assert_raise InvalidSortValueError do
|
2009-12-28 18:43:20 +00:00
|
|
|
@@coll.find().sort(:age, 25).next_document
|
2009-10-08 14:02:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise InvalidSortValueError do
|
2009-12-16 19:03:15 +00:00
|
|
|
@@coll.find().sort(25).next_document
|
2009-10-08 14:02:54 +00:00
|
|
|
end
|
2009-09-04 23:04:11 +00:00
|
|
|
end
|
2009-08-18 15:26:58 +00:00
|
|
|
|
2009-12-28 18:43:20 +00:00
|
|
|
def test_sort_date
|
|
|
|
@@coll.remove
|
|
|
|
5.times{|x| @@coll.insert({"created_at" => Time.utc(2000 + x)}) }
|
|
|
|
|
|
|
|
assert_equal 2000, @@coll.find().sort(:created_at, :asc).next_document["created_at"].year
|
|
|
|
assert_equal 2004, @@coll.find().sort(:created_at, :desc).next_document["created_at"].year
|
|
|
|
|
|
|
|
assert_equal 2000, @@coll.find().sort([:created_at, :asc]).next_document["created_at"].year
|
|
|
|
assert_equal 2004, @@coll.find().sort([:created_at, :desc]).next_document["created_at"].year
|
|
|
|
|
|
|
|
assert_equal 2000, @@coll.find().sort([[:created_at, :asc]]).next_document["created_at"].year
|
|
|
|
assert_equal 2004, @@coll.find().sort([[:created_at, :desc]]).next_document["created_at"].year
|
|
|
|
end
|
|
|
|
|
2010-01-20 23:50:07 +00:00
|
|
|
def test_sort_min_max_keys
|
|
|
|
@@coll.remove
|
|
|
|
@@coll.insert({"n" => 1000000})
|
|
|
|
@@coll.insert({"n" => -1000000})
|
|
|
|
@@coll.insert({"n" => MaxKey.new})
|
|
|
|
@@coll.insert({"n" => MinKey.new})
|
|
|
|
|
|
|
|
results = @@coll.find.sort([:n, :asc]).to_a
|
|
|
|
|
|
|
|
assert_equal MinKey.new, results[0]['n']
|
2011-02-20 16:19:14 +00:00
|
|
|
assert_equal(-1000000, results[1]['n'])
|
2010-01-20 23:50:07 +00:00
|
|
|
assert_equal 1000000, results[2]['n']
|
|
|
|
assert_equal MaxKey.new, results[3]['n']
|
|
|
|
end
|
|
|
|
|
2010-01-22 19:24:17 +00:00
|
|
|
def test_id_range_queries
|
|
|
|
@@coll.remove
|
|
|
|
|
|
|
|
t1 = Time.now
|
2010-09-09 18:27:34 +00:00
|
|
|
t1_id = ObjectId.from_time(t1)
|
2010-01-22 19:24:17 +00:00
|
|
|
@@coll.save({:t => 't1'})
|
|
|
|
@@coll.save({:t => 't1'})
|
|
|
|
@@coll.save({:t => 't1'})
|
|
|
|
sleep(2)
|
|
|
|
t2 = Time.now
|
2010-09-09 18:27:34 +00:00
|
|
|
t2_id = ObjectId.from_time(t2)
|
2010-01-22 19:24:17 +00:00
|
|
|
@@coll.save({:t => 't2'})
|
|
|
|
@@coll.save({:t => 't2'})
|
|
|
|
@@coll.save({:t => 't2'})
|
|
|
|
|
2010-07-12 16:11:01 +00:00
|
|
|
assert_equal 3, @@coll.find({'_id' => {'$gt' => t1_id, '$lt' => t2_id}}).count
|
2010-01-22 19:24:17 +00:00
|
|
|
@@coll.find({'_id' => {'$gt' => t2_id}}).each do |doc|
|
|
|
|
assert_equal 't2', doc['t']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-05 18:25:49 +00:00
|
|
|
def test_limit
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-09-05 18:25:49 +00:00
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
@@coll.save("x" => i)
|
|
|
|
end
|
|
|
|
assert_equal 10, @@coll.find().count()
|
|
|
|
|
|
|
|
results = @@coll.find().limit(5).to_a
|
|
|
|
assert_equal 5, results.length
|
|
|
|
end
|
|
|
|
|
2010-11-04 21:26:21 +00:00
|
|
|
def test_timeout_options
|
|
|
|
cursor = Cursor.new(@@coll)
|
|
|
|
assert_equal true, cursor.timeout
|
|
|
|
|
|
|
|
cursor = @@coll.find
|
|
|
|
assert_equal true, cursor.timeout
|
|
|
|
|
|
|
|
cursor = @@coll.find({}, :timeout => nil)
|
|
|
|
assert_equal true, cursor.timeout
|
|
|
|
|
|
|
|
cursor = Cursor.new(@@coll, :timeout => false)
|
|
|
|
assert_equal false, cursor.timeout
|
|
|
|
|
2011-02-20 16:15:35 +00:00
|
|
|
@@coll.find({}, :timeout => false) do |c|
|
|
|
|
assert_equal false, c.timeout
|
2010-11-04 21:35:58 +00:00
|
|
|
end
|
2010-11-04 21:26:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_timeout
|
2012-03-16 20:17:33 +00:00
|
|
|
opts = Cursor.new(@@coll).options
|
2010-11-04 21:26:21 +00:00
|
|
|
assert_equal 0, opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
|
|
|
|
|
2012-03-16 20:17:33 +00:00
|
|
|
opts = Cursor.new(@@coll, :timeout => false).options
|
2010-11-04 21:26:21 +00:00
|
|
|
assert_equal Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT,
|
|
|
|
opts & Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT
|
|
|
|
end
|
|
|
|
|
2009-09-05 18:25:49 +00:00
|
|
|
def test_limit_exceptions
|
|
|
|
cursor = @@coll.find()
|
2012-03-16 20:17:33 +00:00
|
|
|
cursor.next_document
|
2009-09-16 21:52:41 +00:00
|
|
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
2009-09-05 18:25:49 +00:00
|
|
|
cursor.limit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
cursor = @@coll.find()
|
|
|
|
cursor.close
|
2009-09-16 21:52:41 +00:00
|
|
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
2009-09-05 18:25:49 +00:00
|
|
|
cursor.limit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-16 21:52:41 +00:00
|
|
|
def test_skip
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-09-05 18:25:49 +00:00
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
@@coll.save("x" => i)
|
|
|
|
end
|
|
|
|
assert_equal 10, @@coll.find().count()
|
|
|
|
|
|
|
|
all_results = @@coll.find().to_a
|
2009-09-16 21:52:41 +00:00
|
|
|
skip_results = @@coll.find().skip(2).to_a
|
2009-09-05 18:25:49 +00:00
|
|
|
assert_equal 10, all_results.length
|
2009-09-16 21:52:41 +00:00
|
|
|
assert_equal 8, skip_results.length
|
2009-09-05 18:25:49 +00:00
|
|
|
|
2009-09-16 21:52:41 +00:00
|
|
|
assert_equal all_results.slice(2...10), skip_results
|
2009-09-05 18:25:49 +00:00
|
|
|
end
|
|
|
|
|
2009-09-16 21:52:41 +00:00
|
|
|
def test_skip_exceptions
|
2009-09-05 18:25:49 +00:00
|
|
|
cursor = @@coll.find()
|
2012-03-16 20:17:33 +00:00
|
|
|
cursor.next_document
|
2009-09-16 21:52:41 +00:00
|
|
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
|
|
|
cursor.skip(1)
|
2009-09-05 18:25:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
cursor = @@coll.find()
|
|
|
|
cursor.close
|
2009-09-16 21:52:41 +00:00
|
|
|
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
|
|
|
|
cursor.skip(1)
|
2009-09-05 18:25:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-16 21:52:41 +00:00
|
|
|
def test_limit_skip_chaining
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-09-05 18:25:49 +00:00
|
|
|
10.times do |i|
|
|
|
|
@@coll.save("x" => i)
|
|
|
|
end
|
2009-09-16 21:52:41 +00:00
|
|
|
|
2009-09-05 18:25:49 +00:00
|
|
|
all_results = @@coll.find().to_a
|
2009-09-16 21:52:41 +00:00
|
|
|
limited_skip_results = @@coll.find().limit(5).skip(3).to_a
|
|
|
|
|
|
|
|
assert_equal all_results.slice(3...8), limited_skip_results
|
2009-09-05 18:25:49 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
def test_close_no_query_sent
|
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
cursor = @@coll.find('a' => 1)
|
2009-01-13 19:02:16 +00:00
|
|
|
cursor.close
|
|
|
|
assert cursor.closed?
|
|
|
|
rescue => ex
|
|
|
|
fail ex.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-02 15:49:27 +00:00
|
|
|
def test_refill_via_get_more
|
2009-09-30 14:49:08 +00:00
|
|
|
assert_equal 1, @@coll.count
|
|
|
|
1000.times { |i|
|
|
|
|
assert_equal 1 + i, @@coll.count
|
|
|
|
@@coll.insert('a' => i)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal 1001, @@coll.count
|
|
|
|
count = 0
|
|
|
|
@@coll.find.each { |obj|
|
|
|
|
count += obj['a']
|
|
|
|
}
|
|
|
|
assert_equal 1001, @@coll.count
|
|
|
|
|
|
|
|
# do the same thing again for debugging
|
|
|
|
assert_equal 1001, @@coll.count
|
|
|
|
count2 = 0
|
|
|
|
@@coll.find.each { |obj|
|
|
|
|
count2 += obj['a']
|
|
|
|
}
|
|
|
|
assert_equal 1001, @@coll.count
|
|
|
|
|
|
|
|
assert_equal count, count2
|
|
|
|
assert_equal 499501, count
|
2009-03-02 15:49:27 +00:00
|
|
|
end
|
|
|
|
|
2009-03-09 14:21:08 +00:00
|
|
|
def test_refill_via_get_more_alt_coll
|
2009-09-30 14:49:08 +00:00
|
|
|
coll = @@db.collection('test-alt-coll')
|
2009-10-20 15:31:07 +00:00
|
|
|
coll.remove
|
2009-09-30 14:49:08 +00:00
|
|
|
coll.insert('a' => 1) # collection not created until it's used
|
|
|
|
assert_equal 1, coll.count
|
|
|
|
|
|
|
|
1000.times { |i|
|
|
|
|
assert_equal 1 + i, coll.count
|
|
|
|
coll.insert('a' => i)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal 1001, coll.count
|
|
|
|
count = 0
|
|
|
|
coll.find.each { |obj|
|
|
|
|
count += obj['a']
|
|
|
|
}
|
|
|
|
assert_equal 1001, coll.count
|
|
|
|
|
|
|
|
# do the same thing again for debugging
|
|
|
|
assert_equal 1001, coll.count
|
|
|
|
count2 = 0
|
|
|
|
coll.find.each { |obj|
|
|
|
|
count2 += obj['a']
|
|
|
|
}
|
|
|
|
assert_equal 1001, coll.count
|
|
|
|
|
|
|
|
assert_equal count, count2
|
|
|
|
assert_equal 499501, count
|
2009-03-09 14:21:08 +00:00
|
|
|
end
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
def test_close_after_query_sent
|
|
|
|
begin
|
2009-02-05 15:10:41 +00:00
|
|
|
cursor = @@coll.find('a' => 1)
|
2009-12-16 19:03:15 +00:00
|
|
|
cursor.next_document
|
2009-01-13 19:02:16 +00:00
|
|
|
cursor.close
|
|
|
|
assert cursor.closed?
|
|
|
|
rescue => ex
|
|
|
|
fail ex.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-18 21:36:53 +00:00
|
|
|
def test_kill_cursors
|
|
|
|
@@coll.drop
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
client_cursors = @@db.command("cursorInfo" => 1)["clientCursors_size"]
|
2009-08-18 21:36:53 +00:00
|
|
|
|
|
|
|
10000.times do |i|
|
|
|
|
@@coll.insert("i" => i)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-18 21:36:53 +00:00
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
@@coll.find_one()
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-18 21:36:53 +00:00
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
a = @@coll.find()
|
2009-12-16 19:03:15 +00:00
|
|
|
a.next_document
|
2009-08-18 21:36:53 +00:00
|
|
|
a.close()
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-18 21:36:53 +00:00
|
|
|
|
|
|
|
a = @@coll.find()
|
2009-12-16 19:03:15 +00:00
|
|
|
a.next_document
|
2009-08-18 21:36:53 +00:00
|
|
|
|
|
|
|
assert_not_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-18 21:36:53 +00:00
|
|
|
|
|
|
|
a.close()
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-18 21:36:53 +00:00
|
|
|
|
2009-12-16 19:03:15 +00:00
|
|
|
a = @@coll.find({}, :limit => 10).next_document
|
2009-08-18 21:36:53 +00:00
|
|
|
|
2009-08-19 15:51:30 +00:00
|
|
|
assert_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-19 15:51:30 +00:00
|
|
|
|
|
|
|
@@coll.find() do |cursor|
|
2009-12-16 19:03:15 +00:00
|
|
|
cursor.next_document
|
2009-08-19 15:51:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-19 15:51:30 +00:00
|
|
|
|
|
|
|
@@coll.find() { |cursor|
|
2009-12-16 19:03:15 +00:00
|
|
|
cursor.next_document
|
2009-08-19 15:51:30 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 21:36:53 +00:00
|
|
|
assert_equal(client_cursors,
|
2009-11-23 20:20:05 +00:00
|
|
|
@@db.command("cursorInfo" => 1)["clientCursors_size"])
|
2009-08-18 21:36:53 +00:00
|
|
|
end
|
2009-08-24 21:21:49 +00:00
|
|
|
|
|
|
|
def test_count_with_fields
|
2009-10-20 15:31:07 +00:00
|
|
|
@@coll.remove
|
2009-08-24 21:21:49 +00:00
|
|
|
@@coll.save("x" => 1)
|
|
|
|
|
2009-10-26 18:54:33 +00:00
|
|
|
if @@version < "1.1.3"
|
|
|
|
assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
|
|
|
|
else
|
|
|
|
assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
|
2009-08-24 21:21:49 +00:00
|
|
|
end
|
|
|
|
end
|
2010-03-19 18:31:31 +00:00
|
|
|
|
|
|
|
def test_has_next
|
|
|
|
@@coll.remove
|
|
|
|
200.times do |n|
|
|
|
|
@@coll.save("x" => n)
|
|
|
|
end
|
|
|
|
|
|
|
|
cursor = @@coll.find
|
2011-03-15 18:24:34 +00:00
|
|
|
n = 0
|
2010-03-19 18:31:31 +00:00
|
|
|
while cursor.has_next?
|
2011-03-15 18:24:34 +00:00
|
|
|
assert cursor.next
|
|
|
|
n += 1
|
2010-03-19 18:31:31 +00:00
|
|
|
end
|
|
|
|
|
2011-03-15 18:24:34 +00:00
|
|
|
assert_equal n, 200
|
2010-03-19 18:31:31 +00:00
|
|
|
assert_equal false, cursor.has_next?
|
|
|
|
end
|
2010-07-12 16:11:01 +00:00
|
|
|
|
|
|
|
def test_cursor_invalid
|
|
|
|
@@coll.remove
|
2010-08-02 22:19:54 +00:00
|
|
|
10000.times do |n|
|
2010-07-12 16:11:01 +00:00
|
|
|
@@coll.insert({:a => n})
|
|
|
|
end
|
|
|
|
|
|
|
|
cursor = @@coll.find({})
|
|
|
|
|
2010-08-02 22:19:54 +00:00
|
|
|
assert_raise_error Mongo::OperationFailure, "CURSOR_NOT_FOUND" do
|
|
|
|
9999.times do
|
2010-07-12 16:11:01 +00:00
|
|
|
cursor.next_document
|
2010-08-02 22:19:54 +00:00
|
|
|
cursor.instance_variable_set(:@cursor_id, 1234567890)
|
2010-07-12 16:11:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-08-02 22:19:54 +00:00
|
|
|
|
2010-08-26 16:35:42 +00:00
|
|
|
def test_enumberables
|
|
|
|
@@coll.remove
|
|
|
|
100.times do |n|
|
|
|
|
@@coll.insert({:a => n})
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 100, @@coll.find.to_a.length
|
|
|
|
assert_equal 100, @@coll.find.to_set.length
|
|
|
|
|
|
|
|
cursor = @@coll.find
|
|
|
|
50.times { |n| cursor.next_document }
|
|
|
|
assert_equal 50, cursor.to_a.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rewind
|
|
|
|
@@coll.remove
|
|
|
|
100.times do |n|
|
|
|
|
@@coll.insert({:a => n})
|
|
|
|
end
|
|
|
|
|
|
|
|
cursor = @@coll.find
|
|
|
|
cursor.to_a
|
|
|
|
assert_equal [], cursor.map {|doc| doc }
|
|
|
|
|
|
|
|
cursor.rewind!
|
|
|
|
assert_equal 100, cursor.map {|doc| doc }.length
|
|
|
|
|
|
|
|
cursor.rewind!
|
|
|
|
5.times { cursor.next_document }
|
|
|
|
cursor.rewind!
|
|
|
|
assert_equal 100, cursor.map {|doc| doc }.length
|
|
|
|
end
|
2010-09-09 18:12:12 +00:00
|
|
|
|
2011-03-18 19:03:37 +00:00
|
|
|
def test_transformer
|
|
|
|
transformer = Proc.new { |doc| doc }
|
|
|
|
cursor = Cursor.new(@@coll, :transformer => transformer)
|
|
|
|
assert_equal(transformer, cursor.transformer)
|
2010-09-09 18:12:12 +00:00
|
|
|
end
|
2011-03-18 19:03:37 +00:00
|
|
|
|
|
|
|
def test_instance_transformation_with_next
|
|
|
|
klass = Struct.new(:id, :a)
|
|
|
|
transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
|
|
|
|
cursor = Cursor.new(@@coll, :transformer => transformer)
|
|
|
|
instance = cursor.next
|
|
|
|
|
|
|
|
assert_instance_of(klass, instance)
|
|
|
|
assert_instance_of(BSON::ObjectId, instance.id)
|
|
|
|
assert_equal(1, instance.a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_instance_transformation_with_each
|
|
|
|
klass = Struct.new(:id, :a)
|
|
|
|
transformer = Proc.new { |doc| klass.new(doc['_id'], doc['a']) }
|
|
|
|
cursor = Cursor.new(@@coll, :transformer => transformer)
|
|
|
|
|
|
|
|
cursor.each do |instance|
|
|
|
|
assert_instance_of(klass, instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|