mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Add tests for fields option demonstrating usage on both attributes and relationships (#1839)
* add test for fields whitelisting relationships, and use the JSON API Include Directive to do the heavy lifting
This commit is contained in:
parent
1896e5a525
commit
a319fef239
@ -15,6 +15,7 @@ Fixes:
|
|||||||
- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene)
|
- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene)
|
||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
|
- [#1839](https://github.com/rails-api/active_model_serializers/pull/1839) `fields` tests demonstrating usage for both attributes and relationships. (@NullVoxPopuli)
|
||||||
|
|
||||||
- [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz)
|
- [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz)
|
||||||
|
|
||||||
|
|||||||
57
test/action_controller/json_api/fields_test.rb
Normal file
57
test/action_controller/json_api/fields_test.rb
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActionController
|
||||||
|
module Serialization
|
||||||
|
class JsonApi
|
||||||
|
class FieldsTest < ActionController::TestCase
|
||||||
|
class FieldsTestController < ActionController::Base
|
||||||
|
class PostSerializer < ActiveModel::Serializer
|
||||||
|
type 'posts'
|
||||||
|
attributes :title, :body, :publish_at
|
||||||
|
belongs_to :author
|
||||||
|
has_many :comments
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup_post
|
||||||
|
ActionController::Base.cache_store.clear
|
||||||
|
@author = Author.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')
|
||||||
|
@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] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests FieldsTestController
|
||||||
|
|
||||||
|
test 'fields works on relationships' do
|
||||||
|
get :render_fields_works_on_relationships
|
||||||
|
response = JSON.parse(@response.body)
|
||||||
|
expected = {
|
||||||
|
'data' => {
|
||||||
|
'id' => '1337',
|
||||||
|
'type' => 'posts',
|
||||||
|
'relationships' => {
|
||||||
|
'author' => {
|
||||||
|
'data' => {
|
||||||
|
'id' => '1',
|
||||||
|
'type' => 'authors'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_equal expected, response
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -40,6 +40,27 @@ module ActiveModelSerializers
|
|||||||
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'relationships can be whitelisted via fields' do
|
||||||
|
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, fields: { posts: [:author] })
|
||||||
|
result = @adapter.serializable_hash
|
||||||
|
expected = {
|
||||||
|
data: {
|
||||||
|
id: '1',
|
||||||
|
type: 'posts',
|
||||||
|
relationships: {
|
||||||
|
author: {
|
||||||
|
data: {
|
||||||
|
id: '1',
|
||||||
|
type: 'authors'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_equal expected, result
|
||||||
|
end
|
||||||
|
|
||||||
def test_includes_linked_comments
|
def test_includes_linked_comments
|
||||||
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
|
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
|
||||||
expected = [{
|
expected = [{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user