mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Add some failing tests around has_many assocs...
..where no serializer is defined for the thing that is has_many'd
This commit is contained in:
parent
189b79523c
commit
3710c32cee
@ -17,6 +17,8 @@ module ActiveModel
|
|||||||
@second_comment.post = @post
|
@second_comment.post = @post
|
||||||
@blog = Blog.new(id: 1, name: "My Blog!!")
|
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||||
@post.blog = @blog
|
@post.blog = @blog
|
||||||
|
@tag = Tag.new(id: 1, name: "#hash_tag")
|
||||||
|
@post.tags = [@tag]
|
||||||
|
|
||||||
@serializer = PostSerializer.new(@post)
|
@serializer = PostSerializer.new(@post)
|
||||||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
||||||
@ -28,9 +30,14 @@ module ActiveModel
|
|||||||
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
|
||||||
], @adapter.serializable_hash[:post][:comments])
|
], @adapter.serializable_hash[:post][:comments])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_has_many_with_no_serializer
|
||||||
|
serializer = PostWithTagsSerializer.new(@post)
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
|
assert_includes(adapter.as_json, :tags)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -27,6 +27,8 @@ module ActiveModel
|
|||||||
@blog.articles = [@post]
|
@blog.articles = [@post]
|
||||||
@post.blog = @blog
|
@post.blog = @blog
|
||||||
@post_without_comments.blog = nil
|
@post_without_comments.blog = nil
|
||||||
|
@tag = Tag.new(id: 1, name: "#hash_tag")
|
||||||
|
@post.tags = [@tag]
|
||||||
|
|
||||||
@serializer = PostSerializer.new(@post)
|
@serializer = PostSerializer.new(@post)
|
||||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
||||||
@ -103,6 +105,12 @@ module ActiveModel
|
|||||||
}
|
}
|
||||||
assert_equal expected, actual
|
assert_equal expected, actual
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_has_many_with_no_serializer
|
||||||
|
serializer = PostWithTagsSerializer.new(@post)
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||||
|
assert_includes(adapter.serializable_hash, :tags)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
7
test/fixtures/poro.rb
vendored
7
test/fixtures/poro.rb
vendored
@ -76,6 +76,7 @@ Role = Class.new(Model)
|
|||||||
User = Class.new(Model)
|
User = Class.new(Model)
|
||||||
Location = Class.new(Model)
|
Location = Class.new(Model)
|
||||||
Place = Class.new(Model)
|
Place = Class.new(Model)
|
||||||
|
Tag = Class.new(Model)
|
||||||
Comment = Class.new(Model) do
|
Comment = Class.new(Model) do
|
||||||
# Uses a custom non-time-based cache key
|
# Uses a custom non-time-based cache key
|
||||||
def cache_key
|
def cache_key
|
||||||
@ -224,6 +225,12 @@ PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
belongs_to :author, serializer: AuthorPreviewSerializer
|
belongs_to :author, serializer: AuthorPreviewSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
attributes :id
|
||||||
|
|
||||||
|
has_many :tags
|
||||||
|
end
|
||||||
|
|
||||||
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
attributes :id
|
attributes :id
|
||||||
end
|
end
|
||||||
|
|||||||
@ -29,8 +29,10 @@ module ActiveModel
|
|||||||
@author.roles = []
|
@author.roles = []
|
||||||
@blog = Blog.new({ name: 'AMS Blog' })
|
@blog = Blog.new({ name: 'AMS Blog' })
|
||||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||||
|
@tag = Tag.new({name: '#hashtagged'})
|
||||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||||
@post.comments = [@comment]
|
@post.comments = [@comment]
|
||||||
|
@post.tags = [@tag]
|
||||||
@post.blog = @blog
|
@post.blog = @blog
|
||||||
@comment.post = @post
|
@comment.post = @post
|
||||||
@comment.author = nil
|
@comment.author = nil
|
||||||
@ -65,6 +67,12 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_has_many_with_no_serializer
|
||||||
|
PostWithTagsSerializer.new(@post).each_association do |name, serializer, options|
|
||||||
|
puts "The line above will crash this test"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_serializer_options_are_passed_into_associations_serializers
|
def test_serializer_options_are_passed_into_associations_serializers
|
||||||
@post_serializer.each_association do |name, association|
|
@post_serializer.each_association do |name, association|
|
||||||
if name == :comments
|
if name == :comments
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user