|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class Elasticsearch::Model::ResponsePaginationTest < Test::Unit::TestCase |
| 4 | + context "Response pagination" do |
| 5 | + class ModelClass |
| 6 | + include ::Kaminari::ConfigurationMethods |
| 7 | + |
| 8 | + def self.index_name; 'foo'; end |
| 9 | + def self.document_type; 'bar'; end |
| 10 | + end |
| 11 | + |
| 12 | + RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, |
| 13 | + 'hits' => { 'total' => 100, 'hits' => [ {'_id' => 1} ] } } |
| 14 | + |
| 15 | + setup do |
| 16 | + search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, '*' |
| 17 | + @response = Elasticsearch::Model::Response::Response.new ModelClass, search, RESPONSE |
| 18 | + @response.klass.stubs(:client).returns mock('client') |
| 19 | + end |
| 20 | + |
| 21 | + should "have pagination methods" do |
| 22 | + assert_respond_to @response, :page |
| 23 | + assert_respond_to @response, :limit_value |
| 24 | + assert_respond_to @response, :offset_value |
| 25 | + assert_respond_to @response, :limit |
| 26 | + assert_respond_to @response, :offset |
| 27 | + assert_respond_to @response, :total_count |
| 28 | + end |
| 29 | + |
| 30 | + context "#page method" do |
| 31 | + should "advance the from/size" do |
| 32 | + @response.klass.client |
| 33 | + .expects(:search) |
| 34 | + .with do |definition| |
| 35 | + assert_equal 25, definition[:from] |
| 36 | + assert_equal 25, definition[:size] |
| 37 | + end |
| 38 | + .returns(RESPONSE) |
| 39 | + |
| 40 | + assert_nil @response.search.definition[:from] |
| 41 | + assert_nil @response.search.definition[:size] |
| 42 | + |
| 43 | + @response.page(2).to_a |
| 44 | + assert_equal 25, @response.search.definition[:from] |
| 45 | + assert_equal 25, @response.search.definition[:size] |
| 46 | + end |
| 47 | + |
| 48 | + should "advance the from/size further" do |
| 49 | + @response.klass.client |
| 50 | + .expects(:search) |
| 51 | + .with do |definition| |
| 52 | + assert_equal 75, definition[:from] |
| 53 | + assert_equal 25, definition[:size] |
| 54 | + end |
| 55 | + .returns(RESPONSE) |
| 56 | + |
| 57 | + @response.page(4).to_a |
| 58 | + assert_equal 75, @response.search.definition[:from] |
| 59 | + assert_equal 25, @response.search.definition[:size] |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context "limit/offset readers" do |
| 64 | + should "return the default" do |
| 65 | + assert_equal 0, @response.limit_value |
| 66 | + assert_equal 0, @response.offset_value |
| 67 | + end |
| 68 | + |
| 69 | + should "return the value from URL parameters" do |
| 70 | + search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, '*', size: 10, from: 50 |
| 71 | + @response = Elasticsearch::Model::Response::Response.new ModelClass, search, RESPONSE |
| 72 | + |
| 73 | + assert_equal 10, @response.limit_value |
| 74 | + assert_equal 50, @response.offset_value |
| 75 | + end |
| 76 | + |
| 77 | + should "return the value from body" do |
| 78 | + search = Elasticsearch::Model::Searching::SearchRequest.new ModelClass, { query: { match_all: {} }, from: 10, size: 50 } |
| 79 | + @response = Elasticsearch::Model::Response::Response.new ModelClass, search, RESPONSE |
| 80 | + |
| 81 | + assert_equal 50, @response.limit_value |
| 82 | + assert_equal 10, @response.offset_value |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context "limit setter" do |
| 87 | + setup do |
| 88 | + @response.records |
| 89 | + @response.results |
| 90 | + end |
| 91 | + |
| 92 | + should "set the values" do |
| 93 | + @response.limit(35) |
| 94 | + assert_equal 35, @response.search.definition[:size] |
| 95 | + end |
| 96 | + |
| 97 | + should "reset the variables" do |
| 98 | + assert_not_nil @response.instance_variable_get(:@response) |
| 99 | + assert_not_nil @response.instance_variable_get(:@records) |
| 100 | + assert_not_nil @response.instance_variable_get(:@results) |
| 101 | + |
| 102 | + @response.limit(35) |
| 103 | + |
| 104 | + assert_nil @response.instance_variable_get(:@response) |
| 105 | + assert_nil @response.instance_variable_get(:@records) |
| 106 | + assert_nil @response.instance_variable_get(:@results) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + context "offset setter" do |
| 111 | + setup do |
| 112 | + @response.records |
| 113 | + @response.results |
| 114 | + end |
| 115 | + |
| 116 | + should "set the values" do |
| 117 | + @response.offset(15) |
| 118 | + assert_equal 15, @response.search.definition[:from] |
| 119 | + end |
| 120 | + |
| 121 | + should "reset the variables" do |
| 122 | + assert_not_nil @response.instance_variable_get(:@response) |
| 123 | + assert_not_nil @response.instance_variable_get(:@records) |
| 124 | + assert_not_nil @response.instance_variable_get(:@results) |
| 125 | + |
| 126 | + @response.offset(35) |
| 127 | + |
| 128 | + assert_nil @response.instance_variable_get(:@response) |
| 129 | + assert_nil @response.instance_variable_get(:@records) |
| 130 | + assert_nil @response.instance_variable_get(:@results) |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + context "total" do |
| 135 | + should "return the number of hits" do |
| 136 | + assert_equal 100, @response.total_count |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | +end |
0 commit comments