@@ -6,6 +6,7 @@ def set_content_type_header!
66
77class PostsControllerTest < ActionController ::TestCase
88 def setup
9+ super
910 JSONAPI . configuration . raise_if_parameters_not_allowed = true
1011 JSONAPI . configuration . always_include_to_one_linkage_data = false
1112 end
@@ -445,7 +446,7 @@ def test_sorting_asc
445446 assert_cacheable_get :index , params : { sort : 'title' }
446447
447448 assert_response :success
448- assert_equal "A First Post" , json_response [ 'data' ] [ 0 ] [ 'attributes' ] [ 'title' ]
449+ assert_equal "A 1ST Post" , json_response [ 'data' ] [ 0 ] [ 'attributes' ] [ 'title' ]
449450 end
450451
451452 def test_sorting_desc
@@ -459,7 +460,7 @@ def test_sorting_by_multiple_fields
459460 assert_cacheable_get :index , params : { sort : 'title,body' }
460461
461462 assert_response :success
462- assert_equal '14 ' , json_response [ 'data' ] [ 0 ] [ 'id' ]
463+ assert_equal '15 ' , json_response [ 'data' ] [ 0 ] [ 'id' ]
463464 end
464465
465466 def create_alphabetically_first_user_and_post
@@ -473,8 +474,15 @@ def test_sorting_by_relationship_field
473474
474475 assert_response :success
475476 assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
476- assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the top'
477- assert_equal post . id . to_s , json_response [ 'data' ] [ 1 ] [ 'id' ] , 'alphabetically first user is second'
477+
478+ # Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
479+ if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
480+ assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the start'
481+ assert_equal post . id . to_s , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'alphabetically first user is not first'
482+ else
483+ assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the end'
484+ assert_equal post . id . to_s , json_response [ 'data' ] [ 1 ] [ 'id' ] , 'alphabetically first user is second'
485+ end
478486 end
479487
480488 def test_desc_sorting_by_relationship_field
@@ -483,8 +491,15 @@ def test_desc_sorting_by_relationship_field
483491
484492 assert_response :success
485493 assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
486- assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the bottom'
487- assert_equal post . id . to_s , json_response [ 'data' ] [ -2 ] [ 'id' ] , 'alphabetically first user is second last'
494+
495+ # Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
496+ if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
497+ assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the start'
498+ assert_equal post . id . to_s , json_response [ 'data' ] [ -1 ] [ 'id' ]
499+ else
500+ assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the end'
501+ assert_equal post . id . to_s , json_response [ 'data' ] [ -2 ] [ 'id' ] , 'alphabetically first user is second last'
502+ end
488503 end
489504
490505 def test_sorting_by_relationship_field_include
@@ -493,8 +508,14 @@ def test_sorting_by_relationship_field_include
493508
494509 assert_response :success
495510 assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
496- assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the top'
497- assert_equal post . id . to_s , json_response [ 'data' ] [ 1 ] [ 'id' ] , 'alphabetically first user is second'
511+
512+ if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
513+ assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the top'
514+ assert_equal post . id . to_s , json_response [ 'data' ] [ 0 ] [ 'id' ]
515+ else
516+ assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the top'
517+ assert_equal post . id . to_s , json_response [ 'data' ] [ 1 ] [ 'id' ] , 'alphabetically first user is second'
518+ end
498519 end
499520
500521 def test_invalid_sort_param
@@ -3107,7 +3128,7 @@ def test_type_formatting
31073128 assert json_response [ 'data' ] . is_a? ( Hash )
31083129 assert_equal 'Jane Author' , json_response [ 'data' ] [ 'attributes' ] [ 'spouseName' ]
31093130 assert_equal 'First man to run across Antartica.' , json_response [ 'data' ] [ 'attributes' ] [ 'bio' ]
3110- assert_equal 23.89 /45.6 , json_response [ 'data' ] [ 'attributes' ] [ 'qualityRating' ]
3131+ assert_equal ( 23.89 /45.6 ) . round ( 5 ) , json_response [ 'data' ] [ 'attributes' ] [ 'qualityRating' ] . round ( 5 )
31113132 assert_equal '47000.56' , json_response [ 'data' ] [ 'attributes' ] [ 'salary' ]
31123133 assert_equal '2013-08-07T20:25:00.000Z' , json_response [ 'data' ] [ 'attributes' ] [ 'dateTimeJoined' ]
31133134 assert_equal '1965-06-30' , json_response [ 'data' ] [ 'attributes' ] [ 'birthday' ]
@@ -4707,7 +4728,12 @@ def test_fetch_robots_with_sort_by_name
47074728 Robot . create! name : 'jane' , version : 1
47084729 assert_cacheable_get :index , params : { sort : 'name' }
47094730 assert_response :success
4710- assert_equal 'John' , json_response [ 'data' ] . first [ 'attributes' ] [ 'name' ]
4731+
4732+ if ENV [ 'DATABASE_URL' ] . starts_with? ( 'postgres' )
4733+ assert_equal 'jane' , json_response [ 'data' ] . first [ 'attributes' ] [ 'name' ]
4734+ else
4735+ assert_equal 'John' , json_response [ 'data' ] . first [ 'attributes' ] [ 'name' ]
4736+ end
47114737 end
47124738
47134739 def test_fetch_robots_with_sort_by_lower_name
0 commit comments