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 (94 lines) | stat: -rw-r--r-- 3,068 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
# frozen_string_literal: true

require 'test_helper'

module ActiveModelSerializers
  module Adapter
    class Json
      class KeyCaseTest < ActiveSupport::TestCase
        def mock_request(key_transform = nil)
          context = Minitest::Mock.new
          context.expect(:request_url, URI)
          context.expect(:query_parameters, {})
          options = {}
          options[:key_transform] = key_transform if key_transform
          options[:serialization_context] = context
          serializer = CustomBlogSerializer.new(@blog)
          @adapter = ActiveModelSerializers::Adapter::Json.new(serializer, options)
        end

        class Post < ::Model; end
        class PostSerializer < ActiveModel::Serializer
          attributes :id, :title, :body, :publish_at
        end

        setup do
          ActionController::Base.cache_store.clear
          @blog = Blog.new(id: 1, name: 'My Blog!!', special_attribute: 'neat')
        end

        def test_transform_default
          mock_request
          assert_equal({
                         blog: { id: 1, special_attribute: 'neat', articles: nil }
                       }, @adapter.serializable_hash)
        end

        def test_transform_global_config
          mock_request
          result = with_config(key_transform: :camel_lower) do
            @adapter.serializable_hash
          end
          assert_equal({
                         blog: { id: 1, specialAttribute: 'neat', articles: nil }
                       }, result)
        end

        def test_transform_serialization_ctx_overrides_global_config
          mock_request(:camel)
          result = with_config(key_transform: :camel_lower) do
            @adapter.serializable_hash
          end
          assert_equal({
                         Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
                       }, result)
        end

        def test_transform_undefined
          mock_request(:blam)
          assert_raises NoMethodError do
            @adapter.serializable_hash
          end
        end

        def test_transform_dash
          mock_request(:dash)
          assert_equal({
                         blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
                       }, @adapter.serializable_hash)
        end

        def test_transform_unaltered
          mock_request(:unaltered)
          assert_equal({
                         blog: { id: 1, special_attribute: 'neat', articles: nil }
                       }, @adapter.serializable_hash)
        end

        def test_transform_camel
          mock_request(:camel)
          assert_equal({
                         Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
                       }, @adapter.serializable_hash)
        end

        def test_transform_camel_lower
          mock_request(:camel_lower)
          assert_equal({
                         blog: { id: 1, specialAttribute: 'neat', articles: nil }
                       }, @adapter.serializable_hash)
        end
      end
    end
  end
end