mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
8c7d4018e0
Closes #9459.
The PR #6597 unified the configuration for `include_root_in_json`
in AM and AR to `false`.
Later on with the refactoring commit: e030f26
the value in AR was
set to `true` but I think this was not on purpose.
With this commit both AM and AR will have the same configuration
for `include_root_in_json`, which is `false`.
68 lines
2.4 KiB
Ruby
68 lines
2.4 KiB
Ruby
require "cases/helper"
|
|
require 'models/contact'
|
|
require 'models/topic'
|
|
|
|
class SerializationTest < ActiveRecord::TestCase
|
|
FORMATS = [ :xml, :json ]
|
|
|
|
def setup
|
|
@contact_attributes = {
|
|
:name => 'aaron stack',
|
|
:age => 25,
|
|
:avatar => 'binarydata',
|
|
:created_at => Time.utc(2006, 8, 1),
|
|
:awesome => false,
|
|
:preferences => { :gem => '<strong>ruby</strong>' },
|
|
:alternative_id => nil,
|
|
:id => nil
|
|
}
|
|
end
|
|
|
|
def test_include_root_in_json_is_false_by_default
|
|
assert_equal false, ActiveRecord::Base.include_root_in_json, "include_root_in_json should be false by default but was not"
|
|
end
|
|
|
|
def test_serialize_should_be_reversible
|
|
FORMATS.each do |format|
|
|
@serialized = Contact.new.send("to_#{format}")
|
|
contact = Contact.new.send("from_#{format}", @serialized)
|
|
|
|
assert_equal @contact_attributes.keys.collect(&:to_s).sort, contact.attributes.keys.collect(&:to_s).sort, "For #{format}"
|
|
end
|
|
end
|
|
|
|
def test_serialize_should_allow_attribute_only_filtering
|
|
FORMATS.each do |format|
|
|
@serialized = Contact.new(@contact_attributes).send("to_#{format}", :only => [ :age, :name ])
|
|
contact = Contact.new.send("from_#{format}", @serialized)
|
|
assert_equal @contact_attributes[:name], contact.name, "For #{format}"
|
|
assert_nil contact.avatar, "For #{format}"
|
|
end
|
|
end
|
|
|
|
def test_serialize_should_allow_attribute_except_filtering
|
|
FORMATS.each do |format|
|
|
@serialized = Contact.new(@contact_attributes).send("to_#{format}", :except => [ :age, :name ])
|
|
contact = Contact.new.send("from_#{format}", @serialized)
|
|
assert_nil contact.name, "For #{format}"
|
|
assert_nil contact.age, "For #{format}"
|
|
assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
|
|
end
|
|
end
|
|
|
|
def test_include_root_in_json_allows_inheritance
|
|
original_root_in_json = ActiveRecord::Base.include_root_in_json
|
|
ActiveRecord::Base.include_root_in_json = true
|
|
|
|
klazz = Class.new(ActiveRecord::Base)
|
|
klazz.table_name = 'topics'
|
|
assert klazz.include_root_in_json
|
|
|
|
klazz.include_root_in_json = false
|
|
assert ActiveRecord::Base.include_root_in_json
|
|
assert !klazz.include_root_in_json
|
|
assert !klazz.new.include_root_in_json
|
|
ensure
|
|
ActiveRecord::Base.include_root_in_json = original_root_in_json
|
|
end
|
|
end
|