test/unit/serializer/serializer_test.rb in jsonapi-resources-0.4.4 vs test/unit/serializer/serializer_test.rb in jsonapi-resources-0.5.0

- old
+ new

@@ -9,13 +9,15 @@ @expense_entry = ExpenseEntry.find(1) JSONAPI.configuration.json_key_format = :camelized_key JSONAPI.configuration.route_format = :camelized_route + JSONAPI.configuration.always_include_to_one_linkage_data = false end def after_teardown + JSONAPI.configuration.always_include_to_one_linkage_data = false JSONAPI.configuration.json_key_format = :underscored_key end def test_serializer @@ -40,21 +42,16 @@ relationships: { section: { links: { self: 'http://example.com/posts/1/relationships/section', related: 'http://example.com/posts/1/section' - }, - data: nil + } }, author: { links: { self: 'http://example.com/posts/1/relationships/author', related: 'http://example.com/posts/1/author' - }, - data: { - type: 'people', - id: '1' } }, tags: { links: { self: 'http://example.com/posts/1/relationships/tags', @@ -100,21 +97,16 @@ relationships: { section: { links:{ self: 'http://example.com/api/v1/posts/1/relationships/section', related: 'http://example.com/api/v1/posts/1/section' - }, - data: nil + } }, writer: { links:{ self: 'http://example.com/api/v1/posts/1/relationships/writer', related: 'http://example.com/api/v1/posts/1/writer' - }, - data: { - type: 'writers', - id: '1' } }, comments: { links:{ self: 'http://example.com/api/v1/posts/1/relationships/comments', @@ -146,14 +138,10 @@ relationships: { author: { links: { self: '/posts/1/relationships/author', related: '/posts/1/author' - }, - data: { - type: 'people', - id: '1' } } } } }, @@ -184,12 +172,11 @@ relationships: { section: { links: { self: '/posts/1/relationships/section', related: '/posts/1/section' - }, - data: nil + } }, author: { links: { self: '/posts/1/relationships/author', related: '/posts/1/author' @@ -240,22 +227,17 @@ }, preferences: { links: { self: '/people/1/relationships/preferences', related: '/people/1/preferences' - }, - data: { - type: 'preferences', - id: '1' } }, hairCut: { links: { self: "/people/1/relationships/hairCut", related: "/people/1/hairCut" - }, - data: nil + } }, vehicles: { links: { self: "/people/1/relationships/vehicles", related: "/people/1/vehicles" @@ -292,12 +274,11 @@ relationships: { section: { links: { self: '/posts/1/relationships/section', related: '/posts/1/section' - }, - data: nil + } }, author: { links: { self: '/posts/1/relationships/author', related: '/posts/1/author' @@ -348,22 +329,17 @@ }, preferences: { links: { self: '/people/1/relationships/preferences', related: '/people/1/preferences' - }, - data: { - type: 'preferences', - id: '1' } }, hair_cut: { links: { self: '/people/1/relationships/hairCut', related: '/people/1/hairCut' - }, - data: nil + } }, vehicles: { links: { self: "/people/1/relationships/vehicles", related: "/people/1/vehicles" @@ -395,21 +371,16 @@ relationships: { section: { links: { self: '/posts/1/relationships/section', related: '/posts/1/section' - }, - data: nil + } }, author: { links: { self: '/posts/1/relationships/author', related: '/posts/1/author' - }, - data: { - type: 'people', - id: '1' } }, tags: { links: { self: '/posts/1/relationships/tags', @@ -495,24 +466,16 @@ relationships: { author: { links: { self: '/comments/1/relationships/author', related: '/comments/1/author' - }, - data: { - type: 'people', - id: '1' } }, post: { links: { self: '/comments/1/relationships/post', related: '/comments/1/post' - }, - data: { - type: 'posts', - id: '1' } }, tags: { links: { self: '/comments/1/relationships/tags', @@ -537,24 +500,16 @@ relationships: { author: { links: { self: '/comments/2/relationships/author', related: '/comments/2/author' - }, - data: { - type: 'people', - id: '2' } }, post: { links: { self: '/comments/2/relationships/post', related: '/comments/2/post' - }, - data: { - type: 'posts', - id: '1' } }, tags: { links: { self: '/comments/2/relationships/tags', @@ -612,19 +567,17 @@ }, preferences: { links: { self: "/people/2/relationships/preferences", related: "/people/2/preferences" - }, - data: nil + } }, hairCut: { links: { self: "/people/2/relationships/hairCut", related: "/people/2/hairCut" - }, - data: nil + } }, vehicles: { links: { self: "/people/2/relationships/vehicles", related: "/people/2/vehicles" @@ -645,24 +598,16 @@ relationships: { author: { links: { self: '/comments/2/relationships/author', related: '/comments/2/author' - }, - data: { - type: 'people', - id: '2' } }, post: { links: { self: '/comments/2/relationships/post', related: '/comments/2/post' - }, - data: { - type: 'posts', - id: '1' } }, tags: { links: { self: '/comments/2/relationships/tags', @@ -683,24 +628,16 @@ relationships: { author: { links: { self: '/comments/3/relationships/author', related: '/comments/3/author' - }, - data: { - type: 'people', - id: '2' } }, post: { links: { self: '/comments/3/relationships/post', related: '/comments/3/post' - }, - data: { - type: 'posts', - id: '2' } }, tags: { links: { self: '/comments/3/relationships/tags', @@ -713,17 +650,19 @@ }, serialized ) end - def test_serializer_array_of_resources + def test_serializer_array_of_resources_always_include_to_one_linkage_data posts = [] Post.find(1, 2).each do |post| posts.push PostResource.new(post) end + JSONAPI.configuration.always_include_to_one_linkage_data = true + assert_hash_equals( { data: [ { type: 'posts', @@ -1023,12 +962,288 @@ ] }, JSONAPI::ResourceSerializer.new(PostResource, include: ['comments', 'comments.tags']).serialize_to_hash(posts) ) + JSONAPI.configuration.always_include_to_one_linkage_data = false end + def test_serializer_array_of_resources + + posts = [] + Post.find(1, 2).each do |post| + posts.push PostResource.new(post) + end + + assert_hash_equals( + { + data: [ + { + type: 'posts', + id: '1', + attributes: { + title: 'New post', + body: 'A body!!!', + subject: 'New post' + }, + links: { + self: '/posts/1' + }, + relationships: { + section: { + links: { + self: '/posts/1/relationships/section', + related: '/posts/1/section' + } + }, + author: { + links: { + self: '/posts/1/relationships/author', + related: '/posts/1/author' + } + }, + tags: { + links: { + self: '/posts/1/relationships/tags', + related: '/posts/1/tags' + } + }, + comments: { + links: { + self: '/posts/1/relationships/comments', + related: '/posts/1/comments' + }, + data: [ + {type: 'comments', id: '1'}, + {type: 'comments', id: '2'} + ] + } + } + }, + { + type: 'posts', + id: '2', + attributes: { + title: 'JR Solves your serialization woes!', + body: 'Use JR', + subject: 'JR Solves your serialization woes!' + }, + links: { + self: '/posts/2' + }, + relationships: { + section: { + links: { + self: '/posts/2/relationships/section', + related: '/posts/2/section' + } + }, + author: { + links: { + self: '/posts/2/relationships/author', + related: '/posts/2/author' + } + }, + tags: { + links: { + self: '/posts/2/relationships/tags', + related: '/posts/2/tags' + } + }, + comments: { + links: { + self: '/posts/2/relationships/comments', + related: '/posts/2/comments' + }, + data: [ + {type: 'comments', id: '3'} + ] + } + } + } + ], + included: [ + { + type: 'tags', + id: '1', + attributes: { + name: 'short' + }, + links: { + self: '/tags/1' + }, + relationships: { + posts: { + links: { + self: '/tags/1/relationships/posts', + related: '/tags/1/posts' + } + } + } + }, + { + type: 'tags', + id: '2', + attributes: { + name: 'whiny' + }, + links: { + self: '/tags/2' + }, + relationships: { + posts: { + links: { + self: '/tags/2/relationships/posts', + related: '/tags/2/posts' + } + } + } + }, + { + type: 'tags', + id: '4', + attributes: { + name: 'happy' + }, + links: { + self: '/tags/4' + }, + relationships: { + posts: { + links: { + self: '/tags/4/relationships/posts', + related: '/tags/4/posts' + } + } + } + }, + { + type: 'tags', + id: '5', + attributes: { + name: 'JR' + }, + links: { + self: '/tags/5' + }, + relationships: { + posts: { + links: { + self: '/tags/5/relationships/posts', + related: '/tags/5/posts' + } + } + } + }, + { + type: 'comments', + id: '1', + attributes: { + body: 'what a dumb post' + }, + links: { + self: '/comments/1' + }, + relationships: { + author: { + links: { + self: '/comments/1/relationships/author', + related: '/comments/1/author' + } + }, + post: { + links: { + self: '/comments/1/relationships/post', + related: '/comments/1/post' + } + }, + tags: { + links: { + self: '/comments/1/relationships/tags', + related: '/comments/1/tags' + }, + data: [ + {type: 'tags', id: '1'}, + {type: 'tags', id: '2'} + ] + } + } + }, + { + type: 'comments', + id: '2', + attributes: { + body: 'i liked it' + }, + links: { + self: '/comments/2' + }, + relationships: { + author: { + links: { + self: '/comments/2/relationships/author', + related: '/comments/2/author' + } + }, + post: { + links: { + self: '/comments/2/relationships/post', + related: '/comments/2/post' + } + }, + tags: { + links: { + self: '/comments/2/relationships/tags', + related: '/comments/2/tags' + }, + data: [ + {type: 'tags', id: '4'}, + {type: 'tags', id: '1'} + ] + } + } + }, + { + type: 'comments', + id: '3', + attributes: { + body: 'Thanks man. Great post. But what is JR?' + }, + links: { + self: '/comments/3' + }, + relationships: { + author: { + links: { + self: '/comments/3/relationships/author', + related: '/comments/3/author' + } + }, + post: { + links: { + self: '/comments/3/relationships/post', + related: '/comments/3/post' + } + }, + tags: { + links: { + self: '/comments/3/relationships/tags', + related: '/comments/3/tags' + }, + data: [ + {type: 'tags', id: '5'} + ] + } + } + } + ] + }, + JSONAPI::ResourceSerializer.new(PostResource, + include: ['comments', 'comments.tags']).serialize_to_hash(posts) + ) + end + def test_serializer_array_of_resources_limited_fields posts = [] Post.find(1, 2).each do |post| posts.push PostResource.new(post) @@ -1139,14 +1354,10 @@ relationships: { post: { links: { self: '/comments/1/relationships/post', related: '/comments/1/post' - }, - data: { - type: 'posts', - id: '1' } } } }, { @@ -1161,14 +1372,10 @@ relationships: { post: { links: { self: '/comments/2/relationships/post', related: '/comments/2/post' - }, - data: { - type: 'posts', - id: '1' } } } }, { @@ -1183,14 +1390,10 @@ relationships: { post: { links: { self: '/comments/3/relationships/post', related: '/comments/3/post' - }, - data: { - type: 'posts', - id: '2' } } } } ] @@ -1298,12 +1501,11 @@ relationships: { planetType: { links: { self: '/planets/8/relationships/planetType', related: '/planets/8/planetType' - }, - data: nil + } }, tags: { links: { self: '/planets/8/relationships/tags', related: '/planets/8/tags' @@ -1434,12 +1636,11 @@ relationships: { author: { links: { self: '/preferences/1/relationships/author', related: '/preferences/1/author' - }, - data: nil + } }, friends: { links: { self: '/preferences/1/relationships/friends', related: '/preferences/1/friends' @@ -1477,8 +1678,58 @@ self: '/facts/1' } } }, JSONAPI::ResourceSerializer.new(FactResource).serialize_to_hash(facts) + ) + end + + def test_serializer_to_one + serialized = JSONAPI::ResourceSerializer.new( + Api::V5::AuthorResource, + include: ['author_detail'] + ).serialize_to_hash(Api::V5::AuthorResource.new(Person.find(1))) + + assert_hash_equals( + { + data: { + type: 'authors', + id: '1', + attributes: { + name: 'Joe Author', + }, + links: { + self: '/api/v5/authors/1' + }, + relationships: { + posts: { + links: { + self: '/api/v5/authors/1/relationships/posts', + related: '/api/v5/authors/1/posts' + } + }, + authorDetail: { + links: { + self: '/api/v5/authors/1/relationships/authorDetail', + related: '/api/v5/authors/1/authorDetail' + }, + data: {type: 'authorDetails', id: '1'} + } + } + }, + included: [ + { + type: 'authorDetails', + id: '1', + attributes: { + authorStuff: 'blah blah' + }, + links: { + self: '/api/v5/authorDetails/1' + } + } + ] + }, + serialized ) end end