From a319fef239d355e0530b43c6bb2b92a696272b75 Mon Sep 17 00:00:00 2001 From: "L. Preston Sego III" Date: Wed, 17 Aug 2016 17:12:12 -0400 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + .../action_controller/json_api/fields_test.rb | 57 +++++++++++++++++++ test/adapter/json_api/has_many_test.rb | 21 +++++++ 3 files changed, 79 insertions(+) create mode 100644 test/action_controller/json_api/fields_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 6874babd..751da693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Fixes: - [#1881](https://github.com/rails-api/active_model_serializers/pull/1881) ActiveModelSerializers::Model correctly works with string keys (@yevhene) 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) diff --git a/test/action_controller/json_api/fields_test.rb b/test/action_controller/json_api/fields_test.rb new file mode 100644 index 00000000..4bf08c7e --- /dev/null +++ b/test/action_controller/json_api/fields_test.rb @@ -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 diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index 05a7675a..9da73af9 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -40,6 +40,27 @@ module ActiveModelSerializers 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 = [{