@@ -494,15 +494,15 @@ def test_sorting_by_relationship_field
494494
495495 assert_response :success
496496 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' ] }
497504
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 } "
506506 end
507507
508508 def test_desc_sorting_by_relationship_field
@@ -512,14 +512,15 @@ def test_desc_sorting_by_relationship_field
512512 assert_response :success
513513 assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
514514
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 } "
523524 end
524525
525526 def test_sorting_by_relationship_field_include
@@ -529,13 +530,15 @@ def test_sorting_by_relationship_field_include
529530 assert_response :success
530531 assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
531532
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 } "
539542 end
540543
541544 def test_invalid_sort_param
@@ -4772,11 +4775,11 @@ def test_fetch_robots_with_sort_by_name
47724775 assert_cacheable_get :index , params : { sort : 'name' }
47734776 assert_response :success
47744777
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 } "
47804783 end
47814784
47824785 def test_fetch_robots_with_sort_by_lower_name
0 commit comments