Permalink
Browse files

added DM specs and loaded dm or ar finders based on Merb's orm in use.

  • Loading branch information...
1 parent 3d58c05 commit 5e165efefafc9ef0b31a750a1022d5d5ff9f22f8 @mattetti mattetti committed with mislav Oct 27, 2008
Showing with 86 additions and 0 deletions.
  1. +2 −0 lib/will_paginate.rb
  2. +64 −0 spec/finders/data_mapper_spec.rb
  3. +20 −0 spec/finders/data_mapper_test_connector.rb
View
@@ -40,4 +40,6 @@ def self.enable_named_scope(patch = true)
require 'will_paginate/view_helpers/link_renderer'
# this only includes will_paginate view stuff in Merb (not finder adapters)
Merb::AbstractController.send(:include, WillPaginate::ViewHelpers::Base)
+ adapter = { :datamapper => 'data_mapper', :activerecord => 'active_record' }[Merb.orm]
+ require "will_paginate/finders/#{adapter}"
end
@@ -0,0 +1,64 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'will_paginate/finders/data_mapper'
+require File.dirname(__FILE__) + '/data_mapper_test_connector'
+
+require 'will_paginate'
+
+describe WillPaginate::Finders::DataMapper do
+
+ it "should make #paginate available to DM resource classes" do
+ Animal.should respond_to(:paginate)
+ end
+
+ it "should paginate" do
+ Animal.expects(:all).with(:limit => 5, :offset => 0).returns([])
+ Animal.paginate(:page => 1, :per_page => 5)
+ end
+
+ it "should NOT to paginate_by_sql" do
+ Animal.should_not respond_to(:paginate_by_sql)
+ end
+
+ it "should support explicit :all argument" do
+ Animal.expects(:all).with(instance_of(Hash)).returns([])
+ Animal.paginate(:all, :page => nil)
+ end
+
+ it "should support conditional pagination" do
+ filtered_result = Animal.paginate(:all, :name => 'Dog', :page => nil)
+ filtered_result.size.should == 1
+ filtered_result.first.should == Animal.first(:name => 'Dog')
+ end
+
+
+ it "should leave extra parameters intact" do
+ Animal.expects(:all).with(:name => 'Dog', :limit => 4, :offset => 0 ).returns(Array.new(5))
+ Animal.expects(:count).with({:name => 'Dog'}).returns(1)
+
+ Animal.paginate :name => 'Dog', :page => 1, :per_page => 4
+ end
+
+ describe "counting" do
+ it "should ignore nil in :count parameter" do
+ lambda { Animal.paginate :page => nil, :count => nil }.should_not raise_error
+ end
+
+ it "should guess the total count" do
+ Animal.expects(:all).returns(Array.new(2))
+ Animal.expects(:count).never
+
+ result = Animal.paginate :page => 2, :per_page => 4
+ result.total_entries.should == 6
+ end
+
+ it "should guess that there are no records" do
+ Animal.expects(:all).returns([])
+ Animal.expects(:count).never
+
+ result = Animal.paginate :page => 1, :per_page => 4
+ result.total_entries.should == 0
+ end
+ end
+
+
+end
@@ -0,0 +1,20 @@
+require 'dm-core'
+DataMapper.setup :default, 'sqlite3::memory:'
+
+# Define models
+class Animal
+ include DataMapper::Resource
+ property :id, Serial
+ property :name, String
+ property :notes, Text
+
+ def self.setup
+ Animal.create(:name => 'Dog', :notes => "Man's best friend")
+ Animal.create(:name => 'Cat', :notes => "Woman's best friend")
+ Animal.create(:name => 'Lion', :notes => 'King of the Jungle')
+ end
+end
+
+# Load fixtures
+Animal.auto_migrate!
+Animal.setup

0 comments on commit 5e165ef

Please sign in to comment.