Skip to content

add tests for update resource for correct relationships dirty behavior #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions test/unit/updating_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ def relationships_for_serialization
end
end

class Editor < TestResource
end

class CallbackTest < TestResource
include JsonApiClient::Helpers::Callbacks
before_update do
Expand Down Expand Up @@ -957,4 +960,135 @@ def test_can_update_with_includes_and_fields_with_keep_request_params
Article.keep_request_params = false
end

def test_fetch_with_relationships_and_update_attribute
stub_request(:get, "http://example.com/authors/1?include=editor")
.to_return(headers: {
content_type: "application/vnd.api+json"
}, body: {
data: {
type: "authors",
id: "1",
attributes: {
name: "John Doe"
},
relationships: {
editor: {
links: {
self: "/articles/1/links/editor",
related: "/articles/1/editor"
},
data: {id: "2", type: "editors"}
}
}
}
}.to_json)

authors = Author.includes(:editor).find(1)
author = authors.first

stub_request(:patch, "http://example.com/authors/1")
.with(headers: {
content_type: "application/vnd.api+json",
accept: "application/vnd.api+json"
}, body: {
data: {
id: "1",
type: "authors",
attributes: {
name: "Jane Doe"
}
}
}.to_json)
.to_return(headers: {
status: 200,
content_type: "application/vnd.api+json"
}, body: {
data: {
type: "authors",
id: "1",
relationships: {
editor: {
links: {
self: "/articles/1/links/editor",
related: "/articles/1/editor"
}
}
},
attributes: {
name: "Jane Doe"
}
}
}.to_json)

author.name = "Jane Doe"
assert author.save
end

def test_fetch_with_relationships_and_update_relationships
stub_request(:get, "http://example.com/authors/1?include=editor")
.to_return(headers: {
content_type: "application/vnd.api+json"
}, body: {
data: {
type: "authors",
id: "1",
attributes: {
name: "John Doe"
},
relationships: {
editor: {
links: {
self: "/articles/1/links/editor",
related: "/articles/1/editor"
},
data: {id: "2", type: "editors"}
}
}
}
}.to_json)

authors = Author.includes(:editor).find(1)
author = authors.first

stub_request(:patch, "http://example.com/authors/1")
.with(headers: {
content_type: "application/vnd.api+json",
accept: "application/vnd.api+json"
}, body: {
data: {
id: "1",
type: "authors",
relationships: {
editor: {
data: {type: "editors", id: "3"}
}
},
attributes: {}
}
}.to_json)
.to_return(headers: {
status: 200,
content_type: "application/vnd.api+json"
}, body: {
data: {
type: "authors",
id: "1",
relationships: {
editor: {
links: {
self: "/articles/1/links/editor",
related: "/articles/1/editor"
}
}
},
attributes: {
name: "John Doe"
}
}
}.to_json)

author.relationships.editor = Editor.new(id: '3')
assert author.save
end

end