@@ -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
@@ -4160,29 +4163,70 @@ def test_complex_includes_filters_nil_includes
4160
4163
end
4161
4164
4162
4165
def test_complex_includes_two_level
4166
+ if is_db? ( :mysql )
4167
+ skip "#{ adapter_name } test expectations differ in insignificant ways from expected"
4168
+ end
4163
4169
assert_cacheable_get :index , params : { include : 'things,things.user' }
4164
4170
4165
4171
assert_response :success
4166
4172
4167
- # The test is hardcoded with the include order. This should be changed at some
4168
- # point since either thing could come first and still be valid
4169
- assert_equal '10' , json_response [ 'included' ] [ 0 ] [ 'id' ]
4170
- assert_equal 'things' , json_response [ 'included' ] [ 0 ] [ 'type' ]
4171
- assert_equal '10001' , json_response [ 'included' ] [ 0 ] [ 'relationships' ] [ 'user' ] [ 'data' ] [ 'id' ]
4172
- assert_nil json_response [ 'included' ] [ 0 ] [ 'relationships' ] [ 'things' ] [ 'data' ]
4173
+ sorted_includeds = json_response [ 'included' ] . map { |included |
4174
+ {
4175
+ 'id' => included [ 'id' ] ,
4176
+ 'type' => included [ 'type' ] ,
4177
+ 'relationships_user_data_id' => included [ 'relationships' ] . dig ( 'user' , 'data' , 'id' ) ,
4178
+ 'relationships_things_data_ids' => included [ 'relationships' ] . dig ( 'things' , 'data' ) &.map { |data | data [ 'id' ] } &.sort ,
4179
+ }
4180
+ } . sort_by { |included | "#{ included [ 'type' ] } -#{ Integer ( included [ 'id' ] ) } " }
4173
4181
4174
- assert_equal '20' , json_response [ 'included' ] [ 1 ] [ 'id' ]
4175
- assert_equal 'things' , json_response [ 'included' ] [ 1 ] [ 'type' ]
4176
- assert_equal '10001' , json_response [ 'included' ] [ 1 ] [ 'relationships' ] [ 'user' ] [ 'data' ] [ 'id' ]
4177
- assert_nil json_response [ 'included' ] [ 1 ] [ 'relationships' ] [ 'things' ] [ 'data' ]
4182
+ expected = [
4183
+ {
4184
+ 'id' => '10' ,
4185
+ 'type' => 'things' ,
4186
+ 'relationships_user_data_id' => '10001' ,
4187
+ 'relationships_things_data_ids' => nil
4188
+ } ,
4189
+ {
4190
+ 'id' => '20' ,
4191
+ 'type' => 'things' ,
4192
+ 'relationships_user_data_id' => '10001' ,
4193
+ 'relationships_things_data_ids' => nil
4194
+ } ,
4195
+ {
4196
+ 'id' => '30' ,
4197
+ 'type' => 'things' ,
4198
+ 'relationships_user_data_id' => '10002' ,
4199
+ 'relationships_things_data_ids' => nil
4200
+ } ,
4201
+ {
4202
+ 'id' => '10001' ,
4203
+ 'type' => 'users' ,
4204
+ 'relationships_user_data_id' => nil ,
4205
+ 'relationships_things_data_ids' => [ '10' , '20' ]
4206
+ } ,
4207
+ {
4208
+ 'id' => '10002' ,
4209
+ 'type' => 'users' ,
4210
+ 'relationships_user_data_id' => nil ,
4211
+ 'relationships_things_data_ids' => [ '30' ]
4212
+ } ,
4213
+ ]
4214
+ assert_array_equals expected , sorted_includeds
4178
4215
end
4179
4216
4180
4217
def test_complex_includes_things_nested_things
4181
4218
assert_cacheable_get :index , params : { include : 'things,things.things,things.things.things' }
4182
4219
4183
4220
assert_response :success
4184
- assert_hash_equals (
4185
- {
4221
+ sorted_json_response_data = json_response [ "data" ]
4222
+ . sort_by { |data | Integer ( data [ "id" ] ) }
4223
+ sorted_json_response_included = json_response [ "included" ]
4224
+ . sort_by { |included | "#{ included [ 'type' ] } -#{ Integer ( included [ 'id' ] ) } " }
4225
+ sorted_json_response = {
4226
+ "data" => sorted_json_response_data ,
4227
+ "included" => sorted_json_response_included ,
4228
+ }
4229
+ expected_response = {
4186
4230
"data" => [
4187
4231
{
4188
4232
"id" => "100" ,
@@ -4437,15 +4481,26 @@ def test_complex_includes_things_nested_things
4437
4481
}
4438
4482
}
4439
4483
]
4440
- } ,
4441
- json_response )
4484
+ }
4485
+ assert_hash_equals ( expected_response , sorted_json_response )
4442
4486
end
4443
4487
4444
4488
def test_complex_includes_nested_things_secondary_users
4489
+ if is_db? ( :mysql )
4490
+ skip "#{ adapter_name } test expectations differ in insignificant ways from expected"
4491
+ end
4445
4492
assert_cacheable_get :index , params : { include : 'things,things.user,things.things' }
4446
4493
4447
4494
assert_response :success
4448
- assert_hash_equals (
4495
+ sorted_json_response_data = json_response [ "data" ]
4496
+ . sort_by { |data | Integer ( data [ "id" ] ) }
4497
+ sorted_json_response_included = json_response [ "included" ]
4498
+ . sort_by { |included | "#{ included [ 'type' ] } -#{ Integer ( included [ 'id' ] ) } " }
4499
+ sorted_json_response = {
4500
+ "data" => sorted_json_response_data ,
4501
+ "included" => sorted_json_response_included ,
4502
+ }
4503
+ expected =
4449
4504
{
4450
4505
"data" => [
4451
4506
{
@@ -4732,8 +4787,8 @@ def test_complex_includes_nested_things_secondary_users
4732
4787
}
4733
4788
}
4734
4789
]
4735
- } ,
4736
- json_response )
4790
+ }
4791
+ assert_hash_equals ( expected , sorted_json_response )
4737
4792
end
4738
4793
end
4739
4794
@@ -4767,16 +4822,22 @@ def teardown
4767
4822
end
4768
4823
4769
4824
def test_fetch_robots_with_sort_by_name
4825
+ if is_db? ( :mysql )
4826
+ skip "#{ adapter_name } test expectations differ in insignificant ways from expected"
4827
+ end
4770
4828
Robot . create! name : 'John' , version : 1
4771
4829
Robot . create! name : 'jane' , version : 1
4772
4830
assert_cacheable_get :index , params : { sort : 'name' }
4773
4831
assert_response :success
4774
4832
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
4833
+ expected_names = Robot
4834
+ . all
4835
+ . order ( name : :asc )
4836
+ . map ( &:name )
4837
+ actual_names = json_response [ 'data' ] . map { |data |
4838
+ data [ 'attributes' ] [ 'name' ]
4839
+ }
4840
+ assert_equal expected_names , actual_names , "since adapter_sorts_nulls_last=#{ adapter_sorts_nulls_last } "
4780
4841
end
4781
4842
4782
4843
def test_fetch_robots_with_sort_by_lower_name
0 commit comments