File: mongoid_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 (141 lines) | stat: -rw-r--r-- 4,486 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require_relative './spec_helper'
require 'will_paginate/mongoid'

describe WillPaginate::Mongoid do

  class MongoidModel
    include Mongoid::Document
  end

  before(:all) do
    Mongoid.configure do |config|
      mongodb_host = ENV["MONGODB_HOST"] || "localhost"
      mongodb_port = ENV["MONGODB_PORT"] || "27017"
      config.clients.default = {
        hosts: ["#{mongodb_host}:#{mongodb_port}"],
        database: "will_paginate_test",
      }
      config.log_level = :warn
    end

    MongoidModel.delete_all
    4.times { MongoidModel.create! }
  end

  let(:criteria) { MongoidModel.criteria }

  describe "#page" do
    it "should forward to the paginate method" do
      criteria.expects(:paginate).with(:page => 2).returns("itself")
      criteria.page(2).should == "itself"
    end

    it "should not override per_page if set earlier in the chain" do
      criteria.paginate(:per_page => 10).page(1).per_page.should == 10
      criteria.paginate(:per_page => 20).page(1).per_page.should == 20
    end
  end

  describe "#per_page" do
    it "should set the limit if given an argument" do
      criteria.per_page(10).options[:limit].should == 10
    end

    it "should return the current limit if no argument is given" do
      criteria.per_page.should == nil
      criteria.per_page(10).per_page.should == 10
    end

    it "should be interchangable with limit" do
      criteria.limit(15).per_page.should == 15
    end

    it "should be nil'able" do
      criteria.per_page(nil).per_page.should be_nil
    end
  end

  describe "#paginate" do
    it "should use criteria" do
      criteria.paginate.should be_instance_of(::Mongoid::Criteria)
    end

    it "should not override page number if set earlier in the chain" do
      criteria.page(3).paginate.current_page.should == 3
    end

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

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

    specify "first fallback value for per_page option is the current limit" do
      criteria.limit(12).paginate.options.should include(:limit => 12)
    end

    specify "second fallback value for per_page option is WillPaginate.per_page" do
      criteria.paginate.options.should include(:limit => WillPaginate.per_page)
    end

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

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

    describe "collection compatibility" do
      describe "#total_count" do
        it "should be calculated correctly" do
          criteria.paginate(:per_page => 1).total_entries.should == 4
          criteria.paginate(:per_page => 3).total_entries.should == 4
        end

        it "should be cached" do
          criteria.expects(:count).once.returns(123)
          criteria.paginate
          2.times { criteria.total_entries.should == 123 }
        end
      end

      it "should calculate total_pages" do
        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
        criteria.paginate(:per_page => 1).per_page.should == 1
        criteria.paginate(:per_page => 5).per_page.should == 5
      end

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

        it "should be casted to PageNumber" do
          page = criteria.paginate(:page => 1).current_page
          (page.instance_of? WillPaginate::PageNumber).should be
        end
      end

      it "should return offset" do
        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
        %w(total_entries total_pages current_page).each do |method|
          criteria.should_not respond_to(method)
        end
      end
    end
  end
end