1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/test/cases/serializeration/json_serialization_test.rb

120 lines
3.9 KiB
Ruby
Raw Normal View History

2009-06-17 22:27:36 -04:00
require 'cases/helper'
require 'models/contact'
2009-09-05 19:54:19 -04:00
require 'active_support/core_ext/object/instance_variables'
2009-06-17 22:27:36 -04:00
class Contact
2009-09-05 19:54:19 -04:00
extend ActiveModel::Naming
include ActiveModel::Serializers::JSON
2009-07-21 00:28:58 -04:00
def attributes
instance_values
2009-09-05 20:10:21 -04:00
end unless method_defined?(:attributes)
end
2009-06-17 22:27:36 -04:00
class JsonSerializationTest < ActiveModel::TestCase
2009-06-17 22:27:36 -04:00
def setup
@contact = Contact.new
@contact.name = 'Konata Izumi'
@contact.age = 16
@contact.created_at = Time.utc(2006, 8, 1)
@contact.awesome = true
@contact.preferences = { 'shows' => 'anime' }
end
test "should include root in json" do
json = @contact.to_json
assert_match %r{^\{"contact":\{}, json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test "should not include root in json" do
2009-06-17 22:27:36 -04:00
begin
Contact.include_root_in_json = false
2009-06-17 22:27:36 -04:00
json = @contact.to_json
assert_no_match %r{^\{"contact":\{}, json
2009-06-17 22:27:36 -04:00
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
ensure
Contact.include_root_in_json = true
end
end
test "should include custom root in json" do
json = @contact.to_json(:root => 'json_contact')
assert_match %r{^\{"json_contact":\{}, json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
2009-06-17 22:27:36 -04:00
end
test "should encode all encodable attributes" do
json = @contact.to_json
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test "should allow attribute filtering with only" do
json = @contact.to_json(:only => [:name, :age])
assert_match %r{"name":"Konata Izumi"}, json
assert_match %r{"age":16}, json
assert_no_match %r{"awesome":true}, json
assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
end
test "should allow attribute filtering with except" do
json = @contact.to_json(:except => [:name, :age])
assert_no_match %r{"name":"Konata Izumi"}, json
assert_no_match %r{"age":16}, json
assert_match %r{"awesome":true}, json
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"preferences":\{"shows":"anime"\}}, json
end
test "methods are called on object" do
# Define methods on fixture.
def @contact.label; "Has cheezburger"; end
def @contact.favorite_quote; "Constraints are liberating"; end
# Single method.
assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
# Both methods.
methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
assert_match %r{"label":"Has cheezburger"}, methods_json
assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
end
test "should return OrderedHash for errors" do
car = Automobile.new
# run the validation
car.valid?
hash = ActiveSupport::OrderedHash.new
hash[:make] = "can't be blank"
hash[:model] = "is too short (minimum is 2 characters)"
assert_equal hash.to_json, car.errors.to_json
end
2009-06-17 22:27:36 -04:00
end