mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Improve readability of relationships test (#1904)
* Really fix intermittent relationship test failures * Unify the two JSON API relationship test files I accidentally introduced the duplication when merging https://github.com/rails-api/active_model_serializers/pull/1543#issuecomment-193118782 and they've evolved separately since then. They both have some value and need to be combined. * Resolve duplicate tests from diverged tests files * No longer test Association/Relationship interface directly
This commit is contained in:
parent
2145540795
commit
c69855bfaa
@ -4,13 +4,6 @@ module ActiveModelSerializers
|
|||||||
module Adapter
|
module Adapter
|
||||||
class JsonApi
|
class JsonApi
|
||||||
class RelationshipTest < ActiveSupport::TestCase
|
class RelationshipTest < ActiveSupport::TestCase
|
||||||
setup do
|
|
||||||
@blog = Blog.new(id: 1)
|
|
||||||
@author = Author.new(id: 1, name: 'Steve K.', blog: @blog)
|
|
||||||
@serializer = BlogSerializer.new(@blog)
|
|
||||||
ActionController::Base.cache_store.clear
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_with_data
|
def test_relationship_with_data
|
||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: {
|
||||||
@ -18,26 +11,29 @@ module ActiveModelSerializers
|
|||||||
type: 'blogs'
|
type: 'blogs'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test_relationship(expected, options: { include_data: true })
|
|
||||||
|
model_attributes = { blog: Blog.new(id: 1) }
|
||||||
|
relationship_name = :blog
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_with_nil_model
|
def test_relationship_with_nil_model
|
||||||
@serializer = BlogSerializer.new(nil)
|
|
||||||
expected = { data: nil }
|
expected = { data: nil }
|
||||||
test_relationship(expected, options: { include_data: true })
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_with_nil_serializer
|
model_attributes = { blog: nil }
|
||||||
@serializer = nil
|
relationship_name = :blog
|
||||||
expected = { data: nil }
|
model = new_model(model_attributes)
|
||||||
test_relationship(expected, options: { include_data: true })
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_with_data_array
|
def test_relationship_with_data_array
|
||||||
posts = [Post.new(id: 1), Post.new(id: 2)]
|
|
||||||
@serializer = ActiveModel::Serializer::CollectionSerializer.new(posts)
|
|
||||||
@author.posts = posts
|
|
||||||
@author.blog = nil
|
|
||||||
expected = {
|
expected = {
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
@ -50,126 +46,350 @@ module ActiveModelSerializers
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
test_relationship(expected, options: { include_data: true })
|
|
||||||
|
model_attributes = { posts: [Post.new(id: 1), Post.new(id: 2)] }
|
||||||
|
relationship_name = :posts
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_many :posts
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_data_not_included
|
def test_relationship_data_not_included
|
||||||
test_relationship({ meta: {} }, options: { include_data: false })
|
expected = { meta: {} }
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_simple_link
|
model_attributes = { blog: :does_not_matter }
|
||||||
links = { self: 'a link' }
|
relationship_name = :blog
|
||||||
test_relationship({ links: { self: 'a link' } }, links: links)
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog do
|
||||||
|
include_data false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_many_links
|
def test_relationship_many_links
|
||||||
links = {
|
|
||||||
self: 'a link',
|
|
||||||
related: 'another link'
|
|
||||||
}
|
|
||||||
expected = {
|
expected = {
|
||||||
links: {
|
links: {
|
||||||
self: 'a link',
|
self: 'a link',
|
||||||
related: 'another link'
|
related: 'another link'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test_relationship(expected, links: links)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_block_link
|
model_attributes = { blog: :does_not_matter }
|
||||||
links = { self: proc { object.id.to_s } }
|
relationship_name = :blog
|
||||||
expected = { links: { self: @blog.id.to_s } }
|
model = new_model(model_attributes)
|
||||||
test_relationship(expected, links: links)
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog do
|
||||||
|
include_data false
|
||||||
|
link :self, 'a link'
|
||||||
|
link :related, 'another link'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_block_link_with_meta
|
def test_relationship_block_link_with_meta
|
||||||
links = {
|
|
||||||
self: proc do
|
|
||||||
href object.id.to_s
|
|
||||||
meta(id: object.id)
|
|
||||||
end
|
|
||||||
}
|
|
||||||
expected = {
|
expected = {
|
||||||
links: {
|
links: {
|
||||||
self: {
|
self: {
|
||||||
href: @blog.id.to_s,
|
href: '1',
|
||||||
meta: { id: @blog.id }
|
meta: { id: 1 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test_relationship(expected, links: links)
|
|
||||||
|
model_attributes = { blog: Blog.new(id: 1) }
|
||||||
|
relationship_name = :blog
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog do
|
||||||
|
include_data false
|
||||||
|
link :self do
|
||||||
|
href object.blog.id.to_s
|
||||||
|
meta(id: object.blog.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_simple_meta
|
def test_relationship_simple_meta
|
||||||
meta = { id: '1' }
|
expected = { meta: { id: '1' } }
|
||||||
expected = { meta: meta }
|
|
||||||
test_relationship(expected, meta: meta)
|
model_attributes = { blog: Blog.new(id: 1) }
|
||||||
|
relationship_name = :blog
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog do
|
||||||
|
include_data false
|
||||||
|
meta(id: object.blog.id.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_block_meta
|
def test_relationship_block_meta
|
||||||
meta = proc do
|
|
||||||
{ id: object.id }
|
|
||||||
end
|
|
||||||
expected = {
|
expected = {
|
||||||
meta: {
|
meta: {
|
||||||
id: @blog.id
|
id: 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test_relationship(expected, meta: meta)
|
|
||||||
|
model_attributes = { blog: Blog.new(id: 1) }
|
||||||
|
relationship_name = :blog
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog do
|
||||||
|
include_data false
|
||||||
|
meta(id: object.blog.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_simple_link
|
||||||
|
expected = {
|
||||||
|
data: {
|
||||||
|
id: '1337',
|
||||||
|
type: 'bios'
|
||||||
|
},
|
||||||
|
links: {
|
||||||
|
self: '//example.com/link_author/relationships/bio'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { bio: Bio.new(id: 1337) }
|
||||||
|
relationship_name = :bio
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :bio do
|
||||||
|
link :self, '//example.com/link_author/relationships/bio'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_block_link
|
||||||
|
expected = {
|
||||||
|
data: { id: '1337', type: 'profiles' },
|
||||||
|
links: { related: '//example.com/profiles/1337' }
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { profile: Profile.new(id: 1337) }
|
||||||
|
relationship_name = :profile
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :profile do
|
||||||
|
id = object.profile.id
|
||||||
|
link :related do
|
||||||
|
"//example.com/profiles/#{id}" if id != 123
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_with_everything
|
def test_relationship_with_everything
|
||||||
links = {
|
|
||||||
self: 'a link',
|
|
||||||
related: proc do
|
|
||||||
href object.id.to_s
|
|
||||||
meta object.id
|
|
||||||
end
|
|
||||||
|
|
||||||
}
|
|
||||||
meta = proc do
|
|
||||||
{ id: object.id }
|
|
||||||
end
|
|
||||||
expected = {
|
expected = {
|
||||||
data: {
|
data: [{ id: '1337', type: 'likes' }],
|
||||||
id: '1',
|
|
||||||
type: 'blogs'
|
|
||||||
},
|
|
||||||
links: {
|
links: {
|
||||||
self: 'a link',
|
|
||||||
related: {
|
related: {
|
||||||
href: '1', meta: 1
|
href: '//example.com/likes/1337',
|
||||||
|
meta: { ids: '1337' }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
meta: {
|
meta: { liked: true }
|
||||||
id: @blog.id
|
}
|
||||||
|
|
||||||
|
model_attributes = { likes: [Like.new(id: 1337)] }
|
||||||
|
relationship_name = :likes
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_many :likes do
|
||||||
|
link :related do
|
||||||
|
ids = object.likes.map(&:id).join(',')
|
||||||
|
href "//example.com/likes/#{ids}"
|
||||||
|
meta ids: ids
|
||||||
|
end
|
||||||
|
meta liked: object.likes.any?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_nil_link
|
||||||
|
expected = {
|
||||||
|
data: { id: '123', type: 'profiles' }
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { profile: Profile.new(id: 123) }
|
||||||
|
relationship_name = :profile
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :profile do
|
||||||
|
id = object.profile.id
|
||||||
|
link :related do
|
||||||
|
"//example.com/profiles/#{id}" if id != 123
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_block_link_href
|
||||||
|
expected = {
|
||||||
|
data: [{ id: '1337', type: 'locations' }],
|
||||||
|
links: {
|
||||||
|
related: { href: '//example.com/locations/1337' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
test_relationship(expected, meta: meta, options: { include_data: true }, links: links)
|
|
||||||
|
model_attributes = { locations: [Location.new(id: 1337)] }
|
||||||
|
relationship_name = :locations
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_many :locations do
|
||||||
|
link :related do
|
||||||
|
ids = object.locations.map(&:id).join(',')
|
||||||
|
href "//example.com/locations/#{ids}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_block_link_href_and_meta
|
||||||
|
expected = {
|
||||||
|
data: [{ id: '1337', type: 'posts' }],
|
||||||
|
links: {
|
||||||
|
related: {
|
||||||
|
href: '//example.com/posts/1337',
|
||||||
|
meta: { ids: '1337' }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { posts: [Post.new(id: 1337, comments: [], author: nil)] }
|
||||||
|
relationship_name = :posts
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_many :posts do
|
||||||
|
link :related do
|
||||||
|
ids = object.posts.map(&:id).join(',')
|
||||||
|
href "//example.com/posts/#{ids}"
|
||||||
|
meta ids: ids
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_block_link_meta
|
||||||
|
expected = {
|
||||||
|
data: [{ id: '1337', type: 'comments' }],
|
||||||
|
links: {
|
||||||
|
self: {
|
||||||
|
meta: { ids: [1] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { comments: [Comment.new(id: 1337)] }
|
||||||
|
relationship_name = :comments
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_many :comments do
|
||||||
|
link :self do
|
||||||
|
meta ids: [1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_meta
|
||||||
|
expected = {
|
||||||
|
data: [{ id: 'from-serializer-method', type: 'roles' }],
|
||||||
|
meta: { count: 1 }
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { roles: [Role.new(id: 'from-record')] }
|
||||||
|
relationship_name = :roles
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_many :roles do |serializer|
|
||||||
|
meta count: object.roles.count
|
||||||
|
serializer.cached_roles
|
||||||
|
end
|
||||||
|
def cached_roles
|
||||||
|
[
|
||||||
|
Role.new(id: 'from-serializer-method')
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_not_including_data
|
||||||
|
expected = {
|
||||||
|
links: { self: '//example.com/link_author/relationships/blog' }
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { blog: Object }
|
||||||
|
relationship_name = :blog
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
model.define_singleton_method(:read_attribute_for_serialization) do |attr|
|
||||||
|
fail 'should not be called' if attr == :blog
|
||||||
|
super(attr)
|
||||||
|
end
|
||||||
|
assert_nothing_raised do
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
has_one :blog do
|
||||||
|
link :self, '//example.com/link_author/relationships/blog'
|
||||||
|
include_data false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_relationship_including_data_explicit
|
||||||
|
expected = {
|
||||||
|
data: { id: '1337', type: 'authors' },
|
||||||
|
meta: { name: 'Dan Brown' }
|
||||||
|
}
|
||||||
|
|
||||||
|
model_attributes = { reviewer: Author.new(id: 1337) }
|
||||||
|
relationship_name = :reviewer
|
||||||
|
model = new_model(model_attributes)
|
||||||
|
actual = build_serializer_and_serialize_relationship(model, relationship_name) do
|
||||||
|
belongs_to :reviewer do
|
||||||
|
meta name: 'Dan Brown'
|
||||||
|
include_data true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(expected, actual)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def test_relationship(expected, test_options = {})
|
def build_serializer_and_serialize_relationship(model, relationship_name, &block)
|
||||||
parent_serializer = AuthorSerializer.new(@author)
|
serializer_class = Class.new(ActiveModel::Serializer, &block)
|
||||||
|
hash = serializable(model, serializer: serializer_class, adapter: :json_api).serializable_hash
|
||||||
|
hash[:data][:relationships][relationship_name]
|
||||||
|
end
|
||||||
|
|
||||||
serializable_resource_options = {} # adapter.instance_options
|
def new_model(model_attributes)
|
||||||
|
Class.new(ActiveModelSerializers::Model) do
|
||||||
|
attr_accessor(*model_attributes.keys)
|
||||||
|
|
||||||
options = test_options.delete(:options) || {}
|
def self.name
|
||||||
options[:links] = test_options.delete(:links)
|
'TestModel'
|
||||||
options[:meta] = test_options.delete(:meta)
|
end
|
||||||
association_serializer = @serializer
|
end.new(model_attributes)
|
||||||
if association_serializer && association_serializer.object
|
|
||||||
association_name = association_serializer.json_key.to_sym
|
|
||||||
options[:serializer] = association_serializer
|
|
||||||
association = ::ActiveModel::Serializer::Association.new(association_name, options, nil)
|
|
||||||
else
|
|
||||||
options[:serializer] = association
|
|
||||||
association = ::ActiveModel::Serializer::Association.new(:association_name_not_used, options, nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
relationship = Relationship.new(parent_serializer, serializable_resource_options, association)
|
|
||||||
assert_equal(expected, relationship.as_json)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,234 +0,0 @@
|
|||||||
require 'test_helper'
|
|
||||||
|
|
||||||
module ActiveModel
|
|
||||||
class Serializer
|
|
||||||
module Adapter
|
|
||||||
class JsonApi
|
|
||||||
class RelationshipTest < ActiveSupport::TestCase
|
|
||||||
class RelationshipAuthor < ::Model; end
|
|
||||||
|
|
||||||
class RelationshipAuthorSerializer < ActiveModel::Serializer
|
|
||||||
has_one :bio do
|
|
||||||
link :self, '//example.com/link_author/relationships/bio'
|
|
||||||
end
|
|
||||||
|
|
||||||
has_one :profile do
|
|
||||||
id = object.profile.id
|
|
||||||
link :related do
|
|
||||||
"//example.com/profiles/#{id}" if id != 123
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
has_many :locations do
|
|
||||||
link :related do
|
|
||||||
ids = object.locations.map(&:id).join(',')
|
|
||||||
href "//example.com/locations/#{ids}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
has_many :posts do
|
|
||||||
link :related do
|
|
||||||
ids = object.posts.map(&:id).join(',')
|
|
||||||
href "//example.com/posts/#{ids}"
|
|
||||||
meta ids: ids
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
has_many :comments do
|
|
||||||
link :self do
|
|
||||||
meta ids: [1]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
has_many :roles do |serializer|
|
|
||||||
meta count: object.posts.count
|
|
||||||
serializer.cached_roles
|
|
||||||
end
|
|
||||||
|
|
||||||
has_one :blog do
|
|
||||||
link :self, '//example.com/link_author/relationships/blog'
|
|
||||||
include_data false
|
|
||||||
end
|
|
||||||
|
|
||||||
belongs_to :reviewer do
|
|
||||||
meta name: 'Dan Brown'
|
|
||||||
include_data true
|
|
||||||
end
|
|
||||||
|
|
||||||
has_many :likes do
|
|
||||||
link :related do
|
|
||||||
ids = object.likes.map(&:id).join(',')
|
|
||||||
href "//example.com/likes/#{ids}"
|
|
||||||
meta ids: ids
|
|
||||||
end
|
|
||||||
meta liked: object.likes.any?
|
|
||||||
end
|
|
||||||
|
|
||||||
def cached_roles
|
|
||||||
[
|
|
||||||
Role.new(id: 'from-serializer-method')
|
|
||||||
]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def setup
|
|
||||||
@post = Post.new(id: 1337, comments: [], author: nil)
|
|
||||||
@bio = Bio.new(id: 1337)
|
|
||||||
@like = Like.new(id: 1337)
|
|
||||||
@role = Role.new(id: 'from-record')
|
|
||||||
@profile = Profile.new(id: 1337)
|
|
||||||
@location = Location.new(id: 1337)
|
|
||||||
@reviewer = Author.new(id: 1337)
|
|
||||||
@comment = Comment.new(id: 1337)
|
|
||||||
@author = RelationshipAuthor.new(
|
|
||||||
id: 1337,
|
|
||||||
posts: [@post],
|
|
||||||
reviewer: @reviewer,
|
|
||||||
bio: @bio,
|
|
||||||
likes: [@like],
|
|
||||||
roles: [@role],
|
|
||||||
locations: [@location],
|
|
||||||
profile: @profile,
|
|
||||||
comments: [@comment]
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_simple_link
|
|
||||||
expected = {
|
|
||||||
data: {
|
|
||||||
id: '1337',
|
|
||||||
type: 'bios'
|
|
||||||
},
|
|
||||||
links: {
|
|
||||||
self: '//example.com/link_author/relationships/bio'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :bio)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_block_link
|
|
||||||
expected = {
|
|
||||||
data: { id: '1337', type: 'profiles' },
|
|
||||||
links: { related: '//example.com/profiles/1337' }
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :profile)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_nil_link
|
|
||||||
expected = {
|
|
||||||
data: { id: '123', type: 'profiles' }
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
author.profile.id = 123
|
|
||||||
assert_author_relationship_serialized(expected, author, :profile)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_block_link_href
|
|
||||||
expected = {
|
|
||||||
data: [{ id: '1337', type: 'locations' }],
|
|
||||||
links: {
|
|
||||||
related: { href: '//example.com/locations/1337' }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :locations)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_block_link_href_and_meta
|
|
||||||
expected = {
|
|
||||||
data: [{ id: '1337', type: 'posts' }],
|
|
||||||
links: {
|
|
||||||
related: {
|
|
||||||
href: '//example.com/posts/1337',
|
|
||||||
meta: { ids: '1337' }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :posts)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_block_link_meta
|
|
||||||
expected = {
|
|
||||||
data: [{ id: '1337', type: 'comments' }],
|
|
||||||
links: {
|
|
||||||
self: {
|
|
||||||
meta: { ids: [1] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :comments)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_meta
|
|
||||||
expected = {
|
|
||||||
data: [{ id: 'from-serializer-method', type: 'roles' }],
|
|
||||||
meta: { count: 1 }
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :roles)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_not_including_data
|
|
||||||
expected = {
|
|
||||||
links: { self: '//example.com/link_author/relationships/blog' }
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
author.define_singleton_method(:read_attribute_for_serialization) do |attr|
|
|
||||||
fail 'should not be called' if attr == :blog
|
|
||||||
super(attr)
|
|
||||||
end
|
|
||||||
assert_nothing_raised do
|
|
||||||
assert_author_relationship_serialized(expected, author, :blog)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_including_data_explicit
|
|
||||||
expected = {
|
|
||||||
data: { id: '1337', type: 'authors' },
|
|
||||||
meta: { name: 'Dan Brown' }
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :reviewer)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_relationship_with_everything
|
|
||||||
expected = {
|
|
||||||
data: [{ id: '1337', type: 'likes' }],
|
|
||||||
links: {
|
|
||||||
related: {
|
|
||||||
href: '//example.com/likes/1337',
|
|
||||||
meta: { ids: '1337' }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
meta: { liked: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
author = @author.dup
|
|
||||||
assert_author_relationship_serialized(expected, author, :likes)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def assert_author_relationship_serialized(expected, author, relationship_name)
|
|
||||||
hash = serializable(author, adapter: :json_api).serializable_hash
|
|
||||||
actual_relationship = hash[:data][:relationships][relationship_name]
|
|
||||||
assert_equal(expected, actual_relationship)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Loading…
Reference in New Issue
Block a user