mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Use more meaningful model names for tests
This commit is contained in:
parent
516f5bdceb
commit
4c7599cfff
45
test/fixtures/poro.rb
vendored
45
test/fixtures/poro.rb
vendored
@ -7,30 +7,39 @@ class Model
|
||||
@attributes[name]
|
||||
end
|
||||
|
||||
def model
|
||||
@model ||= Model.new(attr1: 'v1', attr2: 'v2')
|
||||
end
|
||||
|
||||
def id
|
||||
object_id
|
||||
end
|
||||
end
|
||||
|
||||
class ModelSerializer < ActiveModel::Serializer
|
||||
def attr2
|
||||
attr2 = object.read_attribute_for_serialization(:attr2)
|
||||
if scope
|
||||
attr2 + '-' + scope
|
||||
else
|
||||
attr2
|
||||
end
|
||||
end
|
||||
|
||||
attributes :attr1, :attr2
|
||||
###
|
||||
## Models
|
||||
###
|
||||
class User < Model
|
||||
def profile
|
||||
@profile ||= Profile.new(name: 'N1', description: 'D1')
|
||||
end
|
||||
end
|
||||
|
||||
class AnotherSerializer < ActiveModel::Serializer
|
||||
attributes :attr2, :attr3
|
||||
|
||||
has_one :model
|
||||
class Profile < Model
|
||||
end
|
||||
|
||||
|
||||
###
|
||||
## Serializers
|
||||
###
|
||||
class UserSerializer < ActiveModel::Serializer
|
||||
attributes :name, :email
|
||||
|
||||
has_one :profile
|
||||
end
|
||||
|
||||
class ProfileSerializer < ActiveModel::Serializer
|
||||
def description
|
||||
description = object.read_attribute_for_serialization(:description)
|
||||
scope ? "#{description} - #{scope}" : description
|
||||
end
|
||||
|
||||
attributes :name, :description
|
||||
end
|
||||
|
||||
@ -6,7 +6,7 @@ module ActionController
|
||||
class ImplicitSerializerTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_implicit_serializer
|
||||
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
end
|
||||
end
|
||||
|
||||
@ -15,14 +15,14 @@ module ActionController
|
||||
def test_render_using_implicit_serializer
|
||||
get :render_using_implicit_serializer
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"attr1":"value1","attr2":"value2"}', @response.body
|
||||
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
class ImplicitSerializerScopeTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_implicit_serializer_and_scope
|
||||
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
end
|
||||
|
||||
private
|
||||
@ -37,14 +37,14 @@ module ActionController
|
||||
def test_render_using_implicit_serializer_and_scope
|
||||
get :render_using_implicit_serializer_and_scope
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"attr1":"value1","attr2":"value2-current_user"}', @response.body
|
||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_user"}', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
class ExplicitSerializerScopeTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_implicit_serializer_and_explicit_scope
|
||||
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3'), scope: current_admin
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), scope: current_admin
|
||||
end
|
||||
|
||||
private
|
||||
@ -63,14 +63,14 @@ module ActionController
|
||||
def test_render_using_implicit_serializer_and_explicit_scope
|
||||
get :render_using_implicit_serializer_and_explicit_scope
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"attr1":"value1","attr2":"value2-current_admin"}', @response.body
|
||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_admin"}', @response.body
|
||||
end
|
||||
end
|
||||
|
||||
class OverridingSerializationScopeTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_overriding_serialization_scope
|
||||
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
end
|
||||
|
||||
private
|
||||
@ -89,7 +89,7 @@ module ActionController
|
||||
def test_render_overriding_serialization_scope
|
||||
get :render_overriding_serialization_scope
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"attr1":"value1","attr2":"value2-current_admin"}', @response.body
|
||||
assert_equal '{"name":"Name 1","description":"Description 1 - current_admin"}', @response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -5,25 +5,25 @@ module ActiveModel
|
||||
class Serializer
|
||||
class AttributesTest < ActiveModel::TestCase
|
||||
def setup
|
||||
model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||
@model_serializer = ModelSerializer.new(model)
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@profile_serializer = ProfileSerializer.new(@profile)
|
||||
end
|
||||
|
||||
def test_attributes_definition
|
||||
assert_equal(['attr1', 'attr2'],
|
||||
@model_serializer.class._attributes)
|
||||
assert_equal(['name', 'description'],
|
||||
@profile_serializer.class._attributes)
|
||||
end
|
||||
|
||||
def test_attributes_serialization_using_serializable_hash
|
||||
assert_equal({
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
}, @model_serializer.serializable_hash)
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}, @profile_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_attributes_serialization_using_as_json
|
||||
assert_equal({
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
}, @model_serializer.as_json)
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}, @profile_serializer.as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -5,58 +5,58 @@ module ActiveModel
|
||||
class Serializer
|
||||
class HasOneTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||
@model_serializer = AnotherSerializer.new(@model)
|
||||
@model_serializer.class._associations[0].include = false
|
||||
@model_serializer.class._associations[0].embed = :ids
|
||||
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
||||
@user_serializer = UserSerializer.new(@user)
|
||||
@user_serializer.class._associations[0].include = false
|
||||
@user_serializer.class._associations[0].embed = :ids
|
||||
end
|
||||
|
||||
def test_associations_definition
|
||||
associations = @model_serializer.class._associations
|
||||
associations = @user_serializer.class._associations
|
||||
|
||||
assert_equal 1, associations.length
|
||||
assert_kind_of Association::HasOne, associations[0]
|
||||
assert_equal 'model', associations[0].name
|
||||
assert_equal 'profile', associations[0].name
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_serializable_hash
|
||||
assert_equal({
|
||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
||||
}, @model_serializer.serializable_hash)
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_serialization_using_as_json
|
||||
assert_equal({
|
||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id
|
||||
}, @model_serializer.as_json)
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_serializable_hash
|
||||
@model_serializer.class._associations[0].embed = :objects
|
||||
@user_serializer.class._associations[0].embed = :objects
|
||||
assert_equal({
|
||||
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||
}, @model_serializer.serializable_hash)
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_objects_serialization_using_as_json
|
||||
@model_serializer.class._associations[0].embed = :objects
|
||||
@user_serializer.class._associations[0].embed = :objects
|
||||
assert_equal({
|
||||
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||
}, @model_serializer.as_json)
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
||||
@model_serializer.class._associations[0].include = true
|
||||
@user_serializer.class._associations[0].include = true
|
||||
assert_equal({
|
||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id, 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||
}, @model_serializer.serializable_hash)
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
|
||||
}, @user_serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
||||
@model_serializer.class._associations[0].include = true
|
||||
@user_serializer.class._associations[0].include = true
|
||||
assert_equal({
|
||||
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id, 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' }
|
||||
}, @model_serializer.as_json)
|
||||
'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
|
||||
}, @user_serializer.as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,40 +1,39 @@
|
||||
require 'test_helper'
|
||||
require 'active_model/serializer'
|
||||
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class MetaTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
end
|
||||
|
||||
def test_meta
|
||||
model_serializer = ModelSerializer.new(@model, root: 'model', meta: { 'total' => 10 })
|
||||
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta: { 'total' => 10 })
|
||||
|
||||
assert_equal({
|
||||
'model' => {
|
||||
'attr1' => 'value1',
|
||||
'attr2' => 'value2'
|
||||
'profile' => {
|
||||
'name' => 'Name 1',
|
||||
'description' => 'Description 1'
|
||||
},
|
||||
'meta' => {
|
||||
'total' => 10
|
||||
}
|
||||
}, model_serializer.as_json)
|
||||
}, profile_serializer.as_json)
|
||||
end
|
||||
|
||||
def test_meta_using_meta_key
|
||||
model_serializer = ModelSerializer.new(@model, root: 'model', meta_key: :my_meta, my_meta: { 'total' => 10 })
|
||||
profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta_key: :my_meta, my_meta: { 'total' => 10 })
|
||||
|
||||
assert_equal({
|
||||
'model' => {
|
||||
'attr1' => 'value1',
|
||||
'attr2' => 'value2'
|
||||
'profile' => {
|
||||
'name' => 'Name 1',
|
||||
'description' => 'Description 1'
|
||||
},
|
||||
'my_meta' => {
|
||||
'total' => 10
|
||||
}
|
||||
}, model_serializer.as_json)
|
||||
}, profile_serializer.as_json)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -4,38 +4,37 @@ require 'active_model/serializer'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class RootAsOptionTest < ActiveModel::TestCase
|
||||
|
||||
def setup
|
||||
@old_root = ModelSerializer._root
|
||||
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||
@serializer = ModelSerializer.new(@model, root: 'initialize')
|
||||
ModelSerializer._root = true
|
||||
@old_root = ProfileSerializer._root
|
||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@serializer = ProfileSerializer.new(@profile, root: 'initialize')
|
||||
ProfileSerializer._root = true
|
||||
end
|
||||
|
||||
def teardown
|
||||
ModelSerializer._root = @old_root
|
||||
ProfileSerializer._root = @old_root
|
||||
end
|
||||
|
||||
def test_root_is_not_displayed_using_serializable_hash
|
||||
assert_equal({
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}, @serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'initialize' => {
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
|
||||
def test_root_from_serializer_name
|
||||
@serializer = ModelSerializer.new(@model)
|
||||
@serializer = ProfileSerializer.new(@profile)
|
||||
|
||||
assert_equal({
|
||||
'model' => {
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'profile' => {
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -43,44 +42,35 @@ module ActiveModel
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => {
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json(root: 'argument'))
|
||||
end
|
||||
end
|
||||
|
||||
class RootInSerializerTest < ActiveModel::TestCase
|
||||
class Model
|
||||
def initialize(hash={})
|
||||
@attributes = hash
|
||||
end
|
||||
|
||||
def read_attribute_for_serialization(name)
|
||||
@attributes[name]
|
||||
end
|
||||
end
|
||||
|
||||
class ModelSerializer < ActiveModel::Serializer
|
||||
root :in_serializer
|
||||
attributes :attr1, :attr2
|
||||
end
|
||||
|
||||
def setup
|
||||
model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' })
|
||||
@serializer = ModelSerializer.new(model)
|
||||
@rooted_serializer = ModelSerializer.new(model, root: :initialize)
|
||||
@old_root = ProfileSerializer._root
|
||||
ProfileSerializer._root = 'in_serializer'
|
||||
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
@serializer = ProfileSerializer.new(profile)
|
||||
@rooted_serializer = ProfileSerializer.new(profile, root: :initialize)
|
||||
end
|
||||
|
||||
def teardown
|
||||
ProfileSerializer._root = @old_root
|
||||
end
|
||||
|
||||
def test_root_is_not_displayed_using_serializable_hash
|
||||
assert_equal({
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}, @serializer.serializable_hash)
|
||||
end
|
||||
|
||||
def test_root_using_as_json
|
||||
assert_equal({
|
||||
'in_serializer' => {
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}
|
||||
}, @serializer.as_json)
|
||||
end
|
||||
@ -88,7 +78,7 @@ module ActiveModel
|
||||
def test_root_in_initializer_takes_precedence
|
||||
assert_equal({
|
||||
'initialize' => {
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}
|
||||
}, @rooted_serializer.as_json)
|
||||
end
|
||||
@ -96,7 +86,7 @@ module ActiveModel
|
||||
def test_root_as_argument_takes_precedence
|
||||
assert_equal({
|
||||
'argument' => {
|
||||
'attr1' => 'value1', 'attr2' => 'value2'
|
||||
'name' => 'Name 1', 'description' => 'Description 1'
|
||||
}
|
||||
}, @rooted_serializer.as_json(root: :argument))
|
||||
end
|
||||
|
||||
@ -5,7 +5,7 @@ module ActiveModel
|
||||
class Serializer
|
||||
class ScopeTest < ActiveModel::TestCase
|
||||
def setup
|
||||
@serializer = ModelSerializer.new(nil, scope: current_user)
|
||||
@serializer = ProfileSerializer.new(nil, scope: current_user)
|
||||
end
|
||||
|
||||
def test_scope
|
||||
|
||||
Loading…
Reference in New Issue
Block a user