@@ -29,11 +29,11 @@ class Record < DHS::Record
2929
3030 context 'find_batches' do
3131 it 'processes records in batches' do
32- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=1 " ) . to_return ( status : 200 , body : api_response ( ( 1 ..100 ) . to_a , 1 ) )
33- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=101 " ) . to_return ( status : 200 , body : api_response ( ( 101 ..200 ) . to_a , 101 ) )
34- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=201 " ) . to_return ( status : 200 , body : api_response ( ( 201 ..300 ) . to_a , 201 ) )
35- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=301 " ) . to_return ( status : 200 , body : api_response ( ( 301 ..400 ) . to_a , 301 ) )
36- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=401 " ) . to_return ( status : 200 , body : api_response ( ( 401 ..total ) . to_a , 401 ) )
32+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=0 " ) . to_return ( status : 200 , body : api_response ( ( 1 ..100 ) . to_a , 0 ) )
33+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=100 " ) . to_return ( status : 200 , body : api_response ( ( 101 ..200 ) . to_a , 100 ) )
34+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=200 " ) . to_return ( status : 200 , body : api_response ( ( 201 ..300 ) . to_a , 200 ) )
35+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=300 " ) . to_return ( status : 200 , body : api_response ( ( 301 ..400 ) . to_a , 300 ) )
36+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=400 " ) . to_return ( status : 200 , body : api_response ( ( 401 ..total ) . to_a , 400 ) )
3737 length = 0
3838 Record . find_in_batches do |records |
3939 length += records . length
@@ -44,11 +44,11 @@ class Record < DHS::Record
4444 end
4545
4646 it 'adapts to backend max limit' do
47- stub_request ( :get , "#{ datastore } /feedbacks?limit=230&offset=1 " ) . to_return ( status : 200 , body : api_response ( ( 1 ..100 ) . to_a , 1 ) )
48- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=101 " ) . to_return ( status : 200 , body : api_response ( ( 101 ..200 ) . to_a , 101 ) )
49- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=201 " ) . to_return ( status : 200 , body : api_response ( ( 201 ..300 ) . to_a , 201 ) )
50- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=301 " ) . to_return ( status : 200 , body : api_response ( ( 301 ..400 ) . to_a , 301 ) )
51- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=401 " ) . to_return ( status : 200 , body : api_response ( ( 401 ..total ) . to_a , 401 ) )
47+ stub_request ( :get , "#{ datastore } /feedbacks?limit=230&offset=0 " ) . to_return ( status : 200 , body : api_response ( ( 1 ..100 ) . to_a , 0 ) )
48+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=100 " ) . to_return ( status : 200 , body : api_response ( ( 101 ..200 ) . to_a , 100 ) )
49+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=200 " ) . to_return ( status : 200 , body : api_response ( ( 201 ..300 ) . to_a , 200 ) )
50+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=300 " ) . to_return ( status : 200 , body : api_response ( ( 301 ..400 ) . to_a , 300 ) )
51+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=400 " ) . to_return ( status : 200 , body : api_response ( ( 401 ..total ) . to_a , 400 ) )
5252 length = 0
5353 Record . find_in_batches ( batch_size : 230 ) do |records |
5454 length += records . length
@@ -59,8 +59,8 @@ class Record < DHS::Record
5959 end
6060
6161 it 'forwards offset' do
62- stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=401 " ) . to_return ( status : 200 , body : api_response ( ( 401 ..total ) . to_a , 401 ) )
63- Record . find_in_batches ( start : 401 ) do |records |
62+ stub_request ( :get , "#{ datastore } /feedbacks?limit=100&offset=400 " ) . to_return ( status : 200 , body : api_response ( ( 401 ..total ) . to_a , 400 ) )
63+ Record . find_in_batches ( start : 400 ) do |records |
6464 expect ( records . length ) . to eq ( total - 400 )
6565 end
6666 end
@@ -119,4 +119,42 @@ class Record < DHS::Record
119119 expect ( length ) . to eq total
120120 end
121121 end
122+
123+ context 'different pagination strategy' do
124+ before do
125+ class Transaction < DHS ::Record
126+ endpoint 'https://api/transactions'
127+ configuration limit_key : :items_on_page , pagination_strategy : :total_pages , pagination_key : :page , items_key : :transactions , total_key : :total_pages
128+ end
129+
130+ stub_request ( :get , 'https://api/transactions?items_on_page=50&page=1' )
131+ . to_return ( body : {
132+ page : 1 ,
133+ total_pages : 2 ,
134+ items_on_page : 50 ,
135+ transactions : 50 . times . map { |index | { id : index } }
136+ } . to_json )
137+
138+ stub_request ( :get , 'https://api/transactions?items_on_page=50&page=2' )
139+ . to_return ( body : {
140+ page : 2 ,
141+ total_pages : 2 ,
142+ items_on_page : 50 ,
143+ transactions : 22 . times . map { |index | { id : 50 + index } }
144+ } . to_json )
145+ end
146+
147+ it 'find in batches and paginates automatically even for different pagination strategies' do
148+ total = 0
149+ transactions = [ ]
150+
151+ Transaction . find_in_batches ( batch_size : 50 ) do |batch |
152+ total += batch . length
153+ transactions << batch
154+ end
155+
156+ expect ( total ) . to eq ( 72 )
157+ expect ( transactions . flatten . as_json ) . to eq ( 72 . times . map { |index | { id : index } . as_json } )
158+ end
159+ end
122160end
0 commit comments