File: serializer_for_with_namespace_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 (95 lines) | stat: -rw-r--r-- 2,912 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
# frozen_string_literal: true

require 'test_helper'

module ActiveModel
  class Serializer
    class SerializerForWithNamespaceTest < ActiveSupport::TestCase
      class Book < ::Model
        attributes :title, :author_name
        associations :publisher, :pages
      end
      class Ebook < Book; end
      class Page < ::Model; attributes :number, :text end
      class Publisher < ::Model; attributes :name end

      module Api
        module V3
          class BookSerializer < ActiveModel::Serializer
            attributes :title, :author_name

            has_many :pages
            belongs_to :publisher
          end

          class PageSerializer < ActiveModel::Serializer
            attributes :number, :text
          end

          class PublisherSerializer < ActiveModel::Serializer
            attributes :name
          end
        end
      end

      class BookSerializer < ActiveModel::Serializer
        attributes :title, :author_name
      end

      test 'resource without a namespace' do
        book = Book.new(title: 'A Post', author_name: 'hello')

        result = ActiveModelSerializers::SerializableResource.new(book).serializable_hash

        expected = { title: 'A Post', author_name: 'hello' }
        assert_equal expected, result
      end

      test 'resource with namespace' do
        book = Book.new(title: 'A Post', author_name: 'hi')

        result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash

        expected = { title: 'A Post', author_name: 'hi', pages: nil, publisher: nil }
        assert_equal expected, result
      end

      test 'has_many with nested serializer under the namespace' do
        page = Page.new(number: 1, text: 'hello')
        book = Book.new(title: 'A Post', author_name: 'hi', pages: [page])

        result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash

        expected = {
          title: 'A Post', author_name: 'hi',
          publisher: nil,
          pages: [{
            number: 1, text: 'hello'
          }]
        }
        assert_equal expected, result
      end

      test 'belongs_to with nested serializer under the namespace' do
        publisher = Publisher.new(name: 'Disney')
        book = Book.new(title: 'A Post', author_name: 'hi', publisher: publisher)

        result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash

        expected = {
          title: 'A Post', author_name: 'hi',
          pages: nil,
          publisher: {
            name: 'Disney'
          }
        }
        assert_equal expected, result
      end

      test 'follows inheritance with a namespace' do
        serializer = ActiveModel::Serializer.serializer_for(Ebook.new, namespace: Api::V3)
        assert_equal Api::V3::BookSerializer, serializer
      end
    end
  end
end