2009-01-13 19:02:16 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'mongo'
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
# NOTE: assumes Mongo is running
|
|
|
|
class CursorTest < Test::Unit::TestCase
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2009-08-20 22:48:09 +00:00
|
|
|
@@db = Connection.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
|
|
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT).db('ruby-mongo-test')
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll = @@db.collection('test')
|
|
|
|
|
2009-01-13 19:02:16 +00:00
|
|
|
def setup
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll.clear
|
|
|
|
@@coll.insert('a' => 1) # collection not created until it's used
|
|
|
|
@@coll_full_name = 'ruby-mongo-test.test'
|
2009-01-13 19:02:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-02-05 15:10:41 +00:00
|
|
|
@@coll.clear
|
2009-03-13 21:09:19 +00:00
|
|
|
@@db.error
|
2009-01-13 19:02:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
@@coll.clear
|
|
|
|
|
|
|
|
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()
|
|
|
|
assert_equal 10, @@coll.find({}, :offset => 5).count()
|
|
|
|
|
|
|
|
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-05 18:25:49 +00:00
|
|
|
def test_limit
|
|
|
|
@@coll.clear
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_limit_exceptions
|
2009-09-16 21:52:41 +00:00
|
|
|
assert_raise ArgumentError do
|
2009-09-05 18:25:49 +00:00
|
|
|
cursor = @@coll.find().limit('not-an-integer')
|
|
|
|
end
|
|
|
|
|
|
|
|
cursor = @@coll.find()
|
|
|
|
firstResult = cursor.next_object()
|
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-09-05 18:25:49 +00:00
|
|
|
@@coll.clear
|
|
|
|
|
|
|
|
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
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
cursor = @@coll.find().skip('not-an-integer')
|
2009-09-05 18:25:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
cursor = @@coll.find()
|
|
|
|
firstResult = cursor.next_object()
|
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-09-05 18:25:49 +00:00
|
|
|
@@coll.clear
|
|
|
|
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-03-13 19:05:49 +00:00
|
|
|
begin
|
|
|
|
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
|
|
|
|
rescue Test::Unit::AssertionFailedError => ex
|
|
|
|
p @@db.collection_names
|
|
|
|
Process.exit 1
|
|
|
|
end
|
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-03-13 19:05:49 +00:00
|
|
|
begin
|
|
|
|
coll = @@db.collection('test-alt-coll')
|
|
|
|
coll.clear
|
|
|
|
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
|
|
|
|
rescue Test::Unit::AssertionFailedError => ex
|
|
|
|
p @@db.collection_names
|
|
|
|
Process.exit 1
|
|
|
|
end
|
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-01-13 19:02:16 +00:00
|
|
|
cursor.next_object
|
|
|
|
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
|
|
|
|
|
|
|
|
client_cursors = @@db.db_command("cursorInfo" => 1)["clientCursors_size"]
|
|
|
|
by_location = @@db.db_command("cursorInfo" => 1)["byLocation_size"]
|
|
|
|
|
|
|
|
10000.times do |i|
|
|
|
|
@@coll.insert("i" => i)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
@@coll.find_one()
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
|
|
|
|
10.times do |i|
|
|
|
|
a = @@coll.find()
|
|
|
|
a.next_object()
|
|
|
|
a.close()
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
|
|
|
|
a = @@coll.find()
|
|
|
|
a.next_object()
|
|
|
|
|
|
|
|
assert_not_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_not_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
|
|
|
|
a.close()
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
|
|
|
|
a = @@coll.find({}, :limit => 10).next_object()
|
|
|
|
|
2009-08-19 15:51:30 +00:00
|
|
|
assert_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
|
|
|
|
@@coll.find() do |cursor|
|
|
|
|
cursor.next_object()
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
|
|
|
|
@@coll.find() { |cursor|
|
|
|
|
cursor.next_object()
|
|
|
|
}
|
|
|
|
|
2009-08-18 21:36:53 +00:00
|
|
|
assert_equal(client_cursors,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["clientCursors_size"])
|
|
|
|
assert_equal(by_location,
|
|
|
|
@@db.db_command("cursorInfo" => 1)["byLocation_size"])
|
|
|
|
end
|
2009-08-24 21:21:49 +00:00
|
|
|
|
|
|
|
def test_count_with_fields
|
|
|
|
@@coll.clear
|
|
|
|
@@coll.save("x" => 1)
|
|
|
|
|
|
|
|
@@coll.find({}, :fields => ["a"]).each do |doc|
|
|
|
|
fail "shouldn't have any results here"
|
|
|
|
end
|
|
|
|
assert_equal(0, @@coll.find({}, :fields => ["a"]).count())
|
|
|
|
end
|
2009-01-13 19:02:16 +00:00
|
|
|
end
|