-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
f864d9e
df6b428
6660222
1d505f1
bb8e468
3918eca
6ff9295
369c425
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. I just have a hard time finding things in |
||
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 |
There was a problem hiding this comment.
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.