Skip to content

Commit a319fef

Browse files
NullVoxPopulibf4
authored andcommitted
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
1 parent 1896e5a commit a319fef

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fixes:
1515
- [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene)
1616

1717
Misc:
18+
- [#1839](https://github.com/rails-api/active_model_serializers/pull/1839) `fields` tests demonstrating usage for both attributes and relationships. (@NullVoxPopuli)
1819

1920
- [#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)
2021

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'test_helper'
2+
3+
module ActionController
4+
module Serialization
5+
class JsonApi
6+
class FieldsTest < ActionController::TestCase
7+
class FieldsTestController < ActionController::Base
8+
class PostSerializer < ActiveModel::Serializer
9+
type 'posts'
10+
attributes :title, :body, :publish_at
11+
belongs_to :author
12+
has_many :comments
13+
end
14+
15+
def setup_post
16+
ActionController::Base.cache_store.clear
17+
@author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
18+
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
19+
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
20+
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
21+
author: @author, comments: [@comment1, @comment2],
22+
publish_at: '2020-03-16T03:55:25.291Z')
23+
@comment1.post = @post
24+
@comment2.post = @post
25+
end
26+
27+
def render_fields_works_on_relationships
28+
setup_post
29+
render json: @post, serializer: PostSerializer, adapter: :json_api, fields: { posts: [:author] }
30+
end
31+
end
32+
33+
tests FieldsTestController
34+
35+
test 'fields works on relationships' do
36+
get :render_fields_works_on_relationships
37+
response = JSON.parse(@response.body)
38+
expected = {
39+
'data' => {
40+
'id' => '1337',
41+
'type' => 'posts',
42+
'relationships' => {
43+
'author' => {
44+
'data' => {
45+
'id' => '1',
46+
'type' => 'authors'
47+
}
48+
}
49+
}
50+
}
51+
}
52+
assert_equal expected, response
53+
end
54+
end
55+
end
56+
end
57+
end

test/adapter/json_api/has_many_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ def test_includes_comment_ids
4040
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
4141
end
4242

43+
test 'relationships can be whitelisted via fields' do
44+
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, fields: { posts: [:author] })
45+
result = @adapter.serializable_hash
46+
expected = {
47+
data: {
48+
id: '1',
49+
type: 'posts',
50+
relationships: {
51+
author: {
52+
data: {
53+
id: '1',
54+
type: 'authors'
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
assert_equal expected, result
62+
end
63+
4364
def test_includes_linked_comments
4465
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
4566
expected = [{

0 commit comments

Comments
 (0)