Use more meaningful model names for tests

This commit is contained in:
Santiago Pastorino 2013-08-16 19:16:46 -03:00
parent 516f5bdceb
commit 4c7599cfff
7 changed files with 102 additions and 104 deletions

47
test/fixtures/poro.rb vendored
View File

@ -7,30 +7,39 @@ class Model
@attributes[name] @attributes[name]
end end
def model
@model ||= Model.new(attr1: 'v1', attr2: 'v2')
end
def id def id
object_id object_id
end end
end end
class ModelSerializer < ActiveModel::Serializer
def attr2 ###
attr2 = object.read_attribute_for_serialization(:attr2) ## Models
if scope ###
attr2 + '-' + scope class User < Model
else def profile
attr2 @profile ||= Profile.new(name: 'N1', description: 'D1')
end end
end
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 end
attributes :attr1, :attr2 attributes :name, :description
end
class AnotherSerializer < ActiveModel::Serializer
attributes :attr2, :attr3
has_one :model
end end

View File

@ -6,7 +6,7 @@ module ActionController
class ImplicitSerializerTest < ActionController::TestCase class ImplicitSerializerTest < ActionController::TestCase
class MyController < ActionController::Base class MyController < ActionController::Base
def render_using_implicit_serializer 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
end end
@ -15,14 +15,14 @@ module ActionController
def test_render_using_implicit_serializer def test_render_using_implicit_serializer
get :render_using_implicit_serializer get :render_using_implicit_serializer
assert_equal 'application/json', @response.content_type 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
end end
class ImplicitSerializerScopeTest < ActionController::TestCase class ImplicitSerializerScopeTest < ActionController::TestCase
class MyController < ActionController::Base class MyController < ActionController::Base
def render_using_implicit_serializer_and_scope 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 end
private private
@ -37,14 +37,14 @@ module ActionController
def test_render_using_implicit_serializer_and_scope def test_render_using_implicit_serializer_and_scope
get :render_using_implicit_serializer_and_scope get :render_using_implicit_serializer_and_scope
assert_equal 'application/json', @response.content_type 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
end end
class ExplicitSerializerScopeTest < ActionController::TestCase class ExplicitSerializerScopeTest < ActionController::TestCase
class MyController < ActionController::Base class MyController < ActionController::Base
def render_using_implicit_serializer_and_explicit_scope 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 end
private private
@ -63,14 +63,14 @@ module ActionController
def test_render_using_implicit_serializer_and_explicit_scope def test_render_using_implicit_serializer_and_explicit_scope
get :render_using_implicit_serializer_and_explicit_scope get :render_using_implicit_serializer_and_explicit_scope
assert_equal 'application/json', @response.content_type 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 end
class OverridingSerializationScopeTest < ActionController::TestCase class OverridingSerializationScopeTest < ActionController::TestCase
class MyController < ActionController::Base class MyController < ActionController::Base
def render_overriding_serialization_scope 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 end
private private
@ -89,7 +89,7 @@ module ActionController
def test_render_overriding_serialization_scope def test_render_overriding_serialization_scope
get :render_overriding_serialization_scope get :render_overriding_serialization_scope
assert_equal 'application/json', @response.content_type 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 end
end end

View File

@ -5,25 +5,25 @@ module ActiveModel
class Serializer class Serializer
class AttributesTest < ActiveModel::TestCase class AttributesTest < ActiveModel::TestCase
def setup def setup
model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@model_serializer = ModelSerializer.new(model) @profile_serializer = ProfileSerializer.new(@profile)
end end
def test_attributes_definition def test_attributes_definition
assert_equal(['attr1', 'attr2'], assert_equal(['name', 'description'],
@model_serializer.class._attributes) @profile_serializer.class._attributes)
end end
def test_attributes_serialization_using_serializable_hash def test_attributes_serialization_using_serializable_hash
assert_equal({ assert_equal({
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
}, @model_serializer.serializable_hash) }, @profile_serializer.serializable_hash)
end end
def test_attributes_serialization_using_as_json def test_attributes_serialization_using_as_json
assert_equal({ assert_equal({
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
}, @model_serializer.as_json) }, @profile_serializer.as_json)
end end
end end
end end

View File

@ -5,58 +5,58 @@ module ActiveModel
class Serializer class Serializer
class HasOneTest < ActiveModel::TestCase class HasOneTest < ActiveModel::TestCase
def setup def setup
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
@model_serializer = AnotherSerializer.new(@model) @user_serializer = UserSerializer.new(@user)
@model_serializer.class._associations[0].include = false @user_serializer.class._associations[0].include = false
@model_serializer.class._associations[0].embed = :ids @user_serializer.class._associations[0].embed = :ids
end end
def test_associations_definition def test_associations_definition
associations = @model_serializer.class._associations associations = @user_serializer.class._associations
assert_equal 1, associations.length assert_equal 1, associations.length
assert_kind_of Association::HasOne, associations[0] assert_kind_of Association::HasOne, associations[0]
assert_equal 'model', associations[0].name assert_equal 'profile', associations[0].name
end end
def test_associations_embedding_ids_serialization_using_serializable_hash def test_associations_embedding_ids_serialization_using_serializable_hash
assert_equal({ assert_equal({
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id
}, @model_serializer.serializable_hash) }, @user_serializer.serializable_hash)
end end
def test_associations_embedding_ids_serialization_using_as_json def test_associations_embedding_ids_serialization_using_as_json
assert_equal({ assert_equal({
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id
}, @model_serializer.as_json) }, @user_serializer.as_json)
end end
def test_associations_embedding_objects_serialization_using_serializable_hash 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({ assert_equal({
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' } 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
}, @model_serializer.serializable_hash) }, @user_serializer.serializable_hash)
end end
def test_associations_embedding_objects_serialization_using_as_json 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({ assert_equal({
'attr2' => 'value2', 'attr3' => 'value3', 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' } 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile' => { 'name' => 'N1', 'description' => 'D1' }
}, @model_serializer.as_json) }, @user_serializer.as_json)
end end
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash 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({ assert_equal({
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id, 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' } 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
}, @model_serializer.serializable_hash) }, @user_serializer.serializable_hash)
end end
def test_associations_embedding_ids_including_objects_serialization_using_as_json 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({ assert_equal({
'attr2' => 'value2', 'attr3' => 'value3', 'model_id' => @model.model.object_id, 'model' => { 'attr1' => 'v1', 'attr2' => 'v2' } 'name' => 'Name 1', 'email' => 'mail@server.com', 'profile_id' => @user.profile.object_id, 'profile' => { 'name' => 'N1', 'description' => 'D1' }
}, @model_serializer.as_json) }, @user_serializer.as_json)
end end
end end
end end

View File

@ -1,40 +1,39 @@
require 'test_helper' require 'test_helper'
require 'active_model/serializer' require 'active_model/serializer'
module ActiveModel module ActiveModel
class Serializer class Serializer
class MetaTest < ActiveModel::TestCase class MetaTest < ActiveModel::TestCase
def setup def setup
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
end end
def test_meta 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({ assert_equal({
'model' => { 'profile' => {
'attr1' => 'value1', 'name' => 'Name 1',
'attr2' => 'value2' 'description' => 'Description 1'
}, },
'meta' => { 'meta' => {
'total' => 10 'total' => 10
} }
}, model_serializer.as_json) }, profile_serializer.as_json)
end end
def test_meta_using_meta_key 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({ assert_equal({
'model' => { 'profile' => {
'attr1' => 'value1', 'name' => 'Name 1',
'attr2' => 'value2' 'description' => 'Description 1'
}, },
'my_meta' => { 'my_meta' => {
'total' => 10 'total' => 10
} }
}, model_serializer.as_json) }, profile_serializer.as_json)
end end
end end
end end

View File

@ -4,38 +4,37 @@ require 'active_model/serializer'
module ActiveModel module ActiveModel
class Serializer class Serializer
class RootAsOptionTest < ActiveModel::TestCase class RootAsOptionTest < ActiveModel::TestCase
def setup def setup
@old_root = ModelSerializer._root @old_root = ProfileSerializer._root
@model = ::Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@serializer = ModelSerializer.new(@model, root: 'initialize') @serializer = ProfileSerializer.new(@profile, root: 'initialize')
ModelSerializer._root = true ProfileSerializer._root = true
end end
def teardown def teardown
ModelSerializer._root = @old_root ProfileSerializer._root = @old_root
end end
def test_root_is_not_displayed_using_serializable_hash def test_root_is_not_displayed_using_serializable_hash
assert_equal({ assert_equal({
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
}, @serializer.serializable_hash) }, @serializer.serializable_hash)
end end
def test_root_using_as_json def test_root_using_as_json
assert_equal({ assert_equal({
'initialize' => { 'initialize' => {
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
} }
}, @serializer.as_json) }, @serializer.as_json)
end end
def test_root_from_serializer_name def test_root_from_serializer_name
@serializer = ModelSerializer.new(@model) @serializer = ProfileSerializer.new(@profile)
assert_equal({ assert_equal({
'model' => { 'profile' => {
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
} }
}, @serializer.as_json) }, @serializer.as_json)
end end
@ -43,44 +42,35 @@ module ActiveModel
def test_root_as_argument_takes_precedence def test_root_as_argument_takes_precedence
assert_equal({ assert_equal({
'argument' => { 'argument' => {
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
} }
}, @serializer.as_json(root: 'argument')) }, @serializer.as_json(root: 'argument'))
end end
end end
class RootInSerializerTest < ActiveModel::TestCase 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 def setup
model = Model.new({ :attr1 => 'value1', :attr2 => 'value2', :attr3 => 'value3' }) @old_root = ProfileSerializer._root
@serializer = ModelSerializer.new(model) ProfileSerializer._root = 'in_serializer'
@rooted_serializer = ModelSerializer.new(model, root: :initialize) 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 end
def test_root_is_not_displayed_using_serializable_hash def test_root_is_not_displayed_using_serializable_hash
assert_equal({ assert_equal({
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
}, @serializer.serializable_hash) }, @serializer.serializable_hash)
end end
def test_root_using_as_json def test_root_using_as_json
assert_equal({ assert_equal({
'in_serializer' => { 'in_serializer' => {
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
} }
}, @serializer.as_json) }, @serializer.as_json)
end end
@ -88,7 +78,7 @@ module ActiveModel
def test_root_in_initializer_takes_precedence def test_root_in_initializer_takes_precedence
assert_equal({ assert_equal({
'initialize' => { 'initialize' => {
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
} }
}, @rooted_serializer.as_json) }, @rooted_serializer.as_json)
end end
@ -96,7 +86,7 @@ module ActiveModel
def test_root_as_argument_takes_precedence def test_root_as_argument_takes_precedence
assert_equal({ assert_equal({
'argument' => { 'argument' => {
'attr1' => 'value1', 'attr2' => 'value2' 'name' => 'Name 1', 'description' => 'Description 1'
} }
}, @rooted_serializer.as_json(root: :argument)) }, @rooted_serializer.as_json(root: :argument))
end end

View File

@ -5,7 +5,7 @@ module ActiveModel
class Serializer class Serializer
class ScopeTest < ActiveModel::TestCase class ScopeTest < ActiveModel::TestCase
def setup def setup
@serializer = ModelSerializer.new(nil, scope: current_user) @serializer = ProfileSerializer.new(nil, scope: current_user)
end end
def test_scope def test_scope