|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module Elasticsearch |
| 4 | + module Model |
| 5 | + class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase |
| 6 | + |
| 7 | + class ::Article < ActiveRecord::Base |
| 8 | + include Elasticsearch::Model |
| 9 | + |
| 10 | + settings index: { number_of_shards: 1, number_of_replicas: 0 } do |
| 11 | + mapping do |
| 12 | + indexes :title, type: 'string', analyzer: 'snowball' |
| 13 | + indexes :created_at, type: 'date' |
| 14 | + end |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + Kaminari::Hooks.init |
| 19 | + |
| 20 | + context "ActiveRecord pagination" do |
| 21 | + setup do |
| 22 | + ActiveRecord::Schema.define(:version => 1) do |
| 23 | + create_table :articles do |t| |
| 24 | + t.string :title |
| 25 | + t.datetime :created_at, :default => 'NOW()' |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + Article.delete_all |
| 30 | + Article.__elasticsearch__.create_index! force: true |
| 31 | + |
| 32 | + 68.times do |i| ::Article.create! title: "Test #{i}" end |
| 33 | + |
| 34 | + Article.import |
| 35 | + Article.__elasticsearch__.refresh_index! |
| 36 | + end |
| 37 | + |
| 38 | + should "be on the first page by default" do |
| 39 | + records = Article.search('title:test').page(1).records |
| 40 | + |
| 41 | + assert_equal 25, records.size |
| 42 | + assert_equal 1, records.current_page |
| 43 | + assert_equal nil, records.prev_page |
| 44 | + assert_equal 2, records.next_page |
| 45 | + assert_equal 3, records.total_pages |
| 46 | + |
| 47 | + assert records.first_page?, "Should be the first page" |
| 48 | + assert ! records.last_page?, "Should NOT be the last page" |
| 49 | + assert ! records.out_of_range?, "Should NOT be out of range" |
| 50 | + end |
| 51 | + |
| 52 | + should "load next page" do |
| 53 | + records = Article.search('title:test').page(2).records |
| 54 | + |
| 55 | + assert_equal 25, records.size |
| 56 | + assert_equal 2, records.current_page |
| 57 | + assert_equal 1, records.prev_page |
| 58 | + assert_equal 3, records.next_page |
| 59 | + assert_equal 3, records.total_pages |
| 60 | + |
| 61 | + assert ! records.first_page?, "Should NOT be the first page" |
| 62 | + assert ! records.last_page?, "Should NOT be the last page" |
| 63 | + assert ! records.out_of_range?, "Should NOT be out of range" |
| 64 | + end |
| 65 | + |
| 66 | + should "load last page" do |
| 67 | + records = Article.search('title:test').page(3).records |
| 68 | + |
| 69 | + assert_equal 18, records.size |
| 70 | + assert_equal 3, records.current_page |
| 71 | + assert_equal 2, records.prev_page |
| 72 | + assert_equal nil, records.next_page |
| 73 | + assert_equal 3, records.total_pages |
| 74 | + |
| 75 | + assert ! records.first_page?, "Should NOT be the first page" |
| 76 | + assert records.last_page?, "Should be the last page" |
| 77 | + assert ! records.out_of_range?, "Should NOT be out of range" |
| 78 | + end |
| 79 | + |
| 80 | + should "not load invalid page" do |
| 81 | + records = Article.search('title:test').page(6).records |
| 82 | + |
| 83 | + assert_equal 0, records.size |
| 84 | + assert_equal 6, records.current_page |
| 85 | + assert_equal 5, records.prev_page |
| 86 | + assert_equal nil, records.next_page |
| 87 | + assert_equal 3, records.total_pages |
| 88 | + |
| 89 | + assert ! records.first_page?, "Should NOT be the first page" |
| 90 | + assert records.last_page?, "Should be the last page" |
| 91 | + assert records.out_of_range?, "Should be out of range" |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments