-
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.
Merge pull request #1494 from bf4/serializable_serializer
Make serializers serializable, step 1.
- Loading branch information
Showing
3 changed files
with
108 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,55 @@ | ||
module ActiveModel | ||
class Serializer | ||
class SerializationTest < ActiveSupport::TestCase | ||
class Blog < ActiveModelSerializers::Model | ||
attr_accessor :id, :name, :authors | ||
end | ||
class Author < ActiveModelSerializers::Model | ||
attr_accessor :id, :name | ||
end | ||
class BlogSerializer < ActiveModel::Serializer | ||
attributes :id | ||
attribute :name, key: :title | ||
|
||
has_many :authors | ||
end | ||
class AuthorSerializer < ActiveModel::Serializer | ||
attributes :id, :name | ||
end | ||
|
||
setup do | ||
@authors = [Author.new(id: 1, name: 'Blog Author')] | ||
@blog = Blog.new(id: 2, name: 'The Blog', authors: @authors) | ||
@serializer_instance = BlogSerializer.new(@blog) | ||
@serializable = ActiveModel::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes) | ||
@expected_hash = { id: 2, title: 'The Blog', authors: [{ id: 1, name: 'Blog Author' }] } | ||
@expected_json = '{"id":2,"title":"The Blog","authors":[{"id":1,"name":"Blog Author"}]}' | ||
end | ||
|
||
test '#serializable_hash is the same as generated by the attributes adapter' do | ||
assert_equal @serializable.serializable_hash, @serializer_instance.serializable_hash | ||
assert_equal @expected_hash, @serializer_instance.serializable_hash | ||
end | ||
|
||
test '#as_json is the same as generated by the attributes adapter' do | ||
assert_equal @serializable.as_json, @serializer_instance.as_json | ||
assert_equal @expected_hash, @serializer_instance.as_json | ||
end | ||
|
||
test '#to_json is the same as generated by the attributes adapter' do | ||
assert_equal @serializable.to_json, @serializer_instance.to_json | ||
assert_equal @expected_json, @serializer_instance.to_json | ||
end | ||
|
||
test '#to_h is an alias for #serializable_hash' do | ||
assert_equal @serializable.serializable_hash, @serializer_instance.to_h | ||
assert_equal @expected_hash, @serializer_instance.to_h | ||
end | ||
|
||
test '#to_hash is an alias for #serializable_hash' do | ||
assert_equal @serializable.serializable_hash, @serializer_instance.to_hash | ||
assert_equal @expected_hash, @serializer_instance.to_hash | ||
end | ||
end | ||
end | ||
end |