@@ -187,28 +187,37 @@ class TableWithoutGuid < Sequel::Model(:table_without_guid); end
187187 expect ( paginated_result . records [ 3 ] . guid ) . to eq ( app_model2 . guid )
188188 end
189189
190- it 'orders by GUID as a secondary field when available' do
191- options = { page : page , per_page : 2 , order_by : 'created_at' , order_direction : 'asc' }
192- app_model1 . update ( guid : '1' , created_at : '2019-12-25T13:00:00Z' )
193- app_model2 . update ( guid : '2' , created_at : '2019-12-25T13:00:00Z' )
190+ context 'deterministic ordering' do
191+ before do
192+ app_model1 . update ( guid : '2' , created_at : '2019-12-25T13:00:00Z' , updated_at : '2019-12-25T13:00:00Z' )
193+ app_model2 . update ( guid : '1' , created_at : '2019-12-25T13:00:00Z' , updated_at : '2019-12-25T13:00:00Z' )
194+ end
194195
195- pagination_options = PaginationOptions . new ( options )
196- paginated_result = paginator . get_page ( dataset , pagination_options )
197- expect ( paginated_result . records . first . guid ) . to eq ( app_model1 . guid )
198- expect ( paginated_result . records . second . guid ) . to eq ( app_model2 . guid )
199- end
196+ context "when ordered by 'created_at'" do
197+ it "orders by 'id' as a secondary field" do
198+ options = { page : page , per_page : 2 , order_by : 'created_at' , order_direction : 'asc' }
200199
201- it 'does not order by GUID when the table has no GUID' do
202- options = { page : page , per_page : 2 , order_by : 'created_at' , order_direction : 'asc' }
200+ pagination_options = PaginationOptions . new ( options )
201+ expect do
202+ paginated_result = paginator . get_page ( dataset , pagination_options )
203+ expect ( paginated_result . records . first . guid ) . to eq ( app_model1 . guid )
204+ expect ( paginated_result . records . second . guid ) . to eq ( app_model2 . guid )
205+ end . to have_queried_db_times ( /ORDER BY .\w *.\. .created_at. ASC, .\w *.\. .id. ASC LIMIT/i , 1 )
206+ end
207+ end
203208
204- pagination_options = PaginationOptions . new ( options )
205- ds = TableWithoutGuid . dataset
206- expect do
207- paginator . get_page ( ds , pagination_options )
208- end . to have_queried_db_times ( /ORDER BY .\w *.\. .created_at. ASC LIMIT/i , 1 )
209- expect do
210- paginator . get_page ( ds , pagination_options )
211- end . to have_queried_db_times ( /ORDER BY .\w *.\. .created_at. ASC, .\w *.\. .guid. ASC LIMIT/i , 0 )
209+ context "when ordered by a non-unique column other than 'created_at'" do
210+ it "orders by 'guid' as a secondary field" do
211+ options = { page : page , per_page : 2 , order_by : 'updated_at' , order_direction : 'asc' }
212+
213+ pagination_options = PaginationOptions . new ( options )
214+ expect do
215+ paginated_result = paginator . get_page ( dataset , pagination_options )
216+ expect ( paginated_result . records . first . guid ) . to eq ( app_model2 . guid )
217+ expect ( paginated_result . records . second . guid ) . to eq ( app_model1 . guid )
218+ end . to have_queried_db_times ( /ORDER BY .\w *.\. .updated_at. ASC, .\w *.\. .guid. ASC LIMIT/i , 1 )
219+ end
220+ end
212221 end
213222
214223 it 'does not order by GUID when the primary order is by ID' do
@@ -223,6 +232,19 @@ class TableWithoutGuid < Sequel::Model(:table_without_guid); end
223232 end . to have_queried_db_times ( /ORDER BY .\w *.\. .id. ASC, .\w *.\. .guid. ASC LIMIT/i , 0 )
224233 end
225234
235+ it 'does not order by GUID when the table has no GUID' do
236+ options = { page : page , per_page : 2 , order_by : 'name' , order_direction : 'asc' }
237+
238+ pagination_options = PaginationOptions . new ( options )
239+ ds = TableWithoutGuid . dataset
240+ expect do
241+ paginator . get_page ( ds , pagination_options )
242+ end . to have_queried_db_times ( /ORDER BY .\w *.\. .name. ASC LIMIT/i , 1 )
243+ expect do
244+ paginator . get_page ( ds , pagination_options )
245+ end . to have_queried_db_times ( /ORDER BY .\w *.\. .name. ASC, .\w *.\. .guid. ASC LIMIT/i , 0 )
246+ end
247+
226248 context 'when a DISTINCT ON clause is used' do # MySQL uses GROUP BY instead
227249 let ( :distinct_dataset ) { dataset . distinct ( :id ) }
228250
@@ -237,12 +259,12 @@ class TableWithoutGuid < Sequel::Model(:table_without_guid); end
237259 end
238260
239261 context 'when ordered by other column' do
240- let ( :pagination_options ) { PaginationOptions . new ( { order_by : 'created_at ' } ) }
262+ let ( :pagination_options ) { PaginationOptions . new ( { order_by : 'name ' } ) }
241263
242264 it 'uses other column and GUID for DISTINCT ON clause' do
243265 expect do
244266 paginator . get_page ( distinct_dataset , pagination_options )
245- end . to have_queried_db_times ( /(select distinct on \( .*created_at .*,.*guid.*\) .* from)|(group by)/i , paginator . can_paginate_with_window_function? ( dataset ) ? 1 : 2 )
267+ end . to have_queried_db_times ( /(select distinct on \( .*name .*,.*guid.*\) .* from)|(group by)/i , paginator . can_paginate_with_window_function? ( dataset ) ? 1 : 2 )
246268 end
247269
248270 context 'when table has no GUID column' do
0 commit comments