|
| 1 | +require 'test_helper' |
| 2 | +require 'will_paginate' |
| 3 | +require 'will_paginate/collection' |
| 4 | + |
| 5 | +class Elasticsearch::Model::ResponsePaginationWillPaginateTest < Test::Unit::TestCase |
| 6 | + context "Response pagination" do |
| 7 | + class ModelClass |
| 8 | + def self.index_name; 'foo'; end |
| 9 | + def self.document_type; 'bar'; end |
| 10 | + |
| 11 | + # WillPaginate adds this method to models (see WillPaginate::PerPage module) |
| 12 | + def self.per_page |
| 13 | + 33 |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + # Subsclass Response so we can include WillPaginate module without conflicts with Kaminari. |
| 18 | + class WillPaginateResponse < Elasticsearch::Model::Response::Response |
| 19 | + include Elasticsearch::Model::Response::Pagination::WillPaginate |
| 20 | + end |
| 21 | + |
| 22 | + RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, |
| 23 | + 'hits' => { 'total' => 100, 'hits' => (1..100).to_a.map { |i| { _id: i } } } } |
| 24 | + |
| 25 | + setup do |
| 26 | + @search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, '*' |
| 27 | + @response = WillPaginateResponse.new ModelClass, @search, RESPONSE |
| 28 | + @response.klass.stubs(:client).returns mock('client') |
| 29 | + |
| 30 | + @expected_methods = [ |
| 31 | + # methods needed by WillPaginate::CollectionMethods |
| 32 | + :current_page, |
| 33 | + :per_page, |
| 34 | + :total_entries, |
| 35 | + |
| 36 | + # methods defined by WillPaginate::CollectionMethods |
| 37 | + :total_pages, |
| 38 | + :previous_page, |
| 39 | + :next_page, |
| 40 | + :out_of_bounds?, |
| 41 | + ] |
| 42 | + end |
| 43 | + |
| 44 | + should "have pagination methods" do |
| 45 | + assert_respond_to @response, :paginate |
| 46 | + |
| 47 | + @expected_methods.each do |method| |
| 48 | + assert_respond_to @response, method |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + context "response.results" do |
| 53 | + should "have pagination methods" do |
| 54 | + @expected_methods.each do |method| |
| 55 | + assert_respond_to @response.results, method |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context "response.records" do |
| 61 | + should "have pagination methods" do |
| 62 | + @expected_methods.each do |method| |
| 63 | + @response.klass.stubs(:find).returns([]) |
| 64 | + assert_respond_to @response.records, method |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "#paginate method" do |
| 70 | + should "set from/size using defaults" do |
| 71 | + @response.klass.client |
| 72 | + .expects(:search) |
| 73 | + .with do |definition| |
| 74 | + assert_equal 0, definition[:from] |
| 75 | + assert_equal 33, definition[:size] |
| 76 | + end |
| 77 | + .returns(RESPONSE) |
| 78 | + |
| 79 | + assert_nil @response.search.definition[:from] |
| 80 | + assert_nil @response.search.definition[:size] |
| 81 | + |
| 82 | + @response.paginate(page: nil).to_a |
| 83 | + assert_equal 0, @response.search.definition[:from] |
| 84 | + assert_equal 33, @response.search.definition[:size] |
| 85 | + end |
| 86 | + |
| 87 | + should "set from/size using default per_page" do |
| 88 | + @response.klass.client |
| 89 | + .expects(:search) |
| 90 | + .with do |definition| |
| 91 | + assert_equal 33, definition[:from] |
| 92 | + assert_equal 33, definition[:size] |
| 93 | + end |
| 94 | + .returns(RESPONSE) |
| 95 | + |
| 96 | + assert_nil @response.search.definition[:from] |
| 97 | + assert_nil @response.search.definition[:size] |
| 98 | + |
| 99 | + @response.paginate(page: 2).to_a |
| 100 | + assert_equal 33, @response.search.definition[:from] |
| 101 | + assert_equal 33, @response.search.definition[:size] |
| 102 | + end |
| 103 | + |
| 104 | + should "set from/size using custom page and per_page" do |
| 105 | + @response.klass.client |
| 106 | + .expects(:search) |
| 107 | + .with do |definition| |
| 108 | + assert_equal 18, definition[:from] |
| 109 | + assert_equal 9, definition[:size] |
| 110 | + end |
| 111 | + .returns(RESPONSE) |
| 112 | + |
| 113 | + assert_nil @response.search.definition[:from] |
| 114 | + assert_nil @response.search.definition[:size] |
| 115 | + |
| 116 | + @response.paginate(page: 3, per_page: 9).to_a |
| 117 | + assert_equal 18, @response.search.definition[:from] |
| 118 | + assert_equal 9, @response.search.definition[:size] |
| 119 | + end |
| 120 | + |
| 121 | + should "searches for page 1 if specified page is < 1" do |
| 122 | + @response.klass.client |
| 123 | + .expects(:search) |
| 124 | + .with do |definition| |
| 125 | + assert_equal 0, definition[:from] |
| 126 | + assert_equal 33, definition[:size] |
| 127 | + end |
| 128 | + .returns(RESPONSE) |
| 129 | + |
| 130 | + assert_nil @response.search.definition[:from] |
| 131 | + assert_nil @response.search.definition[:size] |
| 132 | + |
| 133 | + @response.paginate(page: "-1").to_a |
| 134 | + assert_equal 0, @response.search.definition[:from] |
| 135 | + assert_equal 33, @response.search.definition[:size] |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + context "#page and #per_page shorthand methods" do |
| 140 | + should "set from/size using default per_page" do |
| 141 | + @response.page(5) |
| 142 | + assert_equal 132, @response.search.definition[:from] |
| 143 | + assert_equal 33, @response.search.definition[:size] |
| 144 | + end |
| 145 | + |
| 146 | + should "set from/size when calling #page then #per_page" do |
| 147 | + @response.page(5).per_page(3) |
| 148 | + assert_equal 12, @response.search.definition[:from] |
| 149 | + assert_equal 3, @response.search.definition[:size] |
| 150 | + end |
| 151 | + |
| 152 | + should "set from/size when calling #per_page then #page" do |
| 153 | + @response.per_page(3).page(5) |
| 154 | + assert_equal 12, @response.search.definition[:from] |
| 155 | + assert_equal 3, @response.search.definition[:size] |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + context "#current_page method" do |
| 160 | + should "return 1 by default" do |
| 161 | + @response.paginate({}) |
| 162 | + assert_equal 1, @response.current_page |
| 163 | + end |
| 164 | + |
| 165 | + should "return current page number" do |
| 166 | + @response.paginate(page: 3, per_page: 9) |
| 167 | + assert_equal 3, @response.current_page |
| 168 | + end |
| 169 | + |
| 170 | + should "return nil if not pagination set" do |
| 171 | + assert_equal nil, @response.current_page |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + context "#per_page method" do |
| 176 | + should "return value set in paginate call" do |
| 177 | + @response.paginate(per_page: 8) |
| 178 | + assert_equal 8, @response.per_page |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + context "#total_entries method" do |
| 183 | + should "return total from response" do |
| 184 | + @response.expects(:results).returns(mock('results', total: 100)) |
| 185 | + assert_equal 100, @response.total_entries |
| 186 | + end |
| 187 | + end |
| 188 | + end |
| 189 | +end |
0 commit comments