mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Adds polymorphic tests and documentation
This commit is contained in:
72
test/adapter/polymorphic_test.rb
Normal file
72
test/adapter/polymorphic_test.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
module Adapter
|
||||
class PolymorphicTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@employee = Employee.new(id: 42, name: 'Zoop Zoopler', email: 'zoop@example.com')
|
||||
@picture = @employee.pictures.new(id: 1, title: 'headshot-1.jpg')
|
||||
@picture.imageable = @employee
|
||||
|
||||
@attributes_serialization = serializable(@picture, serializer: PolymorphicBelongsToSerializer) # uses default adapter: attributes
|
||||
@json_serialization = serializable(@picture, adapter: :json, serializer: PolymorphicBelongsToSerializer)
|
||||
@json_api_serialization = serializable(@picture, adapter: :json_api, serializer: PolymorphicBelongsToSerializer)
|
||||
end
|
||||
|
||||
def test_attributes_serialization
|
||||
expected =
|
||||
{
|
||||
id: 1,
|
||||
title: 'headshot-1.jpg',
|
||||
imageable: {
|
||||
id: 42,
|
||||
name: 'Zoop Zoopler'
|
||||
}
|
||||
}
|
||||
|
||||
assert_equal(expected, @attributes_serialization.as_json)
|
||||
end
|
||||
|
||||
def test_json_serializer
|
||||
expected =
|
||||
{
|
||||
picture: {
|
||||
id: 1,
|
||||
title: 'headshot-1.jpg',
|
||||
imageable: {
|
||||
id: 42,
|
||||
name: 'Zoop Zoopler'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert_equal(expected, @json_serialization.as_json)
|
||||
end
|
||||
|
||||
def test_json_api_serializer
|
||||
expected =
|
||||
{
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'pictures',
|
||||
attributes: {
|
||||
title: 'headshot-1.jpg'
|
||||
},
|
||||
relationships: {
|
||||
imageable: {
|
||||
data: {
|
||||
id: '42',
|
||||
type: 'employees'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert_equal(expected, @json_api_serialization.as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user