File: data_mapper_spec.rb

package info (click to toggle)
ruby-will-paginate 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 444 kB
  • sloc: ruby: 2,940; sh: 50; makefile: 2
file content (117 lines) | stat: -rw-r--r-- 3,361 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require_relative './spec_helper'
require 'will_paginate/data_mapper'
require_relative './data_mapper_test_connector'

describe "WillPaginate::DataMapper" do

  before(:all) do
    DataMapper.setup :default, 'sqlite3::memory:'
    [Animal, Ownership, Human].each do |klass|
      klass.auto_migrate!
    end

    Animal.create(:name => 'Dog', :notes => 'a friend of all')
    Animal.create(:name => 'Cat', :notes => 'a friend or foe')
    Animal.create(:name => 'Lion', :notes => 'some roar')
  end

  it "has per_page" do
    Animal.per_page.should == 30
    begin
      Animal.per_page = 10
      Animal.per_page.should == 10

      subclass = Class.new(Animal)
      subclass.per_page.should == 10
    ensure
      Animal.per_page = 30
    end
  end

  it "doesn't make normal collections appear paginated" do
    Animal.all.should_not be_paginated
  end

  it "paginates to first page by default" do
    animals = Animal.paginate(:page => nil)

    animals.should be_paginated
    animals.current_page.should == 1
    animals.per_page.should == 30
    animals.offset.should == 0
    animals.total_entries.should == 3
    animals.total_pages.should == 1
  end

  it "paginates to first page, explicit limit" do
    animals = Animal.paginate(:page => 1, :per_page => 2)

    animals.current_page.should == 1
    animals.per_page.should == 2
    animals.total_entries.should == 3
    animals.total_pages.should == 2
    animals.map {|a| a.name }.should == %w[ Dog Cat ]
  end

  it "paginates to second page" do
    animals = Animal.paginate(:page => 2, :per_page => 2)

    animals.current_page.should == 2
    animals.offset.should == 2
    animals.map {|a| a.name }.should == %w[ Lion ]
  end

  it "paginates a collection" do
    friends = Animal.all(:notes.like => '%friend%')
    friends.paginate(:page => 1).per_page.should == 30
    friends.paginate(:page => 1, :per_page => 1).total_entries.should == 2
  end

  it "paginates a limited collection" do
    animals = Animal.all(:limit => 2).paginate(:page => 1)
    animals.per_page.should == 2
  end

  it "has page() method" do
    Animal.page(2).per_page.should == 30
    Animal.page(2).offset.should == 30
    Animal.page(2).current_page.should == 2
    Animal.all(:limit => 2).page(2).per_page.should == 2
  end

  it "has total_pages at 1 for empty collections" do
    Animal.all(:conditions => ['1=2']).page(1).total_pages.should == 1
  end

  it "overrides total_entries count with a fixed value" do
    animals = Animal.paginate :page => 1, :per_page => 3, :total_entries => 999
    animals.total_entries.should == 999
  end

  it "supports a non-int for total_entries" do
    topics = Animal.paginate :page => 1, :per_page => 3, :total_entries => "999"
    topics.total_entries.should == 999
  end


  it "can iterate and then call WP methods" do
    animals = Animal.all(:limit => 2).page(1)
    animals.each { |a| }
    animals.total_entries.should == 3
  end

  it "augments to_a to return a WP::Collection" do
    animals = Animal.all(:limit => 2).page(1)
    array = animals.to_a
    array.size.should == 2
    array.should be_kind_of(WillPaginate::Collection)
    array.current_page.should == 1
    array.per_page.should == 2
  end

  it "doesn't have a problem assigning has-one-through relationship" do
    human = Human.create :name => "Mislav"
    human.pet = Animal.first
  end

end