@@ -494,15 +494,15 @@ def test_sorting_by_relationship_field
494
494
495
495
assert_response :success
496
496
assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
497
+ expected = Post
498
+ . all
499
+ . left_joins ( :author )
500
+ . merge ( Person . order ( name : :asc ) )
501
+ . map ( &:id )
502
+ . map ( &:to_s )
503
+ ids = json_response [ 'data' ] . map { |data | data [ 'id' ] }
497
504
498
- # Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
499
- if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
500
- assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the start'
501
- assert_equal post . id . to_s , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'alphabetically first user is not first'
502
- else
503
- assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the end'
504
- assert_equal post . id . to_s , json_response [ 'data' ] [ 1 ] [ 'id' ] , 'alphabetically first user is second'
505
- end
505
+ assert_equal expected , ids , "since adapter_sorts_nulls_last=#{ adapter_sorts_nulls_last } "
506
506
end
507
507
508
508
def test_desc_sorting_by_relationship_field
@@ -512,14 +512,15 @@ def test_desc_sorting_by_relationship_field
512
512
assert_response :success
513
513
assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
514
514
515
- # Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
516
- if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
517
- assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the start'
518
- assert_equal post . id . to_s , json_response [ 'data' ] [ -1 ] [ 'id' ]
519
- else
520
- assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the end'
521
- assert_equal post . id . to_s , json_response [ 'data' ] [ -2 ] [ 'id' ] , 'alphabetically first user is second last'
522
- end
515
+ expected = Post
516
+ . all
517
+ . left_joins ( :author )
518
+ . merge ( Person . order ( name : :desc ) )
519
+ . map ( &:id )
520
+ . map ( &:to_s )
521
+ ids = json_response [ 'data' ] . map { |data | data [ 'id' ] }
522
+
523
+ assert_equal expected , ids , "since adapter_sorts_nulls_last=#{ adapter_sorts_nulls_last } "
523
524
end
524
525
525
526
def test_sorting_by_relationship_field_include
@@ -529,13 +530,15 @@ def test_sorting_by_relationship_field_include
529
530
assert_response :success
530
531
assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
531
532
532
- if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
533
- assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the top'
534
- assert_equal post . id . to_s , json_response [ 'data' ] [ 0 ] [ 'id' ]
535
- else
536
- assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the top'
537
- assert_equal post . id . to_s , json_response [ 'data' ] [ 1 ] [ 'id' ] , 'alphabetically first user is second'
538
- end
533
+ expected = Post
534
+ . all
535
+ . left_joins ( :author )
536
+ . merge ( Person . order ( name : :asc ) )
537
+ . map ( &:id )
538
+ . map ( &:to_s )
539
+ ids = json_response [ 'data' ] . map { |data | data [ 'id' ] }
540
+
541
+ assert_equal expected , ids , "since adapter_sorts_nulls_last=#{ adapter_sorts_nulls_last } "
539
542
end
540
543
541
544
def test_invalid_sort_param
@@ -4772,11 +4775,11 @@ def test_fetch_robots_with_sort_by_name
4772
4775
assert_cacheable_get :index , params : { sort : 'name' }
4773
4776
assert_response :success
4774
4777
4775
- if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
4776
- assert_equal 'jane' , json_response [ 'data' ] . first [ 'attributes' ] [ 'name' ]
4777
- else
4778
- assert_equal 'John' , json_response [ 'data' ] . first [ 'attributes' ] [ ' name' ]
4779
- end
4778
+ expected_names = Robot
4779
+ . all
4780
+ . order ( name : :asc )
4781
+ . map ( & : name)
4782
+ assert_equal expected_names . first , json_response [ 'data' ] . first [ 'attributes' ] [ 'name' ] , "since adapter_sorts_nulls_last= #{ adapter_sorts_nulls_last } "
4780
4783
end
4781
4784
4782
4785
def test_fetch_robots_with_sort_by_lower_name
0 commit comments