Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixture-independent specs in active_record_spec
- Loading branch information
Showing
16 changed files
with
139 additions
and
130 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
require 'spec_helper' | ||
require 'will_paginate/finders/active_record' | ||
require File.dirname(__FILE__) + '/activerecord_test_connector' | ||
|
||
class ArProject < ActiveRecord::Base | ||
end | ||
|
||
ActiverecordTestConnector.setup | ||
|
||
describe WillPaginate::Finders::ActiveRecord do | ||
|
||
before :all do | ||
# Fixtures.create_fixtures ActiverecordTestConnector::FIXTURES_PATH, %w() | ||
end | ||
|
||
it "should integrate with ActiveRecord::Base" do | ||
ActiveRecord::Base.should respond_to(:paginate) | ||
end | ||
|
||
it "should paginate" do | ||
ArProject.expects(:find).with(:all, { :limit => 5, :offset => 0 }).returns([]) | ||
ArProject.paginate(:page => 1, :per_page => 5) | ||
end | ||
|
||
it "should respond to paginate_by_sql" do | ||
ArProject.should respond_to(:paginate_by_sql) | ||
end | ||
|
||
it "should support explicit :all argument" do | ||
ArProject.expects(:find).with(:all, instance_of(Hash)).returns([]) | ||
ArProject.paginate(:all, :page => nil) | ||
end | ||
|
||
it "should put implicit all in dynamic finders" do | ||
ArProject.expects(:find_all_by_foo).returns([]) | ||
ArProject.expects(:count).returns(0) | ||
ArProject.paginate_by_foo :page => 2 | ||
end | ||
|
||
it "should leave extra parameters intact" do | ||
ArProject.expects(:find).with(:all, {:foo => 'bar', :limit => 4, :offset => 0 }).returns(Array.new(5)) | ||
ArProject.expects(:count).with({:foo => 'bar'}).returns(1) | ||
|
||
ArProject.paginate :foo => 'bar', :page => 1, :per_page => 4 | ||
end | ||
|
||
describe "counting" do | ||
it "should ignore nil in :count parameter" do | ||
ArProject.expects(:find).returns([]) | ||
lambda { ArProject.paginate :page => nil, :count => nil }.should_not raise_error | ||
end | ||
|
||
it "should guess the total count" do | ||
ArProject.expects(:find).returns(Array.new(2)) | ||
ArProject.expects(:count).never | ||
|
||
result = ArProject.paginate :page => 2, :per_page => 4 | ||
result.total_entries.should == 6 | ||
end | ||
|
||
it "should guess that there are no records" do | ||
ArProject.expects(:find).returns([]) | ||
ArProject.expects(:count).never | ||
|
||
result = ArProject.paginate :page => 1, :per_page => 4 | ||
result.total_entries.should == 0 | ||
end | ||
end | ||
|
||
it "should not ignore :select parameter when it says DISTINCT" do | ||
ArProject.stubs(:find).returns([]) | ||
ArProject.expects(:count).with(:select => 'DISTINCT salary').returns(0) | ||
ArProject.paginate :select => 'DISTINCT salary', :page => 2 | ||
end | ||
|
||
it "should use :with_foo for scope-out compatibility" do | ||
ArProject.expects(:find_best).returns(Array.new(5)) | ||
ArProject.expects(:with_best).returns(1) | ||
|
||
ArProject.paginate_best :page => 1, :per_page => 4 | ||
end | ||
|
||
describe "paginate_by_sql" do | ||
it "should paginate" do | ||
ArProject.expects(:find_by_sql).with(regexp_matches(/sql LIMIT 3(,| OFFSET) 3/)).returns([]) | ||
ArProject.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql) AS count_table').returns(0) | ||
|
||
ArProject.paginate_by_sql 'sql', :page => 2, :per_page => 3 | ||
end | ||
|
||
it "should respect total_entrier setting" do | ||
ArProject.expects(:find_by_sql).returns([]) | ||
ArProject.expects(:count_by_sql).never | ||
|
||
entries = ArProject.paginate_by_sql 'sql', :page => 1, :total_entries => 999 | ||
entries.total_entries.should == 999 | ||
end | ||
|
||
it "should strip the order when counting" do | ||
ArProject.expects(:find_by_sql).returns([]) | ||
ArProject.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0) | ||
|
||
ArProject.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 2 | ||
end | ||
end | ||
|
||
# TODO: counts would still be wrong! | ||
it "should be able to paginate custom finders" do | ||
# acts_as_taggable defines find_tagged_with(tag, options) | ||
ArProject.expects(:find_tagged_with).with('will_paginate', :offset => 5, :limit => 5).returns([]) | ||
ArProject.expects(:count).with({}).returns(0) | ||
|
||
ArProject.paginate_tagged_with 'will_paginate', :page => 2, :per_page => 5 | ||
end | ||
|
||
it "should not skip count when given an array argument to a finder" do | ||
ids = (1..8).to_a | ||
ArProject.expects(:find_all_by_id).returns([]) | ||
ArProject.expects(:count).returns(0) | ||
|
||
ArProject.paginate_by_id(ids, :per_page => 3, :page => 2, :order => 'id') | ||
end | ||
|
||
it "doesn't mangle options" do | ||
ArProject.expects(:find).returns([]) | ||
options = { :page => 1 } | ||
options.expects(:delete).never | ||
options_before = options.dup | ||
|
||
ArProject.paginate(options) | ||
options.should == options_before | ||
end | ||
|
||
if ActiverecordTestConnector.able_to_connect | ||
# fixture-dependent tests here | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters