-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds polymorphic tests and documentation
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters