forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_record_pagination_test.rb
145 lines (113 loc) · 5.07 KB
/
active_record_pagination_test.rb
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
142
143
144
145
require 'test_helper'
require 'active_record'
module Elasticsearch
module Model
class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase
context "ActiveRecord pagination" do
setup do
class ::ArticleForPagination < ActiveRecord::Base
include Elasticsearch::Model
scope :published, -> { where(published: true) }
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mapping do
indexes :title, type: 'string', analyzer: 'snowball'
indexes :created_at, type: 'date'
end
end
end
ActiveRecord::Schema.define(:version => 1) do
create_table ::ArticleForPagination.table_name do |t|
t.string :title
t.datetime :created_at, :default => 'NOW()'
t.boolean :published
end
end
Kaminari::Hooks.init
ArticleForPagination.delete_all
ArticleForPagination.__elasticsearch__.create_index! force: true
68.times do |i|
::ArticleForPagination.create! title: "Test #{i}", published: (i % 2 == 0)
end
ArticleForPagination.import
ArticleForPagination.__elasticsearch__.refresh_index!
end
should "be on the first page by default" do
records = ArticleForPagination.search('title:test').page(1).records
assert_equal 25, records.size
assert_equal 1, records.current_page
assert_equal nil, records.prev_page
assert_equal 2, records.next_page
assert_equal 3, records.total_pages
assert records.first_page?, "Should be the first page"
assert ! records.last_page?, "Should NOT be the last page"
assert ! records.out_of_range?, "Should NOT be out of range"
end
should "load next page" do
records = ArticleForPagination.search('title:test').page(2).records
assert_equal 25, records.size
assert_equal 2, records.current_page
assert_equal 1, records.prev_page
assert_equal 3, records.next_page
assert_equal 3, records.total_pages
assert ! records.first_page?, "Should NOT be the first page"
assert ! records.last_page?, "Should NOT be the last page"
assert ! records.out_of_range?, "Should NOT be out of range"
end
should "load last page" do
records = ArticleForPagination.search('title:test').page(3).records
assert_equal 18, records.size
assert_equal 3, records.current_page
assert_equal 2, records.prev_page
assert_equal nil, records.next_page
assert_equal 3, records.total_pages
assert ! records.first_page?, "Should NOT be the first page"
assert records.last_page?, "Should be the last page"
assert ! records.out_of_range?, "Should NOT be out of range"
end
should "not load invalid page" do
records = ArticleForPagination.search('title:test').page(6).records
assert_equal 0, records.size
assert_equal 6, records.current_page
assert_equal 5, records.prev_page
assert_equal nil, records.next_page
assert_equal 3, records.total_pages
assert ! records.first_page?, "Should NOT be the first page"
assert records.last_page?, "Should be the last page"
assert records.out_of_range?, "Should be out of range"
end
should "be combined with scopes" do
records = ArticleForPagination.search('title:test').page(2).records.published
assert records.all? { |r| r.published? }
assert_equal 12, records.size
end
should "respect sort" do
search = ArticleForPagination.search({ query: { match: { title: 'test' } }, sort: [ { id: 'desc' } ] })
records = search.page(2).records
assert_equal 43, records.first.id # 68 - 25 = 42
records = search.page(3).records
assert_equal 18, records.first.id # 68 - (2 * 25) = 18
records = search.page(2).per(5).records
assert_equal 63, records.first.id # 68 - 5 = 63
end
should "set the limit per request" do
records = ArticleForPagination.search('title:test').limit(50).page(2).records
assert_equal 18, records.size
assert_equal 2, records.current_page
assert_equal 1, records.prev_page
assert_equal nil, records.next_page
assert_equal 2, records.total_pages
assert records.last_page?, "Should be the last page"
end
context "with specific model settings" do
teardown do
ArticleForPagination.instance_variable_set(:@_default_per_page, nil)
end
should "respect paginates_per" do
ArticleForPagination.paginates_per 50
assert_equal 50, ArticleForPagination.search('*').page(1).records.size
end
end
end
end
end
end