File: transform_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 (191 lines) | stat: -rw-r--r-- 6,342 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# frozen_string_literal: true

require 'test_helper'

module ActionController
  module Serialization
    class JsonApi
      class KeyTransformTest < ActionController::TestCase
        class KeyTransformTestController < ActionController::Base
          class Post < ::Model
            attributes :title, :body, :publish_at
            associations :author, :top_comments
          end
          class Author < ::Model
            attributes :first_name, :last_name
          end
          class TopComment < ::Model
            attributes :body
            associations :author, :post
          end
          class PostSerializer < ActiveModel::Serializer
            type 'posts'
            attributes :title, :body, :publish_at
            belongs_to :author
            has_many :top_comments

            link(:post_authors) { 'https://example.com/post_authors' }

            meta do
              {
                rating: 5,
                favorite_count: 10
              }
            end
          end

          class AuthorSerializer < ActiveModel::Serializer
            type 'authors'
            attributes :first_name, :last_name
          end

          class TopCommentSerializer < ActiveModel::Serializer
            type 'top_comments'
            attributes :body
            belongs_to :author
          end

          def setup_post
            ActionController::Base.cache_store.clear
            @author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
            @comment1 = TopComment.new(id: 7, body: 'cool', author: @author)
            @comment2 = TopComment.new(id: 12, body: 'awesome', author: @author)
            @post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
                             author: @author, top_comments: [@comment1, @comment2],
                             publish_at: '2020-03-16T03:55:25.291Z')
            @comment1.post = @post
            @comment2.post = @post
          end

          def render_resource_with_transform
            setup_post
            render json: @post, serializer: PostSerializer, adapter: :json_api,
                   key_transform: :camel
          end

          def render_resource_with_transform_nil
            setup_post
            render json: @post, serializer: PostSerializer, adapter: :json_api,
                   key_transform: nil
          end

          def render_resource_with_transform_with_global_config
            old_transform = ActiveModelSerializers.config.key_transform
            setup_post
            ActiveModelSerializers.config.key_transform = :camel_lower
            render json: @post, serializer: PostSerializer, adapter: :json_api
          ensure
            ActiveModelSerializers.config.key_transform = old_transform
          end
        end

        tests KeyTransformTestController

        def test_render_resource_with_transform
          get :render_resource_with_transform
          response = JSON.parse(@response.body)
          expected = {
            'Data' => {
              'Id' => '1337',
              'Type' => 'Posts',
              'Attributes' => {
                'Title' => 'Title 1',
                'Body' => 'Body 1',
                'PublishAt' => '2020-03-16T03:55:25.291Z'
              },
              'Relationships' => {
                'Author' => {
                  'Data' => {
                    'Id' => '1',
                    'Type' => 'Authors'
                  }
                },
                'TopComments' => {
                  'Data' => [
                    { 'Id' => '7', 'Type' => 'TopComments' },
                    { 'Id' => '12', 'Type' => 'TopComments' }
                  ]
                }
              },
              'Links' => {
                'PostAuthors' => 'https://example.com/post_authors'
              },
              'Meta' => { 'Rating' => 5, 'FavoriteCount' => 10 }
            }
          }
          assert_equal expected, response
        end

        def test_render_resource_with_transform_nil
          get :render_resource_with_transform_nil
          response = JSON.parse(@response.body)
          expected = {
            'data' => {
              'id' => '1337',
              'type' => 'posts',
              'attributes' => {
                'title' => 'Title 1',
                'body' => 'Body 1',
                'publish-at' => '2020-03-16T03:55:25.291Z'
              },
              'relationships' => {
                'author' => {
                  'data' => {
                    'id' => '1',
                    'type' => 'authors'
                  }
                },
                'top-comments' => {
                  'data' => [
                    { 'id' => '7', 'type' => 'top-comments' },
                    { 'id' => '12', 'type' => 'top-comments' }
                  ]
                }
              },
              'links' => {
                'post-authors' => 'https://example.com/post_authors'
              },
              'meta' => { 'rating' => 5, 'favorite-count' => 10 }
            }
          }
          assert_equal expected, response
        end

        def test_render_resource_with_transform_with_global_config
          get :render_resource_with_transform_with_global_config
          response = JSON.parse(@response.body)
          expected =  {
            'data' => {
              'id' => '1337',
              'type' => 'posts',
              'attributes' => {
                'title' => 'Title 1',
                'body' => 'Body 1',
                'publishAt' => '2020-03-16T03:55:25.291Z'
              },
              'relationships' => {
                'author' => {
                  'data' => {
                    'id' => '1',
                    'type' => 'authors'
                  }
                },
                'topComments' => {
                  'data' => [
                    { 'id' => '7', 'type' => 'topComments' },
                    { 'id' => '12', 'type' => 'topComments' }
                  ]
                }
              },
              'links' => {
                'postAuthors' => 'https://example.com/post_authors'
              },
              'meta' => { 'rating' => 5, 'favoriteCount' => 10 }
            }
          }
          assert_equal expected, response
        end
      end
    end
  end
end