qa 5.5.2 → 5.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -2
- data/app/controllers/qa/terms_controller.rb +34 -23
- data/app/services/qa/pagination_service.rb +586 -0
- data/config/initializers/mime_types.rb +4 -0
- data/config/routes.rb +4 -0
- data/lib/qa/authorities/local/file_based_authority.rb +3 -2
- data/lib/qa/version.rb +1 -1
- data/spec/controllers/terms_controller_spec.rb +102 -1
- data/spec/fixtures/authorities/authority_U.yml +13 -0
- data/spec/lib/authorities/{file_based_authority_spec.rb → local/file_based_authority_spec.rb} +11 -0
- data/spec/lib/authorities/{mysql_table_based_authority_spec.rb → local/mysql_table_based_authority_spec.rb} +0 -0
- data/spec/lib/authorities/{table_based_authority_spec.rb → local/table_based_authority_spec.rb} +0 -0
- data/spec/lib/authorities/local_spec.rb +3 -1
- data/spec/services/pagination_service_spec.rb +682 -0
- metadata +20 -14
@@ -0,0 +1,682 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Qa::PaginationService do
|
4
|
+
let(:results) do
|
5
|
+
results = []
|
6
|
+
1.upto(36) { |i| results << { "id": i.to_s, "label": "term #{i}" } }
|
7
|
+
results
|
8
|
+
end
|
9
|
+
let(:request) { instance_double(ActionDispatch::Request) }
|
10
|
+
let(:requested_format) { :json }
|
11
|
+
let(:params) do
|
12
|
+
{
|
13
|
+
"q" => "n",
|
14
|
+
"controller" => "qa/terms",
|
15
|
+
"action" => "search",
|
16
|
+
"vocab" => "local",
|
17
|
+
"subauthority" => "my_terms"
|
18
|
+
}.with_indifferent_access
|
19
|
+
end
|
20
|
+
let(:query_params) do
|
21
|
+
{ "q" => "term" }.with_indifferent_access
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:service) do
|
25
|
+
described_class.new(request: request,
|
26
|
+
results: results,
|
27
|
+
format: requested_format)
|
28
|
+
end
|
29
|
+
let(:base_url) { 'http://example.com' }
|
30
|
+
let(:url_path) { '/qa/search/local/my_terms' }
|
31
|
+
let(:query_string) { 'q=term' }
|
32
|
+
|
33
|
+
before do
|
34
|
+
allow(request).to receive(:params).and_return(params)
|
35
|
+
allow(request).to receive(:query_parameters).and_return(query_params)
|
36
|
+
allow(request).to receive(:query_string).and_return(query_string)
|
37
|
+
allow(request).to receive(:base_url).and_return(base_url)
|
38
|
+
allow(request).to receive(:path).and_return(url_path)
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#build_response' do
|
42
|
+
let(:response) { service.build_response }
|
43
|
+
|
44
|
+
# rubocop:disable RSpec/NestedGroups
|
45
|
+
context 'when json format (default) is requested' do
|
46
|
+
context 'and page_offset is missing' do
|
47
|
+
context 'and page_limit is missing' do
|
48
|
+
it 'returns all results as an array' do
|
49
|
+
expect(response).to match_array(results)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'and page_limit is passed in as "3"' do
|
54
|
+
before { params[:page_limit] = "3" }
|
55
|
+
|
56
|
+
it 'returns the first 3 results as an array' do
|
57
|
+
expect(response).to match_array(results[0..2])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'and page_offset is passed in' do
|
63
|
+
before { params[:page_offset] = "4" }
|
64
|
+
|
65
|
+
context 'and page_limit is missing' do
|
66
|
+
it 'returns the first DEFAULT_PAGE_LIMIT (10) records starting at 4th result' do
|
67
|
+
expect(response).to match_array(results[3..12])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'and page_limit is passed in' do
|
72
|
+
before { params[:page_limit] = "3" }
|
73
|
+
|
74
|
+
it 'returns the first 3 results as an array starting at 4th result' do
|
75
|
+
expect(response).to match_array(results[3..5])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'when json api format is requested' do
|
82
|
+
let(:requested_format) { :jsonapi }
|
83
|
+
let(:first_page) { "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=1" }
|
84
|
+
let(:second_page) { "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=11" }
|
85
|
+
let(:third_page) { "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=21" }
|
86
|
+
let(:fourth_page) { "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=31" }
|
87
|
+
let(:last_page) { fourth_page }
|
88
|
+
let(:query_string) { 'q=term&format=jsonapi' }
|
89
|
+
before do
|
90
|
+
params[:format] = "jsonapi"
|
91
|
+
query_params[:format] = "jsonapi"
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'with invalid page_offset' do
|
95
|
+
context 'that is not an integer' do
|
96
|
+
let(:query_string) { 'q=term&format=jsonapi&page_offset=BAD' }
|
97
|
+
|
98
|
+
before do
|
99
|
+
params[:page_offset] = "BAD"
|
100
|
+
query_params[:page_offset] = "BAD"
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'sets invalid error' do
|
104
|
+
error = response['errors'].first
|
105
|
+
expect(error['status']).to eq '200'
|
106
|
+
expect(error['source']).to include("page_offset" => "BAD")
|
107
|
+
expect(error['title']).to eq 'Page Offset Invalid'
|
108
|
+
expect(error['detail']).to eq "Page offset BAD is not an Integer. Returning empty results."
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'returns json api response with no results' do
|
112
|
+
expect(response["data"]).to match_array([])
|
113
|
+
end
|
114
|
+
|
115
|
+
it "sets meta['page'] stats with page_offset showing the passed in value" do
|
116
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "BAD"
|
117
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
118
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "0"
|
119
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
120
|
+
end
|
121
|
+
|
122
|
+
it "sets prev and next links to nil" do
|
123
|
+
expect(response['links']['prev']).to be_nil
|
124
|
+
expect(response['links']['next']).to be_nil
|
125
|
+
end
|
126
|
+
|
127
|
+
it "uses request values in self link" do
|
128
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_offset=BAD"
|
129
|
+
expect(response['links']['self']).to eq self_url
|
130
|
+
end
|
131
|
+
|
132
|
+
it "uses valid values in first and last links" do
|
133
|
+
expect(response['links']['first']).to eq first_page
|
134
|
+
expect(response['links']['last']).to eq last_page
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context 'that is < 1' do
|
139
|
+
let(:query_string) { 'q=term&format=jsonapi&page_offset=0' }
|
140
|
+
before do
|
141
|
+
params[:page_offset] = "0"
|
142
|
+
query_params[:page_offset] = "0"
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'sets out of range error' do
|
146
|
+
error = response['errors'].first
|
147
|
+
expect(error['status']).to eq '200'
|
148
|
+
expect(error['source']).to include("page_offset" => "0")
|
149
|
+
expect(error['title']).to eq 'Page Offset Out of Range'
|
150
|
+
expect(error['detail']).to eq "Page offset 0 < 1 (first result). Returning empty results."
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'returns json api response with no results' do
|
154
|
+
expect(response["data"]).to match_array([])
|
155
|
+
end
|
156
|
+
|
157
|
+
it "sets meta['page'] stats with page_offset showing the passed in value" do
|
158
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "0"
|
159
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
160
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "0"
|
161
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
162
|
+
end
|
163
|
+
|
164
|
+
it "sets prev and next links to nil" do
|
165
|
+
expect(response['links']['prev']).to be_nil
|
166
|
+
expect(response['links']['next']).to be_nil
|
167
|
+
end
|
168
|
+
|
169
|
+
it "uses request values in self link" do
|
170
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_offset=0"
|
171
|
+
expect(response['links']['self']).to eq self_url
|
172
|
+
end
|
173
|
+
|
174
|
+
it "uses valid values in first and last links" do
|
175
|
+
expect(response['links']['first']).to eq first_page
|
176
|
+
expect(response['links']['last']).to eq last_page
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context 'that is > number of results found' do
|
181
|
+
let(:query_string) { 'q=term&format=jsonapi&page_offset=40' }
|
182
|
+
before do
|
183
|
+
params[:page_offset] = "40"
|
184
|
+
query_params[:page_offset] = "40"
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'sets out of range error' do
|
188
|
+
error = response['errors'].first
|
189
|
+
expect(error['status']).to eq '200'
|
190
|
+
expect(error['source']).to include("page_offset" => "40")
|
191
|
+
expect(error['title']).to eq 'Page Offset Out of Range'
|
192
|
+
expect(error['detail']).to eq "Page offset 40 > 36 (total number of results). Returning empty results."
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'returns json api response with no results' do
|
196
|
+
expect(response["data"]).to match_array([])
|
197
|
+
end
|
198
|
+
|
199
|
+
it "sets meta['page'] stats with page_offset showing the passed in value" do
|
200
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "40"
|
201
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
202
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "0"
|
203
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
204
|
+
end
|
205
|
+
|
206
|
+
it "sets prev and next links to nil" do
|
207
|
+
expect(response['links']['prev']).to be_nil
|
208
|
+
expect(response['links']['next']).to be_nil
|
209
|
+
end
|
210
|
+
|
211
|
+
it "uses request values in self link" do
|
212
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_offset=40"
|
213
|
+
expect(response['links']['self']).to eq self_url
|
214
|
+
end
|
215
|
+
|
216
|
+
it "uses valid values in first and last links" do
|
217
|
+
expect(response['links']['first']).to eq first_page
|
218
|
+
expect(response['links']['last']).to eq last_page
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
context 'with invalid page_limit' do
|
224
|
+
context 'that is not an integer' do
|
225
|
+
let(:query_string) { 'q=term&format=jsonapi&page_limit=BAD' }
|
226
|
+
before do
|
227
|
+
params[:page_limit] = "BAD"
|
228
|
+
query_params[:page_limit] = "BAD"
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'sets invalid error' do
|
232
|
+
error = response['errors'].first
|
233
|
+
expect(error['status']).to eq '200'
|
234
|
+
expect(error['source']).to include("page_limit" => "BAD")
|
235
|
+
expect(error['title']).to eq 'Page Limit Invalid'
|
236
|
+
expect(error['detail']).to eq "Page limit BAD is not an Integer. Returning empty results."
|
237
|
+
end
|
238
|
+
|
239
|
+
it 'returns json api response with no results' do
|
240
|
+
expect(response["data"]).to match_array([])
|
241
|
+
end
|
242
|
+
|
243
|
+
it "sets meta['page'] stats with page_limit showing the passed in value" do
|
244
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "1"
|
245
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "BAD"
|
246
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "0"
|
247
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
248
|
+
end
|
249
|
+
|
250
|
+
it "sets prev and next links to nil" do
|
251
|
+
expect(response['links']['prev']).to be_nil
|
252
|
+
expect(response['links']['next']).to be_nil
|
253
|
+
end
|
254
|
+
|
255
|
+
it "uses request values in self link" do
|
256
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=BAD"
|
257
|
+
expect(response['links']['self']).to eq self_url
|
258
|
+
end
|
259
|
+
|
260
|
+
it "uses valid values in first and last links" do
|
261
|
+
expect(response['links']['first']).to eq first_page
|
262
|
+
expect(response['links']['last']).to eq last_page
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context 'that is < 1' do
|
267
|
+
let(:query_string) { 'q=term&format=jsonapi&page_limit=0' }
|
268
|
+
before do
|
269
|
+
params[:page_limit] = "0"
|
270
|
+
query_params[:page_limit] = "0"
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'sets out of range error' do
|
274
|
+
error = response['errors'].first
|
275
|
+
expect(error['status']).to eq '200'
|
276
|
+
expect(error['source']).to include("page_limit" => "0")
|
277
|
+
expect(error['title']).to eq 'Page Limit Out of Range'
|
278
|
+
expect(error['detail']).to eq "Page limit 0 < 1 (minimum limit). Returning empty results."
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'returns json api response with no results' do
|
282
|
+
expect(response["data"]).to match_array([])
|
283
|
+
end
|
284
|
+
|
285
|
+
it "sets meta['page'] stats with page_limit showing the passed in value" do
|
286
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "1"
|
287
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "0"
|
288
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "0"
|
289
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
290
|
+
end
|
291
|
+
|
292
|
+
it "sets prev and next links to nil" do
|
293
|
+
expect(response['links']['prev']).to be_nil
|
294
|
+
expect(response['links']['next']).to be_nil
|
295
|
+
end
|
296
|
+
|
297
|
+
it "uses request values in self link" do
|
298
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=0"
|
299
|
+
expect(response['links']['self']).to eq self_url
|
300
|
+
end
|
301
|
+
|
302
|
+
it "uses valid values in first and last links" do
|
303
|
+
expect(response['links']['first']).to eq first_page
|
304
|
+
expect(response['links']['last']).to eq last_page
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
context 'with multiple errors' do
|
310
|
+
context 'where page_offset is > number of results found and page_limit is < 1' do
|
311
|
+
let(:query_string) { 'q=term&format=jsonapi&page_limit=0&page_offset=40' }
|
312
|
+
before do
|
313
|
+
params[:page_limit] = "0"
|
314
|
+
params[:page_offset] = "40"
|
315
|
+
query_params[:page_limit] = "0"
|
316
|
+
query_params[:page_offset] = "40"
|
317
|
+
end
|
318
|
+
|
319
|
+
it 'sets page_offset out of range and page_limit out of range errors' do
|
320
|
+
expect(response['errors'].size).to eq 2
|
321
|
+
offset_error = response['errors'].find { |err| err['title'].starts_with? 'Page Offset' }
|
322
|
+
limit_error = response['errors'].find { |err| err['title'].starts_with? 'Page Limit' }
|
323
|
+
|
324
|
+
expect(offset_error['status']).to eq '200'
|
325
|
+
expect(offset_error['source']).to include("page_offset" => "40")
|
326
|
+
expect(offset_error['title']).to eq 'Page Offset Out of Range'
|
327
|
+
expect(offset_error['detail']).to eq "Page offset 40 > 36 (total number of results). Returning empty results."
|
328
|
+
|
329
|
+
expect(limit_error['status']).to eq '200'
|
330
|
+
expect(limit_error['source']).to include("page_limit" => "0")
|
331
|
+
expect(limit_error['title']).to eq 'Page Limit Out of Range'
|
332
|
+
expect(limit_error['detail']).to eq "Page limit 0 < 1 (minimum limit). Returning empty results."
|
333
|
+
end
|
334
|
+
|
335
|
+
it 'returns json api response with no results' do
|
336
|
+
expect(response["data"]).to match_array([])
|
337
|
+
end
|
338
|
+
|
339
|
+
it "sets meta['page'] stats with page_offset and page_limit showing the passed in values" do
|
340
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "40"
|
341
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "0"
|
342
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "0"
|
343
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
344
|
+
end
|
345
|
+
|
346
|
+
it "sets prev and next links to nil" do
|
347
|
+
expect(response['links']['prev']).to be_nil
|
348
|
+
expect(response['links']['next']).to be_nil
|
349
|
+
end
|
350
|
+
|
351
|
+
it "uses request values in self link" do
|
352
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=0&page_offset=40"
|
353
|
+
expect(response['links']['self']).to eq self_url
|
354
|
+
end
|
355
|
+
|
356
|
+
it "uses valid values in first and last links" do
|
357
|
+
expect(response['links']['first']).to eq first_page
|
358
|
+
expect(response['links']['last']).to eq last_page
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
context 'with page_offset missing' do
|
364
|
+
context 'and page_limit is missing' do
|
365
|
+
it 'returns the first DEFAULT_PAGE_LIMIT (10) records' do
|
366
|
+
expect(response["data"]).to match_array(results[0..9])
|
367
|
+
end
|
368
|
+
|
369
|
+
it "sets meta['page'] stats" do
|
370
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "1"
|
371
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
372
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "10"
|
373
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
374
|
+
end
|
375
|
+
|
376
|
+
it "sets prev link to nil and next link to next page" do
|
377
|
+
expect(response['links']['prev']).to be_nil
|
378
|
+
expect(response['links']['next']).to eq second_page
|
379
|
+
end
|
380
|
+
|
381
|
+
it "sets first and self links to first page and last link to third page" do
|
382
|
+
expect(response['links']['first']).to eq first_page
|
383
|
+
expect(response['links']['last']).to eq last_page
|
384
|
+
expect(response['links']['self']).to eq first_page
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
context 'and page_limit is passed in' do
|
389
|
+
let(:query_string) { 'q=term&format=jsonapi&page_limit=8' }
|
390
|
+
let(:first_page) { "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=8&page_offset=1" }
|
391
|
+
let(:second_page) { "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=8&page_offset=9" }
|
392
|
+
let(:fifth_page) { "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=8&page_offset=33" }
|
393
|
+
before do
|
394
|
+
params[:page_limit] = "8"
|
395
|
+
query_params[:page_limit] = "8"
|
396
|
+
end
|
397
|
+
|
398
|
+
it 'returns json api response with the first 3 results as an array' do
|
399
|
+
expect(response["data"]).to match_array(results[0..7])
|
400
|
+
end
|
401
|
+
|
402
|
+
it "sets meta['page'] stats" do
|
403
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "1"
|
404
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "8"
|
405
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "8"
|
406
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
407
|
+
end
|
408
|
+
|
409
|
+
it "sets prev link to nil and next link to next page" do
|
410
|
+
expect(response['links']['prev']).to be_nil
|
411
|
+
expect(response['links']['next']).to eq second_page
|
412
|
+
end
|
413
|
+
|
414
|
+
it "sets first and self links to first page and last link to sixth page" do
|
415
|
+
expect(response['links']['first']).to eq first_page
|
416
|
+
expect(response['links']['last']).to eq fifth_page
|
417
|
+
expect(response['links']['self']).to eq first_page
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
context 'with page_offset passed being the middle of a page' do
|
423
|
+
let(:query_string) { 'q=term&format=jsonapi&page_offset=4' }
|
424
|
+
before do
|
425
|
+
params[:page_offset] = "4"
|
426
|
+
query_params[:page_offset] = "4"
|
427
|
+
end
|
428
|
+
|
429
|
+
context 'and page_limit is missing' do
|
430
|
+
it 'returns json api response with the first DEFAULT_PAGE_LIMIT (10) records starting at 4th result' do
|
431
|
+
expect(response["data"]).to match_array(results[3..12])
|
432
|
+
end
|
433
|
+
|
434
|
+
it "sets meta['page'] stats" do
|
435
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "4"
|
436
|
+
expect(response["meta"]["page"]["page_limit"]).to eq described_class::DEFAULT_PAGE_LIMIT.to_s
|
437
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq described_class::DEFAULT_PAGE_LIMIT.to_s
|
438
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
439
|
+
end
|
440
|
+
|
441
|
+
it "sets self to requested page_offset and default page_limit" do
|
442
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=4"
|
443
|
+
expect(response['links']['self']).to eq self_url
|
444
|
+
end
|
445
|
+
|
446
|
+
it "sets prev link to first page and next link to next offset" do
|
447
|
+
first_page = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=1"
|
448
|
+
next_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=14"
|
449
|
+
expect(response['links']['prev']).to eq first_page
|
450
|
+
expect(response['links']['next']).to eq next_url
|
451
|
+
end
|
452
|
+
|
453
|
+
it "sets first to first page and last link to third page" do
|
454
|
+
first_page = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=1"
|
455
|
+
third_page = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=31"
|
456
|
+
expect(response['links']['first']).to eq first_page
|
457
|
+
expect(response['links']['last']).to eq third_page
|
458
|
+
end
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
context 'and there are no results' do
|
463
|
+
let(:results) { [] }
|
464
|
+
|
465
|
+
it 'returns no results' do
|
466
|
+
expect(response["data"]).to match_array([])
|
467
|
+
end
|
468
|
+
|
469
|
+
it "sets meta['page'] stats" do
|
470
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "1"
|
471
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
472
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "0"
|
473
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "0"
|
474
|
+
end
|
475
|
+
|
476
|
+
it 'sets links with prev and next set to nil, last set to first' do
|
477
|
+
expect(response['links']["self"]).to eq first_page
|
478
|
+
expect(response['links']["first"]).to eq first_page
|
479
|
+
expect(response['links']["prev"]).to eq nil
|
480
|
+
expect(response['links']["next"]).to eq nil
|
481
|
+
expect(response['links']["last"]).to eq first_page
|
482
|
+
end
|
483
|
+
|
484
|
+
it 'does not include errors' do
|
485
|
+
expect(response.key?('errors')).to eq false
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
context 'and results do not include a full page' do
|
490
|
+
context 'and there are several results in range' do
|
491
|
+
let(:query_string) { 'q=term&format=jsonapi&page_offset=31&page_limit=10' }
|
492
|
+
|
493
|
+
before do
|
494
|
+
params[:page_offset] = "31"
|
495
|
+
query_params[:page_offset] = "31"
|
496
|
+
end
|
497
|
+
|
498
|
+
it 'returns json api response with a partial page of results starting at 31th result' do
|
499
|
+
expect(response["data"]).to match_array(results[30..35])
|
500
|
+
end
|
501
|
+
|
502
|
+
it "sets meta['page'] stats" do
|
503
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "31"
|
504
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
505
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "6"
|
506
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
507
|
+
end
|
508
|
+
|
509
|
+
it "sets self to last page" do
|
510
|
+
expect(response['links']['self']).to eq last_page
|
511
|
+
end
|
512
|
+
|
513
|
+
it "sets prev link to 3rd page and next link to nil" do
|
514
|
+
expect(response['links']['prev']).to eq third_page
|
515
|
+
expect(response['links']['next']).to eq nil
|
516
|
+
end
|
517
|
+
|
518
|
+
it "sets first to first page and last link to fourth page" do
|
519
|
+
expect(response['links']['first']).to eq first_page
|
520
|
+
expect(response['links']['last']).to eq last_page
|
521
|
+
end
|
522
|
+
end
|
523
|
+
|
524
|
+
context 'and only the last result is in range' do
|
525
|
+
let(:query_string) { "q=term&format=jsonapi&page_offset=#{results.length}" }
|
526
|
+
before do
|
527
|
+
params[:page_offset] = results.length.to_s
|
528
|
+
query_params[:page_offset] = results.length.to_s
|
529
|
+
end
|
530
|
+
|
531
|
+
it 'returns json api response with a partial page with one result' do
|
532
|
+
expect(response["data"]).to match_array(results[35..35])
|
533
|
+
end
|
534
|
+
|
535
|
+
it "sets meta['page'] stats" do
|
536
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "36"
|
537
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
538
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "1"
|
539
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
540
|
+
end
|
541
|
+
|
542
|
+
it "sets self to second page" do
|
543
|
+
self_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=36"
|
544
|
+
expect(response['links']['self']).to eq self_url
|
545
|
+
end
|
546
|
+
|
547
|
+
it "sets prev link to one page back and next link to nil" do
|
548
|
+
# when offset is middle of a page, then prev will also be middle of
|
549
|
+
# a page with `prev_offset = current_offset - page_limit`
|
550
|
+
prev_url = "#{base_url}#{url_path}?q=term&format=jsonapi&page_limit=10&page_offset=26"
|
551
|
+
expect(response['links']['prev']).to eq prev_url
|
552
|
+
expect(response['links']['next']).to eq nil
|
553
|
+
end
|
554
|
+
|
555
|
+
it "sets first to first page and last link to second page" do
|
556
|
+
# calculated assuming starting from offset=1
|
557
|
+
expect(response['links']['first']).to eq first_page
|
558
|
+
expect(response['links']['last']).to eq last_page
|
559
|
+
end
|
560
|
+
end
|
561
|
+
end
|
562
|
+
|
563
|
+
context 'and there are multiple pages of results' do
|
564
|
+
# data defined in main section has 3 pages of data
|
565
|
+
let(:query_string) { "q=term&format=jsonapi&page_offset=#{page_offset}" }
|
566
|
+
before do
|
567
|
+
params[:page_offset] = page_offset
|
568
|
+
query_params[:page_offset] = page_offset
|
569
|
+
end
|
570
|
+
|
571
|
+
context 'and page_offset is start of first page' do
|
572
|
+
let(:page_offset) { "1" }
|
573
|
+
|
574
|
+
it 'returns json api response first page of results' do
|
575
|
+
expect(response["data"]).to match_array(results[0..9])
|
576
|
+
end
|
577
|
+
|
578
|
+
it "sets meta['page'] stats" do
|
579
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "1"
|
580
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
581
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "10"
|
582
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
583
|
+
end
|
584
|
+
|
585
|
+
it 'sets links with prev set to nil' do
|
586
|
+
expect(response['links']["self"]).to eq first_page
|
587
|
+
expect(response['links']["first"]).to eq first_page
|
588
|
+
expect(response['links']["prev"]).to be_nil
|
589
|
+
expect(response['links']["next"]).to eq second_page
|
590
|
+
expect(response['links']["last"]).to eq last_page
|
591
|
+
end
|
592
|
+
|
593
|
+
it 'does not include errors' do
|
594
|
+
expect(response.key?('errors')).to eq false
|
595
|
+
end
|
596
|
+
end
|
597
|
+
|
598
|
+
context 'and page_offset is start of second page' do
|
599
|
+
let(:page_offset) { "11" }
|
600
|
+
|
601
|
+
it 'returns json api response second page of results' do
|
602
|
+
expect(response["data"]).to match_array(results[10..19])
|
603
|
+
end
|
604
|
+
|
605
|
+
it "sets meta['page'] stats" do
|
606
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "11"
|
607
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
608
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "10"
|
609
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
610
|
+
end
|
611
|
+
|
612
|
+
it 'sets links with prev and next having values' do
|
613
|
+
expect(response['links']["self"]).to eq second_page
|
614
|
+
expect(response['links']["first"]).to eq first_page
|
615
|
+
expect(response['links']["prev"]).to eq first_page
|
616
|
+
expect(response['links']["next"]).to eq third_page
|
617
|
+
expect(response['links']["last"]).to eq last_page
|
618
|
+
end
|
619
|
+
|
620
|
+
it 'does not include errors' do
|
621
|
+
expect(response.key?('errors')).to eq false
|
622
|
+
end
|
623
|
+
end
|
624
|
+
|
625
|
+
context 'and page_offset is start of third page' do
|
626
|
+
let(:page_offset) { "21" }
|
627
|
+
|
628
|
+
it 'returns json api response third page of results' do
|
629
|
+
expect(response["data"]).to match_array(results[20..29])
|
630
|
+
end
|
631
|
+
|
632
|
+
it "sets meta['page'] stats" do
|
633
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "21"
|
634
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
635
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "10"
|
636
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
637
|
+
end
|
638
|
+
|
639
|
+
it 'sets links with prev and next having values' do
|
640
|
+
expect(response['links']["self"]).to eq third_page
|
641
|
+
expect(response['links']["first"]).to eq first_page
|
642
|
+
expect(response['links']["prev"]).to eq second_page
|
643
|
+
expect(response['links']["next"]).to eq last_page
|
644
|
+
expect(response['links']["last"]).to eq last_page
|
645
|
+
end
|
646
|
+
|
647
|
+
it 'does not include errors' do
|
648
|
+
expect(response.key?('errors')).to eq false
|
649
|
+
end
|
650
|
+
end
|
651
|
+
|
652
|
+
context 'and page_offset is start of fourth page (last page)' do
|
653
|
+
let(:page_offset) { "31" }
|
654
|
+
|
655
|
+
it 'returns json api response third page of results' do
|
656
|
+
expect(response["data"]).to match_array(results[30..35])
|
657
|
+
end
|
658
|
+
|
659
|
+
it "sets meta['page'] stats" do
|
660
|
+
expect(response["meta"]["page"]["page_offset"]).to eq "31"
|
661
|
+
expect(response["meta"]["page"]["page_limit"]).to eq "10"
|
662
|
+
expect(response["meta"]["page"]["actual_page_size"]).to eq "6"
|
663
|
+
expect(response["meta"]["page"]["total_num_found"]).to eq "36"
|
664
|
+
end
|
665
|
+
|
666
|
+
it 'sets links with next set to nil' do
|
667
|
+
expect(response['links']["self"]).to eq last_page
|
668
|
+
expect(response['links']["first"]).to eq first_page
|
669
|
+
expect(response['links']["prev"]).to eq third_page
|
670
|
+
expect(response['links']["next"]).to be_nil
|
671
|
+
expect(response['links']["last"]).to eq last_page
|
672
|
+
end
|
673
|
+
|
674
|
+
it 'does not include errors' do
|
675
|
+
expect(response.key?('errors')).to eq false
|
676
|
+
end
|
677
|
+
end
|
678
|
+
end
|
679
|
+
end
|
680
|
+
# rubocop:enable RSpec/NestedGroups
|
681
|
+
end
|
682
|
+
end
|