2009-06-17 22:27:36 -04:00
|
|
|
require 'cases/helper'
|
2009-06-28 23:12:10 -04:00
|
|
|
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
|
|
|
|
2009-06-28 23:12:10 -04:00
|
|
|
class Contact
|
2009-09-05 19:54:19 -04:00
|
|
|
extend ActiveModel::Naming
|
2009-06-28 23:12:10 -04:00
|
|
|
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)
|
2009-06-28 23:12:10 -04:00
|
|
|
end
|
2009-06-17 22:27:36 -04:00
|
|
|
|
2009-06-28 23:12:10 -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
|
2010-06-07 19:02:45 -04:00
|
|
|
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
|
2010-06-07 19:02:45 -04:00
|
|
|
Contact.include_root_in_json = false
|
2009-06-17 22:27:36 -04:00
|
|
|
json = @contact.to_json
|
|
|
|
|
2010-06-07 19:02:45 -04:00
|
|
|
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
|
2010-05-01 00:02:11 -04:00
|
|
|
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
|
2010-06-07 19:02:45 -04:00
|
|
|
Contact.include_root_in_json = true
|
2010-05-01 00:02:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should include custom root in json" do
|
2010-06-07 19:02:45 -04:00
|
|
|
json = @contact.to_json(:root => 'json_contact')
|
2010-05-01 00:02:11 -04:00
|
|
|
|
2010-06-07 19:02:45 -04:00
|
|
|
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
|
2009-06-28 23:12:10 -04:00
|
|
|
|
2010-07-30 16:47:26 -04:00
|
|
|
test "methods are called on object" do
|
2009-06-28 23:12:10 -04:00
|
|
|
# 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
|
2010-07-30 16:47:26 -04:00
|
|
|
|
|
|
|
test "should return OrderedHash for errors" do
|
|
|
|
car = Automobile.new
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-07-30 16:47:26 -04:00
|
|
|
# run the validation
|
2010-08-14 01:13:00 -04:00
|
|
|
car.valid?
|
|
|
|
|
2010-07-30 16:47:26 -04:00
|
|
|
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
|
2010-08-14 01:13:00 -04:00
|
|
|
|
|
|
|
|
2009-06-17 22:27:36 -04:00
|
|
|
end
|