@@ -1162,4 +1162,97 @@ def test_get_resource_with_belongs_to_relationship_and_changed_primary_key
11621162 assert_equal 'access_cards' , included . first [ 'type' ]
11631163 assert_equal access_card . token , included . first [ 'id' ]
11641164 end
1165+
1166+ def test_update_option_link_with_optionable_include_optionable
1167+ android = Android . create! version_name : '1.0'
1168+ option = Option . create!
1169+ json_request = {
1170+ data : {
1171+ id : option . id . to_s ,
1172+ type : 'options' ,
1173+ relationships : {
1174+ optionable : {
1175+ data : {
1176+ id : android . id . to_s ,
1177+ type : 'androids'
1178+ }
1179+ }
1180+ }
1181+ }
1182+ }
1183+ json_api_headers = {
1184+ 'CONTENT_TYPE' => JSONAPI ::MEDIA_TYPE ,
1185+ 'Accept' => JSONAPI ::MEDIA_TYPE
1186+ }
1187+ patch "/options/#{ option . id } ?include=optionable" , params : json_request . to_json , headers : json_api_headers
1188+ assert_jsonapi_response 200
1189+ relationship_data = { 'id' => android . id . to_s , 'type' => 'androids' }
1190+ assert_equal relationship_data , json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] [ 'data' ]
1191+ assert_equal relationship_data , json_response [ 'included' ] . first . slice ( 'id' , 'type' )
1192+ assert_equal android , option . reload . optionable
1193+ end
1194+
1195+ def test_update_option_unlink_from_optionable_include_optionable
1196+ android = Android . create! version_name : '1.0'
1197+ option = Option . create! optionable : android
1198+ json_request = {
1199+ data : {
1200+ id : option . id . to_s ,
1201+ type : 'options' ,
1202+ relationships : {
1203+ optionable : {
1204+ data : nil
1205+ }
1206+ }
1207+ }
1208+ }
1209+ json_api_headers = {
1210+ 'CONTENT_TYPE' => JSONAPI ::MEDIA_TYPE ,
1211+ 'Accept' => JSONAPI ::MEDIA_TYPE
1212+ }
1213+ patch "/options/#{ option . id } ?include=optionable" , params : json_request . to_json , headers : json_api_headers
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+ assert_nil option . reload . optionable
1219+ end
1220+
1221+ def test_fetch_option_linked_with_optionable_include_optionable
1222+ android = Android . create! version_name : '1.0'
1223+ option = Option . create! optionable : android
1224+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=optionable"
1225+ assert_jsonapi_response 200
1226+ relationship_data = { 'id' => android . id . to_s , 'type' => 'androids' }
1227+ assert_equal relationship_data , json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] [ 'data' ]
1228+ assert_equal relationship_data , json_response [ 'included' ] . first . slice ( 'id' , 'type' )
1229+ end
1230+
1231+ def test_fetch_option_not_linked_with_optionable_include_optionable
1232+ option = Option . create!
1233+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=optionable"
1234+ assert_jsonapi_response 200
1235+ assert_equal true , json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] . has_key? ( 'data' )
1236+ assert_nil json_response [ 'data' ] [ 'relationships' ] [ 'optionable' ] [ 'data' ]
1237+ assert_equal false , json_response . has_key? ( 'included' )
1238+ end
1239+
1240+ def test_fetch_option_not_linked_with_maintainer_include_maintainer
1241+ option = Option . create!
1242+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=maintainer"
1243+ assert_jsonapi_response 200
1244+ assert_equal true , json_response [ 'data' ] [ 'relationships' ] [ 'maintainer' ] . has_key? ( 'data' )
1245+ assert_nil json_response [ 'data' ] [ 'relationships' ] [ 'maintainer' ] [ 'data' ]
1246+ assert_equal false , json_response . has_key? ( 'included' )
1247+ end
1248+
1249+ def test_fetch_option_linked_with_maintainer_include_maintainer
1250+ maintainer = Maintainer . create! name : 'John Doe'
1251+ option = Option . create! maintainer : maintainer
1252+ assert_cacheable_jsonapi_get "/options/#{ option . id } ?include=maintainer"
1253+ assert_jsonapi_response 200
1254+ relationship_data = { 'id' => maintainer . id . to_s , 'type' => 'maintainers' }
1255+ assert_equal relationship_data , json_response [ 'data' ] [ 'relationships' ] [ 'maintainer' ] [ 'data' ]
1256+ assert_equal relationship_data , json_response [ 'included' ] . first . slice ( 'id' , 'type' )
1257+ end
11651258end
0 commit comments