|
1 | 1 | require 'test_helper'
|
2 |
| - |
3 | 2 | module ActiveModel
|
4 | 3 | class Serializer
|
5 | 4 | class AssociationsTest < ActiveSupport::TestCase
|
@@ -104,13 +103,13 @@ def test_associations_inheritance_with_new_association
|
104 | 103 | end
|
105 | 104 |
|
106 | 105 | assert(
|
107 |
| - PostSerializer._reflections.all? do |reflection| |
108 |
| - inherited_klass._reflections.include?(reflection) |
| 106 | + PostSerializer._reflections.values.all? do |reflection| |
| 107 | + inherited_klass._reflections.values.include?(reflection) |
109 | 108 | end
|
110 | 109 | )
|
111 | 110 |
|
112 | 111 | assert(
|
113 |
| - inherited_klass._reflections.any? do |reflection| |
| 112 | + inherited_klass._reflections.values.any? do |reflection| |
114 | 113 | reflection.name == :top_comments
|
115 | 114 | end
|
116 | 115 | )
|
@@ -290,6 +289,61 @@ def test_illegal_conditional_associations
|
290 | 289 | assert_match(/:if should be a Symbol, String or Proc/, exception.message)
|
291 | 290 | end
|
292 | 291 | end
|
| 292 | + |
| 293 | + class InheritedSerializerTest < ActiveSupport::TestCase |
| 294 | + InheritedPostSerializer = Class.new(PostSerializer) do |
| 295 | + belongs_to :author, polymorphic: true |
| 296 | + has_many :comments, key: :reviews |
| 297 | + end |
| 298 | + |
| 299 | + InheritedAuthorSerializer = Class.new(AuthorSerializer) do |
| 300 | + has_many :roles, polymorphic: true |
| 301 | + has_one :bio, polymorphic: true |
| 302 | + end |
| 303 | + |
| 304 | + def setup |
| 305 | + @author = Author.new(name: 'Steve K.') |
| 306 | + @post = Post.new(title: 'New Post', body: 'Body') |
| 307 | + @post_serializer = PostSerializer.new(@post) |
| 308 | + @author_serializer = AuthorSerializer.new(@author) |
| 309 | + @inherited_post_serializer = InheritedPostSerializer.new(@post) |
| 310 | + @inherited_author_serializer = InheritedAuthorSerializer.new(@author) |
| 311 | + @author_associations = @author_serializer.associations.to_a |
| 312 | + @inherited_author_associations = @inherited_author_serializer.associations.to_a |
| 313 | + @post_associations = @post_serializer.associations.to_a |
| 314 | + @inherited_post_associations = @inherited_post_serializer.associations.to_a |
| 315 | + end |
| 316 | + |
| 317 | + test 'an author serializer must have [posts,roles,bio] associations' do |
| 318 | + expected = [:posts, :roles, :bio].sort |
| 319 | + result = @author_serializer.associations.map(&:name).sort |
| 320 | + assert_equal(result, expected) |
| 321 | + end |
| 322 | + |
| 323 | + test 'a post serializer must have [author,comments,blog] associations' do |
| 324 | + expected = [:author, :comments, :blog].sort |
| 325 | + result = @post_serializer.associations.map(&:name).sort |
| 326 | + assert_equal(result, expected) |
| 327 | + end |
| 328 | + |
| 329 | + test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do |
| 330 | + expected = [:roles, :bio].sort |
| 331 | + result = (@inherited_author_associations - @author_associations).map(&:name).sort |
| 332 | + assert_equal(result, expected) |
| 333 | + end |
| 334 | + |
| 335 | + test 'a serializer inheriting from another serializer can redefine belongs_to associations' do |
| 336 | + expected = [:author, :comments, :blog].sort |
| 337 | + result = (@inherited_post_associations - @post_associations).map(&:name).sort |
| 338 | + assert_equal(result, expected) |
| 339 | + end |
| 340 | + |
| 341 | + test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do |
| 342 | + expected = [:author, :comments, :blog, :reviews].sort |
| 343 | + result = @inherited_post_serializer.associations.map { |a| a.options.fetch(:key, a.name) }.sort |
| 344 | + assert_equal(result, expected) |
| 345 | + end |
| 346 | + end |
293 | 347 | end
|
294 | 348 | end
|
295 | 349 | end
|
0 commit comments