File: explicit_serializer_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 (137 lines) | stat: -rw-r--r-- 4,624 bytes parent folder | download
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
# frozen_string_literal: true

require 'test_helper'

module ActionController
  module Serialization
    class ExplicitSerializerTest < ActionController::TestCase
      class ExplicitSerializerTestController < ActionController::Base
        def render_using_explicit_serializer
          @profile = Profile.new(name: 'Name 1',
                                 description: 'Description 1',
                                 comments: 'Comments 1')
          render json: @profile, serializer: ProfilePreviewSerializer
        end

        def render_array_using_explicit_serializer
          array = [
            Profile.new(name: 'Name 1',
                        description: 'Description 1',
                        comments: 'Comments 1'),
            Profile.new(name: 'Name 2',
                        description: 'Description 2',
                        comments: 'Comments 2')
          ]
          render json: array,
                 serializer: PaginatedSerializer,
                 each_serializer: ProfilePreviewSerializer
        end

        def render_array_using_implicit_serializer
          array = [
            Profile.new(name: 'Name 1',
                        description: 'Description 1',
                        comments: 'Comments 1'),
            Profile.new(name: 'Name 2',
                        description: 'Description 2',
                        comments: 'Comments 2')
          ]
          render json: array,
                 each_serializer: ProfilePreviewSerializer
        end

        def render_array_using_explicit_serializer_and_custom_serializers
          @post = Post.new(title: 'New Post', body: 'Body')
          @author = Author.new(name: 'Jane Blogger')
          @author.posts = [@post]
          @post.author = @author
          @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
          @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
          @post.comments = [@first_comment, @second_comment]
          @first_comment.post = @post
          @first_comment.author = nil
          @second_comment.post = @post
          @second_comment.author = nil
          @blog = Blog.new(id: 23, name: 'AMS Blog')
          @post.blog = @blog

          render json: [@post], each_serializer: PostPreviewSerializer
        end

        def render_using_explicit_each_serializer
          location       = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309')
          place          = Place.new(id: 1337, name: 'Amazing Place', locations: [location])

          render json: place, each_serializer: PlaceSerializer
        end
      end

      tests ExplicitSerializerTestController

      def test_render_using_explicit_serializer
        get :render_using_explicit_serializer

        assert_match(%r{\Aapplication/json}, @response.content_type)
        assert_equal '{"name":"Name 1"}', @response.body
      end

      def test_render_array_using_explicit_serializer
        get :render_array_using_explicit_serializer
        assert_match(%r{\Aapplication/json}, @response.content_type)

        expected = [
          { 'name' => 'Name 1' },
          { 'name' => 'Name 2' }
        ]

        assert_equal expected.to_json, @response.body
      end

      def test_render_array_using_implicit_serializer
        get :render_array_using_implicit_serializer
        assert_match(%r{\Aapplication/json}, @response.content_type)

        expected = [
          { 'name' => 'Name 1' },
          { 'name' => 'Name 2' }
        ]
        assert_equal expected.to_json, @response.body
      end

      def test_render_array_using_explicit_serializer_and_custom_serializers
        get :render_array_using_explicit_serializer_and_custom_serializers

        expected = [
          {
            'title' => 'New Post',
            'body' => 'Body',
            'id' => @controller.instance_variable_get(:@post).id,
            'comments' => [{ 'id' => 1 }, { 'id' => 2 }],
            'author' => { 'id' => @controller.instance_variable_get(:@author).id }
          }
        ]

        assert_equal expected.to_json, @response.body
      end

      def test_render_using_explicit_each_serializer
        get :render_using_explicit_each_serializer

        expected = {
          id: 1337,
          name: 'Amazing Place',
          locations: [
            {
              id: 42,
              lat: '-23.550520',
              lng: '-46.633309',
              address: 'Nowhere' # is a virtual attribute on LocationSerializer
            }
          ]
        }

        assert_equal expected.to_json, response.body
      end
    end
  end
end