Skip to content

Commit 3710c32

Browse files
JustinAikenjoaomdmoura
authored andcommitted
Add some failing tests around has_many assocs...
..where no serializer is defined for the thing that is has_many'd
1 parent 189b795 commit 3710c32

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

test/adapter/json/has_many_test.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def setup
1717
@second_comment.post = @post
1818
@blog = Blog.new(id: 1, name: "My Blog!!")
1919
@post.blog = @blog
20+
@tag = Tag.new(id: 1, name: "#hash_tag")
21+
@post.tags = [@tag]
2022

2123
@serializer = PostSerializer.new(@post)
2224
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
@@ -28,9 +30,14 @@ def test_has_many
2830
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
2931
], @adapter.serializable_hash[:post][:comments])
3032
end
33+
34+
def test_has_many_with_no_serializer
35+
serializer = PostWithTagsSerializer.new(@post)
36+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
37+
assert_includes(adapter.as_json, :tags)
38+
end
3139
end
3240
end
3341
end
3442
end
3543
end
36-

test/adapter/json_api/has_many_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def setup
2727
@blog.articles = [@post]
2828
@post.blog = @blog
2929
@post_without_comments.blog = nil
30+
@tag = Tag.new(id: 1, name: "#hash_tag")
31+
@post.tags = [@tag]
3032

3133
@serializer = PostSerializer.new(@post)
3234
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
@@ -103,6 +105,12 @@ def test_include_type_for_association_when_different_than_name
103105
}
104106
assert_equal expected, actual
105107
end
108+
109+
def test_has_many_with_no_serializer
110+
serializer = PostWithTagsSerializer.new(@post)
111+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
112+
assert_includes(adapter.serializable_hash, :tags)
113+
end
106114
end
107115
end
108116
end

test/fixtures/poro.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class ProfilePreviewSerializer < ActiveModel::Serializer
7676
User = Class.new(Model)
7777
Location = Class.new(Model)
7878
Place = Class.new(Model)
79+
Tag = Class.new(Model)
7980
Comment = Class.new(Model) do
8081
# Uses a custom non-time-based cache key
8182
def cache_key
@@ -224,6 +225,12 @@ def self.root_name
224225
belongs_to :author, serializer: AuthorPreviewSerializer
225226
end
226227

228+
PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do
229+
attributes :id
230+
231+
has_many :tags
232+
end
233+
227234
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
228235
attributes :id
229236
end

test/serializers/associations_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ def setup
2929
@author.roles = []
3030
@blog = Blog.new({ name: 'AMS Blog' })
3131
@post = Post.new({ title: 'New Post', body: 'Body' })
32+
@tag = Tag.new({name: '#hashtagged'})
3233
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
3334
@post.comments = [@comment]
35+
@post.tags = [@tag]
3436
@post.blog = @blog
3537
@comment.post = @post
3638
@comment.author = nil
@@ -65,6 +67,12 @@ def test_has_many_and_has_one
6567
end
6668
end
6769

70+
def test_has_many_with_no_serializer
71+
PostWithTagsSerializer.new(@post).each_association do |name, serializer, options|
72+
puts "The line above will crash this test"
73+
end
74+
end
75+
6876
def test_serializer_options_are_passed_into_associations_serializers
6977
@post_serializer.each_association do |name, association|
7078
if name == :comments

0 commit comments

Comments
 (0)