|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActiveModelSerializers |
| 4 | + module Adapter |
| 5 | + class JsonApi |
| 6 | + class KeyCaseTest < ActiveSupport::TestCase |
| 7 | + Post = Class.new(::Model) |
| 8 | + class PostSerializer < ActiveModel::Serializer |
| 9 | + type 'posts' |
| 10 | + attributes :title, :body, :publish_at |
| 11 | + belongs_to :author |
| 12 | + has_many :comments |
| 13 | + |
| 14 | + link(:self) { post_url(object.id) } |
| 15 | + link(:post_authors) { post_authors_url(object.id) } |
| 16 | + link(:subscriber_comments) { post_comments_url(object.id) } |
| 17 | + |
| 18 | + meta do |
| 19 | + { |
| 20 | + rating: 5, |
| 21 | + favorite_count: 10 |
| 22 | + } |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + Author = Class.new(::Model) |
| 27 | + class AuthorSerializer < ActiveModel::Serializer |
| 28 | + type 'authors' |
| 29 | + attributes :first_name, :last_name |
| 30 | + end |
| 31 | + |
| 32 | + Comment = Class.new(::Model) |
| 33 | + class CommentSerializer < ActiveModel::Serializer |
| 34 | + type 'comments' |
| 35 | + attributes :body |
| 36 | + belongs_to :author |
| 37 | + end |
| 38 | + |
| 39 | + def mock_request(key_case = :default) |
| 40 | + context = Minitest::Mock.new |
| 41 | + context.expect(:request_url, URI) |
| 42 | + context.expect(:query_parameters, {}) |
| 43 | + context.expect(:key_case, key_case) |
| 44 | + context.expect(:url_helpers, Rails.application.routes.url_helpers) |
| 45 | + @options = {} |
| 46 | + @options[:serialization_context] = context |
| 47 | + end |
| 48 | + |
| 49 | + def setup |
| 50 | + Rails.application.routes.draw do |
| 51 | + resources :posts do |
| 52 | + resources :authors |
| 53 | + resources :comments |
| 54 | + end |
| 55 | + end |
| 56 | + @publish_at = 1.day.from_now |
| 57 | + @author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones') |
| 58 | + @comment1 = Comment.new(id: 7, body: 'cool', author: @author) |
| 59 | + @comment2 = Comment.new(id: 12, body: 'awesome', author: @author) |
| 60 | + @post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1', |
| 61 | + author: @author, comments: [@comment1, @comment2], |
| 62 | + publish_at: @publish_at) |
| 63 | + @comment1.post = @post |
| 64 | + @comment2.post = @post |
| 65 | + end |
| 66 | + |
| 67 | + def test_success_key_case_default |
| 68 | + mock_request |
| 69 | + serializer = PostSerializer.new(@post) |
| 70 | + adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer) |
| 71 | + result = adapter.serializable_hash(@options) |
| 72 | + assert_equal({ |
| 73 | + data: { |
| 74 | + id: "1337", |
| 75 | + type: "posts", |
| 76 | + attributes: { |
| 77 | + title: "Title 1", |
| 78 | + body: "Body 1", |
| 79 | + publish_at: @publish_at |
| 80 | + }, |
| 81 | + relationships: { |
| 82 | + author: { |
| 83 | + data: { id: "1", type: "authors" } |
| 84 | + }, |
| 85 | + comments: { |
| 86 | + data: [ |
| 87 | + { id: "7", type: "comments" }, |
| 88 | + { id: "12", type: "comments" } |
| 89 | + ]} |
| 90 | + }, |
| 91 | + links: { |
| 92 | + self: "http://example.com/posts/1337", |
| 93 | + post_authors: "http://example.com/posts/1337/authors", |
| 94 | + subscriber_comments: "http://example.com/posts/1337/comments" |
| 95 | + }, |
| 96 | + meta: { rating: 5, favorite_count: 10 } |
| 97 | + } |
| 98 | + }, result) |
| 99 | + end |
| 100 | + |
| 101 | + def test_success_key_case_camel |
| 102 | + mock_request(:camel) |
| 103 | + serializer = PostSerializer.new(@post) |
| 104 | + adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer) |
| 105 | + result = adapter.serializable_hash(@options) |
| 106 | + assert_equal({ |
| 107 | + Data: { |
| 108 | + Id: "1337", |
| 109 | + Type: "posts", |
| 110 | + Attributes: { |
| 111 | + Title: "Title 1", |
| 112 | + Body: "Body 1", |
| 113 | + PublishAt: @publish_at |
| 114 | + }, |
| 115 | + Relationships: { |
| 116 | + Author: { |
| 117 | + Data: { Id: "1", Type: "authors" } |
| 118 | + }, |
| 119 | + Comments: { |
| 120 | + Data: [ |
| 121 | + { Id: "7", Type: "comments" }, |
| 122 | + { Id: "12", Type: "comments" } |
| 123 | + ]} |
| 124 | + }, |
| 125 | + Links: { |
| 126 | + Self: "http://example.com/posts/1337", |
| 127 | + PostAuthors: "http://example.com/posts/1337/authors", |
| 128 | + SubscriberComments: "http://example.com/posts/1337/comments" |
| 129 | + }, |
| 130 | + Meta: { Rating: 5, FavoriteCount: 10 } |
| 131 | + } |
| 132 | + }, result) |
| 133 | + end |
| 134 | + |
| 135 | + def test_success_key_case_camel_lower |
| 136 | + mock_request(:camel_lower) |
| 137 | + serializer = PostSerializer.new(@post) |
| 138 | + adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer) |
| 139 | + result = adapter.serializable_hash(@options) |
| 140 | + assert_equal({ |
| 141 | + data: { |
| 142 | + id: "1337", |
| 143 | + type: "posts", |
| 144 | + attributes: { |
| 145 | + title: "Title 1", |
| 146 | + body: "Body 1", |
| 147 | + publishAt: @publish_at |
| 148 | + }, |
| 149 | + relationships: { |
| 150 | + author: { |
| 151 | + data: { id: "1", type: "authors" } |
| 152 | + }, |
| 153 | + comments: { |
| 154 | + data: [ |
| 155 | + { id: "7", type: "comments" }, |
| 156 | + { id: "12", type: "comments" } |
| 157 | + ]} |
| 158 | + }, |
| 159 | + links: { |
| 160 | + self: "http://example.com/posts/1337", |
| 161 | + postAuthors: "http://example.com/posts/1337/authors", |
| 162 | + subscriberComments: "http://example.com/posts/1337/comments" |
| 163 | + }, |
| 164 | + meta: { rating: 5, favoriteCount: 10 } |
| 165 | + } |
| 166 | + }, result) |
| 167 | + end |
| 168 | + |
| 169 | + def test_error_document_key_case_default |
| 170 | + mock_request(:default) |
| 171 | + |
| 172 | + resource = ModelWithErrors.new |
| 173 | + resource.errors.add(:published_at, 'must be in the future') |
| 174 | + resource.errors.add(:title, 'must be longer') |
| 175 | + |
| 176 | + serializer = ActiveModel::Serializer::ErrorSerializer.new(resource) |
| 177 | + adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer) |
| 178 | + result = adapter.serializable_hash(@options) |
| 179 | + |
| 180 | + expected_errors_object = |
| 181 | + { :errors => |
| 182 | + [ |
| 183 | + { :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' }, |
| 184 | + { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' } |
| 185 | + ] |
| 186 | + } |
| 187 | + assert_equal expected_errors_object, result |
| 188 | + end |
| 189 | + |
| 190 | + def test_error_document_key_case_camel |
| 191 | + mock_request(:camel) |
| 192 | + |
| 193 | + resource = ModelWithErrors.new |
| 194 | + resource.errors.add(:published_at, 'must be in the future') |
| 195 | + resource.errors.add(:title, 'must be longer') |
| 196 | + |
| 197 | + serializer = ActiveModel::Serializer::ErrorSerializer.new(resource) |
| 198 | + adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer) |
| 199 | + result = adapter.serializable_hash(@options) |
| 200 | + |
| 201 | + expected_errors_object = |
| 202 | + { :Errors => |
| 203 | + [ |
| 204 | + { :Source => { :Pointer => '/data/attributes/published_at' }, :Detail => 'must be in the future' }, |
| 205 | + { :Source => { :Pointer => '/data/attributes/title' }, :Detail => 'must be longer' } |
| 206 | + ] |
| 207 | + } |
| 208 | + assert_equal expected_errors_object, result |
| 209 | + end |
| 210 | + |
| 211 | + def test_error_document_key_case_camel_lower |
| 212 | + mock_request(:camel_lower) |
| 213 | + |
| 214 | + resource = ModelWithErrors.new |
| 215 | + resource.errors.add(:published_at, 'must be in the future') |
| 216 | + resource.errors.add(:title, 'must be longer') |
| 217 | + |
| 218 | + serializer = ActiveModel::Serializer::ErrorSerializer.new(resource) |
| 219 | + adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer) |
| 220 | + result = adapter.serializable_hash(@options) |
| 221 | + |
| 222 | + expected_errors_object = |
| 223 | + { :errors => |
| 224 | + [ |
| 225 | + { :source => { :pointer => '/data/attributes/published_at' }, :detail => 'must be in the future' }, |
| 226 | + { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be longer' } |
| 227 | + ] |
| 228 | + } |
| 229 | + assert_equal expected_errors_object, result |
| 230 | + end |
| 231 | + end |
| 232 | + end |
| 233 | + end |
| 234 | +end |
0 commit comments