From 5b2eb64ceb08cd005dc06b721935de5853971473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Nov 2011 18:38:28 +0100 Subject: Revert "Implement ArraySerializer and move old serialization API to a new namespace." This reverts commit 8896b4fdc8a543157cdf4dfc378607ebf6c10ab0. Conflicts: activemodel/lib/active_model.rb activemodel/lib/active_model/serializable.rb activemodel/lib/active_model/serializer.rb activemodel/test/cases/serializer_test.rb --- activemodel/test/cases/serializable/json_test.rb | 221 --------------------- activemodel/test/cases/serializable/xml_test.rb | 208 ------------------- activemodel/test/cases/serializable_test.rb | 151 -------------- activemodel/test/cases/serialization_test.rb | 151 ++++++++++++++ .../cases/serializers/json_serialization_test.rb | 221 +++++++++++++++++++++ .../cases/serializers/xml_serialization_test.rb | 208 +++++++++++++++++++ 6 files changed, 580 insertions(+), 580 deletions(-) delete mode 100644 activemodel/test/cases/serializable/json_test.rb delete mode 100644 activemodel/test/cases/serializable/xml_test.rb delete mode 100644 activemodel/test/cases/serializable_test.rb create mode 100644 activemodel/test/cases/serialization_test.rb create mode 100644 activemodel/test/cases/serializers/json_serialization_test.rb create mode 100644 activemodel/test/cases/serializers/xml_serialization_test.rb (limited to 'activemodel/test') diff --git a/activemodel/test/cases/serializable/json_test.rb b/activemodel/test/cases/serializable/json_test.rb deleted file mode 100644 index e5364d9858..0000000000 --- a/activemodel/test/cases/serializable/json_test.rb +++ /dev/null @@ -1,221 +0,0 @@ -require 'cases/helper' -require 'models/contact' -require 'models/automobile' -require 'active_support/core_ext/object/instance_variables' - -class Contact - extend ActiveModel::Naming - include ActiveModel::Serializable::JSON - include ActiveModel::Validations - - def attributes=(hash) - hash.each do |k, v| - instance_variable_set("@#{k}", v) - end - end - - remove_method :attributes if method_defined?(:attributes) - - def attributes - instance_values - end -end - -class JsonSerializationTest < ActiveModel::TestCase - 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 (class method)" do - begin - Contact.include_root_in_json = false - json = @contact.to_json - - assert_no_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 - ensure - Contact.include_root_in_json = true - end - end - - test "should include root in json (option) even if the default is set to false" do - begin - Contact.include_root_in_json = false - json = @contact.to_json(:root => true) - assert_match %r{^\{"contact":\{}, json - ensure - Contact.include_root_in_json = true - end - end - - test "should not include root in json (option)" do - - json = @contact.to_json(:root => false) - - assert_no_match %r{^\{"contact":\{}, json - 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 - 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 - contact = Contact.new - contact.errors.add :name, "can't be blank" - contact.errors.add :name, "is too short (minimum is 2 characters)" - contact.errors.add :age, "must be 16 or over" - - hash = ActiveSupport::OrderedHash.new - hash[:name] = ["can't be blank", "is too short (minimum is 2 characters)"] - hash[:age] = ["must be 16 or over"] - assert_equal hash.to_json, contact.errors.to_json - end - - test "serializable_hash should not modify options passed in argument" do - options = { :except => :name } - @contact.serializable_hash(options) - - assert_nil options[:only] - assert_equal :name, options[:except] - end - - test "as_json should return a hash" do - json = @contact.as_json - - assert_kind_of Hash, json - assert_kind_of Hash, json['contact'] - %w(name age created_at awesome preferences).each do |field| - assert_equal @contact.send(field), json['contact'][field] - end - end - - test "from_json should set the object's attributes" do - json = @contact.to_json - result = Contact.new.from_json(json) - - assert_equal result.name, @contact.name - assert_equal result.age, @contact.age - assert_equal Time.parse(result.created_at), @contact.created_at - assert_equal result.awesome, @contact.awesome - assert_equal result.preferences, @contact.preferences - end - - test "from_json should work without a root (method parameter)" do - json = @contact.to_json(:root => false) - result = Contact.new.from_json(json, false) - - assert_equal result.name, @contact.name - assert_equal result.age, @contact.age - assert_equal Time.parse(result.created_at), @contact.created_at - assert_equal result.awesome, @contact.awesome - assert_equal result.preferences, @contact.preferences - end - - test "from_json should work without a root (class attribute)" do - begin - Contact.include_root_in_json = false - json = @contact.to_json - result = Contact.new.from_json(json) - - assert_equal result.name, @contact.name - assert_equal result.age, @contact.age - assert_equal Time.parse(result.created_at), @contact.created_at - assert_equal result.awesome, @contact.awesome - assert_equal result.preferences, @contact.preferences - ensure - Contact.include_root_in_json = true - end - end - - test "custom as_json should be honored when generating json" do - def @contact.as_json(options); { :name => name, :created_at => created_at }; end - json = @contact.to_json - - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json - assert_no_match %r{"awesome":}, json - assert_no_match %r{"preferences":}, json - end - - test "custom as_json options should be extendible" do - def @contact.as_json(options = {}); super(options.merge(:only => [:name])); end - json = @contact.to_json - - assert_match %r{"name":"Konata Izumi"}, json - assert_no_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json - assert_no_match %r{"awesome":}, json - assert_no_match %r{"preferences":}, json - end - -end diff --git a/activemodel/test/cases/serializable/xml_test.rb b/activemodel/test/cases/serializable/xml_test.rb deleted file mode 100644 index 834c4de1e1..0000000000 --- a/activemodel/test/cases/serializable/xml_test.rb +++ /dev/null @@ -1,208 +0,0 @@ -require 'cases/helper' -require 'models/contact' -require 'active_support/core_ext/object/instance_variables' -require 'ostruct' - -class Contact - extend ActiveModel::Naming - include ActiveModel::Serializable::XML - - attr_accessor :address, :friends - - remove_method :attributes if method_defined?(:attributes) - - def attributes - instance_values.except("address", "friends") - end -end - -module Admin - class Contact < ::Contact - end -end - -class Customer < Struct.new(:name) -end - -class Address - extend ActiveModel::Naming - include ActiveModel::Serializable::XML - - attr_accessor :street, :city, :state, :zip - - def attributes - instance_values - end -end - -class SerializableContact < Contact - def serializable_hash(options={}) - super(options.merge(:only => [:name, :age])) - end -end - -class XmlSerializationTest < ActiveModel::TestCase - def setup - @contact = Contact.new - @contact.name = 'aaron stack' - @contact.age = 25 - @contact.created_at = Time.utc(2006, 8, 1) - @contact.awesome = false - customer = Customer.new - customer.name = "John" - @contact.preferences = customer - @contact.address = Address.new - @contact.address.street = "123 Lane" - @contact.address.city = "Springfield" - @contact.address.state = "CA" - @contact.address.zip = 11111 - @contact.friends = [Contact.new, Contact.new] - end - - test "should serialize default root" do - @xml = @contact.to_xml - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should serialize namespaced root" do - @xml = Admin::Contact.new(@contact.attributes).to_xml - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should serialize default root with namespace" do - @xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact" - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should serialize custom root" do - @xml = @contact.to_xml :root => 'xml_contact' - assert_match %r{^}, @xml - assert_match %r{$}, @xml - end - - test "should allow undasherized tags" do - @xml = @contact.to_xml :root => 'xml_contact', :dasherize => false - assert_match %r{^}, @xml - assert_match %r{$}, @xml - assert_match %r{ 'xml_contact', :camelize => true - assert_match %r{^}, @xml - assert_match %r{$}, @xml - assert_match %r{ 'xml_contact', :camelize => :lower - assert_match %r{^}, @xml - assert_match %r{$}, @xml - assert_match %r{aaron stack}, @xml - assert_match %r{25}, @xml - assert_no_match %r{}, @xml - end - - test "should allow skipped types" do - @xml = @contact.to_xml :skip_types => true - assert_match %r{25}, @xml - end - - test "should include yielded additions" do - @xml = @contact.to_xml do |xml| - xml.creator "David" - end - assert_match %r{David}, @xml - end - - test "should serialize string" do - assert_match %r{aaron stack}, @contact.to_xml - end - - test "should serialize nil" do - assert_match %r{}, @contact.to_xml(:methods => :pseudonyms) - end - - test "should serialize integer" do - assert_match %r{25}, @contact.to_xml - end - - test "should serialize datetime" do - assert_match %r{2006-08-01T00:00:00Z}, @contact.to_xml - end - - test "should serialize boolean" do - assert_match %r{false}, @contact.to_xml - end - - test "should serialize array" do - assert_match %r{\s*twitter\s*github\s*}, @contact.to_xml(:methods => :social) - end - - test "should serialize hash" do - assert_match %r{\s*github\s*}, @contact.to_xml(:methods => :network) - end - - test "should serialize yaml" do - assert_match %r{--- !ruby/struct:Customer(\s*)\nname: John\n}, @contact.to_xml - end - - test "should call proc on object" do - proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') } - xml = @contact.to_xml(:procs => [ proc ]) - assert_match %r{unknown}, xml - end - - test 'should supply serializable to second proc argument' do - proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) } - xml = @contact.to_xml(:procs => [ proc ]) - assert_match %r{kcats noraa}, xml - end - - test "should serialize string correctly when type passed" do - xml = @contact.to_xml :type => 'Contact' - assert_match %r{}, xml - assert_match %r{aaron stack}, xml - end - - test "include option with singular association" do - xml = @contact.to_xml :include => :address, :indent => 0 - assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) - end - - test "include option with plural association" do - xml = @contact.to_xml :include => :friends, :indent => 0 - assert_match %r{}, xml - assert_match %r{}, xml - end - - test "multiple includes" do - xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => [ :address, :friends ] - assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) - assert_match %r{}, xml - assert_match %r{}, xml - end - - test "include with options" do - xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => { :address => { :only => :city } } - assert xml.include?(%(>
Springfield
)) - end - - test "propagates skip_types option to included associations" do - xml = @contact.to_xml :include => :friends, :indent => 0, :skip_types => true - assert_match %r{}, xml - assert_match %r{}, xml - end -end diff --git a/activemodel/test/cases/serializable_test.rb b/activemodel/test/cases/serializable_test.rb deleted file mode 100644 index 46ee372c6f..0000000000 --- a/activemodel/test/cases/serializable_test.rb +++ /dev/null @@ -1,151 +0,0 @@ -require "cases/helper" -require 'active_support/core_ext/object/instance_variables' - -class SerializationTest < ActiveModel::TestCase - class User - include ActiveModel::Serializable - - attr_accessor :name, :email, :gender, :address, :friends - - def initialize(name, email, gender) - @name, @email, @gender = name, email, gender - @friends = [] - end - - def attributes - instance_values.except("address", "friends") - end - - def foo - 'i_am_foo' - end - end - - class Address - include ActiveModel::Serializable - - attr_accessor :street, :city, :state, :zip - - def attributes - instance_values - end - end - - setup do - @user = User.new('David', 'david@example.com', 'male') - @user.address = Address.new - @user.address.street = "123 Lane" - @user.address.city = "Springfield" - @user.address.state = "CA" - @user.address.zip = 11111 - @user.friends = [User.new('Joe', 'joe@example.com', 'male'), - User.new('Sue', 'sue@example.com', 'female')] - end - - def test_method_serializable_hash_should_work - expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash - end - - def test_method_serializable_hash_should_work_with_only_option - expected = {"name"=>"David"} - assert_equal expected , @user.serializable_hash(:only => [:name]) - end - - def test_method_serializable_hash_should_work_with_except_option - expected = {"gender"=>"male", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash(:except => [:name]) - end - - def test_method_serializable_hash_should_work_with_methods_option - expected = {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash(:methods => [:foo]) - end - - def test_method_serializable_hash_should_work_with_only_and_methods - expected = {:foo=>"i_am_foo"} - assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo]) - end - - def test_method_serializable_hash_should_work_with_except_and_methods - expected = {"gender"=>"male", :foo=>"i_am_foo"} - assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo]) - end - - def test_should_not_call_methods_that_dont_respond - expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} - assert_equal expected , @user.serializable_hash(:methods => [:bar]) - end - - def test_should_use_read_attribute_for_serialization - def @user.read_attribute_for_serialization(n) - "Jon" - end - - expected = { "name" => "Jon" } - assert_equal expected, @user.serializable_hash(:only => :name) - end - - def test_include_option_with_singular_association - expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com", - :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} - assert_equal expected , @user.serializable_hash(:include => :address) - end - - def test_include_option_with_plural_association - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected , @user.serializable_hash(:include => :friends) - end - - def test_include_option_with_empty_association - @user.friends = [] - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", :friends=>[]} - assert_equal expected , @user.serializable_hash(:include => :friends) - end - - def test_multiple_includes - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected , @user.serializable_hash(:include => [:address, :friends]) - end - - def test_include_with_options - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane"}} - assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}}) - end - - def test_nested_include - @user.friends.first.friends = [@user] - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male', - :friends => [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', :friends => []}]} - assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}}) - end - - def test_only_include - expected = {"name"=>"David", :friends => [{"name" => "Joe"}, {"name" => "Sue"}]} - assert_equal expected , @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}}) - end - - def test_except_include - expected = {"name"=>"David", "email"=>"david@example.com", - :friends => [{"name" => 'Joe', "email" => 'joe@example.com'}, - {"name" => "Sue", "email" => 'sue@example.com'}]} - assert_equal expected , @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}}) - end - - def test_multiple_includes_with_options - expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", - :address=>{"street"=>"123 Lane"}, - :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, - {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} - assert_equal expected , @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends]) - end - -end diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb new file mode 100644 index 0000000000..b8dad9d51f --- /dev/null +++ b/activemodel/test/cases/serialization_test.rb @@ -0,0 +1,151 @@ +require "cases/helper" +require 'active_support/core_ext/object/instance_variables' + +class SerializationTest < ActiveModel::TestCase + class User + include ActiveModel::Serialization + + attr_accessor :name, :email, :gender, :address, :friends + + def initialize(name, email, gender) + @name, @email, @gender = name, email, gender + @friends = [] + end + + def attributes + instance_values.except("address", "friends") + end + + def foo + 'i_am_foo' + end + end + + class Address + include ActiveModel::Serialization + + attr_accessor :street, :city, :state, :zip + + def attributes + instance_values + end + end + + setup do + @user = User.new('David', 'david@example.com', 'male') + @user.address = Address.new + @user.address.street = "123 Lane" + @user.address.city = "Springfield" + @user.address.state = "CA" + @user.address.zip = 11111 + @user.friends = [User.new('Joe', 'joe@example.com', 'male'), + User.new('Sue', 'sue@example.com', 'female')] + end + + def test_method_serializable_hash_should_work + expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash + end + + def test_method_serializable_hash_should_work_with_only_option + expected = {"name"=>"David"} + assert_equal expected , @user.serializable_hash(:only => [:name]) + end + + def test_method_serializable_hash_should_work_with_except_option + expected = {"gender"=>"male", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash(:except => [:name]) + end + + def test_method_serializable_hash_should_work_with_methods_option + expected = {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash(:methods => [:foo]) + end + + def test_method_serializable_hash_should_work_with_only_and_methods + expected = {:foo=>"i_am_foo"} + assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo]) + end + + def test_method_serializable_hash_should_work_with_except_and_methods + expected = {"gender"=>"male", :foo=>"i_am_foo"} + assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo]) + end + + def test_should_not_call_methods_that_dont_respond + expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"} + assert_equal expected , @user.serializable_hash(:methods => [:bar]) + end + + def test_should_use_read_attribute_for_serialization + def @user.read_attribute_for_serialization(n) + "Jon" + end + + expected = { "name" => "Jon" } + assert_equal expected, @user.serializable_hash(:only => :name) + end + + def test_include_option_with_singular_association + expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com", + :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}} + assert_equal expected , @user.serializable_hash(:include => :address) + end + + def test_include_option_with_plural_association + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} + assert_equal expected , @user.serializable_hash(:include => :friends) + end + + def test_include_option_with_empty_association + @user.friends = [] + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", :friends=>[]} + assert_equal expected , @user.serializable_hash(:include => :friends) + end + + def test_multiple_includes + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}, + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} + assert_equal expected , @user.serializable_hash(:include => [:address, :friends]) + end + + def test_include_with_options + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :address=>{"street"=>"123 Lane"}} + assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}}) + end + + def test_nested_include + @user.friends.first.friends = [@user] + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male', + :friends => [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', :friends => []}]} + assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}}) + end + + def test_only_include + expected = {"name"=>"David", :friends => [{"name" => "Joe"}, {"name" => "Sue"}]} + assert_equal expected , @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}}) + end + + def test_except_include + expected = {"name"=>"David", "email"=>"david@example.com", + :friends => [{"name" => 'Joe', "email" => 'joe@example.com'}, + {"name" => "Sue", "email" => 'sue@example.com'}]} + assert_equal expected , @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}}) + end + + def test_multiple_includes_with_options + expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", + :address=>{"street"=>"123 Lane"}, + :friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'}, + {"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]} + assert_equal expected , @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends]) + end + +end diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb new file mode 100644 index 0000000000..4ac5fb1779 --- /dev/null +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -0,0 +1,221 @@ +require 'cases/helper' +require 'models/contact' +require 'models/automobile' +require 'active_support/core_ext/object/instance_variables' + +class Contact + extend ActiveModel::Naming + include ActiveModel::Serializers::JSON + include ActiveModel::Validations + + def attributes=(hash) + hash.each do |k, v| + instance_variable_set("@#{k}", v) + end + end + + remove_method :attributes if method_defined?(:attributes) + + def attributes + instance_values + end +end + +class JsonSerializationTest < ActiveModel::TestCase + 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 (class method)" do + begin + Contact.include_root_in_json = false + json = @contact.to_json + + assert_no_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 + ensure + Contact.include_root_in_json = true + end + end + + test "should include root in json (option) even if the default is set to false" do + begin + Contact.include_root_in_json = false + json = @contact.to_json(:root => true) + assert_match %r{^\{"contact":\{}, json + ensure + Contact.include_root_in_json = true + end + end + + test "should not include root in json (option)" do + + json = @contact.to_json(:root => false) + + assert_no_match %r{^\{"contact":\{}, json + 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 + 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 + contact = Contact.new + contact.errors.add :name, "can't be blank" + contact.errors.add :name, "is too short (minimum is 2 characters)" + contact.errors.add :age, "must be 16 or over" + + hash = ActiveSupport::OrderedHash.new + hash[:name] = ["can't be blank", "is too short (minimum is 2 characters)"] + hash[:age] = ["must be 16 or over"] + assert_equal hash.to_json, contact.errors.to_json + end + + test "serializable_hash should not modify options passed in argument" do + options = { :except => :name } + @contact.serializable_hash(options) + + assert_nil options[:only] + assert_equal :name, options[:except] + end + + test "as_json should return a hash" do + json = @contact.as_json + + assert_kind_of Hash, json + assert_kind_of Hash, json['contact'] + %w(name age created_at awesome preferences).each do |field| + assert_equal @contact.send(field), json['contact'][field] + end + end + + test "from_json should set the object's attributes" do + json = @contact.to_json + result = Contact.new.from_json(json) + + assert_equal result.name, @contact.name + assert_equal result.age, @contact.age + assert_equal Time.parse(result.created_at), @contact.created_at + assert_equal result.awesome, @contact.awesome + assert_equal result.preferences, @contact.preferences + end + + test "from_json should work without a root (method parameter)" do + json = @contact.to_json(:root => false) + result = Contact.new.from_json(json, false) + + assert_equal result.name, @contact.name + assert_equal result.age, @contact.age + assert_equal Time.parse(result.created_at), @contact.created_at + assert_equal result.awesome, @contact.awesome + assert_equal result.preferences, @contact.preferences + end + + test "from_json should work without a root (class attribute)" do + begin + Contact.include_root_in_json = false + json = @contact.to_json + result = Contact.new.from_json(json) + + assert_equal result.name, @contact.name + assert_equal result.age, @contact.age + assert_equal Time.parse(result.created_at), @contact.created_at + assert_equal result.awesome, @contact.awesome + assert_equal result.preferences, @contact.preferences + ensure + Contact.include_root_in_json = true + end + end + + test "custom as_json should be honored when generating json" do + def @contact.as_json(options); { :name => name, :created_at => created_at }; end + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json + assert_no_match %r{"awesome":}, json + assert_no_match %r{"preferences":}, json + end + + test "custom as_json options should be extendible" do + def @contact.as_json(options = {}); super(options.merge(:only => [:name])); end + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_no_match %r{"created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}}, json + assert_no_match %r{"awesome":}, json + assert_no_match %r{"preferences":}, json + end + +end diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb new file mode 100644 index 0000000000..38aecf51ff --- /dev/null +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -0,0 +1,208 @@ +require 'cases/helper' +require 'models/contact' +require 'active_support/core_ext/object/instance_variables' +require 'ostruct' + +class Contact + extend ActiveModel::Naming + include ActiveModel::Serializers::Xml + + attr_accessor :address, :friends + + remove_method :attributes if method_defined?(:attributes) + + def attributes + instance_values.except("address", "friends") + end +end + +module Admin + class Contact < ::Contact + end +end + +class Customer < Struct.new(:name) +end + +class Address + extend ActiveModel::Naming + include ActiveModel::Serializers::Xml + + attr_accessor :street, :city, :state, :zip + + def attributes + instance_values + end +end + +class SerializableContact < Contact + def serializable_hash(options={}) + super(options.merge(:only => [:name, :age])) + end +end + +class XmlSerializationTest < ActiveModel::TestCase + def setup + @contact = Contact.new + @contact.name = 'aaron stack' + @contact.age = 25 + @contact.created_at = Time.utc(2006, 8, 1) + @contact.awesome = false + customer = Customer.new + customer.name = "John" + @contact.preferences = customer + @contact.address = Address.new + @contact.address.street = "123 Lane" + @contact.address.city = "Springfield" + @contact.address.state = "CA" + @contact.address.zip = 11111 + @contact.friends = [Contact.new, Contact.new] + end + + test "should serialize default root" do + @xml = @contact.to_xml + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should serialize namespaced root" do + @xml = Admin::Contact.new(@contact.attributes).to_xml + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should serialize default root with namespace" do + @xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact" + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should serialize custom root" do + @xml = @contact.to_xml :root => 'xml_contact' + assert_match %r{^}, @xml + assert_match %r{$}, @xml + end + + test "should allow undasherized tags" do + @xml = @contact.to_xml :root => 'xml_contact', :dasherize => false + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{ 'xml_contact', :camelize => true + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{ 'xml_contact', :camelize => :lower + assert_match %r{^}, @xml + assert_match %r{$}, @xml + assert_match %r{aaron stack}, @xml + assert_match %r{25}, @xml + assert_no_match %r{}, @xml + end + + test "should allow skipped types" do + @xml = @contact.to_xml :skip_types => true + assert_match %r{25}, @xml + end + + test "should include yielded additions" do + @xml = @contact.to_xml do |xml| + xml.creator "David" + end + assert_match %r{David}, @xml + end + + test "should serialize string" do + assert_match %r{aaron stack}, @contact.to_xml + end + + test "should serialize nil" do + assert_match %r{}, @contact.to_xml(:methods => :pseudonyms) + end + + test "should serialize integer" do + assert_match %r{25}, @contact.to_xml + end + + test "should serialize datetime" do + assert_match %r{2006-08-01T00:00:00Z}, @contact.to_xml + end + + test "should serialize boolean" do + assert_match %r{false}, @contact.to_xml + end + + test "should serialize array" do + assert_match %r{\s*twitter\s*github\s*}, @contact.to_xml(:methods => :social) + end + + test "should serialize hash" do + assert_match %r{\s*github\s*}, @contact.to_xml(:methods => :network) + end + + test "should serialize yaml" do + assert_match %r{--- !ruby/struct:Customer(\s*)\nname: John\n}, @contact.to_xml + end + + test "should call proc on object" do + proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') } + xml = @contact.to_xml(:procs => [ proc ]) + assert_match %r{unknown}, xml + end + + test 'should supply serializable to second proc argument' do + proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) } + xml = @contact.to_xml(:procs => [ proc ]) + assert_match %r{kcats noraa}, xml + end + + test "should serialize string correctly when type passed" do + xml = @contact.to_xml :type => 'Contact' + assert_match %r{}, xml + assert_match %r{aaron stack}, xml + end + + test "include option with singular association" do + xml = @contact.to_xml :include => :address, :indent => 0 + assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) + end + + test "include option with plural association" do + xml = @contact.to_xml :include => :friends, :indent => 0 + assert_match %r{}, xml + assert_match %r{}, xml + end + + test "multiple includes" do + xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => [ :address, :friends ] + assert xml.include?(@contact.address.to_xml(:indent => 0, :skip_instruct => true)) + assert_match %r{}, xml + assert_match %r{}, xml + end + + test "include with options" do + xml = @contact.to_xml :indent => 0, :skip_instruct => true, :include => { :address => { :only => :city } } + assert xml.include?(%(>
Springfield
)) + end + + test "propagates skip_types option to included associations" do + xml = @contact.to_xml :include => :friends, :indent => 0, :skip_types => true + assert_match %r{}, xml + assert_match %r{}, xml + end +end -- cgit v1.2.3