spec/mongomodel/support/scope_spec.rb in mongomodel-0.2.14 vs spec/mongomodel/support/scope_spec.rb in mongomodel-0.2.15
- old
+ new
@@ -1,6 +1,7 @@
require 'spec_helper'
+require 'active_support/time'
module MongoModel
describe Scope do
define_class(:Post, Document) do
property :published, Boolean, :default => false
@@ -314,51 +315,82 @@
subject.from(NotAPost.collection.name).collection.name.should == NotAPost.collection.name
end
end
describe "#first" do
- context "when no matching documents exist" do
- before(:each) { model.stub_find([]) }
+ context "with count argument" do
+ context "when no matching documents exist" do
+ before(:each) { model.stub_find([]) }
- always do
- it "should return nil" do
- subject.first.should be_nil
+ always do
+ it "should return an empty array" do
+ subject.first(3).should == []
+ end
end
- end
- subject_loaded do
- it "should not perform a find" do
- model.should_not_find do
- subject.first
+ subject_loaded do
+ it "should not perform a find" do
+ model.should_not_find { subject.first(3) }
end
end
+
+ subject_not_loaded do
+ it "should find with a limit of 3" do
+ model.should_find(finder_options.merge(:limit => 3), []) { subject.first(3) }
+ end
+ end
end
+
+ context "when matching documents exist" do
+ before(:each) { model.stub_find([posts[0], posts[1], posts[2]]) }
- subject_not_loaded do
- it "should find with a limit of 1" do
- model.should_find(finder_options.merge(:limit => 1), []) do
- subject.first
+ always do
+ it "should return the first documents in an array" do
+ subject.first(3).should == [posts[0], posts[1], posts[2]]
end
end
end
end
- context "when matching documents exist" do
- let(:post) { posts.first }
- before(:each) { model.stub_find([post]) }
+ context "with no argument" do
+ context "when no matching documents exist" do
+ before(:each) { model.stub_find([]) }
- always do
- it "should return the first document" do
- subject.first.should == post
+ always do
+ it "should return nil" do
+ subject.first.should be_nil
+ end
end
+
+ subject_loaded do
+ it "should not perform a find" do
+ model.should_not_find { subject.first }
+ end
+ end
+
+ subject_not_loaded do
+ it "should find with a limit of 1" do
+ model.should_find(finder_options.merge(:limit => 1), []) { subject.first }
+ end
+ end
end
+
+ context "when matching documents exist" do
+ before(:each) { model.stub_find([posts[0]]) }
+
+ always do
+ it "should return the first document" do
+ subject.first.should == posts[0]
+ end
+ end
- subject_not_loaded do
- it "should cache find result" do
- model.should_find(finder_options.merge(:limit => 1), [post]) do
- subject.first
- subject.first
+ subject_not_loaded do
+ it "should cache find result" do
+ model.should_find(finder_options.merge(:limit => 1), [posts[0]]) do
+ subject.first
+ subject.first
+ end
end
end
end
end
end
@@ -367,50 +399,82 @@
def reversed_finder_options
order = MongoModel::MongoOrder.parse(finder_options[:order] || [:id.asc])
finder_options.merge(:order => order.reverse.to_a)
end
- context "when no matching documents exist" do
- before(:each) { model.stub_find([]) }
+ context "with count argument" do
+ context "when no matching documents exist" do
+ before(:each) { model.stub_find([]) }
- always do
- it "should return nil" do
- subject.last.should be_nil
+ always do
+ it "should return an empty array" do
+ subject.last(2).should == []
+ end
end
- end
- subject_loaded do
- it "should not perform a find" do
- model.should_not_find do
- subject.last
+ subject_loaded do
+ it "should not perform a find" do
+ model.should_not_find { subject.last(2) }
end
end
+
+ subject_not_loaded do
+ it "should find with a limit of 2" do
+ model.should_find(reversed_finder_options.merge(:limit => 2), []) { subject.last(2) }
+ end
+ end
end
+
+ context "when matching documents exist" do
+ before(:each) { model.stub_find([posts[0], posts[1]]) }
- subject_not_loaded do
- it "should find with a limit of 1" do
- model.should_find(reversed_finder_options.merge(:limit => 1), []) do
- subject.last
+ always do
+ it "should return the last documents in an array" do
+ subject.last(2).should == [posts[0], posts[1]]
end
end
end
end
- context "when matching documents exist" do
- let(:post) { posts.last }
- before(:each) { model.stub_find([post]) }
+ context "with no argument" do
+ context "when no matching documents exist" do
+ before(:each) { model.stub_find([]) }
- always do
- it "should return the last document" do
- subject.last.should == post
+ always do
+ it "should return nil" do
+ subject.last.should be_nil
+ end
end
+
+ subject_loaded do
+ it "should not perform a find" do
+ model.should_not_find { subject.last }
+ end
+ end
+
+ subject_not_loaded do
+ it "should find with a limit of 1" do
+ model.should_find(reversed_finder_options.merge(:limit => 1), []) { subject.last }
+ end
+ end
end
+
+ context "when matching documents exist" do
+ let(:post) { posts.last }
+ before(:each) { model.stub_find([post]) }
+
+ always do
+ it "should return the last document" do
+ subject.last.should == post
+ end
+ end
- subject_not_loaded do
- it "should cache find result" do
- model.should_find(reversed_finder_options.merge(:limit => 1), [post]) do
- subject.last
- subject.last
+ subject_not_loaded do
+ it "should cache find result" do
+ model.should_find(reversed_finder_options.merge(:limit => 1), [post]) do
+ subject.last
+ subject.last
+ end
end
end
end
end
end