Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for fields option demonstrating usage on both attributes and relationships #1839

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features:
Fixes:

Misc:
- [#1839](https://github.com/rails-api/active_model_serializers/pull/1839) `fields` tests demonstrating usage for both attributes and relationships. (@NullVoxPopuli)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bf4 changelog entry moved, and re-worded.


### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)

Expand Down
57 changes: 57 additions & 0 deletions test/action_controller/json_api/fields_test.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a side note, I think we need some organization of the tests.
maybe split them in to Unit, and Integration, and the further folders in Integration by topic of what's being tested.
Unit would follow the structure of lib - If this sounds like a good idea, lemme know -- this could cause conflicts with all the PRs, though. :-\

I just have a hard time finding things in tests

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
21 changes: 21 additions & 0 deletions test/adapter/json_api/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ def test_includes_comment_ids
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
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
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
expected = [{
Expand Down