File: toplevel_jsonapi_test.rb

package info (click to toggle)
ruby-active-model-serializers 0.10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,752 kB
  • sloc: ruby: 13,138; sh: 53; makefile: 6
file content (84 lines) | stat: -rw-r--r-- 2,676 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

require 'test_helper'

module ActiveModelSerializers
  module Adapter
    class JsonApi
      class TopLevelJsonApiTest < ActiveSupport::TestCase
        def setup
          @author = Author.new(id: 1, name: 'Steve K.')
          @author.bio = nil
          @author.roles = []
          @blog = Blog.new(id: 23, name: 'AMS Blog')
          @post = Post.new(id: 42, title: 'New Post', body: 'Body')
          @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
          @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
          @post.comments = [@comment]
          @post.blog = @blog
          @anonymous_post.comments = []
          @anonymous_post.blog = nil
          @comment.post = @post
          @comment.author = nil
          @post.author = @author
          @anonymous_post.author = nil
          @blog = Blog.new(id: 1, name: 'My Blog!!')
          @blog.writer = @author
          @blog.articles = [@post, @anonymous_post]
          @author.posts = []
        end

        def test_toplevel_jsonapi_defaults_to_false
          assert_equal config.fetch(:jsonapi_include_toplevel_object), false
        end

        def test_disable_toplevel_jsonapi
          with_config(jsonapi_include_toplevel_object: false) do
            hash = serialize(@post)
            assert_nil(hash[:jsonapi])
          end
        end

        def test_enable_toplevel_jsonapi
          with_config(jsonapi_include_toplevel_object: true) do
            hash = serialize(@post)
            refute_nil(hash[:jsonapi])
          end
        end

        def test_default_toplevel_jsonapi_version
          with_config(jsonapi_include_toplevel_object: true) do
            hash = serialize(@post)
            assert_equal('1.0', hash[:jsonapi][:version])
          end
        end

        def test_toplevel_jsonapi_no_meta
          with_config(jsonapi_include_toplevel_object: true) do
            hash = serialize(@post)
            assert_nil(hash[:jsonapi][:meta])
          end
        end

        def test_toplevel_jsonapi_meta
          new_config = {
            jsonapi_include_toplevel_object: true,
            jsonapi_toplevel_meta: {
              'copyright' => 'Copyright 2015 Example Corp.'
            }
          }
          with_config(new_config) do
            hash = serialize(@post)
            assert_equal(new_config[:jsonapi_toplevel_meta], hash.fetch(:jsonapi).fetch(:meta))
          end
        end

        private

        def serialize(resource, options = {})
          serializable(resource, { adapter: :json_api }.merge!(options)).serializable_hash
        end
      end
    end
  end
end