File: belongs_to_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 (157 lines) | stat: -rw-r--r-- 5,347 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# frozen_string_literal: true

require 'test_helper'

module ActiveModelSerializers
  module Adapter
    class JsonApi
      class BelongsToTest < 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 = []

          @serializer = CommentSerializer.new(@comment)
          @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
          ActionController::Base.cache_store.clear
        end

        def test_includes_post_id
          expected = { data: { type: 'posts', id: '42' } }

          assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:post])
        end

        def test_includes_linked_post
          @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:post])
          expected = [{
            id: '42',
            type: 'posts',
            attributes: {
              title: 'New Post',
              body: 'Body'
            },
            relationships: {
              comments: { data: [{ type: 'comments', id: '1' }] },
              blog: { data: { type: 'blogs', id: '999' } },
              author: { data: { type: 'authors', id: '1' } }
            }
          }]
          assert_equal expected, @adapter.serializable_hash[:included]
        end

        def test_limiting_linked_post_fields
          @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:post], fields: { post: [:title, :comments, :blog, :author] })
          expected = [{
            id: '42',
            type: 'posts',
            attributes: {
              title: 'New Post'
            },
            relationships: {
              comments: { data: [{ type: 'comments', id: '1' }] },
              blog: { data: { type: 'blogs', id: '999' } },
              author: { data: { type: 'authors', id: '1' } }
            }
          }]
          assert_equal expected, @adapter.serializable_hash[:included]
        end

        def test_include_nil_author
          serializer = PostSerializer.new(@anonymous_post)
          adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)

          assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } }, adapter.serializable_hash[:data][:relationships])
        end

        def test_include_type_for_association_when_different_than_name
          serializer = BlogSerializer.new(@blog)
          adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
          relationships = adapter.serializable_hash[:data][:relationships]
          expected = {
            writer: {
              data: {
                type: 'authors',
                id: '1'
              }
            },
            articles: {
              data: [
                {
                  type: 'posts',
                  id: '42'
                },
                {
                  type: 'posts',
                  id: '43'
                }
              ]
            }
          }
          assert_equal expected, relationships
        end

        def test_include_linked_resources_with_type_name
          serializer = BlogSerializer.new(@blog)
          adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, include: [:writer, :articles])
          linked = adapter.serializable_hash[:included]
          expected = [
            {
              id: '1',
              type: 'authors',
              attributes: {
                name: 'Steve K.'
              },
              relationships: {
                posts: { data: [] },
                roles: { data: [] },
                bio: { data: nil }
              }
            }, {
              id: '42',
              type: 'posts',
              attributes: {
                title: 'New Post',
                body: 'Body'
              },
              relationships: {
                comments: { data: [{ type: 'comments', id: '1' }] },
                blog: { data: { type: 'blogs', id: '999' } },
                author: { data: { type: 'authors', id: '1' } }
              }
            }, {
              id: '43',
              type: 'posts',
              attributes: {
                title: 'Hello!!',
                body: 'Hello, world!!'
              },
              relationships: {
                comments: { data: [] },
                blog: { data: { type: 'blogs', id: '999' } },
                author: { data: nil }
              }
            }
          ]
          assert_equal expected, linked
        end
      end
    end
  end
end