Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveModel::Serializer#default_include #1845

Closed
wants to merge 7 commits into from
Prev Previous commit
Next Next commit
tests refactoring using #serializable tests helper
  • Loading branch information
iMacTia committed Jul 14, 2016
commit a9744ea944c91c209591d403998de9c97bbc7f22
8 changes: 4 additions & 4 deletions test/serializers/always_include_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ class CategorySerializer < ActiveModel::Serializer
end

test '#always_include option populate "included" for json_api adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :json_api).as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :json_api).as_json

assert_equal serialized[:included].size, 3
assert_equal 'The first post', serialized[:included].first[:attributes][:title]
end

test '#always_include merges with the render include for json_api adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :json_api, include: 'posts.category').as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :json_api, include: 'posts.category').as_json

assert_equal serialized[:included].size, 4
assert_equal 'The first post', serialized[:included].first[:attributes][:title]
end

test '#always_include option include associations for attributes adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes).as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :attributes).as_json

assert_equal serialized[:posts].size, 2
assert_equal 'Blog Author', serialized[:posts].first[:author][:name]
end

test '#always_include merges with the render include for attributes adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes, include: 'posts.category').as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :attributes, include: 'posts.category').as_json

assert_equal serialized[:posts].size, 2
assert_equal 'Blog Author', serialized[:posts].first[:author][:name]
Expand Down
8 changes: 4 additions & 4 deletions test/serializers/default_include_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ class CategorySerializer < ActiveModel::Serializer
end

test '#default_include option populate "included" for json_api adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :json_api).as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :json_api).as_json

assert_equal serialized[:included].size, 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for both these tests and the always tests, I'd actually make sure that the correct types are rendered, just so whomever is looking at these tests in the future can very easily see what is being rendered.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check for them in my latest commit 😄

assert_equal 'The first post', serialized[:included].first[:attributes][:title]
end

test '#default_include merges with the render include for json_api adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :json_api, include: 'posts.category').as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :json_api, include: 'posts.category').as_json

assert_equal serialized[:included].size, 4
assert_equal 'The first post', serialized[:included].first[:attributes][:title]
end

test '#default_include option include associations for attributes adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes).as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :attributes).as_json

assert_equal serialized[:posts].size, 2
assert_equal 'Blog Author', serialized[:posts].first[:author][:name]
end

test '#default_include merges with the render include for attributes adapter' do
serialized = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes, include: 'posts.category').as_json
serialized = serializable(@blog, serializer: BlogSerializer, adapter: :attributes, include: 'posts.category').as_json

assert_equal serialized[:posts].size, 2
assert_equal 'Blog Author', serialized[:posts].first[:author][:name]
Expand Down