@@ -1142,4 +1142,97 @@ def test_get_resource_with_belongs_to_relationship_and_changed_primary_key
1142
1142
assert_equal 'access_cards' , included . first [ 'type' ]
1143
1143
assert_equal access_card . token , included . first [ 'id' ]
1144
1144
end
1145
+
1146
+ def test_update_option_link_with_optionable_include_optionable
1147
+ android = Android . create! version_name : '1.0'
1148
+ option = Option . create!
1149
+ json_request = {
1150
+ data : {
1151
+ id : option . id . to_s ,
1152
+ type : 'options' ,
1153
+ relationships : {
1154
+ optionable : {
1155
+ data : {
1156
+ id : android . id . to_s ,
1157
+ type : 'androids'
1158
+ }
1159
+ }
1160
+ }
1161
+ }
1162
+ }
1163
+ json_api_headers = {
1164
+ 'CONTENT_TYPE' => JSONAPI ::MEDIA_TYPE ,
1165
+ 'Accept' => JSONAPI ::MEDIA_TYPE
1166
+ }
1167
+ patch "/options/#{ option . id } ?include=optionable" , params : json_request . to_json , headers : json_api_headers
1168
+ assert_jsonapi_response 200
1169
+ relationship_data = { 'id' => android . id . to_s , 'type' => 'androids' }
1170
+ assert_equal relationship_data , json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] [ 'data' ]
1171
+ assert_equal relationship_data , json_response [ 'included' ] . first . slice ( 'id' , 'type' )
1172
+ assert_equal android , option . reload . optionable
1173
+ end
1174
+
1175
+ def test_update_option_unlink_from_optionable_include_optionable
1176
+ android = Android . create! version_name : '1.0'
1177
+ option = Option . create! optionable : android
1178
+ json_request = {
1179
+ data : {
1180
+ id : option . id . to_s ,
1181
+ type : 'options' ,
1182
+ relationships : {
1183
+ optionable : {
1184
+ data : nil
1185
+ }
1186
+ }
1187
+ }
1188
+ }
1189
+ json_api_headers = {
1190
+ 'CONTENT_TYPE' => JSONAPI ::MEDIA_TYPE ,
1191
+ 'Accept' => JSONAPI ::MEDIA_TYPE
1192
+ }
1193
+ patch "/options/#{ option . id } ?include=optionable" , params : json_request . to_json , headers : json_api_headers
1194
+ assert_jsonapi_response 200
1195
+ assert_equal true , json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] . has_key? ( 'data' )
1196
+ assert_nil json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] [ 'data' ]
1197
+ assert_equal false , json_response . has_key? ( 'included' )
1198
+ assert_nil option . reload . optionable
1199
+ end
1200
+
1201
+ def test_fetch_option_linked_with_optionable_include_optionable
1202
+ android = Android . create! version_name : '1.0'
1203
+ option = Option . create! optionable : android
1204
+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=optionable"
1205
+ assert_jsonapi_response 200
1206
+ relationship_data = { 'id' => android . id . to_s , 'type' => 'androids' }
1207
+ assert_equal relationship_data , json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] [ 'data' ]
1208
+ assert_equal relationship_data , json_response [ 'included' ] . first . slice ( 'id' , 'type' )
1209
+ end
1210
+
1211
+ def test_fetch_option_not_linked_with_optionable_include_optionable
1212
+ option = Option . create!
1213
+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=optionable"
1214
+ assert_jsonapi_response 200
1215
+ assert_equal true , json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] . has_key? ( 'data' )
1216
+ assert_nil json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] [ 'data' ]
1217
+ assert_equal false , json_response . has_key? ( 'included' )
1218
+ end
1219
+
1220
+ def test_fetch_option_not_linked_with_maintainer_include_maintainer
1221
+ option = Option . create!
1222
+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=maintainer"
1223
+ assert_jsonapi_response 200
1224
+ assert_equal true , json_response [ 'data' ] [ 'relationships' ] [ 'maintainer' ] . has_key? ( 'data' )
1225
+ assert_nil json_response [ 'data' ] [ 'relationships' ] [ 'maintainer' ] [ 'data' ]
1226
+ assert_equal false , json_response . has_key? ( 'included' )
1227
+ end
1228
+
1229
+ def test_fetch_option_linked_with_maintainer_include_maintainer
1230
+ maintainer = Maintainer . create! name : 'John Doe'
1231
+ option = Option . create! maintainer : maintainer
1232
+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=maintainer"
1233
+ assert_jsonapi_response 200
1234
+ relationship_data = { 'id' => maintainer . id . to_s , 'type' => 'maintainers' }
1235
+ assert_equal relationship_data , json_response [ 'data' ] [ 'relationships' ] [ 'maintainer' ] [ 'data' ]
1236
+ assert_equal relationship_data , json_response [ 'included' ] . first . slice ( 'id' , 'type' )
1237
+ end
1145
1238
end
0 commit comments