require File.join(File.dirname(__FILE__), '..', 'test_helper.rb') class PageCollectionTest < Test::Unit::TestCase include SimplePagination context "creating a new instance" do should "require :current_offset or :current_page" do args = {:page_size => 10, :total_records => 100} assert_raise(ArgumentError) {PageCollection.new(args)} assert_nothing_raised {PageCollection.new(args.merge(:current_offset => 0))} assert_nothing_raised {PageCollection.new(args.merge(:current_page => 1))} end should "require :total_records or :total_pages" do args = {:page_size => 10, :current_page => 1} assert_raise(ArgumentError) {PageCollection.new(args)} assert_nothing_raised {PageCollection.new(args.merge(:total_pages => 10))} assert_nothing_raised {PageCollection.new(args.merge(:total_records => 10))} end should "require :page_size" do args = {:total_records => 100, :current_offset => 0} assert_raise(ArgumentError) {PageCollection.new(args)} assert_nothing_raised {PageCollection.new(args.merge(:page_size => 10))} end end context "page_size" do should "provide page_size accessor" do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0) assert_equal 10, @collection.page_size end end context "length" do context "when given total_pages" do should "be equal to total_pages" do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0) assert_equal 10, @collection.length end end context "when given total_records" do should "be zero when total_records is zero" do @collection = PageCollection.new(:total_records => 0, :page_size => 10, :current_offset => 0) assert_equal 0, @collection.length end should "be one if total_records is less than page_size results" do @collection = PageCollection.new(:total_records => 5, :page_size => 10, :current_offset => 0) assert_equal 1, @collection.length end should "be ceil(total_records / page_size)" do @collection = PageCollection.new(:total_records => 21, :page_size => 10, :current_offset => 0) assert_equal 3, @collection.length end end end context "accessing a numbered page" do setup do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page=>1) end should "create and return a new Page instance" do p = Page.new(1, @collection) Page.expects(:new).with(1, @collection).returns(p) assert_same p, @collection[1] end should "return the same Page across multiple invocations" do assert_same @collection[3], @collection[3] assert_not_same @collection[3], @collection[4] end context "when the page is out of bounds" do should "return nil" do assert_equal nil, @collection[100] assert_equal nil, @collection[0] assert_equal nil, @collection[-1] end end end context "current page" do context "when given current_page" do should "provide page with index == current_page" do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 3) @collection.expects(:[]).with(3) @collection.current end end context "when given current_offset" do should "provide page 1 if current_offset is zero" do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0) @collection.expects(:[]).with(1) @collection.current end should "provide page with index == floor(current_offset / page_size) + 1" do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 21) @collection.expects(:[]).with(3) @collection.current end end end context "first page" do should "provide page 1" do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 5) @collection.expects(:[]).with(1) @collection.first end end context "last page" do # this is not an exhaustive test but I shouldn't need to make it so since # the tests for length above do this... should "provide page with index == length" do @collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 5) @collection.expects(:[]).with(10) @collection.last @collection = PageCollection.new(:total_records => 21, :page_size => 10, :current_offset => 0) @collection.expects(:[]).with(3) @collection.last end end end