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:
@@ -15,7 +15,7 @@ module ActionController
|
||||
end
|
||||
|
||||
def render_skipping_adapter
|
||||
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||
@profile = Profile.new(id: 'render_skipping_adapter_id', name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
||||
render json: @profile, adapter: false
|
||||
end
|
||||
end
|
||||
@@ -46,7 +46,7 @@ module ActionController
|
||||
|
||||
def test_render_skipping_adapter
|
||||
get :render_skipping_adapter
|
||||
assert_equal '{"name":"Name 1","description":"Description 1","comments":"Comments 1"}', response.body
|
||||
assert_equal '{"id":"render_skipping_adapter_id","name":"Name 1","description":"Description 1"}', response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,16 @@ module ActionController
|
||||
class JsonApi
|
||||
class FieldsTest < ActionController::TestCase
|
||||
class FieldsTestController < ActionController::Base
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
class AuthorWithName < Author
|
||||
attributes :first_name, :last_name
|
||||
end
|
||||
class AuthorWithNameSerializer < AuthorSerializer
|
||||
type 'authors'
|
||||
end
|
||||
class PostWithPublishAt < Post
|
||||
attributes :publish_at
|
||||
end
|
||||
class PostWithPublishAtSerializer < ActiveModel::Serializer
|
||||
type 'posts'
|
||||
attributes :title, :body, :publish_at
|
||||
belongs_to :author
|
||||
@@ -14,19 +23,19 @@ module ActionController
|
||||
|
||||
def setup_post
|
||||
ActionController::Base.cache_store.clear
|
||||
@author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
|
||||
@author = AuthorWithName.new(id: 1, first_name: 'Bob', last_name: 'Jones')
|
||||
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
|
||||
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
|
||||
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
||||
author: @author, comments: [@comment1, @comment2],
|
||||
publish_at: '2020-03-16T03:55:25.291Z')
|
||||
@post = PostWithPublishAt.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
||||
author: @author, comments: [@comment1, @comment2],
|
||||
publish_at: '2020-03-16T03:55:25.291Z')
|
||||
@comment1.post = @post
|
||||
@comment2.post = @post
|
||||
end
|
||||
|
||||
def render_fields_works_on_relationships
|
||||
setup_post
|
||||
render json: @post, serializer: PostSerializer, adapter: :json_api, fields: { posts: [:author] }
|
||||
render json: @post, serializer: PostWithPublishAtSerializer, adapter: :json_api, fields: { posts: [:author] }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -5,9 +5,17 @@ module ActionController
|
||||
class JsonApi
|
||||
class KeyTransformTest < ActionController::TestCase
|
||||
class KeyTransformTestController < ActionController::Base
|
||||
class Post < ::Model; end
|
||||
class Author < ::Model; end
|
||||
class TopComment < ::Model; end
|
||||
class Post < ::Model
|
||||
attributes :title, :body, :publish_at
|
||||
associations :author, :top_comments
|
||||
end
|
||||
class Author < ::Model
|
||||
attributes :first_name, :last_name
|
||||
end
|
||||
class TopComment < ::Model
|
||||
attributes :body
|
||||
associations :author, :post
|
||||
end
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
type 'posts'
|
||||
attributes :title, :body, :publish_at
|
||||
|
||||
@@ -3,10 +3,16 @@ require 'test_helper'
|
||||
module ActionController
|
||||
module Serialization
|
||||
class NamespaceLookupTest < ActionController::TestCase
|
||||
class Book < ::Model; end
|
||||
class Page < ::Model; end
|
||||
class Chapter < ::Model; end
|
||||
class Writer < ::Model; end
|
||||
class Book < ::Model
|
||||
attributes :title, :body
|
||||
associations :writer, :chapters
|
||||
end
|
||||
class Chapter < ::Model
|
||||
attributes :title
|
||||
end
|
||||
class Writer < ::Model
|
||||
attributes :name
|
||||
end
|
||||
|
||||
module Api
|
||||
module V2
|
||||
@@ -93,7 +99,7 @@ module ActionController
|
||||
end
|
||||
|
||||
def invalid_namespace
|
||||
book = Book.new(title: 'New Post', body: 'Body')
|
||||
book = Book.new(id: 'invalid_namespace_book_id', title: 'New Post', body: 'Body')
|
||||
|
||||
render json: book, namespace: :api_v2
|
||||
end
|
||||
@@ -205,7 +211,7 @@ module ActionController
|
||||
|
||||
assert_serializer ActiveModel::Serializer::Null
|
||||
|
||||
expected = { 'title' => 'New Post', 'body' => 'Body' }
|
||||
expected = { 'id' => 'invalid_namespace_book_id', 'title' => 'New Post', 'body' => 'Body' }
|
||||
actual = JSON.parse(@response.body)
|
||||
|
||||
assert_equal expected, actual
|
||||
|
||||
@@ -135,7 +135,7 @@ module ActionController
|
||||
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
|
||||
|
||||
generate_cached_serializer(like)
|
||||
like.likable = comment2
|
||||
like.likeable = comment2
|
||||
like.time = Time.zone.now.to_s
|
||||
|
||||
render json: like
|
||||
|
||||
Reference in New Issue
Block a user