-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1127 from NullVoxPopuli/support-nested-associatio…
…ns-for-json-adapter Support nested associations for Json and Attributes adapters + Refactor Attributes adapter
- Loading branch information
Showing
5 changed files
with
289 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
require 'test_helper' | ||
|
||
module ActionController | ||
module Serialization | ||
class Json | ||
class IncludeTest < ActionController::TestCase | ||
class IncludeTestController < ActionController::Base | ||
def setup_data | ||
ActionController::Base.cache_store.clear | ||
|
||
@author = Author.new(id: 1, name: 'Steve K.') | ||
|
||
@post = Post.new(id: 42, title: 'New Post', body: 'Body') | ||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') | ||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') | ||
|
||
@post.comments = [@first_comment, @second_comment] | ||
@post.author = @author | ||
|
||
@first_comment.post = @post | ||
@second_comment.post = @post | ||
|
||
@blog = Blog.new(id: 1, name: 'My Blog!!') | ||
@post.blog = @blog | ||
@author.posts = [@post] | ||
|
||
@first_comment.author = @author | ||
@second_comment.author = @author | ||
@author.comments = [@first_comment, @second_comment] | ||
@author.roles = [] | ||
@author.bio = {} | ||
end | ||
|
||
def render_without_include | ||
setup_data | ||
render json: @author, adapter: :json | ||
end | ||
|
||
def render_resource_with_include_hash | ||
setup_data | ||
render json: @author, include: { posts: :comments }, adapter: :json | ||
end | ||
|
||
def render_resource_with_include_string | ||
setup_data | ||
render json: @author, include: 'posts.comments', adapter: :json | ||
end | ||
|
||
def render_resource_with_deep_include | ||
setup_data | ||
render json: @author, include: 'posts.comments.author', adapter: :json | ||
end | ||
end | ||
|
||
tests IncludeTestController | ||
|
||
def test_render_without_include | ||
get :render_without_include | ||
response = JSON.parse(@response.body) | ||
expected = { | ||
'author' => { | ||
'id' => 1, | ||
'name' => 'Steve K.', | ||
'posts' => [ | ||
{ | ||
'id' => 42, 'title' => 'New Post', 'body' => 'Body' | ||
} | ||
], | ||
'roles' => [], | ||
'bio' => {} | ||
} | ||
} | ||
|
||
assert_equal(expected, response) | ||
end | ||
|
||
def test_render_resource_with_include_hash | ||
get :render_resource_with_include_hash | ||
response = JSON.parse(@response.body) | ||
expected = { | ||
'author' => { | ||
'id' => 1, | ||
'name' => 'Steve K.', | ||
'posts' => [ | ||
{ | ||
'id' => 42, 'title' => 'New Post', 'body' => 'Body', | ||
'comments' => [ | ||
{ | ||
'id' => 1, 'body' => 'ZOMG A COMMENT' | ||
}, | ||
{ | ||
'id' => 2, 'body' => 'ZOMG ANOTHER COMMENT' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
assert_equal(expected, response) | ||
end | ||
|
||
def test_render_resource_with_include_string | ||
get :render_resource_with_include_string | ||
|
||
response = JSON.parse(@response.body) | ||
expected = { | ||
'author' => { | ||
'id' => 1, | ||
'name' => 'Steve K.', | ||
'posts' => [ | ||
{ | ||
'id' => 42, 'title' => 'New Post', 'body' => 'Body', | ||
'comments' => [ | ||
{ | ||
'id' => 1, 'body' => 'ZOMG A COMMENT' | ||
}, | ||
{ | ||
'id' => 2, 'body' => 'ZOMG ANOTHER COMMENT' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
assert_equal(expected, response) | ||
end | ||
|
||
def test_render_resource_with_deep_include | ||
get :render_resource_with_deep_include | ||
|
||
response = JSON.parse(@response.body) | ||
expected = { | ||
'author' => { | ||
'id' => 1, | ||
'name' => 'Steve K.', | ||
'posts' => [ | ||
{ | ||
'id' => 42, 'title' => 'New Post', 'body' => 'Body', | ||
'comments' => [ | ||
{ | ||
'id' => 1, 'body' => 'ZOMG A COMMENT', | ||
'author' => { | ||
'id' => 1, | ||
'name' => 'Steve K.' | ||
} | ||
}, | ||
{ | ||
'id' => 2, 'body' => 'ZOMG ANOTHER COMMENT', | ||
'author' => { | ||
'id' => 1, | ||
'name' => 'Steve K.' | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
assert_equal(expected, response) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class IncludeTree | ||
module Parsing | ||
class IncludeArgsToHashTest < MiniTest::Test | ||
def test_include_args_to_hash_from_symbol | ||
expected = { author: {} } | ||
input = :author | ||
actual = Parsing.include_args_to_hash(input) | ||
|
||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_include_args_to_hash_from_array | ||
expected = { author: {}, comments: {} } | ||
input = [:author, :comments] | ||
actual = Parsing.include_args_to_hash(input) | ||
|
||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_include_args_to_hash_from_nested_array | ||
expected = { author: {}, comments: { author: {} } } | ||
input = [:author, comments: [:author]] | ||
actual = Parsing.include_args_to_hash(input) | ||
|
||
assert_equal(expected, actual) | ||
end | ||
|
||
def test_include_args_to_hash_from_array_of_hashes | ||
expected = { | ||
author: {}, | ||
blogs: { posts: { contributors: {} } }, | ||
comments: { author: { blogs: { posts: {} } } } | ||
} | ||
input = [ | ||
:author, | ||
blogs: [posts: :contributors], | ||
comments: { author: { blogs: :posts } } | ||
] | ||
actual = Parsing.include_args_to_hash(input) | ||
|
||
assert_equal(expected, actual) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |