test/plucky/test_criteria_hash.rb in plucky-0.4.4 vs test/plucky/test_criteria_hash.rb in plucky-0.5.0
- old
+ new
@@ -22,10 +22,56 @@
CriteriaHash.new(:age.gt => 12, :age.lt => 20)[:age].should == {
'$gt' => 12, '$lt' => 20
}
end
+ context "nested clauses" do
+ context "::NestingOperators" do
+ should "return array of operators that take nested queries" do
+ CriteriaHash::NestingOperators.should == [:$or, :$and, :$nor]
+ end
+ end
+
+ CriteriaHash::NestingOperators.each do |operator|
+ context "#{operator}" do
+ should "work with symbol operators" do
+ nested1 = {:age.gt => 12, :age.lt => 20}
+ translated1 = {:age => {'$gt' => 12, '$lt' => 20 }}
+ nested2 = {:type.nin => ['friend', 'enemy']}
+ translated2 = {:type => {'$nin' => ['friend', 'enemy']}}
+
+ given = {operator.to_s => [nested1, nested2]}
+
+ CriteriaHash.new(given)[operator].should == [translated1, translated2]
+ end
+
+ should "honor criteria hash options" do
+ nested = {:post_id => '4f5ead6378fca23a13000001'}
+ translated = {:post_id => BSON::ObjectId.from_string('4f5ead6378fca23a13000001')}
+ given = {operator.to_s => [nested]}
+
+ CriteriaHash.new(given, :object_ids => [:post_id])[operator].should == [translated]
+ end
+ end
+ end
+
+ context "doubly nested" do
+ should "work with symbol operators" do
+ nested1 = {:age.gt => 12, :age.lt => 20}
+ translated1 = {:age => {'$gt' => 12, '$lt' => 20}}
+ nested2 = {:type.nin => ['friend', 'enemy']}
+ translated2 = {:type => {'$nin' => ['friend', 'enemy']}}
+ nested3 = {'$and' => [nested2]}
+ translated3 = {:$and => [translated2]}
+
+ given = {'$or' => [nested1, nested3]}
+
+ CriteriaHash.new(given)[:$or].should == [translated1, translated3]
+ end
+ end
+ end
+
context "#initialize_copy" do
setup do
@original = CriteriaHash.new({
:comments => {:_id => 1}, :tags => ['mongo', 'ruby'],
}, :object_ids => [:_id])
@@ -156,10 +202,14 @@
end
should "not turn value to $in with $and key" do
CriteriaHash.new(:$and => [{:numbers => 1}, {:numbers => 2}] )[:$and].should == [{:numbers=>1}, {:numbers=>2}]
end
+
+ should "not turn value to $in with $nor key" do
+ CriteriaHash.new(:$nor => [{:numbers => 1}, {:numbers => 2}] )[:$nor].should == [{:numbers=>1}, {:numbers=>2}]
+ end
end
context "with set value" do
should "default to $in and convert to array" do
CriteriaHash.new(:numbers => [1,2,3].to_set)[:numbers].should == {'$in' => [1,2,3]}
@@ -208,16 +258,21 @@
context "with string ids for object id keys (nested)" do
setup do
@id1 = BSON::ObjectId.new
@id2 = BSON::ObjectId.new
- @criteria = CriteriaHash.new({:_id => {'$in' => [@id1.to_s, @id2.to_s]}}, :object_ids => [:_id])
+ @ids = [@id1.to_s, @id2.to_s]
+ @criteria = CriteriaHash.new({:_id => {'$in' => @ids}}, :object_ids => [:_id])
end
should "convert strings to object ids" do
@criteria[:_id].should == {'$in' => [@id1, @id2]}
end
+
+ should "not modify original array of string ids" do
+ @ids.should == [@id1.to_s, @id2.to_s]
+ end
end
context "#merge" do
should "work when no keys match" do
c1 = CriteriaHash.new(:foo => 'bar')
@@ -295,6 +350,6 @@
should "be false if querying only by _type" do
CriteriaHash.new(:_type => 'Foo').should_not be_simple
end
end
end
-end
\ No newline at end of file
+end