Skip to content

Commit 36d750b

Browse files
authored
Merge pull request #318 from senid231/add-test-for-update-resource
add tests for update resource for correct relationships dirty behavior
2 parents ec0fc21 + 17b9a05 commit 36d750b

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

test/unit/updating_test.rb

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ def relationships_for_serialization
88
end
99
end
1010

11+
class Editor < TestResource
12+
end
13+
1114
class CallbackTest < TestResource
1215
include JsonApiClient::Helpers::Callbacks
1316
before_update do
@@ -957,4 +960,135 @@ def test_can_update_with_includes_and_fields_with_keep_request_params
957960
Article.keep_request_params = false
958961
end
959962

963+
def test_fetch_with_relationships_and_update_attribute
964+
stub_request(:get, "http://example.com/authors/1?include=editor")
965+
.to_return(headers: {
966+
content_type: "application/vnd.api+json"
967+
}, body: {
968+
data: {
969+
type: "authors",
970+
id: "1",
971+
attributes: {
972+
name: "John Doe"
973+
},
974+
relationships: {
975+
editor: {
976+
links: {
977+
self: "/articles/1/links/editor",
978+
related: "/articles/1/editor"
979+
},
980+
data: {id: "2", type: "editors"}
981+
}
982+
}
983+
}
984+
}.to_json)
985+
986+
authors = Author.includes(:editor).find(1)
987+
author = authors.first
988+
989+
stub_request(:patch, "http://example.com/authors/1")
990+
.with(headers: {
991+
content_type: "application/vnd.api+json",
992+
accept: "application/vnd.api+json"
993+
}, body: {
994+
data: {
995+
id: "1",
996+
type: "authors",
997+
attributes: {
998+
name: "Jane Doe"
999+
}
1000+
}
1001+
}.to_json)
1002+
.to_return(headers: {
1003+
status: 200,
1004+
content_type: "application/vnd.api+json"
1005+
}, body: {
1006+
data: {
1007+
type: "authors",
1008+
id: "1",
1009+
relationships: {
1010+
editor: {
1011+
links: {
1012+
self: "/articles/1/links/editor",
1013+
related: "/articles/1/editor"
1014+
}
1015+
}
1016+
},
1017+
attributes: {
1018+
name: "Jane Doe"
1019+
}
1020+
}
1021+
}.to_json)
1022+
1023+
author.name = "Jane Doe"
1024+
assert author.save
1025+
end
1026+
1027+
def test_fetch_with_relationships_and_update_relationships
1028+
stub_request(:get, "http://example.com/authors/1?include=editor")
1029+
.to_return(headers: {
1030+
content_type: "application/vnd.api+json"
1031+
}, body: {
1032+
data: {
1033+
type: "authors",
1034+
id: "1",
1035+
attributes: {
1036+
name: "John Doe"
1037+
},
1038+
relationships: {
1039+
editor: {
1040+
links: {
1041+
self: "/articles/1/links/editor",
1042+
related: "/articles/1/editor"
1043+
},
1044+
data: {id: "2", type: "editors"}
1045+
}
1046+
}
1047+
}
1048+
}.to_json)
1049+
1050+
authors = Author.includes(:editor).find(1)
1051+
author = authors.first
1052+
1053+
stub_request(:patch, "http://example.com/authors/1")
1054+
.with(headers: {
1055+
content_type: "application/vnd.api+json",
1056+
accept: "application/vnd.api+json"
1057+
}, body: {
1058+
data: {
1059+
id: "1",
1060+
type: "authors",
1061+
relationships: {
1062+
editor: {
1063+
data: {type: "editors", id: "3"}
1064+
}
1065+
},
1066+
attributes: {}
1067+
}
1068+
}.to_json)
1069+
.to_return(headers: {
1070+
status: 200,
1071+
content_type: "application/vnd.api+json"
1072+
}, body: {
1073+
data: {
1074+
type: "authors",
1075+
id: "1",
1076+
relationships: {
1077+
editor: {
1078+
links: {
1079+
self: "/articles/1/links/editor",
1080+
related: "/articles/1/editor"
1081+
}
1082+
}
1083+
},
1084+
attributes: {
1085+
name: "John Doe"
1086+
}
1087+
}
1088+
}.to_json)
1089+
1090+
author.relationships.editor = Editor.new(id: '3')
1091+
assert author.save
1092+
end
1093+
9601094
end

0 commit comments

Comments
 (0)