2009-10-22 18:10:12 +00:00
|
|
|
require 'test/test_helper'
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
class CursorTest < Test::Unit::TestCase
|
|
|
|
context "Cursor options" do
|
|
|
|
setup do
|
|
|
|
@connection = stub(:class => Connection)
|
|
|
|
@db = stub(:name => "testing", :slave_ok? => false, :connection => @connection)
|
|
|
|
@collection = stub(:db => @db, :name => "items")
|
|
|
|
@cursor = Cursor.new(@collection)
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set admin to false" do
|
|
|
|
assert_equal false, @cursor.admin
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
@cursor = Cursor.new(@collection, :admin => true)
|
|
|
|
assert_equal true, @cursor.admin
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set selector" do
|
|
|
|
assert @cursor.selector == {}
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
@cursor = Cursor.new(@collection, :selector => {:name => "Jones"})
|
|
|
|
assert @cursor.selector == {:name => "Jones"}
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set fields" do
|
|
|
|
assert_nil @cursor.fields
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
@cursor = Cursor.new(@collection, :fields => [:name, :date])
|
|
|
|
assert @cursor.fields == {:name => 1, :date => 1}
|
|
|
|
end
|
2010-02-20 00:17:38 +00:00
|
|
|
|
2010-03-22 03:33:37 +00:00
|
|
|
should "set mix fields 0 and 1" do
|
|
|
|
assert_nil @cursor.fields
|
|
|
|
|
|
|
|
@cursor = Cursor.new(@collection, :fields => {:name => 1, :date => 0})
|
|
|
|
assert @cursor.fields == {:name => 1, :date => 0}
|
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set limit" do
|
|
|
|
assert_equal 0, @cursor.limit
|
2010-02-20 00:17:38 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
@cursor = Cursor.new(@collection, :limit => 10)
|
|
|
|
assert_equal 10, @cursor.limit
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set skip" do
|
|
|
|
assert_equal 0, @cursor.skip
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
@cursor = Cursor.new(@collection, :skip => 5)
|
|
|
|
assert_equal 5, @cursor.skip
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set sort order" do
|
|
|
|
assert_nil @cursor.order
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
@cursor = Cursor.new(@collection, :order => "last_name")
|
|
|
|
assert_equal "last_name", @cursor.order
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set hint" do
|
|
|
|
assert_nil @cursor.hint
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
@cursor = Cursor.new(@collection, :hint => "name")
|
|
|
|
assert_equal "name", @cursor.hint
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "cache full collection name" do
|
|
|
|
assert_equal "testing.items", @cursor.full_collection_name
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
context "Query fields" do
|
|
|
|
setup do
|
|
|
|
@connection = stub(:class => Collection)
|
|
|
|
@db = stub(:slave_ok? => true, :name => "testing", :connection => @connection)
|
|
|
|
@collection = stub(:db => @db, :name => "items")
|
|
|
|
end
|
|
|
|
|
|
|
|
should "when an array should return a hash with each key" do
|
|
|
|
@cursor = Cursor.new(@collection, :fields => [:name, :age])
|
|
|
|
result = @cursor.fields
|
|
|
|
assert_equal result.keys.sort{|a,b| a.to_s <=> b.to_s}, [:age, :name].sort{|a,b| a.to_s <=> b.to_s}
|
|
|
|
assert result.values.all? {|v| v == 1}
|
|
|
|
end
|
|
|
|
|
|
|
|
should "when a string, return a hash with just the key" do
|
|
|
|
@cursor = Cursor.new(@collection, :fields => "name")
|
|
|
|
result = @cursor.fields
|
|
|
|
assert_equal result.keys.sort, ["name"]
|
|
|
|
assert result.values.all? {|v| v == 1}
|
|
|
|
end
|
|
|
|
|
|
|
|
should "return nil when neither hash nor string nor symbol" do
|
|
|
|
@cursor = Cursor.new(@collection, :fields => 1234567)
|
|
|
|
assert_nil @cursor.fields
|
|
|
|
end
|
2009-10-22 18:10:12 +00:00
|
|
|
end
|
|
|
|
end
|