|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActiveModelSerializers |
| 4 | + module Adapter |
| 5 | + class Json |
| 6 | + class KeyCaseTest < ActiveSupport::TestCase |
| 7 | + def mock_request(key_transform = nil) |
| 8 | + context = Minitest::Mock.new |
| 9 | + context.expect(:request_url, URI) |
| 10 | + context.expect(:query_parameters, {}) |
| 11 | + context.expect(:key_transform, key_transform) |
| 12 | + @options = {} |
| 13 | + @options[:serialization_context] = context |
| 14 | + end |
| 15 | + |
| 16 | + Post = Class.new(::Model) |
| 17 | + class PostSerializer < ActiveModel::Serializer |
| 18 | + attributes :id, :title, :body, :publish_at |
| 19 | + end |
| 20 | + |
| 21 | + def setup |
| 22 | + ActionController::Base.cache_store.clear |
| 23 | + @blog = Blog.new(id: 1, name: 'My Blog!!', special_attribute: 'neat') |
| 24 | + serializer = CustomBlogSerializer.new(@blog) |
| 25 | + @adapter = ActiveModelSerializers::Adapter::Json.new(serializer) |
| 26 | + end |
| 27 | + |
| 28 | + def test_key_transform_default |
| 29 | + mock_request |
| 30 | + assert_equal({ |
| 31 | + blog: { id: 1, special_attribute: 'neat', articles: nil } |
| 32 | + }, @adapter.serializable_hash(@options)) |
| 33 | + end |
| 34 | + |
| 35 | + def test_key_transform_undefined |
| 36 | + mock_request(:blam) |
| 37 | + result = nil |
| 38 | + assert_raises NoMethodError do |
| 39 | + result = @adapter.serializable_hash(@options) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def test_key_transform_dashed |
| 44 | + mock_request(:dashed) |
| 45 | + assert_equal({ |
| 46 | + blog: { id: 1, :"special-attribute" => 'neat', articles: nil } |
| 47 | + }, @adapter.serializable_hash(@options)) |
| 48 | + end |
| 49 | + |
| 50 | + def test_key_transform_unaltered |
| 51 | + mock_request(:unaltered) |
| 52 | + assert_equal({ |
| 53 | + blog: { id: 1, special_attribute: 'neat', articles: nil } |
| 54 | + }, @adapter.serializable_hash(@options)) |
| 55 | + end |
| 56 | + |
| 57 | + def test_key_transform_camel |
| 58 | + mock_request(:camel) |
| 59 | + assert_equal({ |
| 60 | + Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil } |
| 61 | + }, @adapter.serializable_hash(@options)) |
| 62 | + end |
| 63 | + |
| 64 | + def test_key_transform_camel_lower |
| 65 | + mock_request(:camel_lower) |
| 66 | + assert_equal({ |
| 67 | + blog: { id: 1, specialAttribute: 'neat', articles: nil } |
| 68 | + }, @adapter.serializable_hash(@options)) |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
0 commit comments