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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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