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
|
|
|
|
|
|
|
|
include XGen::Mongo::Driver
|
|
|
|
|
2009-02-05 15:10:41 +00:00
|
|
|
@@db = Mongo.new(ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
|
|
|
|
ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT).db('ruby-mongo-test')
|
|
|
|
@@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-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
|
|
|
|
|
|
|
|
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
|
|
|
|
1000.times { |i|
|
|
|
|
@@coll.insert('a' => i)
|
|
|
|
}
|
2009-03-11 17:37:42 +00:00
|
|
|
|
|
|
|
assert_equal 1001, @@coll.count
|
2009-03-02 15:49:27 +00:00
|
|
|
count = 0
|
|
|
|
@@coll.find.each { |obj|
|
|
|
|
count += obj['a']
|
|
|
|
}
|
2009-03-11 17:42:19 +00:00
|
|
|
assert_equal 1001, @@coll.count
|
2009-03-09 13:37:33 +00:00
|
|
|
|
|
|
|
# do the same thing again for debugging
|
2009-03-11 17:37:42 +00:00
|
|
|
assert_equal 1001, @@coll.count
|
2009-03-09 13:37:33 +00:00
|
|
|
count2 = 0
|
|
|
|
@@coll.find.each { |obj|
|
|
|
|
count2 += obj['a']
|
|
|
|
}
|
2009-03-11 17:42:19 +00:00
|
|
|
assert_equal 1001, @@coll.count
|
2009-03-09 13:37:33 +00:00
|
|
|
|
|
|
|
assert_equal count, count2
|
2009-03-02 15:49:27 +00:00
|
|
|
assert_equal 499501, count
|
|
|
|
end
|
|
|
|
|
2009-03-09 14:21:08 +00:00
|
|
|
def test_refill_via_get_more_alt_coll
|
|
|
|
coll = @@db.collection('test-alt-coll')
|
|
|
|
coll.clear
|
|
|
|
coll.insert('a' => 1) # collection not created until it's used
|
|
|
|
|
|
|
|
1000.times { |i|
|
|
|
|
coll.insert('a' => i)
|
|
|
|
}
|
2009-03-11 17:37:42 +00:00
|
|
|
|
|
|
|
assert_equal 1001, coll.count
|
2009-03-09 14:21:08 +00:00
|
|
|
count = 0
|
|
|
|
coll.find.each { |obj|
|
|
|
|
count += obj['a']
|
|
|
|
}
|
2009-03-11 17:42:19 +00:00
|
|
|
assert_equal 1001, coll.count
|
2009-03-09 14:21:08 +00:00
|
|
|
|
|
|
|
# do the same thing again for debugging
|
2009-03-11 17:37:42 +00:00
|
|
|
assert_equal 1001, coll.count
|
2009-03-09 14:21:08 +00:00
|
|
|
count2 = 0
|
|
|
|
coll.find.each { |obj|
|
|
|
|
count2 += obj['a']
|
|
|
|
}
|
2009-03-11 17:42:19 +00:00
|
|
|
assert_equal 1001, coll.count
|
2009-03-09 14:21:08 +00:00
|
|
|
|
|
|
|
assert_equal count, count2
|
|
|
|
assert_equal 499501, count
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|