spec/unit/mongoid/contexts/mongo_spec.rb in mongoid-1.2.7 vs spec/unit/mongoid/contexts/mongo_spec.rb in mongoid-1.2.8
- old
+ new
@@ -71,11 +71,17 @@
let(:options) { { :skip => 20 } }
before do
@cursor = stub(:count => 500)
@collection = mock
- @klass = stub(:collection => @collection, :hereditary => false, :instantiate => @person)
+ @klass = stub(
+ :collection => @collection,
+ :hereditary => false,
+ :instantiate => @person,
+ :enslaved? => false,
+ :cached? => false
+ )
@criteria = Mongoid::Criteria.new(@klass)
@criteria.where(selector).skip(20)
@context = Mongoid::Contexts::Mongo.new(@criteria)
end
@@ -149,32 +155,65 @@
describe ".initialize" do
let(:selector) { { :field => "value" } }
let(:options) { { :skip => 20 } }
let(:klass) { Person }
+ let(:criteria) { Mongoid::Criteria.new(klass) }
+ let(:context) { Mongoid::Contexts::Mongo.new(criteria) }
before do
- @criteria = Mongoid::Criteria.new(klass)
- @criteria.where(selector).skip(20)
- @context = Mongoid::Contexts::Mongo.new(@criteria)
+ criteria.where(selector).skip(20)
end
it "sets the selector" do
- @context.selector.should == @criteria.selector
+ context.selector.should == criteria.selector
end
it "sets the options" do
- @context.options.should == options
+ context.options.should == options
end
it "sets the klass" do
- @context.klass.should == klass
+ context.klass.should == klass
end
- it "set the selector to query across the _type of the Criteria's klass when it is hereditary" do
- @context.selector[:_type].should == {'$in' => Person._types}
+ context "when persisting type" do
+
+ it "set the selector to query across the _type when it is hereditary" do
+ context.selector[:_type].should == {'$in' => Person._types}
+ end
+
end
+
+ context "when not persisting type" do
+
+ before do
+ Mongoid.persist_types = false
+ end
+
+ after do
+ Mongoid.persist_types = true
+ end
+
+ it "does not add the type to the selector" do
+ context.selector[:_type].should be_nil
+ end
+
+ end
+
+ context "enslaved and cached classes" do
+
+ let(:klass) { Game }
+
+ it "enslaves the criteria" do
+ context.criteria.should be_enslaved
+ end
+
+ it "caches the criteria" do
+ context.criteria.should be_cached
+ end
+ end
end
describe "#last" do
before do
@@ -234,11 +273,11 @@
end
describe "#max" do
before do
- @reduce = Mongoid::Contexts::Mongo::MAX_REDUCE.gsub("[field]", "age")
+ @reduce = Mongoid::Javascript.max.gsub("[field]", "age")
@collection = mock
Person.expects(:collection).returns(@collection)
@criteria = Mongoid::Criteria.new(Person)
@context = Mongoid::Contexts::Mongo.new(@criteria)
end
@@ -257,11 +296,11 @@
end
describe "#min" do
before do
- @reduce = Mongoid::Contexts::Mongo::MIN_REDUCE.gsub("[field]", "age")
+ @reduce = Mongoid::Javascript.min.gsub("[field]", "age")
@collection = mock
Person.expects(:collection).returns(@collection)
@criteria = Mongoid::Criteria.new(Person)
@context = Mongoid::Contexts::Mongo.new(@criteria)
end
@@ -369,10 +408,10 @@
describe "#sum" do
context "when klass not provided" do
before do
- @reduce = Mongoid::Contexts::Mongo::SUM_REDUCE.gsub("[field]", "age")
+ @reduce = Mongoid::Javascript.sum.gsub("[field]", "age")
@collection = mock
@criteria = Mongoid::Criteria.new(Person)
@context = Mongoid::Contexts::Mongo.new(@criteria)
Person.expects(:collection).returns(@collection)
end