Skip to content

Commit

Permalink
Clean up mongoid specs by letting criteria be MongoidModel.criteria.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbackeus committed Oct 25, 2011
1 parent 18d5d17 commit a14d97d
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions spec/finders/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class MongoidModel
end

describe "will paginate mongoid" do
let(:criteria) { MongoidModel.criteria }

it "should have the #paginate method" do
MongoidModel.criteria.should respond_to(:paginate)
criteria.should respond_to(:paginate)
end

describe "pagination" do
Expand All @@ -19,62 +21,58 @@ class MongoidModel
end

it "should use criteria" do
MongoidModel.all.paginate.should be_instance_of(::Mongoid::Criteria)
criteria.paginate.should be_instance_of(::Mongoid::Criteria)
end

it "should limit according to per_page parameter" do
criteria = MongoidModel.all.paginate(:per_page => 10)
criteria.options.should include(:limit => 10)
criteria.paginate(:per_page => 10).options.should include(:limit => 10)
end

it "should skip according to page and per_page parameters" do
criteria = MongoidModel.all.paginate(:page => 2, :per_page => 5)
criteria.options.should include(:skip => 5)
criteria.paginate(:page => 2, :per_page => 5).options.should include(:skip => 5)
end

specify "per_page should default to value configured for WillPaginate" do
criteria = MongoidModel.all.paginate
criteria.options.should include(:limit => WillPaginate.per_page)
criteria.paginate.options.should include(:limit => WillPaginate.per_page)
end

specify "page should default to 1" do
MongoidModel.all.paginate.options.should include(:skip => 0)
criteria.paginate.options.should include(:skip => 0)
end

it "should convert strings to integers" do
MongoidModel.all.paginate(:page => "2", :per_page => "3").options.should include(:limit => 3, :limit => 3)
criteria.paginate(:page => "2", :per_page => "3").options.should include(:limit => 3, :limit => 3)
end

describe "collection compatibility" do
it "should calculate total_count" do
MongoidModel.all.paginate(:per_page => 1).total_entries.should == 4
MongoidModel.all.paginate(:per_page => 3).total_entries.should == 4
criteria.paginate(:per_page => 1).total_entries.should == 4
criteria.paginate(:per_page => 3).total_entries.should == 4
end

it "should calculate total_pages" do
MongoidModel.all.paginate(:per_page => 1).total_pages.should == 4
MongoidModel.all.paginate(:per_page => 3).total_pages.should == 2
MongoidModel.all.paginate(:per_page => 10).total_pages.should == 1
criteria.paginate(:per_page => 1).total_pages.should == 4
criteria.paginate(:per_page => 3).total_pages.should == 2
criteria.paginate(:per_page => 10).total_pages.should == 1
end

it "should return per_page" do
MongoidModel.all.paginate(:per_page => 1).per_page.should == 1
MongoidModel.all.paginate(:per_page => 5).per_page.should == 5
criteria.paginate(:per_page => 1).per_page.should == 1
criteria.paginate(:per_page => 5).per_page.should == 5
end

it "should return current_page" do
MongoidModel.all.paginate(:page => 1).current_page.should == 1
MongoidModel.all.paginate(:page => 3).current_page.should == 3
criteria.paginate(:page => 1).current_page.should == 1
criteria.paginate(:page => 3).current_page.should == 3
end

it "should return offset" do
MongoidModel.all.paginate(:page => 1).offset.should == 0
MongoidModel.all.paginate(:page => 2, :per_page => 5).offset.should == 5
MongoidModel.all.paginate(:page => 3, :per_page => 10).offset.should == 20
criteria.paginate(:page => 1).offset.should == 0
criteria.paginate(:page => 2, :per_page => 5).offset.should == 5
criteria.paginate(:page => 3, :per_page => 10).offset.should == 20
end

it "should not pollute plain mongoid criterias" do
criteria = MongoidModel.criteria
%w(total_entries total_pages per_page current_page).each do |method|
criteria.should_not respond_to(method)
end
Expand Down

0 comments on commit a14d97d

Please sign in to comment.