mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Make test attributes explicit
- Organize test poros with associations and by serializer - Freeze derived attributes/associations against mutation - Cleanup PORO fixtures
This commit is contained in:
@@ -8,7 +8,7 @@ module ActiveModel
|
||||
@author.roles = []
|
||||
@blog = Blog.new(name: 'AMS Blog')
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
@tag = Tag.new(name: '#hashtagged')
|
||||
@tag = Tag.new(id: 'tagid', name: '#hashtagged')
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@post.comments = [@comment]
|
||||
@post.tags = [@tag]
|
||||
@@ -53,7 +53,7 @@ module ActiveModel
|
||||
|
||||
assert_equal :tags, key
|
||||
assert_nil serializer
|
||||
assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
|
||||
assert_equal [{ id: 'tagid', name: '#hashtagged' }].to_json, options[:virtual_value].to_json
|
||||
end
|
||||
end
|
||||
|
||||
@@ -62,7 +62,13 @@ module ActiveModel
|
||||
.associations
|
||||
.detect { |assoc| assoc.key == :comments }
|
||||
|
||||
assert association.serializer.first.custom_options[:custom_options]
|
||||
comment_serializer = association.serializer.first
|
||||
class << comment_serializer
|
||||
def custom_options
|
||||
instance_options
|
||||
end
|
||||
end
|
||||
assert comment_serializer.custom_options.fetch(:custom_options)
|
||||
end
|
||||
|
||||
def test_belongs_to
|
||||
@@ -159,7 +165,9 @@ module ActiveModel
|
||||
|
||||
class NamespacedResourcesTest < ActiveSupport::TestCase
|
||||
class ResourceNamespace
|
||||
class Post < ::Model; end
|
||||
class Post < ::Model
|
||||
associations :comments, :author, :description
|
||||
end
|
||||
class Comment < ::Model; end
|
||||
class Author < ::Model; end
|
||||
class Description < ::Model; end
|
||||
@@ -200,7 +208,9 @@ module ActiveModel
|
||||
end
|
||||
|
||||
class NestedSerializersTest < ActiveSupport::TestCase
|
||||
class Post < ::Model; end
|
||||
class Post < ::Model
|
||||
associations :comments, :author, :description
|
||||
end
|
||||
class Comment < ::Model; end
|
||||
class Author < ::Model; end
|
||||
class Description < ::Model; end
|
||||
@@ -240,7 +250,10 @@ module ActiveModel
|
||||
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
def test_conditional_associations
|
||||
model = ::Model.new(true: true, false: false)
|
||||
model = Class.new(::Model) do
|
||||
attributes :true, :false
|
||||
associations :association
|
||||
end.new(true: true, false: false)
|
||||
|
||||
scenarios = [
|
||||
{ options: { if: :true }, included: true },
|
||||
|
||||
@@ -81,7 +81,7 @@ module ActiveModel
|
||||
assert_equal('custom', hash[:blog][:id])
|
||||
end
|
||||
|
||||
class PostWithVirtualAttribute < ::Model; end
|
||||
class PostWithVirtualAttribute < ::Model; attributes :first_name, :last_name end
|
||||
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
|
||||
attribute :name do
|
||||
"#{object.first_name} #{object.last_name}"
|
||||
@@ -98,7 +98,9 @@ module ActiveModel
|
||||
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
def test_conditional_associations
|
||||
model = ::Model.new(true: true, false: false)
|
||||
model = Class.new(::Model) do
|
||||
attributes :true, :false, :attribute
|
||||
end.new(true: true, false: false)
|
||||
|
||||
scenarios = [
|
||||
{ options: { if: :true }, included: true },
|
||||
|
||||
@@ -3,18 +3,29 @@ require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class OptionsTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@profile = Profile.new(name: 'Name 1', description: 'Description 1')
|
||||
class ModelWithOptions < ActiveModelSerializers::Model
|
||||
attributes :name, :description
|
||||
end
|
||||
class ModelWithOptionsSerializer < ActiveModel::Serializer
|
||||
attributes :name, :description
|
||||
|
||||
def arguments_passed_in?
|
||||
instance_options[:my_options] == :accessible
|
||||
end
|
||||
end
|
||||
|
||||
setup do
|
||||
@model_with_options = ModelWithOptions.new(name: 'Name 1', description: 'Description 1')
|
||||
end
|
||||
|
||||
def test_options_are_accessible
|
||||
@profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
|
||||
assert @profile_serializer.arguments_passed_in?
|
||||
model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options, my_options: :accessible)
|
||||
assert model_with_options_serializer.arguments_passed_in?
|
||||
end
|
||||
|
||||
def test_no_option_is_passed_in
|
||||
@profile_serializer = ProfileSerializer.new(@profile)
|
||||
refute @profile_serializer.arguments_passed_in?
|
||||
model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options)
|
||||
refute model_with_options_serializer.arguments_passed_in?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,9 +3,12 @@ require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class SerializerForWithNamespaceTest < ActiveSupport::TestCase
|
||||
class Book < ::Model; end
|
||||
class Page < ::Model; end
|
||||
class Publisher < ::Model; end
|
||||
class Book < ::Model
|
||||
attributes :title, :author_name
|
||||
associations :publisher, :pages
|
||||
end
|
||||
class Page < ::Model; attributes :number, :text end
|
||||
class Publisher < ::Model; attributes :name end
|
||||
|
||||
module Api
|
||||
module V3
|
||||
@@ -18,8 +21,6 @@ module ActiveModel
|
||||
|
||||
class PageSerializer < ActiveModel::Serializer
|
||||
attributes :number, :text
|
||||
|
||||
belongs_to :book
|
||||
end
|
||||
|
||||
class PublisherSerializer < ActiveModel::Serializer
|
||||
|
||||
Reference in New Issue
Block a user